From a40110520d5ef6449cff688fa9d0c6b73ceaa924 Mon Sep 17 00:00:00 2001 From: arf20 Date: Fri, 9 Dec 2022 22:44:37 +0100 Subject: [PATCH] Trench reset --- src/game.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++----- src/main.hpp | 1 + src/renderer.cpp | 8 ++++---- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 3d4e70d..c3e66bf 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -98,7 +98,8 @@ void findMapPath() { while (Game::selectedMap->map[my][mx] == ' ') { my++; } Game::MapPathPoint point; - point.type = Game::MapPathPoint::GROUND; + point.type = Game::MapPathPoint::GROUND; // when GROUND, action is ignored + point.action = Game::MapPathPoint::MARCH; if (my < prevmy) { point.pos = {float(TILE_SIZE * mx), float(TILE_SIZE * (my + 1))}; @@ -112,6 +113,7 @@ void findMapPath() { if (Game::selectedMap->map[my][mx] == 't') { point.type = Game::MapPathPoint::TRENCH; + point.action = Game::MapPathPoint::HOLD; point.pos = {float((TILE_SIZE * mx) + (TILE_SIZE / 2)), float(TILE_SIZE * (my + 1))}; Game::friendlyMapPath.push_back(point); } @@ -121,6 +123,7 @@ void findMapPath() { // append two points further in the edges Game::MapPathPoint point; point.type = Game::MapPathPoint::GROUND; + point.action = Game::MapPathPoint::MARCH; point.pos = { Game::friendlyMapPath[0].pos.x - 40.0f, Game::friendlyMapPath[0].pos.y}; Game::friendlyMapPath.insert(Game::friendlyMapPath.begin(), point); @@ -240,12 +243,44 @@ auto findNearestTarget(const Game::Soldier& soldier, const std::vector& soldiers) { + if (soldiers.size() < 1) return; + + // find leftmost and rightmost soldiers + float minx = Game::selectedMap->width * TILE_SIZE; + float maxx = 0.0f; + std::vector::iterator leftmost = soldiers.end(); + std::vector::iterator rightmost = soldiers.end(); + for (int i = 0; i < soldiers.size(); i++) { + if (soldiers[i].pos.x < minx) { minx = soldiers[i].pos.x; leftmost = soldiers.begin() + i; } + if (soldiers[i].pos.x > maxx) { maxx = soldiers[i].pos.x; rightmost = soldiers.begin() + i; } + } + + // if there are trenches on clear more advanced than the most advanced soldier, reset it + if (soldiers[0].friendly) { + if (rightmost != soldiers.end()) + for (int i = 0; i < Game::friendlyMapPath.size(); i++) + if (Game::friendlyMapPath[i].type == Game::MapPathPoint::TRENCH) + if (Game::friendlyMapPath[i].action != Game::MapPathPoint::HOLD) + if (Game::friendlyMapPath[i].pos.x > rightmost->pos.x + (rightmost->character->size.x / 2.0f)) + Game::friendlyMapPath[i].action = Game::MapPathPoint::HOLD; + } + else { + if (leftmost != soldiers.end()) + for (int i = 0; i < Game::enemyMapPath.size(); i++) + if (Game::enemyMapPath[i].type == Game::MapPathPoint::TRENCH) + if (Game::enemyMapPath[i].action != Game::MapPathPoint::HOLD) + if (Game::enemyMapPath[i].pos.x < leftmost->pos.x + (leftmost->character->size.x / 2.0f)) + Game::enemyMapPath[i].action = Game::MapPathPoint::HOLD; + } +} + // targetEnemies relative to 'soldiers' void updateFaction(std::vector& soldiers, const std::vector& targetEnemies, float deltaTime) { for (auto it = soldiers.begin(); it < soldiers.end(); it++) { Game::Soldier& soldier = *it; - // soldier dies when health runs out + // death logic if (soldier.health <= 0) soldierDeath(it); @@ -254,7 +289,7 @@ void updateFaction(std::vector& soldiers, const std::vector& soldiers, const std::vector soldier.pos.x + (soldier.character->size.x / 2.0f)) { if (mapcheck) - if (Game::friendlyMapPath[i - 1].type == Game::MapPathPoint::GROUND) { + if (Game::friendlyMapPath[i - 1].action == Game::MapPathPoint::MARCH) { soldier.prevState = soldier.state; soldier.state = Game::Soldier::MARCHING; } else { @@ -315,7 +351,7 @@ void updateFaction(std::vector& soldiers, const std::vector= 0; i--) { if (Game::enemyMapPath[i].pos.x < soldier.pos.x + (soldier.character->size.x / 2.0f)) { if (mapcheck) - if (Game::enemyMapPath[i + 1].type == Game::MapPathPoint::GROUND) { + if (Game::enemyMapPath[i + 1].action == Game::MapPathPoint::MARCH) { soldier.prevState = soldier.state; soldier.state = Game::Soldier::MARCHING; } else { @@ -341,6 +377,8 @@ void updateFaction(std::vector& soldiers, const std::vectorpos.x) < TILE_SIZE) Game::enemiesHoldingObjective++; } + + resetTrenches(soldiers); } void gameUpdate(float deltaTime) { diff --git a/src/main.hpp b/src/main.hpp index e331db3..ed87e85 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -155,6 +155,7 @@ namespace Game { struct MapPathPoint { enum PointType { GROUND, TRENCH } type; + enum Action { MARCH, HOLD } action; vector pos; }; diff --git a/src/renderer.cpp b/src/renderer.cpp index 5cc6c1b..55c05c5 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -261,9 +261,9 @@ void gameKeyHandler(SDL_Keycode key) { case SDLK_q: { for (int i = 0; i < Game::friendlyMapPath.size(); i++) { auto& p = Game::friendlyMapPath[i]; - if (p.type == Game::MapPathPoint::TRENCH) { + if (p.action == Game::MapPathPoint::HOLD) { if (Game::friendlyObjective != (Game::friendlyMapPath.begin() + i)) - p.type = Game::MapPathPoint::GROUND; + p.action = Game::MapPathPoint::MARCH; break; } } @@ -271,9 +271,9 @@ void gameKeyHandler(SDL_Keycode key) { case SDLK_e: { for (int i = Game::enemyMapPath.size() - 1; i >= 0; i--) { auto& p = Game::enemyMapPath[i]; - if (p.type == Game::MapPathPoint::TRENCH) { + if (p.action == Game::MapPathPoint::HOLD) { if (Game::enemyObjective != (Game::enemyMapPath.begin() + i)) - p.type = Game::MapPathPoint::GROUND; + p.action = Game::MapPathPoint::MARCH; break; } }