From c8523351fabca547e0c1e236a56db4f8aa25941e Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 26 Jan 2024 14:46:24 +0200 Subject: [PATCH] reiniting all game objects --- Assets/Scripts/GameObjects/resourceData.lua | 2 ++ game.cpp | 18 +++++++++++--- gameObject.cpp | 5 ++++ gameObject.h | 1 + player.h | 1 + projectile.cpp | 13 ++++++++++ projectile.h | 1 + resourceDeposit.cpp | 12 +++++++++ resourceDeposit.h | 2 ++ unit.cpp | 27 +++++++++++---------- unit.h | 3 +-- 11 files changed, 67 insertions(+), 18 deletions(-) diff --git a/Assets/Scripts/GameObjects/resourceData.lua b/Assets/Scripts/GameObjects/resourceData.lua index b6ae446..ea5fc8e 100644 --- a/Assets/Scripts/GameObjects/resourceData.lua +++ b/Assets/Scripts/GameObjects/resourceData.lua @@ -1,3 +1,5 @@ +ResourceId = {RESOURCE = 0} + resources = { { name = 'Resource', diff --git a/game.cpp b/game.cpp index 4f7970d..bed889a 100644 --- a/game.cpp +++ b/game.cpp @@ -135,9 +135,21 @@ namespace battleship{ guiManager->readLuaScreenScript("inGame.lua", activeState->getButtons()); AssetManager::getSingleton()->load(gm->getPath() + (string)generateView()["modelPrefix"], true); - for(Player *p : Game::getSingleton()->getPlayers()) - for(Unit *u : p->getUnits()) - u->reinit(); + vector gameObjs; + + for(Player *pl : Game::getSingleton()->getPlayers()){ + for(Unit *u : pl->getUnits()) + gameObjs.push_back((GameObject*)u); + + for(Projectile *proj : pl->getProjectiles()) + gameObjs.push_back((GameObject*)proj); + + for(ResourceDeposit *dep : pl->getResourceDeposits()) + gameObjs.push_back((GameObject*)dep); + } + + for(GameObject *obj : gameObjs) + obj->reinit(); gm->getStateManager()->attachAppState(activeState); } diff --git a/gameObject.cpp b/gameObject.cpp index d066674..b557994 100644 --- a/gameObject.cpp +++ b/gameObject.cpp @@ -19,6 +19,11 @@ namespace battleship{ using namespace gameBase; using namespace configData; + void GameObject::reinit(){ + placeAt(pos); + orientAt(rot); + } + void GameObject::update(){ leftVec = model->getGlobalAxis(0); upVec = model->getGlobalAxis(1); diff --git a/gameObject.h b/gameObject.h index 48fa3d6..28b8ab0 100644 --- a/gameObject.h +++ b/gameObject.h @@ -20,6 +20,7 @@ namespace battleship{ GameObject(Type t, int i, Player *pl, vb01::Vector3 vec, vb01::Quaternion quat) : type(t), id(i), player(pl), pos(vec), rot(quat){} ~GameObject(){} + virtual void reinit(); virtual void update(); virtual void select(){} void placeAt(vb01::Vector3); diff --git a/player.h b/player.h index 0941def..a1b39f3 100755 --- a/player.h +++ b/player.h @@ -36,6 +36,7 @@ namespace battleship{ inline int getNumResourceDeposits(){return resourceDeposits.size();} inline void addProjectile(Projectile *proj){projectiles.push_back(proj);} inline int getNumProjectiles(){return projectiles.size();} + inline std::vector& getProjectiles(){return projectiles;} inline Unit* getUnit(int i){return units[i];} inline std::vector& getUnits(){return units;} inline void setTeam(int t){team = t;} diff --git a/projectile.cpp b/projectile.cpp index 9c4c905..97f3260 100755 --- a/projectile.cpp +++ b/projectile.cpp @@ -33,9 +33,22 @@ namespace battleship{ } Projectile::~Projectile(){ + destroySound(); destroyModel(); } + void Projectile::reinit(){ + destroySound(); + destroyModel(); + + initProperties(); + + initModel(); + initSound(); + + GameObject::reinit(); + } + void Projectile::initProperties(){ GameObject::initProperties(); diff --git a/projectile.h b/projectile.h index e29c028..0062ddf 100755 --- a/projectile.h +++ b/projectile.h @@ -26,6 +26,7 @@ namespace battleship { void initSound(); void checkCollision(); protected: + virtual void reinit(); virtual void checkUnitCollision(); virtual void checkSurfaceCollision(); diff --git a/resourceDeposit.cpp b/resourceDeposit.cpp index 9ede9c9..76ea15c 100644 --- a/resourceDeposit.cpp +++ b/resourceDeposit.cpp @@ -19,4 +19,16 @@ namespace battleship{ destroyHitbox(); destroyModel(); } + + void ResourceDeposit::reinit(){ + destroyHitbox(); + destroyModel(); + + initProperties(); + + initModel(); + initHitbox(); + + GameObject::reinit(); + } } diff --git a/resourceDeposit.h b/resourceDeposit.h index a56ba41..f1e5f8d 100644 --- a/resourceDeposit.h +++ b/resourceDeposit.h @@ -13,6 +13,8 @@ namespace battleship{ inline void decrementAmmount(){ammount--;} private: int ammount, initAmmount; + + void reinit(); }; } diff --git a/unit.cpp b/unit.cpp index 3f37f3a..b6f0edd 100755 --- a/unit.cpp +++ b/unit.cpp @@ -100,7 +100,13 @@ namespace battleship{ this->player = player; selectable = true; - init(); + initProperties(); + initModel(); + initHitbox(); + initSound(); + initWeapons(); + placeAt(pos); + orientAt(rot); Vector2 size = Vector2(lenHpBar, 10); hpBackgroundNode = createBar(Vector2::VEC_ZERO, size, Vector4(0, 0, 0, 1)); @@ -116,16 +122,6 @@ namespace battleship{ destroyWeapons(); } - void Unit::init(){ - initProperties(); - initModel(); - initHitbox(); - initSound(); - initWeapons(); - placeAt(pos); - orientAt(rot); - } - void Unit::initProperties(){ GameObject::initProperties(); @@ -205,6 +201,7 @@ namespace battleship{ delete selectionSfxBuffer; } + //TODO factor out initSound() void Unit::initSound(){ GameObject::initSound(); selectionSfxBuffer = new sf::SoundBuffer(); @@ -216,12 +213,16 @@ namespace battleship{ destroySound(); destroyHitbox(); destroyModel(); + destroyWeapons(); + initProperties(); + initModel(); initHitbox(); initSound(); - placeAt(pos); - orientAt(rot); + initWeapons(); + + GameObject::reinit(); } bool Unit::canGarrison(Vehicle *vehicle){ diff --git a/unit.h b/unit.h index a57c42d..861907c 100755 --- a/unit.h +++ b/unit.h @@ -124,7 +124,6 @@ namespace battleship{ void setOrder(Order); std::vector getProjectiles(); virtual void addOrder(Order); - virtual void reinit(); bool canGarrison(Vehicle*); inline Engineer* toEngineer(){return (Engineer*)this;} inline Factory* toFactory(){return (Factory*)this;} @@ -147,7 +146,6 @@ namespace battleship{ private: void renderOrderLine(bool); void updateScreenCoordinates(); - void init(); void initWeapons(); void destroyWeapons(); inline bool canDisplayOrderLine(){return vb01::getTime() - orderLineDispTime < orderVecDispLength;} @@ -170,6 +168,7 @@ namespace battleship{ std::vector getSelectingPlayers(); void removeOrder(int); + virtual void reinit(); virtual void initProperties(); virtual void destroySound(); virtual void initSound();