From ef290ee880036780fc40c1d5ff1309c6c7ca563e Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 16 Feb 2024 11:16:58 +0200 Subject: [PATCH] automatically attacking hostle units --- Assets/Scripts/GameObjects/Units/unitData.lua | 2 +- unit.cpp | 20 +++++++++++++++++++ unit.h | 6 +++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/GameObjects/Units/unitData.lua b/Assets/Scripts/GameObjects/Units/unitData.lua index 7ff03c8..2b9a8f1 100644 --- a/Assets/Scripts/GameObjects/Units/unitData.lua +++ b/Assets/Scripts/GameObjects/Units/unitData.lua @@ -63,7 +63,7 @@ units = { cost = 500, size = {x = 2.9, y = 8.22, z = 2.42}, hitboxOffset = {x = 0, y = 0, z = 0}, - lineOfSight = 5, + lineOfSight = 20, name = 'War mech', basePath = PATH .. vehiclePrefix .. 'WarMechs/', meshPath = 'warMech.xml', diff --git a/unit.cpp b/unit.cpp index b6f0edd..9d9317b 100755 --- a/unit.cpp +++ b/unit.cpp @@ -295,12 +295,32 @@ namespace battleship{ } } + void Unit::targetUnitsAutomatically(){ + vector players = Game::getSingleton()->getPlayers(); + vector units; + + for(Player *pl : players) + if(!(pl == player || pl->getTeam() == player->getTeam())){ + vector un = pl->getUnits(); + units.insert(units.end(), un.begin(), un.end()); + } + + if(orders.empty()) + for(Unit *unit : units) + if(unit->getPos().getDistanceFrom(pos) < lineOfSight){ + setOrder(Order(Order::TYPE::ATTACK, vector{Order::Target(unit)}, Vector3::VEC_ZERO, false)); + break; + } + } + void Unit::update() { GameObject::update(); ActiveGameState *activeState = (ActiveGameState*)GameManager::getSingleton()->getStateManager()->getAppStateByType(AppStateType::ACTIVE_STATE); Player *mainPlayer = (activeState ? activeState->getPlayer() : nullptr); + targetUnitsAutomatically(); + vector selectingPlayers = getSelectingPlayers(); bool mainPlayerSelecting = (find(selectingPlayers.begin(), selectingPlayers.end(), mainPlayer) != selectingPlayers.end()); bool mainPlayerOwner = (player == mainPlayer); diff --git a/unit.h b/unit.h index a72ea33..4fcfe62 100755 --- a/unit.h +++ b/unit.h @@ -46,11 +46,12 @@ namespace battleship{ TYPE type; int lineId = -1; + bool playerAssigned = true; vb01::Vector3 direction; std::vector targets; Order(){} - Order(TYPE t, std::vector targ, vb01::Vector3 dir, int lid = -1) : type(t), lineId(lid), targets(targ), direction(dir){} + Order(TYPE t, std::vector targ, vb01::Vector3 dir, bool pa = true, int lid = -1) : type(t), playerAssigned(pa), lineId(lid), targets(targ), direction(dir){} }; enum class MoveDir {LEFT, UP, FORW}; @@ -116,6 +117,7 @@ namespace battleship{ }; enum class Armor {CAST, COMBINED, MECHANIC, SHELL, STEEL}; + enum class State {CHASE, STAND_GROUND, HOLD_FIRE}; Unit(Player*, int, vb01::Vector3, vb01::Quaternion); virtual ~Unit(); @@ -168,9 +170,11 @@ namespace battleship{ std::vector garrisonSlots; std::vector armorTypes; std::vector weapons; + State state = State::STAND_GROUND; std::vector getSelectingPlayers(); void removeOrder(int); + virtual void targetUnitsAutomatically(); virtual void reinit(); virtual void initProperties(); virtual void destroySound();