From 1c680aebf2163d0a3732f5935be6138f440ea9f0 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 23 Jan 2024 15:46:39 +0200 Subject: [PATCH] added bars for extractors/deposits and cargo ships --- Assets/Scripts/GameObjects/Units/unitData.lua | 18 +++--- Assets/Scripts/GameObjects/resourceData.lua | 16 ++--- extractor.cpp | 60 ++++++++++++++++++- extractor.h | 11 +++- gameObjectFactory.cpp | 4 +- gameObjectFactory.h | 2 +- map.cpp | 3 +- resourceDeposit.cpp | 6 +- resourceDeposit.h | 6 +- resourceRover.cpp | 32 ++++++---- resourceRover.h | 4 +- 11 files changed, 126 insertions(+), 36 deletions(-) diff --git a/Assets/Scripts/GameObjects/Units/unitData.lua b/Assets/Scripts/GameObjects/Units/unitData.lua index 632fedf..3090f8a 100644 --- a/Assets/Scripts/GameObjects/Units/unitData.lua +++ b/Assets/Scripts/GameObjects/Units/unitData.lua @@ -28,7 +28,7 @@ structurePrefix = modelPrefix .. "Units/Structures/" units = { weapons = { - {{type = WeaponClass.HITSCAN, rateOfFire = 100, fireSfx = PATH .. 'Sounds/Units/WarMechs/fire.ogg', damage = 50, maxRange = 3}}, + {{type = WeaponClass.HITSCAN, rateOfFire = 100, fireSfx = PATH .. 'Sounds/Units/WarMechs/fire.ogg', damage = 50, maxRange = 10}}, {{type = WeaponClass.HITSCAN, rateOfFire = 500, fireSfx = PATH .. 'Sounds/Units/Tanks/attack.ogg', damage = 200, maxRange = 20}}, {{type = WeaponClass.HITSCAN, rateOfFire = 2000, fireSfx = PATH .. 'Sounds/Units/Tanks/attack.ogg', damage = 5000, maxRange = 45}}, {}, @@ -373,14 +373,14 @@ units = { {x = .5, y =.15, z =.7} }, { - {x = .5, y =-.1, z =-1.3}, - {x = -.5, y =-.1, z =-1.3}, - {x = -.5, y =-.1, z =.7}, - {x = .5, y =-.1, z =.7}, - {x = .5, y =.15, z =-1.3}, - {x = -.5, y =.15, z =-1.3}, - {x = -.5, y =.15, z =.7}, - {x = .5, y =.15, z =.7} + {x = 2, y =-.1, z =-2}, + {x = -2, y =-.1, z =-2}, + {x = -2, y =-.1, z =2}, + {x = 2, y =-.1, z =2}, + {x = 2, y =.15, z =-2}, + {x = -2, y =.15, z =-2}, + {x = -2, y =.15, z =2}, + {x = 2, y =.15, z =2} }, { {x = .5, y =-.1, z =-1.3}, diff --git a/Assets/Scripts/GameObjects/resourceData.lua b/Assets/Scripts/GameObjects/resourceData.lua index 19acb1f..0a3099d 100644 --- a/Assets/Scripts/GameObjects/resourceData.lua +++ b/Assets/Scripts/GameObjects/resourceData.lua @@ -10,14 +10,14 @@ resources = { }, unitCornerPoints = { { - {x = 1, y = .571, z = -1}, - {x = -1, y = .571, z = -1}, - {x = -1, y = .571, z = 1}, - {x = 1, y = .571, z = 1}, - {x = 1, y = .571, z = -1}, - {x = -1, y = .571, z = -1}, - {x = -1, y = .571, z = 1}, - {x = 1, y = .571, z = 1} + {x = 0, y = 0, z = 0}, + {x = 0, y = 0, z = 0}, + {x = 0, y = 0, z = 0}, + {x = 0, y = 0, z = 0}, + {x = 0, y = 0, z = 0}, + {x = 0, y = 0, z = 0}, + {x = 0, y = 0, z = 0}, + {x = 0, y = 0, z = 0}, } }, hitboxOffset = {{x = 0, y = 0, z = 0}} diff --git a/extractor.cpp b/extractor.cpp index f8f8cfe..c4416e4 100644 --- a/extractor.cpp +++ b/extractor.cpp @@ -1,8 +1,66 @@ #include "extractor.h" +#include "player.h" +#include "game.h" +#include "activeGameState.h" +#include "resourceDeposit.h" + +#include namespace battleship{ using namespace std; using namespace vb01; + using namespace gameBase; - Extractor::Extractor(Player *player, int id, Vector3 pos, Quaternion rot, int buildStatus) : Structure(player, id, pos, rot, buildStatus){} + Extractor::Extractor(Player *player, int id, Vector3 pos, Quaternion rot, int buildStatus, ResourceDeposit *rd) : Structure(player, id, pos, rot, buildStatus){ + if(!rd){ + vector deposits; + + for(Player *pl : Game::getSingleton()->getPlayers()){ + vector dep = pl->getResourceDeposits(); + deposits.insert(deposits.end(), dep.begin(), dep.end()); + } + + for(ResourceDeposit *dep : deposits) + if(dep->getPos().getDistanceFrom(pos) < .001){ + deposit = dep; + break; + } + } + + Vector2 size = Vector2(lenHpBar, 10); + ammountBackground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(0, 0, 0, 1)); + ammountForeground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(0, 0, 1, 1)); + } + + Extractor::~Extractor(){ + removeBar(ammountForeground); + removeBar(ammountBackground); + } + + void Extractor::update(){ + Structure::update(); + + ActiveGameState *activeState = (ActiveGameState*)GameManager::getSingleton()->getStateManager()->getAppStateByType(AppStateType::ACTIVE_STATE); + Player *mainPlayer = (activeState ? activeState->getPlayer() : nullptr); + + vector selectingPlayers = getSelectingPlayers(); + bool mainPlayerSelecting = (activeState && find(selectingPlayers.begin(), selectingPlayers.end(), mainPlayer) != selectingPlayers.end()); + int ammount = 0, initAmmount = 0; + + if(deposit){ + ammount = deposit->getAmmount(); + initAmmount = deposit->getInitAmmount(); + } + + Unit::displayUnitStats(ammountForeground, ammountBackground, ammount, initAmmount, mainPlayer == player && mainPlayerSelecting, Vector2(0, -10)); + } + + void Extractor::draw(){ + if(canDraw()){ + deposit->decrementAmmount(); + lastDrawTime = getTime(); + } + } + + bool Extractor::canDraw(){return vb01::getTime() - lastDrawTime > drawRate && deposit->getAmmount() > 0;} } diff --git a/extractor.h b/extractor.h index f315343..bec4483 100644 --- a/extractor.h +++ b/extractor.h @@ -6,12 +6,21 @@ #include namespace battleship{ + class ResourceDeposit; + class Extractor : public Structure{ public: - Extractor(Player*, int, vb01::Vector3, vb01::Quaternion, int); + Extractor(Player*, int, vb01::Vector3, vb01::Quaternion, int, ResourceDeposit *rd = nullptr); + ~Extractor(); + void update(); + void draw(); private: int drawRate; vb01::s64 lastDrawTime = 0; + vb01::Node *ammountBackground = nullptr, *ammountForeground = nullptr; + ResourceDeposit *deposit = nullptr; + + bool canDraw(); }; } diff --git a/gameObjectFactory.cpp b/gameObjectFactory.cpp index b780b66..ae929e5 100644 --- a/gameObjectFactory.cpp +++ b/gameObjectFactory.cpp @@ -63,7 +63,7 @@ namespace battleship{ } } - ResourceDeposit* GameObjectFactory::createResourceDeposit(Player *player, int id, Vector3 pos, Quaternion rot){ - return new ResourceDeposit(player, id, pos, rot); + ResourceDeposit* GameObjectFactory::createResourceDeposit(Player *player, int id, Vector3 pos, Quaternion rot, int initAmmount){ + return new ResourceDeposit(player, id, pos, rot, initAmmount); } } diff --git a/gameObjectFactory.h b/gameObjectFactory.h index be6e47b..87221a0 100644 --- a/gameObjectFactory.h +++ b/gameObjectFactory.h @@ -15,7 +15,7 @@ namespace battleship{ enum GameObjectType{UNIT, PROJECTILE, RESOURCE_DEPOSIT}; static Unit* createUnit(Player*, int, vb01::Vector3, vb01::Quaternion, int = 0); static Projectile* createProjectile(Unit*, int, vb01::Vector3, vb01::Quaternion); - static ResourceDeposit* createResourceDeposit(Player*, int, vb01::Vector3, vb01::Quaternion); + static ResourceDeposit* createResourceDeposit(Player*, int, vb01::Vector3, vb01::Quaternion, int = 0); private: }; } diff --git a/map.cpp b/map.cpp index dab328b..2516e02 100755 --- a/map.cpp +++ b/map.cpp @@ -172,8 +172,9 @@ namespace battleship{ sol::table rotTable = npcObjTable["rot"]; Quaternion rot = Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"]); + int initAmmount = SOL_LUA_VIEW[mapTable][playerInd][i + 1][resDepInd][j + 1]["initAmmount"]; - player->addResourceDeposit(GameObjectFactory::createResourceDeposit(player, id, pos, rot)); + player->addResourceDeposit(GameObjectFactory::createResourceDeposit(player, id, pos, rot, initAmmount)); } //int spawnPointId = SOL_LUA_STATE[mapTable]["spawnPointInd"][i + 1][spawnPointId]; diff --git a/resourceDeposit.cpp b/resourceDeposit.cpp index 7fe4ec0..9ede9c9 100644 --- a/resourceDeposit.cpp +++ b/resourceDeposit.cpp @@ -3,7 +3,11 @@ namespace battleship{ using namespace vb01; - ResourceDeposit::ResourceDeposit(Player *player, int id, Vector3 pos, Quaternion rot) : GameObject(GameObject::Type::RESOURCE_DEPOSIT, id, player, pos, rot){ + ResourceDeposit::ResourceDeposit(Player *player, int id, Vector3 pos, Quaternion rot, int ia) : + GameObject(GameObject::Type::RESOURCE_DEPOSIT, id, player, pos, rot), + initAmmount(ia), + ammount(ia) + { initProperties(); initModel(); initHitbox(); diff --git a/resourceDeposit.h b/resourceDeposit.h index be11fad..a56ba41 100644 --- a/resourceDeposit.h +++ b/resourceDeposit.h @@ -6,9 +6,13 @@ namespace battleship{ class ResourceDeposit : public GameObject{ public: - ResourceDeposit(Player*, int, vb01::Vector3, vb01::Quaternion); + ResourceDeposit(Player*, int, vb01::Vector3, vb01::Quaternion, int); ~ResourceDeposit(); + inline int getAmmount(){return ammount;} + inline int getInitAmmount(){return initAmmount;} + inline void decrementAmmount(){ammount--;} private: + int ammount, initAmmount; }; } diff --git a/resourceRover.cpp b/resourceRover.cpp index d946f4b..7b453d1 100644 --- a/resourceRover.cpp +++ b/resourceRover.cpp @@ -1,6 +1,8 @@ #include "resourceRover.h" #include "map.h" #include "player.h" +#include "extractor.h" +#include "structure.h" #include "activeGameState.h" #include @@ -17,7 +19,7 @@ namespace battleship{ { Vector2 size = Vector2(lenHpBar, 10); loadBackground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(0, 0, 0, 1)); - loadForeground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(0, 1, 1, 1)); + loadForeground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(1, 1, 0, 1)); } ResourceRover::~ResourceRover(){ @@ -31,13 +33,14 @@ namespace battleship{ if(!pathPoints.empty()) navigate(minDist); else{ - vector units = player->getUnits(), extractors, refineries; + vector units = player->getUnits(); + vector extractors, refineries; for(Unit *unit : units){ if(unit->getUnitClass() == UnitClass::EXTRACTOR) - extractors.push_back(unit); + extractors.push_back((Structure*)unit); else if(unit->getUnitClass() == UnitClass::REFINERY) - refineries.push_back(unit); + refineries.push_back((Structure*)unit); } nearestExtractor = getClosestUnit(extractors); @@ -45,6 +48,7 @@ namespace battleship{ if(nearestExtractor->getPos().getDistanceFrom(pos) <= minDist){ if(canLoad()){ + ((Extractor*)nearestExtractor)->draw(); load++; lastLoadTime = getTime(); } @@ -74,15 +78,23 @@ namespace battleship{ Unit::displayUnitStats(loadForeground, loadBackground, load, capacity, mainPlayer == player && mainPlayerSelecting, Vector2(0, -10)); } - Unit* ResourceRover::getClosestUnit(vector units){ - if(units.empty()) return nullptr; + Unit* ResourceRover::getClosestUnit(vector structs){ + if(structs.empty()) return nullptr; - int minDistId = 0; + int minDistId = -1; - for(int i = 0; i < units.size(); i++) - if(units[minDistId]->getPos().getDistanceFrom(pos) > units[i]->getPos().getDistanceFrom(pos)) + for(int i = 0; i < structs.size(); i++) + if(structs[i]->getBuildStatus() == 100){ + minDistId = i; + break; + } + + if(minDistId == -1) return nullptr; + + for(int i = 0; i < structs.size(); i++) + if(structs[i]->getBuildStatus() == 100 && structs[minDistId]->getPos().getDistanceFrom(pos) > structs[i]->getPos().getDistanceFrom(pos)) minDistId = i; - return units[minDistId]; + return structs[minDistId]; } } diff --git a/resourceRover.h b/resourceRover.h index 84d10f7..2d07b5c 100644 --- a/resourceRover.h +++ b/resourceRover.h @@ -6,6 +6,8 @@ #include namespace battleship{ + class Structure; + class ResourceRover : public Vehicle{ public: ResourceRover(Player*, int, vb01::Vector3, vb01::Quaternion); @@ -13,7 +15,7 @@ namespace battleship{ void update(); private: void supply(Order); - Unit* getClosestUnit(std::vector); + Unit* getClosestUnit(std::vector); inline bool canLoad(){return vb01::getTime() - lastLoadTime > loadRate && load < capacity;} inline bool canUnload(){return vb01::getTime() - lastLoadTime > loadRate && load > 0;}