Skip to content
Snippets Groups Projects
Commit 58a84720 authored by Frederik's avatar Frederik
Browse files

Fix EventHandler for Movement and Attack range that got broken by merge

parent dfc7bff5
No related branches found
No related tags found
1 merge request!23Unit movement
......@@ -120,6 +120,11 @@ Spritesheet* Engine::getSpritesheet()
return m_spritesheet.value();
}
Config& Engine::getUnitConfig()
{
return m_unitConfig;
}
SDL_Renderer* Engine::renderer()
{
return this->m_SDLRenderer;
......
......@@ -371,7 +371,8 @@ Effect Level::removeEffect(int id)
return value;
}
void Level::handleRecruitingEvent(Engine& engine, SDL_Event& event) {
void Level::handleRecruitingEvent(Engine& engine, SDL_Event& event)
{
switch (event.type)
{
......@@ -390,15 +391,19 @@ void Level::handleRecruitingEvent(Engine& engine, SDL_Event& event) {
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));
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, engine.getUnitConfig()));
m_state = LevelState::SELECTING_STATE;
m_selectedBuilding = -1;
}
}
}
}}
}
}
//*******************helper functions for event Handling*************************************
......@@ -415,6 +420,9 @@ void Level::handleAttack(std::pair<int, int> tilePos)
Unit& attacking = m_units.at(m_selectedUnit);
Unit& defending = m_units.at(targetedUnit);
if (m_attackableUnitIds.find(targetedUnit) != m_attackableUnitIds.end())
{
attacking.attack(defending);
if (attacking.m_health <= 0)
{
......@@ -425,9 +433,16 @@ void Level::handleAttack(std::pair<int, int> tilePos)
removeUnit(targetedUnit);
}
m_selectedUnit = -1;
m_showAttackableTiles = false;
m_showReachableTiles = false;
m_state = LevelState::SELECTING_STATE;
}
else
{
std::cout << "No target in range clicked!" << std::endl;
}
}
else
{
std::cout << "No valid target clicked" << std::endl;
}
......@@ -444,10 +459,31 @@ void Level::handleMovement(std::pair<int, int> tilePos)
return;
}
}
bool isReachable = false;
for (auto& pos : m_reachableTiles)
{
if (pos == tilePos)
{
isReachable = true;
break;
}
}
if (isReachable)
{
m_units.at(m_selectedUnit).updatePosition(tilePos.first, tilePos.second);
m_selectedUnit = -1;
m_showAttackableTiles = false;
m_showReachableTiles = false;
m_state = LevelState::SELECTING_STATE;
}
else
{
std::cout << "Unglültige Bewegunsposition!" << std::endl;
}
}
void Level::handlePositionMarker(Engine& engine, SDL_Event& event)
{
......@@ -486,6 +522,40 @@ void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
(tilePos.second * 16 + 15) * RENDERING_SCALE);
if (m_selectedUnit >= 0)
{
m_reachableTiles = calculateMovementRange(m_units.at(m_selectedUnit));
m_units.at(m_selectedUnit).on_left_click(event);
m_showReachableTiles = true;
std::vector<Unit*> allUnits;
for (auto& [id, unit] : m_units)
{
allUnits.push_back(&unit);
}
std::vector<Unit*> attackableTargets =
m_units.at(m_selectedUnit).getUnitsInRangeWithDamagePotential(allUnits);
m_attackableTiles.clear();
m_showAttackableTiles = true;
m_attackableUnitIds.clear();
for (Unit* target : attackableTargets)
{
// Füge die Position jedes angreifbaren Ziels hinzu
m_attackableTiles.emplace_back(target->m_x, target->m_y);
// Angreifbaren Einheits-ID setzen
for (auto& [id, unit] : m_units)
{
if (&unit == target)
{
m_attackableUnitIds.insert(id);
break;
}
}
}
m_contextMenu.setOptions({"Move", "Attack", "Info", "Wait"});
}
else
......@@ -519,6 +589,8 @@ void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
}
else
{
m_showReachableTiles = false;
m_showAttackableTiles = false;
m_state = LevelState::SELECTING_STATE;
}
}
......@@ -537,6 +609,8 @@ void Level::handleMenuActiveEvents(Engine& engine, SDL_Event& event)
m_selectedUnit = -1;
m_selectedBuilding = -1;
m_state = LevelState::SELECTING_STATE;
m_showAttackableTiles = false;
m_showReachableTiles = false;
}
if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN)
{
......@@ -580,17 +654,10 @@ void Level::handleMenuActiveEvents(Engine& engine, SDL_Event& event)
m_recruitingMenu.update(
(tilePos.first * 16 + 15) * RENDERING_SCALE,
(tilePos.second * 16 + 15) * RENDERING_SCALE);
m_recruitingMenu.setOptions({
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,
m_recruitingMenu.setOptions(
{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});
std::cout << "no training here" << std::endl;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment