Skip to content
Snippets Groups Projects
Commit 212f0cd1 authored by Frederik Alexander Keens's avatar Frederik Alexander Keens
Browse files

Merge branch 'building_interaction' into 'main'

Implement check_spawn function

See merge request !22
parents 639e4955 f8314106
Branches
No related tags found
2 merge requests!29Merge main into box2d to implement physics,!22Implement check_spawn function
#include "Building.hpp"
#include "Spritesheet.hpp"
#include <iostream>
namespace advanced_wars
{
......@@ -28,4 +29,79 @@ void Building::render(Engine& engine, int scale)
&dst, 0, NULL, SDL_FLIP_NONE);
}
void Building::switch_faction(BuildingFaction faction)
{
this->m_faction = faction;
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()
{
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)
{
if (unit.m_x == this->m_x && unit.m_y == this->m_y)
{
return false;
}
}
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)
{
return false;
}
return true;
}
std::vector<UnitId> Building::recruitableUnits()
{
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 {UnitId::LANDER, UnitId::CRUISER, UnitId::SUBMARINE, UnitId::BATTLESHIP};
}
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
......@@ -2,18 +2,19 @@
#include "Engine.hpp"
#include "Scene.hpp"
#include "Unit.hpp"
#include <unordered_map>
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
......@@ -21,8 +22,8 @@ enum class BuildingId
HEADQUARTER = 0,
CITY = 1,
FACTORY = 2,
PORT = 3,
SATELLITE = 4,
AIRPORT = 3,
PORT = 4,
};
class Building
......@@ -36,6 +37,41 @@ class Building
BuildingFaction m_faction;
void render(Engine& engine, int scale);
/**
Changes the faction to the specified one
@param faction The new faction the unit will belong to
*/
void switch_faction(BuildingFaction faction);
/*
checks if the tile ontop of the building is free
*/
bool check_spawn(std::unordered_map<int, advanced_wars::Unit>& units);
/*
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();
/**
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<UnitId> recruitableUnits();
};
} // namespace advanced_wars
\ No newline at end of file
......@@ -48,6 +48,9 @@ Level::Level(
{
throw std::runtime_error("level tile mismatch");
}
m_selectedBuilding = -1;
m_selectedUnit = -1;
};
Level Level::loadLevel(std::string path)
......@@ -143,6 +146,7 @@ int Level::selectBuilding(int tileX, int tileY)
return -1;
}
void Level::handleEvent(Engine& engine, SDL_Event& event)
{
switch (m_state)
......@@ -162,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;
}
......@@ -261,6 +268,24 @@ 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 -> 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, u_id, UnitState::IDLE));
m_state = LevelState::SELECTING_STATE;
m_selectedBuilding = -1;
}
}
}
void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
{
switch (event.type)
......@@ -349,8 +374,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;
}
}
......
......@@ -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
......@@ -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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment