Skip to content
Snippets Groups Projects
Commit 21e608fb authored by Frederik's avatar Frederik
Browse files

Add checks for attacking allies and debug output for clicking non valid target

parent b5ffe3d9
Branches
No related tags found
2 merge requests!29Merge main into box2d to implement physics,!20Level event handler
......@@ -411,9 +411,17 @@ void Level::handleAttackingEvents(Engine& engine, SDL_Event& event)
if (event.button.button == SDL_BUTTON_LEFT)
{
std::pair<int, int> tilePos = calcTilePos(event.button.x, event.button.y);
int targetedUnit;
if ((targetedUnit = selectUnit(tilePos.first, tilePos.second)) >= 0)
int targetedUnit = selectUnit(tilePos.first, tilePos.second);
if (targetedUnit >= 0)
{
if (m_units.at(m_selectedUnit).getFaction() ==
m_units.at(targetedUnit).getFaction())
{
std::cout << "You cannot attack your allies!" << std::endl;
return;
}
Unit& attacking = m_units.at(m_selectedUnit);
Unit& defending = m_units.at(targetedUnit);
attacking.attack(defending);
......@@ -428,6 +436,10 @@ void Level::handleAttackingEvents(Engine& engine, SDL_Event& event)
m_selectedUnit = -1;
m_state = LevelState::SELECTING_STATE;
}
else
{
std::cout << "No valid target clicked" << std::endl;
}
}
break;
default:
......
......@@ -216,4 +216,9 @@ bool Unit::inRange(Unit& enemy)
return false;
}
UnitFaction Unit::getFaction()
{
return this->m_faction;
}
} // namespace advanced_wars
\ No newline at end of file
......@@ -116,6 +116,8 @@ class Unit
*/
void on_left_click(SDL_Event event);
UnitFaction getFaction();
private:
UnitFaction m_faction;
UnitId m_id;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment