diff --git a/src/game/Building.cpp b/src/game/Building.cpp index efa12b6c3c34b52730ba6be8a7a30329334d5c9f..8382753499b30666d8e145e8754a7af9faa46bce 100644 --- a/src/game/Building.cpp +++ b/src/game/Building.cpp @@ -1,5 +1,6 @@ #include "Building.hpp" #include "Spritesheet.hpp" +#include <iostream> namespace advanced_wars { @@ -28,4 +29,79 @@ void Building::render(Engine& engine, int scale) &dst, 0, NULL, SDL_FLIP_NONE); } +void Building::switch_faction(BuildingFaction faction) +{ + + this->m_faction = faction; + + if (this->m_id == BuildingId::HEADQUARTER) + { + std::cout << "The game is over!" << std::endl; + } +} + +// implement call to UI to show available units +void Building::on_click() +{ + std::cout << "A building is selected!" << std::endl; +}; + +bool Building::check_spawn(std::unordered_map<int, advanced_wars::Unit>& units) +{ + + for (auto& [id, unit] : units) + { + if (unit.m_x == this->m_x && unit.m_y == this->m_y) + { + return false; + } + } + + return true; +} +// can be added as soon as the playerobject is available +bool Building::check_money(int price) +{ + // replace 400 with player.money and replace price with chosenUnit.price + if (400 > price) + { + return false; + } + return true; +} + +std::vector<UnitId> Building::recruitableUnits() +{ + + if (this->m_id == BuildingId::FACTORY) + { + return { + UnitId::INFANTERY, + UnitId::MECHANIZED_INFANTERY, + UnitId::RECON, + UnitId::APC, + UnitId::ARTILLERY, + UnitId::ANTI_AIR_TANK, + UnitId::ANTI_AIR_MISSILE_LAUNCHER, + UnitId::ROCKET_ARTILLERY, + UnitId::MEDIUM_TANK, + UnitId::NEO_TANK, + UnitId::HEAVY_TANK}; + } + + if (this->m_id == BuildingId::PORT) + { + return {UnitId::LANDER, UnitId::CRUISER, UnitId::SUBMARINE, UnitId::BATTLESHIP}; + } + + if (this->m_id == BuildingId::AIRPORT) + { + return { + UnitId::TRANSPORT_HELICOPTER, UnitId::BATTLE_HELICOPTER, UnitId::FIGHTER, + UnitId::BOMBER}; + } + + return {}; +} + } // namespace advanced_wars \ No newline at end of file diff --git a/src/game/Building.hpp b/src/game/Building.hpp index 031c315a6694823138ea96d5212e4a55ba576b40..2bceaa93698658ac06cd85dc7f8fb0a22ccbb0c3 100644 --- a/src/game/Building.hpp +++ b/src/game/Building.hpp @@ -2,18 +2,19 @@ #include "Engine.hpp" #include "Scene.hpp" +#include "Unit.hpp" +#include <unordered_map> namespace advanced_wars { enum class BuildingFaction { - RED = 0, - BLUE = 1, - YELLOW = 2, - GREEN = 3, - PURPLE = 4, - NEUTRAL = 5, + URED = 0, + UBLUE = 1, + UGREEN = 2, + UYELLOW = 3, + UPURPLE = 4, }; enum class BuildingId @@ -21,8 +22,8 @@ enum class BuildingId HEADQUARTER = 0, CITY = 1, FACTORY = 2, - PORT = 3, - SATELLITE = 4, + AIRPORT = 3, + PORT = 4, }; class Building @@ -36,6 +37,41 @@ class Building BuildingFaction m_faction; void render(Engine& engine, int scale); + + /** + Changes the faction to the specified one + + @param faction The new faction the unit will belong to + */ + void switch_faction(BuildingFaction faction); + + /* + checks if the tile ontop of the building is free + */ + bool check_spawn(std::unordered_map<int, advanced_wars::Unit>& units); + + /* + checks if the player has enough money for the unit to be recruited + */ + bool check_money(int price); + + /* + When the building is selected, the player should have the ability to recruit a selection of + units They should be displayed by the UI On_click(); + */ + void recruit_unit(); + + /** + If the building is clicked, it shows information to the player, here it will be a list of + all available units + */ + void on_click(); + + /** + * Provides a vector of recruitable units, depending on the building id + * + */ + std::vector<UnitId> recruitableUnits(); }; } // namespace advanced_wars \ No newline at end of file diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 4082176721d5511c48edf30e65b3c8f20b4f9b3f..df984b4e24bd6be17aeec86df814171e26912b49 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -48,6 +48,9 @@ Level::Level( { throw std::runtime_error("level tile mismatch"); } + + m_selectedBuilding = -1; + m_selectedUnit = -1; }; Level Level::loadLevel(std::string path) @@ -143,6 +146,7 @@ int Level::selectBuilding(int tileX, int tileY) return -1; } + void Level::handleEvent(Engine& engine, SDL_Event& event) { switch (m_state) @@ -162,6 +166,9 @@ void Level::handleEvent(Engine& engine, SDL_Event& event) case LevelState::ATTACKING_STATE: handleAttackingEvents(engine, event); break; + case LevelState::RECRUITING_STATE: + handleRecruitingEvent(engine, event); + break; default: break; } @@ -261,6 +268,24 @@ Effect Level::removeEffect(int id) return value; } +void Level::handleRecruitingEvent(Engine& engine, SDL_Event& event) { + + Building& b = m_buildings.at(m_selectedBuilding); + UnitFaction u_faction = static_cast<UnitFaction> (b.m_faction); + + //show appropriate interface -> provide vector of UnitId + //select UnitId + UnitId u_id = UnitId::INFANTERY; + + if(b.check_money(500)) { + if(b.check_spawn(m_units)){ + addUnit(Unit(b.m_x, b.m_y, u_faction, u_id, UnitState::IDLE)); + m_state = LevelState::SELECTING_STATE; + m_selectedBuilding = -1; + } + } +} + void Level::handleSelectingEvents(Engine& engine, SDL_Event& event) { switch (event.type) @@ -349,8 +374,8 @@ void Level::handleMenuActiveEvents(Engine& engine, SDL_Event& event) } if (cmd == "Train") { - // hier Einheitenrekrutierung einsetzen - std::cout << "no training here" << std::endl; + m_state = LevelState::RECRUITING_STATE; + } } diff --git a/src/game/Level.hpp b/src/game/Level.hpp index 0b843ee2d19cd796ac99602c8831d35c72252957..e8ba8a43528561ce5ee3854bdb3e875401401160 100644 --- a/src/game/Level.hpp +++ b/src/game/Level.hpp @@ -21,7 +21,8 @@ enum class LevelState MOVEMENT_STATE, ANIMATING_STATE, MENUACTIVE_STATE, - ATTACKING_STATE + ATTACKING_STATE, + RECRUITING_STATE }; /** @@ -90,6 +91,7 @@ class Level : public Scene void handleMenuActiveEvents(Engine& engine, SDL_Event& event); void handleMovementEvents(Engine& engine, SDL_Event& event); void handleAttackingEvents(Engine& engine, SDL_Event& event); + void handleRecruitingEvent(Engine& engine, SDL_Event& event); }; } // namespace advanced_wars diff --git a/src/game/Unit.hpp b/src/game/Unit.hpp index 686c4bfa9ee1d7438ea7f9f911274ac120374470..1d399d992024d7be86581cb754a788c01aa5f9f7 100644 --- a/src/game/Unit.hpp +++ b/src/game/Unit.hpp @@ -68,6 +68,7 @@ class Unit int m_x; int m_y; int m_health; // health equals max_health at construction + int m_price; Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state); ~Unit()