diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 3af9f1fab9583b61ffc528269f42472f0edd17bc..9f6a4dfc3379abf5f1a7d19e0ff71af0997b5432 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -277,26 +277,32 @@ Effect Level::removeEffect(int id) void Level::handleRecruitingEvent(Engine& engine, SDL_Event& event) { - Building& b = m_buildings.at(m_selectedBuilding); - UnitFaction u_faction = static_cast<UnitFaction> (b.m_faction); - - - std::vector<UnitId> recruitableUnits = b.recruitableUnits(); - //m_recruitingmenu.setoptions(recruitableUnits) - //show appropriate interface -> provide vector of UnitId - - //show Interface here - //select UnitId - UnitId unit_id = UnitId::INFANTERY; - - if(b.check_money(500)) { - if(b.check_spawn(m_units)){ - addUnit(Unit(b.m_x, b.m_y, u_faction, unit_id, UnitState::IDLE)); - m_state = LevelState::SELECTING_STATE; - m_selectedBuilding = -1; + switch (event.type) + { + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_ESCAPE) + { + m_state = LevelState::MENUACTIVE_STATE; } - } -} + if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN) + { + m_recruitingMenu.handleEvent(engine, event); + } + if (event.key.keysym.sym == SDLK_RETURN) + { + Building& b = m_buildings.at(m_selectedBuilding); + UnitFaction u_faction = static_cast<UnitFaction> (b.m_faction); + UnitId unit_id = m_recruitingMenu.getSelectedOption(); + + if(b.check_money(500)) { + if(b.check_spawn(m_units)){ + addUnit(Unit(b.m_x, b.m_y, u_faction, unit_id, UnitState::IDLE)); + m_state = LevelState::SELECTING_STATE; + m_selectedBuilding = -1; + } + } + } +}} //*******************helper functions for event Handling************************************* diff --git a/src/game/ui/Recruitingmenu.cpp b/src/game/ui/Recruitingmenu.cpp index fee0fc106b14857f9079c48b2594836bdf4e42b7..1ca6289adddee22ad95bbbac612e0a434e19ae98 100644 --- a/src/game/ui/Recruitingmenu.cpp +++ b/src/game/ui/Recruitingmenu.cpp @@ -84,7 +84,10 @@ namespace advanced_wars for (auto& [render_name, cost] : m_options) { //std::pair<std::string, int> unit_option = unitNames.at(cost2UnitId.at(cost)); - + if(i == m_selectedOption) { + m_selectedId = cost2UnitId.at(cost); + } + SDL_Surface* textSurface = TTF_RenderText_Solid( font, render_name.c_str(), (i == m_selectedOption) ? yellow : white); if (!textSurface) @@ -169,4 +172,8 @@ void RecruitingMenu::update(int x, int y) this->m_y = y; } +UnitId RecruitingMenu::getSelectedOption(){ + return m_selectedId; +} + }//namespace advance_wars \ No newline at end of file diff --git a/src/game/ui/Recruitingmenu.hpp b/src/game/ui/Recruitingmenu.hpp index a2bfdcb546ddea2841e6a4983b11576ee158e1b4..01bfdb83bd93b2881071bdc027e3b824ad2bcbc8 100644 --- a/src/game/ui/Recruitingmenu.hpp +++ b/src/game/ui/Recruitingmenu.hpp @@ -16,17 +16,16 @@ namespace advanced_wars int m_y; const std::unordered_map <UnitId ,std::pair <std::string, int>> unitNames; std::unordered_map<int, UnitId> cost2UnitId; + UnitId m_selectedId; - void handleEvent(Engine& engine, SDL_Event& event); - - - void selectSprite(); - std::string getSelectedOption(); - public: + UnitId getSelectedOption(); + + void handleEvent(Engine& engine, SDL_Event& event); + void update(int x, int y); RecruitingMenu();