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

Implement player losing money when recruiting units

parent 7724f108
Branches
No related tags found
2 merge requests!29Merge main into box2d to implement physics,!28Add player interaction handling
......@@ -65,14 +65,10 @@ 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(int price, int playerMoney)
{
// replace 400 with player.money and replace price with chosenUnit.price
if (400 > price)
{
return false;
}
return true;
return (playerMoney >= price);
}
std::vector<UnitId> Building::recruitableUnits()
......
......@@ -37,7 +37,7 @@ class Building
/*
checks if the player has enough money for the unit to be recruited
*/
bool check_money(int price);
bool check_money(int price, int playerMoney);
/*
When the building is selected, the player should have the ability to recruit a selection of
......
......@@ -430,17 +430,25 @@ void Level::handleRecruitingEvent(Engine& engine, SDL_Event& event)
Building& b = m_buildings.at(m_selectedBuilding);
UnitFaction u_faction = static_cast<UnitFaction>(b.m_faction);
UnitId unit_id = m_recruitingMenu.getSelectedOption();
int cost = engine.getUnitConfig().getUnitCost(unit_id);
if (b.check_money(500))
if (b.check_money(cost, m_turnQ.front().getMoney()))
{
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_turnQ.front().spendMoney(cost);
m_selectedBuilding = -1;
}
}
else
{
std::cout << "You dont have enough money, current money: "
<< m_turnQ.front().getMoney() << " || needed money: " << cost
<< std::endl;
}
}
}
}
......
......@@ -85,4 +85,14 @@ UnitFaction Player::getFaction()
{
return m_faction;
}
int Player::getMoney()
{
return m_money;
}
void Player::spendMoney(int toSpend)
{
m_money -= toSpend;
}
} // namespace advanced_wars
\ No newline at end of file
......@@ -38,6 +38,10 @@ class Player
void endTurn(std::unordered_map<int, Unit>& lvUnits);
UnitFaction getFaction();
int getMoney();
void spendMoney(int toSpend);
};
} // namespace advanced_wars
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment