From 88e3c635d9a7ee1ea7fba9709e445da88b499806 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 5 Nov 2022 13:39:00 +0200 Subject: [PATCH] unit stats can be updated from Lua scripts --- inGameAppState.cpp | 30 ++++++++++++++++++++---------- inGameAppState.h | 1 + unit.cpp | 24 +++++++++++++++++++++++- unit.h | 3 +++ 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/inGameAppState.cpp b/inGameAppState.cpp index f156d37..cc3c3d1 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -197,16 +197,7 @@ namespace battleship{ AssetManager *assetManager = AssetManager::getSingleton(); assetManager->load(DEFAULT_TEXTURE); - LuaManager *lm = LuaManager::getSingleton(); - string pathBase = GameManager::getSingleton()->getPath() + "Scripts/"; - lm->buildScript(vector{pathBase + "defPaths.lua", pathBase + "unitData.lua"}); - int numUnits = lm->getInt("numUnits"); - - for(int i = 0; i < numUnits; ++i){ - string basePath = lm->getStringFromTable("basePath", vector{Index(i + 1)}); - string meshPath = lm->getStringFromTable("meshPath", vector{Index(i + 1)}); - assetManager->load(basePath + meshPath); - } + loadModels(); } void InGameAppState::onDettached() {} @@ -269,6 +260,19 @@ namespace battleship{ updateProjectiles(); } + void InGameAppState::loadModels(){ + LuaManager *lm = LuaManager::getSingleton(); + string pathBase = GameManager::getSingleton()->getPath() + "Scripts/"; + lm->buildScript(vector{pathBase + "defPaths.lua", pathBase + "unitData.lua"}); + int numUnits = lm->getInt("numUnits"); + + for(int i = 0; i < numUnits; ++i){ + string basePath = lm->getStringFromTable("basePath", vector{Index(i + 1)}); + string meshPath = lm->getStringFromTable("meshPath", vector{Index(i + 1)}); + AssetManager::getSingleton()->load(basePath + meshPath); + } + } + void InGameAppState::toggleMainMenu() { GameManager *gm = GameManager::getSingleton(); @@ -302,6 +306,12 @@ namespace battleship{ guiState->removeButton(optionsButton); guiState->removeButton(exitButton); guiState->removeButton(resumeButton); + + loadModels(); + + for(Player *p : players) + for(Unit *u : p->getUnits()) + u->reinitUnit(); } } diff --git a/inGameAppState.h b/inGameAppState.h index 872c186..f7b7008 100755 --- a/inGameAppState.h +++ b/inGameAppState.h @@ -108,6 +108,7 @@ namespace battleship{ ActiveGameState* activeState; GuiAppState* guiState; + void loadModels(); void toggleMainMenu(); void updateUnits(Player*); void updateProjectiles(); diff --git a/unit.cpp b/unit.cpp index df45cf0..17af808 100755 --- a/unit.cpp +++ b/unit.cpp @@ -28,11 +28,12 @@ namespace battleship{ initProperties(); initModel(); - placeUnit(pos); initSound(); initUnitStats(); + placeUnit(pos); } + //TODO complete implementation Unit::~Unit() { } @@ -65,6 +66,11 @@ namespace battleship{ height = corners[4].y - corners[0].y; length = corners[3].z - corners[0].z; } + + void Unit::destroyModel(){ + Root::getSingleton()->getRootNode()->dettachChild(model); + delete model; + } void Unit::initModel(){ LuaManager *lm = LuaManager::getSingleton(); @@ -86,6 +92,12 @@ namespace battleship{ root->getRootNode()->attachChild(model); } + void Unit::destroySound(){ + selectionSfx->stop(); + delete selectionSfx; + delete selectionSfxBuffer; + } + void Unit::initSound(){ LuaManager *lm = LuaManager::getSingleton(); string name = lm->getStringFromTable("name", vector{Index(id + 1)}); @@ -96,6 +108,16 @@ namespace battleship{ selectionSfx = new sf::Sound(*selectionSfxBuffer); } + void Unit::reinitUnit(){ + destroySound(); + destroyModel(); + initModel(); + initSound(); + initProperties(); + placeUnit(pos); + orientUnit(rot); + } + void Unit::initUnitStats(){ Root *root = Root::getSingleton(); diff --git a/unit.h b/unit.h index be2bf27..fb8be76 100755 --- a/unit.h +++ b/unit.h @@ -61,6 +61,7 @@ namespace battleship{ void addProjectile(Projectile*); std::vector getProjectiles(); void addOrder(Order); + virtual void reinitUnit(); inline vb01::Vector3 getCorner(int i){return corners[i];} inline bool isSelected(){return selected;} inline bool isSelectable(){return selectable;} @@ -86,7 +87,9 @@ namespace battleship{ inline vb01::Vector3 getUpVec() {return upVec;} private: void initProperties(); + void destroyModel(); void initModel(); + void destroySound(); void initSound(); void initUnitStats(); void updateScreenCoordinates();