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

Merge branch 'buildingFeatures' into 'main'

Building features

See merge request !34
parents 88f1189a 1687c1cf
Branches
No related tags found
2 merge requests!34Building features,!29Merge main into box2d to implement physics
......@@ -105,4 +105,8 @@ std::vector<UnitId> Building::recruitableUnits()
return {};
}
BuildingId Building::getBuildingId() {
return this->m_id;
}
} // namespace advanced_wars
\ No newline at end of file
......@@ -39,12 +39,6 @@ class Building
*/
bool check_money(int price, int playerMoney);
/*
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
......@@ -56,6 +50,8 @@ class Building
*
*/
std::vector<UnitId> recruitableUnits();
BuildingId getBuildingId();
};
} // namespace advanced_wars
\ No newline at end of file
......@@ -613,7 +613,6 @@ void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
if (event.key.keysym.sym == SDLK_RETURN)
{
std::pair<int, int> tilePos = m_currentPos.getPosition();
selectEntity(
tilePos.first * 16 * RENDERING_SCALE, tilePos.second * 16 * RENDERING_SCALE);
......@@ -662,17 +661,47 @@ void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
}
}
// Show according menu options if unit has same/different faction than current
// player
Unit& u = m_units.at(m_selectedUnit);
if (m_units.at(m_selectedUnit).getFaction() == m_turnQ.front().getFaction() &&
m_units.at(m_selectedUnit).getState() != UnitState::UNAVAILABLE)
{
m_contextMenu.setOptions({"Move", "Attack", "Info", "Wait", "End Turn"});
m_captureBuilding = -1;
for (auto& [id, building] : m_buildings)
{
if (building.m_x == u.m_x && building.m_y == u.m_y)
{
if (building.getFaction() !=
static_cast<BuildingFaction>(u.getFaction()))
{
m_captureBuilding = id;
m_contextMenu.setOptions(
{"Capture", "Move", "Attack", "Info", "Wait", "End Turn"});
break;
}
}
}
if (m_captureBuilding == -1)
{
m_contextMenu.setOptions(
{"Move", "Attack", "Info", "Wait", "End Turn"});
}
}
else
{
m_contextMenu.setOptions({"Info", "End Turn"});
}
m_state = LevelState::MENUACTIVE_STATE;
}
else
{
BuildingId b_id = m_buildings.at(m_selectedBuilding).getBuildingId();
BuildingFaction b_faction = m_buildings.at(m_selectedBuilding).getFaction();
if (b_id == BuildingId::CITY || b_id == BuildingId::HEADQUARTER ||
b_faction == static_cast<BuildingFaction>(5))
{
m_contextMenu.setOptions({"Info", "End Turn"});
}
else
{
......@@ -691,6 +720,7 @@ void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
m_state = LevelState::MENUACTIVE_STATE;
}
}
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
......@@ -801,13 +831,24 @@ 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,
UnitId::HEAVY_TANK});
m_recruitingMenu.setOptions(m_buildings.at(m_selectedBuilding).recruitableUnits());
std::cout << "no training here" << std::endl;
}
if (cmd == "Capture")
{
Building& b = m_buildings.at(m_captureBuilding);
UnitFaction u_f = m_units.at(m_selectedUnit).getFaction();
BuildingFaction b_f = static_cast<BuildingFaction>(u_f);
b.switch_faction(b_f);
m_units.at(m_selectedUnit).setState(UnitState::UNAVAILABLE);
m_state = LevelState::SELECTING_STATE;
m_selectedBuilding = -1;
m_selectedUnit = -1;
m_showReachableTiles = false;
m_showAttackableTiles = false;
}
if (cmd == "End Turn")
{
m_state = LevelState::SELECTING_STATE;
......@@ -816,7 +857,6 @@ void Level::handleMenuActiveEvents(Engine& engine, SDL_Event& event)
changeTurn();
}
}
break;
default:
break;
......@@ -831,7 +871,6 @@ void Level::handleMovementEvents(Engine& engine, SDL_Event& event)
handlePositionMarker(engine, event);
if (event.key.keysym.sym == SDLK_RETURN)
{
handleMovement(m_currentPos.getPosition());
}
if (event.key.keysym.sym == SDLK_ESCAPE)
......@@ -886,5 +925,7 @@ void Level::handleAttackingEvents(Engine& engine, SDL_Event& event)
break;
}
}
//************end event handler delegates for different level states*****************************
//************end event handler delegates for different level
// states*****************************
} // namespace advanced_wars
......@@ -134,6 +134,7 @@ class Level : public Scene
int m_selectedUnit;
int m_selectedBuilding;
int m_captureBuilding;
ContextMenu m_contextMenu;
RecruitingMenu m_recruitingMenu;
bool toggle_Helpmenu = false;
......
......@@ -109,7 +109,7 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
// Buildings
std::vector<std::string> building_factions(
{"red", "blue", "yellow", "green", "purple", "neutral"});
{"red", "blue", "green", "yellow", "purple", "neutral"});
// every sub data set of buildings
for (std::string faction : building_factions)
......
......@@ -324,4 +324,12 @@ void Unit::setState(UnitState state)
this->m_state = state;
}
bool Unit::hasAttacked() {
return this->m_hasAttacked;
}
bool Unit::hasMoved() {
return this->m_hasMoved;
}
} // namespace advanced_wars
\ No newline at end of file
......@@ -137,6 +137,9 @@ class Unit
*/
std::vector<Unit*> getUnitsInRangeWithDamagePotential(const std::vector<Unit*>& allUnits);
bool hasMoved();
bool hasAttacked();
private:
UnitFaction m_faction; // The faction to which this unit belongs.
UnitId m_id; // The identifier for the unit type.
......
#include "Menu.hpp"
#include "../Level.hpp"
#include "../Building.hpp"
#include "../Config.hpp"
#include "../Level.hpp"
#include "../Spritesheet.hpp"
#include "../Tile.hpp"
#include "../Unit.hpp"
......@@ -16,8 +16,7 @@ namespace advanced_wars
Menu::Menu(int selectedOption, const std::string& level_filepath)
: m_selectedOption(selectedOption), m_level_filepath(level_filepath),
m_options({"Start Game", "Options", "Exit"}),
m_backgroundTexture(nullptr)
m_options({"Start Game", "Options", "Exit"}), m_backgroundTexture(nullptr)
{
}
......@@ -199,9 +198,8 @@ void Menu::handleEvent(Engine& engine, SDL_Event& event)
Effect(5, 15, EffectId::AIR_EXPLOSION, true),
Effect(5, 18, EffectId::NAVAL_EXPLOSION, true)});
// std::shared_ptr<Level> level =
// std::make_shared<Level>("Osnabrück", 20, 20, tiles, buildings, units,
// effects, std::queue<Player>{});
std::shared_ptr<Level> level = std::make_shared<Level>(
"Osnabrück", 20, 20, tiles, buildings, units, effects, std::queue<Player>{});
engine.pushScene(Level::loadLevel(m_level_filepath, engine));
}
......
......@@ -5,46 +5,40 @@
namespace advanced_wars
{
RecruitingMenu::RecruitingMenu() : m_selectedOption(0), unitNames({
{UnitId::INFANTERY, {"Infantry", 100}},
{UnitId::MECHANIZED_INFANTERY, {"Bazooka", 200}},
{UnitId::RECON, {"Recon", 300}},
{UnitId::APC, {"APC", 400}},
{UnitId::ARTILLERY, {"Artillery", 500}},
{UnitId::ANTI_AIR_TANK, {"AA Tank", 600}},
{UnitId::ANTI_AIR_MISSILE_LAUNCHER, {"Rocket AA", 700}},
{UnitId::ROCKET_ARTILLERY, {"MLRS", 800}},
{UnitId::MEDIUM_TANK, {"Medium Tank", 900}},
{UnitId::NEO_TANK, {"Neo Tank", 1000}},
{UnitId::HEAVY_TANK, {"Heavy Tank", 1100}},
{UnitId::LANDER, {"Lander", 1200}},
{UnitId::CRUISER, {"Cruiser", 1300}},
{UnitId::SUBMARINE, {"Submarine", 1400}},
{UnitId::BATTLESHIP, {"Battleship", 1500}},
{UnitId::TRANSPORT_HELICOPTER, {"Chinook", 1600}},
{UnitId::BATTLE_HELICOPTER, {"Helicopter", 1700}},
{UnitId::FIGHTER, {"Fighter", 1800}},
{UnitId::BOMBER, {"Bomber", 1900}}
}) {
{UnitId::INFANTERY, "Infantry"},
{UnitId::MECHANIZED_INFANTERY, "Bazooka"},
{UnitId::RECON, "Recon"},
{UnitId::APC, "APC"},
{UnitId::ARTILLERY, "Artillery"},
{UnitId::ANTI_AIR_TANK, "AA Tank"},
{UnitId::ANTI_AIR_MISSILE_LAUNCHER, "Rocket AA"},
{UnitId::ROCKET_ARTILLERY, "MLRS"},
{UnitId::MEDIUM_TANK, "Medium Tank"},
{UnitId::NEO_TANK, "Neo Tank"},
{UnitId::HEAVY_TANK, "Heavy Tank"},
{UnitId::LANDER, "Lander"},
{UnitId::CRUISER, "Cruiser"},
{UnitId::SUBMARINE, "Submarine"},
{UnitId::BATTLESHIP, "Battleship"},
{UnitId::TRANSPORT_HELICOPTER, "Chinook"},
{UnitId::BATTLE_HELICOPTER, "Helicopter"},
{UnitId::FIGHTER, "Fighter"},
{UnitId::BOMBER, "Bomber"}
})
{
}
void RecruitingMenu::setOptions(const std::vector<UnitId> recruitableUnits) {
std::vector<std::pair<std::string, int>> options;
for (UnitId id : recruitableUnits) {
options.push_back(unitNames.at(id));
cost2UnitId.insert(std::make_pair(unitNames.at(id).second, id));
}
m_options = options;
m_options = recruitableUnits;
m_selectedOption = 0;
}
void RecruitingMenu::render(Engine& engine)
{
Config& config = engine.getUnitConfig();
Spritesheet* spritesheet = engine.getSpritesheet();
if (TTF_Init() == -1)
......@@ -75,21 +69,21 @@ namespace advanced_wars
int spacing = 20; // Abstand zwischen den Optionen
// box around options
SDL_SetRenderDrawColor(engine.renderer(), 0, 0, 255, 255);
SDL_Rect box = {m_x, m_y - 3, 150, static_cast<int>(m_options.size() * spacing)};
SDL_Rect box = {m_x, m_y - 3, 175, static_cast<int>(m_options.size() * spacing)};
SDL_RenderFillRect(engine.renderer(), &box);
SDL_SetRenderDrawColor(engine.renderer(), 0, 0, 0, 255);
int i = 0;
for (auto& [render_name, cost] : m_options)
for (UnitId id : m_options)
{
//std::pair<std::string, int> unit_option = unitNames.at(cost2UnitId.at(cost));
if(i == m_selectedOption) {
m_selectedId = cost2UnitId.at(cost);
m_selectedId = id;
}
SDL_Surface* textSurface = TTF_RenderText_Solid(
font, render_name.c_str(), (i == m_selectedOption) ? yellow : white);
font, unitNames.at(id).c_str(), (i == m_selectedOption) ? yellow : white);
if (!textSurface)
{
continue;
......@@ -103,7 +97,7 @@ namespace advanced_wars
SDL_Texture* unit_texture = spritesheet->getUnitTextures()
.at(static_cast<int>(UnitFaction::URED))
.at(static_cast<int>(cost2UnitId.at(cost)))
.at(static_cast<int>(id))
.at(static_cast<int>(UnitState::IDLE))
.first;
......@@ -124,7 +118,7 @@ namespace advanced_wars
SDL_RenderCopy(engine.renderer(), unit_texture, &src_rect, &trgt_rect);
SDL_Surface* costSurface = TTF_RenderText_Solid(
font, std::to_string(cost).c_str(), (i == m_selectedOption) ? yellow : white);
font, std::to_string(config.getUnitCost(id)).c_str(), (i == m_selectedOption) ? yellow : white);
if (!textSurface)
{
continue;
......
......@@ -11,10 +11,10 @@ namespace advanced_wars
private:
size_t m_selectedOption;
std::vector<std::pair<std::string, int>> m_options;
std::vector<UnitId> m_options;
int m_x;
int m_y;
const std::unordered_map <UnitId ,std::pair <std::string, int>> unitNames;
const std::unordered_map <UnitId , std::string> unitNames;
std::unordered_map<int, UnitId> cost2UnitId;
UnitId m_selectedId;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment