Skip to content
Snippets Groups Projects
Commit 94c17aa5 authored by Lorenz Martin Diel's avatar Lorenz Martin Diel
Browse files

Updated unit_context_menu + ChangingUnitState to Unavailable + Updated...

Updated unit_context_menu + ChangingUnitState to Unavailable + Updated getUnitsInRangeWithDamagePotential to care about faction
parent 512e95d0
No related branches found
No related tags found
No related merge requests found
......@@ -74,6 +74,8 @@ file(GLOB HDF5_FILES ${RES_DIR}/*.h5)
# Executable erstellen
add_executable(advanced_wars ${ADVANCED_WARS_SOURCES})
set(CMAKE_MODULE_PATH ${ADVANCED_WARS_SOURCE_DIR}/cmake/ ${CMAKE_MODULE_PATH})
# Plattform-spezifische Konfiguration
if(APPLE)
# SDL2 Frameworks für macOS
......@@ -159,4 +161,3 @@ foreach(H5_FILE ${HDF5_FILES})
)
endforeach()
set(CMAKE_MODULE_PATH ${ADVANCED_WARS_SOURCE_DIR}/cmake/ ${CMAKE_MODULE_PATH})
\ No newline at end of file
......@@ -83,6 +83,7 @@ std::shared_ptr<Level> Level::loadLevel(std::string path, Engine& engine)
std::vector<Unit> units;
tiles.reserve(width * height);
bool has_factions[] = {false, false, false, false, false};
for (int i = 0; i < level_tilesarray.size(); i++)
{
int x = i % width;
......@@ -476,6 +477,7 @@ void Level::handleAttack(std::pair<int, int> tilePos)
m_showAttackableTiles = false;
m_showReachableTiles = false;
m_state = LevelState::SELECTING_STATE;
attacking.setState(UnitState::UNAVAILABLE);
}
else
{
......@@ -490,6 +492,8 @@ void Level::handleAttack(std::pair<int, int> tilePos)
void Level::handleMovement(std::pair<int, int> tilePos)
{
//Herausnehmen um sich nicht bewegen zu müssen um ins kontextmenü zu kommen
/*
for (auto& [id, unit] : m_units)
{
if (unit.m_x == tilePos.first && unit.m_y == tilePos.second)
......@@ -498,7 +502,7 @@ void Level::handleMovement(std::pair<int, int> tilePos)
std::cout << "Unit already at clicked position" << std::endl;
return;
}
}
}*/
bool isReachable = false;
......@@ -514,10 +518,13 @@ void Level::handleMovement(std::pair<int, int> tilePos)
if (isReachable)
{
m_units.at(m_selectedUnit).updatePosition(tilePos.first, tilePos.second);
m_selectedUnit = -1;
// m_selectedUnit = -1;
m_showAttackableTiles = false;
m_showReachableTiles = false;
m_state = LevelState::SELECTING_STATE;
m_state = LevelState::MENUACTIVE_STATE;
m_contextMenu.update(
(tilePos.first * 16 + 15) * RENDERING_SCALE,
(tilePos.second * 16 + 15) * RENDERING_SCALE);
}
else
{
......@@ -557,56 +564,33 @@ void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
tilePos.first * 16 * RENDERING_SCALE, tilePos.second * 16 * RENDERING_SCALE);
if (m_selectedUnit >= 0 || m_selectedBuilding >= 0)
{
m_contextMenu.update(
(tilePos.first * 16 + 15) * RENDERING_SCALE,
(tilePos.second * 16 + 15) * RENDERING_SCALE);
if (m_selectedUnit >= 0)
{
if (m_units.at(m_selectedUnit).getState() != UnitState::UNAVAILABLE)
{
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);
m_contextMenu.setOptions({"Attack", "Info", "Wait"});
m_state = LevelState::MOVEMENT_STATE;
}
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)
else
{
m_attackableUnitIds.insert(id);
break;
}
}
std::cout << "unit is unavailable" << std::endl;
}
m_contextMenu.setOptions({"Move", "Attack", "Info", "Wait"});
}
else
{
m_contextMenu.setOptions({"Train", "Info", "Wait"});
}
m_state = LevelState::MENUACTIVE_STATE;
}
}
}
break;
case SDL_MOUSEBUTTONDOWN:
/*
if (event.button.button == SDL_BUTTON_LEFT)
{
selectEntity(event.button.x, event.button.y);
......@@ -634,6 +618,7 @@ void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
m_state = LevelState::SELECTING_STATE;
}
}
*/
default:
break;
}
......@@ -646,11 +631,9 @@ void Level::handleMenuActiveEvents(Engine& engine, SDL_Event& event)
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
m_selectedUnit = -1;
m_selectedBuilding = -1;
m_state = LevelState::SELECTING_STATE;
m_showAttackableTiles = false;
m_showReachableTiles = false;
m_units.at(m_selectedUnit)
.updatePosition(unit_fallback_position.first, unit_fallback_position.second);
}
if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN)
{
......@@ -661,12 +644,20 @@ void Level::handleMenuActiveEvents(Engine& engine, SDL_Event& event)
std::string cmd = m_contextMenu.getSelectedOption();
if (cmd == "Wait")
{
// m_units.at(m_selectedUnit).setState(UnitState::UNAVAILABLE);
// Pruefen, ob der Key existiert
auto it = m_units.find(m_selectedUnit);
if (it != m_units.end())
{
it->second.setState(UnitState::UNAVAILABLE);
std::cout << "Unit state set to UNAVAILABLE." << std::endl;
m_state = LevelState::SELECTING_STATE;
}
if (cmd == "Move")
else
{
m_state = LevelState::MOVEMENT_STATE;
// Hier Pathfinding einsetzen
std::cerr << "Selected unit id is invalid: " << m_selectedUnit << std::endl;
}
}
if (cmd == "Attack")
{
......@@ -717,11 +708,26 @@ void Level::handleMovementEvents(Engine& engine, SDL_Event& event)
handlePositionMarker(engine, event);
if (event.key.keysym.sym == SDLK_RETURN)
{
if (m_units.find(m_selectedUnit) != m_units.end())
{
Unit& selectedUnit = m_units.at(m_selectedUnit);
unit_fallback_position = {selectedUnit.m_x, selectedUnit.m_y};
}
else
{
std::cerr << "No unit selected or invalid unit index!" << std::endl;
}
handleMovement(m_currentPos.getPosition());
}
if (event.key.keysym.sym == SDLK_ESCAPE)
{
m_state = LevelState::MENUACTIVE_STATE;
m_selectedUnit = -1;
m_selectedBuilding = -1;
m_state = LevelState::SELECTING_STATE;
m_showAttackableTiles = false;
m_showReachableTiles = false;
}
break;
case SDL_MOUSEBUTTONDOWN:
......@@ -740,6 +746,35 @@ void Level::handleMovementEvents(Engine& engine, SDL_Event& event)
void Level::handleAttackingEvents(Engine& engine, SDL_Event& event)
{
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;
}
}
}
switch (event.type)
{
case SDL_KEYDOWN:
......@@ -747,6 +782,9 @@ void Level::handleAttackingEvents(Engine& engine, SDL_Event& event)
if (event.key.keysym.sym == SDLK_ESCAPE)
{
m_state = LevelState::MENUACTIVE_STATE;
m_attackableTiles.clear();
m_showAttackableTiles = false;
m_attackableUnitIds.clear();
}
if (event.key.keysym.sym == SDLK_RETURN)
{
......
......@@ -164,6 +164,8 @@ class Level : public Scene
void handleAttack(std::pair<int, int> tilePos);
void handleMovement(std::pair<int, int> tilePos);
void handlePositionMarker(Engine& engine, SDL_Event& event);
std::pair<int, int> unit_fallback_position;
};
} // namespace advanced_wars
......@@ -10,6 +10,9 @@ Unit::Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state, Config
{
// Allgemeine Einheiteneinstellungen aus Konfiguration holen
m_cost = config.getUnitCost(id);
std::cout<<"cost"<<m_cost<< std::endl;
m_movementPoints = config.getUnitMovementPoints(id);
m_ammo = config.getUnitAmmo(id);
m_minRange = config.getUnitMinRange(id);
......@@ -242,7 +245,7 @@ std::vector<Unit*> Unit::getUnitsInRangeWithDamagePotential(const std::vector<Un
for (Unit* unit : allUnits)
{ // Iterate over all units
// except itself
if (unit == this)
if (unit->getFaction() == this->m_faction)
{
continue;
}
......
......@@ -125,6 +125,8 @@ class Unit
void setState(UnitState state);
inline UnitState getState() const { return m_state; }
/**
* Retrieves units within range that this unit can deal damage to.
* Considers all units provided in 'allUnits', excluding itself, and checks movement and
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment