From d7696570cc5630694239b2876e09a557de7d454e Mon Sep 17 00:00:00 2001 From: Max Cherris <MCherris@protonmail.com> Date: Mon, 3 Feb 2025 19:32:12 +0100 Subject: [PATCH 01/11] Add building interaction to level.cpp --- src/game/Level.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 67e35e5..d2218ac 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -203,7 +203,7 @@ void Level::handleEvent(Engine& engine, SDL_Event& event) if (m_selectedBuilding > -1) { - // building stuff + m_buildings.at(m_selectedBuilding).on_click(); } } else -- GitLab From ce875a76a9b61069215255c0cd50f291db707e07 Mon Sep 17 00:00:00 2001 From: Max Cherris <MCherris@protonmail.com> Date: Mon, 3 Feb 2025 19:32:32 +0100 Subject: [PATCH 02/11] Add functions for interaction with buildings --- src/game/Building.cpp | 33 +++++++++++++++++++++++++++++++++ src/game/Building.hpp | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/src/game/Building.cpp b/src/game/Building.cpp index efa12b6..cef1c53 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,36 @@ void Building::render(Engine& engine, int scale) &dst, 0, NULL, SDL_FLIP_NONE); } +void Building::switch_allegiance(BuildingFaction faction) { + + this->m_faction = faction; + + if(this->m_id == BuildingId::HEADQUARTER) { + std::cout << "The game is over!" << std::endl; + } +} + +bool Building::check_money(int price) { + + if(50 >= price) { + return true; + } else { + return false; + } +} + +bool Building::check_spawn() { + + for(auto& [id, unit] : m_units) { + if(this->m_x == unit.m_x && this->m_y == unit.m_y) { + return false; + } + } + return true; +} + +void Building::on_click() { + std::cout << "A building is selected!" << std::endl; +}; + } // namespace advanced_wars \ No newline at end of file diff --git a/src/game/Building.hpp b/src/game/Building.hpp index 031c315..cddec68 100644 --- a/src/game/Building.hpp +++ b/src/game/Building.hpp @@ -8,12 +8,11 @@ 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 @@ -36,6 +35,33 @@ class Building BuildingFaction m_faction; void render(Engine& engine, int scale); + + /* + If a unit moves onto a building, it will have the opportunity to capture the building, using an entire turn + Interaction should be triggerd via the UI + Checking if the capture is possible should be implemented in the unit.cpp + If the Headquarter falls, the game should end in a victory for the corresponding player + */ + void switch_allegiance(BuildingFaction faction); + + + /* + checks if the tile ontop of the building is free + */ + bool check_spawn(); + + /* + 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(); + + void on_click(); }; } // namespace advanced_wars \ No newline at end of file -- GitLab From 17fcdd9645c5d1a3ae4ed3354bdf8d194b99774f Mon Sep 17 00:00:00 2001 From: Max Cherris <MCherris@protonmail.com> Date: Mon, 3 Feb 2025 20:14:43 +0100 Subject: [PATCH 03/11] Add function to spawn a unit (infantery) when clicking on a building --- src/game/Building.cpp | 22 ++++++---------------- src/game/Building.hpp | 2 +- src/game/Level.cpp | 24 ++++++++++++++++++++---- src/game/Unit.hpp | 1 + 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/game/Building.cpp b/src/game/Building.cpp index cef1c53..5d03bb5 100644 --- a/src/game/Building.cpp +++ b/src/game/Building.cpp @@ -38,27 +38,17 @@ void Building::switch_allegiance(BuildingFaction faction) { } } -bool Building::check_money(int price) { - - if(50 >= price) { - return true; - } else { - return false; - } -} +void Building::on_click() { + std::cout << "A building is selected!" << std::endl; +}; bool Building::check_spawn() { + return true; +} - for(auto& [id, unit] : m_units) { - if(this->m_x == unit.m_x && this->m_y == unit.m_y) { - return false; - } - } +bool Building::check_money() { return true; } -void Building::on_click() { - std::cout << "A building is selected!" << std::endl; -}; } // namespace advanced_wars \ No newline at end of file diff --git a/src/game/Building.hpp b/src/game/Building.hpp index cddec68..2c144ff 100644 --- a/src/game/Building.hpp +++ b/src/game/Building.hpp @@ -53,7 +53,7 @@ class Building /* checks if the player has enough money for the unit to be recruited */ - bool check_money(int price); + bool check_money(); /* When the building is selected, the player should have the ability to recruit a selection of units diff --git a/src/game/Level.cpp b/src/game/Level.cpp index d2218ac..5469c2f 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -47,6 +47,9 @@ Level::Level( { throw std::runtime_error("level tile mismatch"); } + + m_selectedBuilding = -1; + m_selectedUnit = -1; }; Level Level::loadLevel(std::string path) @@ -100,11 +103,13 @@ bool Level::clickCheckLeft(int tileX, int tileY) if (selectUnit(tileX, tileY)) { + std::cout << "returning true for unit" << std::endl; return true; } if (selectBuilding(tileX, tileY)) { + std::cout << "returning true for building" << std::endl; return true; } @@ -150,7 +155,7 @@ bool Level::targetUnit(int tileX, int tileY) if (unit.m_x == tileX && unit.m_y == tileY) { - // std::cout << "unitX:" << unit.x << "unitY:" << unit.y << std::endl; + //std::cout << "unitX:" << unit.m_x << "unitY:" << unit.m_y << std::endl; m_targetedUnit = id; return true; @@ -168,7 +173,7 @@ bool Level::selectBuilding(int tileX, int tileY) if (building.m_x == tileX && building.m_y == tileY) { - // std::cout << "X:" << unit.x << "Y:" << unit.y << std::endl; + //std::cout << "Building_X:" << building.m_x << "Building_Y:" << building.m_y << std::endl; m_selectedBuilding = id; return true; } @@ -189,9 +194,10 @@ void Level::handleEvent(Engine& engine, SDL_Event& event) // the current unit debug combat should be handled by the contextmenu with its menu options if (event.button.button == SDL_BUTTON_LEFT) { - + int tileX = event.button.x / (16 * RENDERING_SCALE); int tileY = event.button.y / (16 * RENDERING_SCALE); + //std::cout << "selected building:"<< m_selectedBuilding << std::endl; if (clickCheckLeft(tileX, tileY)) { @@ -203,7 +209,17 @@ void Level::handleEvent(Engine& engine, SDL_Event& event) if (m_selectedBuilding > -1) { - m_buildings.at(m_selectedBuilding).on_click(); + //std::cout << "building is > -1" << std::endl; + Building c_building = m_buildings.at(m_selectedBuilding); + c_building.on_click(); + + + if(c_building.check_spawn()) { + if(c_building.check_money()){ + this->addUnit(Unit(c_building.m_x, c_building.m_y, advanced_wars::UnitFaction::URED, advanced_wars::UnitId::INFANTERY,advanced_wars::UnitState::IDLE )); + } + } + } } else diff --git a/src/game/Unit.hpp b/src/game/Unit.hpp index 544c3d9..6b4828f 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() -- GitLab From a72c826c8ee699295656f5d77c59fe23e0c39441 Mon Sep 17 00:00:00 2001 From: Max Cherris <MCherris@protonmail.com> Date: Mon, 3 Feb 2025 20:26:57 +0100 Subject: [PATCH 04/11] Implement check_spawn function Note: Should be refactored i think (?) --- src/game/Building.cpp | 9 ++++++++- src/game/Building.hpp | 4 +++- src/game/Level.cpp | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/game/Building.cpp b/src/game/Building.cpp index 5d03bb5..84b5f47 100644 --- a/src/game/Building.cpp +++ b/src/game/Building.cpp @@ -42,7 +42,14 @@ void Building::on_click() { std::cout << "A building is selected!" << std::endl; }; -bool Building::check_spawn() { +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; } diff --git a/src/game/Building.hpp b/src/game/Building.hpp index 2c144ff..7ddbb2b 100644 --- a/src/game/Building.hpp +++ b/src/game/Building.hpp @@ -1,7 +1,9 @@ #pragma once +#include <unordered_map> #include "Engine.hpp" #include "Scene.hpp" +#include "Unit.hpp" namespace advanced_wars { @@ -48,7 +50,7 @@ class Building /* checks if the tile ontop of the building is free */ - bool check_spawn(); + bool check_spawn(std::unordered_map<int, advanced_wars::Unit>& units); /* checks if the player has enough money for the unit to be recruited diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 5469c2f..89c8b99 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -214,7 +214,7 @@ void Level::handleEvent(Engine& engine, SDL_Event& event) c_building.on_click(); - if(c_building.check_spawn()) { + if(c_building.check_spawn(m_units)) { if(c_building.check_money()){ this->addUnit(Unit(c_building.m_x, c_building.m_y, advanced_wars::UnitFaction::URED, advanced_wars::UnitId::INFANTERY,advanced_wars::UnitState::IDLE )); } -- GitLab From 60fe23f9cb3e2f2d27c042b40d27ba67697265c3 Mon Sep 17 00:00:00 2001 From: Max Cherris <MCherris@protonmail.com> Date: Mon, 3 Feb 2025 20:34:07 +0100 Subject: [PATCH 05/11] Refactor comments --- src/game/Building.cpp | 2 +- src/game/Building.hpp | 14 ++++++++------ src/game/Level.cpp | 7 ------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/game/Building.cpp b/src/game/Building.cpp index 84b5f47..04a61e4 100644 --- a/src/game/Building.cpp +++ b/src/game/Building.cpp @@ -29,7 +29,7 @@ void Building::render(Engine& engine, int scale) &dst, 0, NULL, SDL_FLIP_NONE); } -void Building::switch_allegiance(BuildingFaction faction) { +void Building::switch_faction(BuildingFaction faction) { this->m_faction = faction; diff --git a/src/game/Building.hpp b/src/game/Building.hpp index 7ddbb2b..144f499 100644 --- a/src/game/Building.hpp +++ b/src/game/Building.hpp @@ -38,13 +38,12 @@ class Building void render(Engine& engine, int scale); - /* - If a unit moves onto a building, it will have the opportunity to capture the building, using an entire turn - Interaction should be triggerd via the UI - Checking if the capture is possible should be implemented in the unit.cpp - If the Headquarter falls, the game should end in a victory for the corresponding player + /** + Changes the faction to the specified one + + @param faction The new faction the unit will belong to */ - void switch_allegiance(BuildingFaction faction); + void switch_faction(BuildingFaction faction); /* @@ -63,6 +62,9 @@ class Building */ 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(); }; diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 89c8b99..e18c423 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -130,13 +130,11 @@ bool Level::clickCheckRight(int tileX, int tileY) bool Level::selectUnit(int tileX, int tileY) { - // std::cout << "tileX:" << tileX << "tileX:" << tileY << std::endl; for (auto& [id, unit] : m_units) { if (unit.m_x == tileX && unit.m_y == tileY) { - // std::cout << "unitX:" << unit.x << "unitY:" << unit.y << std::endl; m_selectedUnit = id; return true; @@ -149,13 +147,11 @@ bool Level::selectUnit(int tileX, int tileY) bool Level::targetUnit(int tileX, int tileY) { - // std::cout << "tileX:" << tileX << "tileX:" << tileY << std::endl; for (auto& [id, unit] : m_units) { if (unit.m_x == tileX && unit.m_y == tileY) { - //std::cout << "unitX:" << unit.m_x << "unitY:" << unit.m_y << std::endl; m_targetedUnit = id; return true; @@ -173,7 +169,6 @@ bool Level::selectBuilding(int tileX, int tileY) if (building.m_x == tileX && building.m_y == tileY) { - //std::cout << "Building_X:" << building.m_x << "Building_Y:" << building.m_y << std::endl; m_selectedBuilding = id; return true; } @@ -197,7 +192,6 @@ void Level::handleEvent(Engine& engine, SDL_Event& event) int tileX = event.button.x / (16 * RENDERING_SCALE); int tileY = event.button.y / (16 * RENDERING_SCALE); - //std::cout << "selected building:"<< m_selectedBuilding << std::endl; if (clickCheckLeft(tileX, tileY)) { @@ -209,7 +203,6 @@ void Level::handleEvent(Engine& engine, SDL_Event& event) if (m_selectedBuilding > -1) { - //std::cout << "building is > -1" << std::endl; Building c_building = m_buildings.at(m_selectedBuilding); c_building.on_click(); -- GitLab From 7d47b58fce5fbfff108cc59c8fcce3eefea8e4aa Mon Sep 17 00:00:00 2001 From: Max Cherris <MCherris@protonmail.com> Date: Mon, 3 Feb 2025 20:46:55 +0100 Subject: [PATCH 06/11] Add pricechecking mock --- src/game/Building.cpp | 9 +++++++-- src/game/Building.hpp | 2 +- src/game/Level.cpp | 6 +++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/game/Building.cpp b/src/game/Building.cpp index 04a61e4..6d2f827 100644 --- a/src/game/Building.cpp +++ b/src/game/Building.cpp @@ -38,6 +38,7 @@ void Building::switch_faction(BuildingFaction faction) { } } +//implement call to UI to show available units void Building::on_click() { std::cout << "A building is selected!" << std::endl; }; @@ -52,10 +53,14 @@ bool Building::check_spawn(std::unordered_map<int, advanced_wars::Unit>& units) return true; } +// can be added as soon as the playerobject is available +bool Building::check_money(int price) { -bool Building::check_money() { + if(400 > price) { + return false; + } return true; -} +} } // namespace advanced_wars \ No newline at end of file diff --git a/src/game/Building.hpp b/src/game/Building.hpp index 144f499..2451a9a 100644 --- a/src/game/Building.hpp +++ b/src/game/Building.hpp @@ -54,7 +54,7 @@ class Building /* checks if the player has enough money for the unit to be recruited */ - bool check_money(); + bool check_money(int price); /* When the building is selected, the player should have the ability to recruit a selection of units diff --git a/src/game/Level.cpp b/src/game/Level.cpp index e18c423..4147b7e 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -208,9 +208,13 @@ void Level::handleEvent(Engine& engine, SDL_Event& event) if(c_building.check_spawn(m_units)) { - if(c_building.check_money()){ + if(c_building.check_money(500)){ this->addUnit(Unit(c_building.m_x, c_building.m_y, advanced_wars::UnitFaction::URED, advanced_wars::UnitId::INFANTERY,advanced_wars::UnitState::IDLE )); + } else { + std::cout << "Not enough money to recruit selected unit!" << std::endl; } + } else { + std::cout << "No free space to spawn the unit!" << std::endl; } } -- GitLab From 1ae0adba962e24f7bc7db492479de5e819b66d51 Mon Sep 17 00:00:00 2001 From: Max Cherris <MCherris@protonmail.com> Date: Mon, 3 Feb 2025 20:55:51 +0100 Subject: [PATCH 07/11] Change minor --- src/game/Building.cpp | 2 +- src/game/Level.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/game/Building.cpp b/src/game/Building.cpp index 6d2f827..bd499e6 100644 --- a/src/game/Building.cpp +++ b/src/game/Building.cpp @@ -55,7 +55,7 @@ bool Building::check_spawn(std::unordered_map<int, advanced_wars::Unit>& units) } // 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; } diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 4147b7e..f89ddf1 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -123,7 +123,16 @@ bool Level::clickCheckRight(int tileX, int tileY) { return true; } +// just for checking + for (auto& [id, building] : m_buildings) + { + if (building.m_x == tileX && building.m_y == tileY) + { + + return true; + } + } return false; } -- GitLab From 134e405de2d73b9a9d80c626fb0fb035dcf8901e Mon Sep 17 00:00:00 2001 From: Max Cherris <MCherris@protonmail.com> Date: Wed, 5 Feb 2025 15:55:13 +0100 Subject: [PATCH 08/11] Add building recruiting --- src/game/Building.cpp | 2 ++ src/game/Level.cpp | 25 +++++++++++++++++++++++-- src/game/Level.hpp | 4 +++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/game/Building.cpp b/src/game/Building.cpp index bd499e6..8c22104 100644 --- a/src/game/Building.cpp +++ b/src/game/Building.cpp @@ -43,6 +43,8 @@ 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) { diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 0f5ca9b..8526a95 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -146,6 +146,7 @@ int Level::selectBuilding(int tileX, int tileY) return -1; } + void Level::handleEvent(Engine& engine, SDL_Event& event) { switch (m_state) @@ -165,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; } @@ -264,6 +268,23 @@ 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 + //select UnitId + + + if(b.check_money(500)) { + if(b.check_spawn(m_units)){ + addUnit(Unit(b.m_x, b.m_y, u_faction, UnitId::INFANTERY, UnitState::IDLE)); + m_state = LevelState::SELECTING_STATE; + m_selectedBuilding = -1; + } + } +} + void Level::handleSelectingEvents(Engine& engine, SDL_Event& event) { switch (event.type) @@ -352,8 +373,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 0b843ee..e8ba8a4 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 -- GitLab From cc26295222891596fa5280c9634ef2598e34c8ab Mon Sep 17 00:00:00 2001 From: Max Cherris <MCherris@protonmail.com> Date: Wed, 5 Feb 2025 16:06:28 +0100 Subject: [PATCH 09/11] Update handleRecruitingEvents --- src/game/Building.cpp | 15 +++++++++++++++ src/game/Building.hpp | 6 ++++++ src/game/Level.cpp | 7 ++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/game/Building.cpp b/src/game/Building.cpp index 8c22104..e839cf1 100644 --- a/src/game/Building.cpp +++ b/src/game/Building.cpp @@ -65,4 +65,19 @@ bool Building::check_money(int price) { } +std::vector<int> Building::recruitableUnits() { + + if(this->m_id == BuildingId::FACTORY) { + return {0,1,2,3,4,5,6,7,8,9,10}; + } + + if(this->m_id == BuildingId::PORT) { + return {}; + } + + if(this->m_id ==BuildingId::SATELLITE) { + return {}; + } +} + } // namespace advanced_wars \ No newline at end of file diff --git a/src/game/Building.hpp b/src/game/Building.hpp index 2451a9a..21705d7 100644 --- a/src/game/Building.hpp +++ b/src/game/Building.hpp @@ -66,6 +66,12 @@ class Building 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<int> recruitableUnits(); }; } // namespace advanced_wars \ No newline at end of file diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 8526a95..df984b4 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -272,13 +272,14 @@ 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 - //select UnitId + //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, UnitId::INFANTERY, UnitState::IDLE)); + addUnit(Unit(b.m_x, b.m_y, u_faction, u_id, UnitState::IDLE)); m_state = LevelState::SELECTING_STATE; m_selectedBuilding = -1; } -- GitLab From c21b116549f073a6c1c2f6bfe223e4cd52201c3d Mon Sep 17 00:00:00 2001 From: Frederik <frederik@prasch.de> Date: Wed, 5 Feb 2025 16:49:37 +0100 Subject: [PATCH 10/11] Rename satelite to airport and switch enum position to reflect right index in textures --- src/game/Building.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/game/Building.hpp b/src/game/Building.hpp index 21705d7..e6474c1 100644 --- a/src/game/Building.hpp +++ b/src/game/Building.hpp @@ -22,8 +22,8 @@ enum class BuildingId HEADQUARTER = 0, CITY = 1, FACTORY = 2, - PORT = 3, - SATELLITE = 4, + AIRPORT = 3, + PORT = 4, }; class Building -- GitLab From 6cde609a129bc4e20fc08b9aa6c976e879392c9c Mon Sep 17 00:00:00 2001 From: Frederik <frederik@prasch.de> Date: Wed, 5 Feb 2025 16:50:45 +0100 Subject: [PATCH 11/11] Fill recuitable units with the right values for relevant building types --- src/game/Building.cpp | 64 +++++++++++++++++++++++++++++-------------- src/game/Building.hpp | 14 +++++----- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/game/Building.cpp b/src/game/Building.cpp index e839cf1..8382753 100644 --- a/src/game/Building.cpp +++ b/src/game/Building.cpp @@ -29,26 +29,30 @@ void Building::render(Engine& engine, int scale) &dst, 0, NULL, SDL_FLIP_NONE); } -void Building::switch_faction(BuildingFaction faction) { +void Building::switch_faction(BuildingFaction faction) +{ this->m_faction = faction; - if(this->m_id == BuildingId::HEADQUARTER) { + 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() { +// 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) +{ - -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) { + for (auto& [id, unit] : units) + { + if (unit.m_x == this->m_x && unit.m_y == this->m_y) + { return false; } } @@ -56,28 +60,48 @@ bool Building::check_spawn(std::unordered_map<int, advanced_wars::Unit>& units) 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) { +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<int> Building::recruitableUnits() { +std::vector<UnitId> Building::recruitableUnits() +{ - if(this->m_id == BuildingId::FACTORY) { - return {0,1,2,3,4,5,6,7,8,9,10}; + 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 {}; + if (this->m_id == BuildingId::PORT) + { + return {UnitId::LANDER, UnitId::CRUISER, UnitId::SUBMARINE, UnitId::BATTLESHIP}; } - if(this->m_id ==BuildingId::SATELLITE) { - return {}; + 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 e6474c1..2bceaa9 100644 --- a/src/game/Building.hpp +++ b/src/game/Building.hpp @@ -1,9 +1,9 @@ #pragma once -#include <unordered_map> #include "Engine.hpp" #include "Scene.hpp" #include "Unit.hpp" +#include <unordered_map> namespace advanced_wars { @@ -45,7 +45,6 @@ class Building */ void switch_faction(BuildingFaction faction); - /* checks if the tile ontop of the building is free */ @@ -57,21 +56,22 @@ class Building 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(); + 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 + 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<int> recruitableUnits(); + std::vector<UnitId> recruitableUnits(); }; } // namespace advanced_wars \ No newline at end of file -- GitLab