From 631ccfa140f2b64f748d27f9ed7398e8d7f11414 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 9 Jun 2024 17:59:25 +0300 Subject: [PATCH] FX removal --- Assets/Scripts/GameObjects/Units/unitData.lua | 3 +- fxManager.cpp | 43 ++++++------------- fxManager.h | 7 ++- game.cpp | 2 +- unit.cpp | 41 ++++++++++-------- 5 files changed, 43 insertions(+), 53 deletions(-) diff --git a/Assets/Scripts/GameObjects/Units/unitData.lua b/Assets/Scripts/GameObjects/Units/unitData.lua index b050a92..6b9ddd2 100644 --- a/Assets/Scripts/GameObjects/Units/unitData.lua +++ b/Assets/Scripts/GameObjects/Units/unitData.lua @@ -75,8 +75,7 @@ units = { path = PATH .. 'Sounds/Units/WarMechs/fire.ogg' }, }, - hitFx = { - } + hitFx = {} } }, unitClass = UnitClass.WAR_MECH, diff --git a/fxManager.cpp b/fxManager.cpp index 64f4f6f..f276e05 100644 --- a/fxManager.cpp +++ b/fxManager.cpp @@ -11,13 +11,11 @@ namespace battleship{ using namespace sf; FxManager::Fx::Component::Component(void *c, bool v, vb01::s64 dur, vb01::s64 ot) : comp(c), vfx(v), duration(dur), offsetTime(ot) { - if(v) - ((vb01::Node*)c)->setVisible(false); + if(v) ((vb01::Node*)c)->setVisible(false); } - FxManager::Fx::Fx(std::vector comps, bool act, bool re) : initTime(vb01::getTime()), components(comps), reuse(re){ - if(act) - toggleComponents(true); + FxManager::Fx::Fx(std::vector comps, bool re) : initTime(vb01::getTime()), components(comps), reuse(re){ + toggleComponents(!re); } void FxManager::Fx::toggleComponents(bool active){ @@ -71,6 +69,7 @@ namespace battleship{ } if(fx->components.empty()){ + delete fx; fxs.erase(fxs.begin() + i); i--; } @@ -97,33 +96,19 @@ namespace battleship{ fx->components[cid].comp = nullptr; } - FxManager::Fx* FxManager::addFx(Fx *fx){ - fxs.push_back(fx); - return fxs[fxs.size() - 1]; - } + void FxManager::removeFx(Fx *fx){ + int id = -1; - //TODO remove dublicatory code - void FxManager::removeFx(int id){ - Fx *fx = fxs[id]; + for(int i = 0; i < fxs.size(); i++) + if(fxs[i] == fx){ + id = i; + break; + } - for(int j = 0; j < fx->components.size(); j++) - destroyFxComponent(id, j); - - while(fx->components.size() > 0) - fx->components.pop_back(); + for(int i = 0; i < fx->components.size(); i++) + destroyFxComponent(id, i); fxs.erase(fxs.begin() + id); + delete fx; } - - /* - void FxManager::removeFx(Fx &fx){ - for(int j = 0; j < fx.components.size(); j++) - destroyFxComponent(id, j); - - while(fx.components.size() > 0) - fx.components.pop_back(); - - fxs.erase(fxs.begin() + id); - } - */ } diff --git a/fxManager.h b/fxManager.h index 7ed8c5d..b5e9569 100644 --- a/fxManager.h +++ b/fxManager.h @@ -19,15 +19,14 @@ namespace battleship{ vb01::s64 initTime; std::vector components; - Fx(std::vector, bool = true, bool = false); + Fx(std::vector, bool = false); void toggleComponents(bool); }; static FxManager* getSingleton(); void update(); - Fx* addFx(Fx*); - void removeFx(int); - void removeFx(Fx&); + void removeFx(Fx*); + inline void addFx(Fx *fx){fxs.push_back(fx);} private: FxManager(){} void destroyFxComponent(int, int); diff --git a/game.cpp b/game.cpp index c12ae47..fbb9374 100644 --- a/game.cpp +++ b/game.cpp @@ -175,7 +175,7 @@ namespace battleship{ typedef FxManager::Fx Fx; typedef FxManager::Fx::Component Component; - FxManager::getSingleton()->addFx(new Fx(vector{Component((void*)node, true, 50), Component((void*)explosionSfx, false, 2500)}, true)); + FxManager::getSingleton()->addFx(new Fx(vector{Component((void*)node, true, 50), Component((void*)explosionSfx, false, 2500)})); } void Game::changeUnitPlayer(Unit *unit, Player *newPlayer){ diff --git a/unit.cpp b/unit.cpp index 8d27298..fc79f79 100755 --- a/unit.cpp +++ b/unit.cpp @@ -80,41 +80,46 @@ namespace battleship{ if((sol::optional)weaponTable[vfxKey] == sol::nullopt) return nullptr; - int numComponents = ((sol::table)weaponTable[vfxKey]).size(); + sol::table fxTbl = weaponTable[vfxKey]; + int numComponents = fxTbl.size(); + + if(numComponents == 0) + return nullptr; + vector fxComponents; for(int i = 0; i < numComponents; i++){ - sol::table vfxTbl = weaponTable[vfxKey][i + 1]; + sol::table compTbl = fxTbl[i + 1]; - bool vfx = vfxTbl["vfx"]; - s64 duration = vfxTbl["duration"]; + bool vfx = compTbl["vfx"]; + s64 duration = compTbl["duration"]; if(vfx){ Material *mat = new Material(Root::getSingleton()->getLibPath() + "texture"); - if((sol::optional)vfxTbl["texture"] != sol::nullopt){ - string p[]{vfxTbl["texture"]}; + if((sol::optional)compTbl["texture"] != sol::nullopt){ + string p[]{compTbl["texture"]}; Texture *tex = new Texture(p, 1, false); mat->addBoolUniform("texturingEnabled", true); mat->addTexUniform("diffuseMap[0]", tex, false); } else{ - sol::table colorTable = vfxTbl["color"]; + sol::table colorTable = compTbl["color"]; mat->addVec4Uniform("diffuseColor", Vector4(colorTable["x"], colorTable["y"], colorTable["z"], colorTable["a"])); mat->addBoolUniform("texturingEnabled", false); } - Model *flashModel = new Model((string)vfxTbl["path"]); + Model *flashModel = new Model((string)compTbl["path"]); flashModel->setMaterial(mat); flashModel->setVisible(false); unit->getModel()->attachChild(flashModel); - sol::table posTable = vfxTbl["pos"], rotTable = vfxTbl["rot"]; + sol::table posTable = compTbl["pos"], rotTable = compTbl["rot"]; flashModel->setPosition(Vector3(posTable["x"], posTable["y"], posTable["z"])); flashModel->setOrientation(Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"])); - if((sol::optional)vfxTbl["scale"] != sol::nullopt){ - float sc = vfxTbl["scale"]; + if((sol::optional)compTbl["scale"] != sol::nullopt){ + float sc = compTbl["scale"]; flashModel->setScale(Vector3(sc, sc, sc)); } @@ -122,20 +127,22 @@ namespace battleship{ } else{ sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); - sf::Sound *sfx = GameObject::prepareSfx(sfxBuffer, vfxTbl["path"]); + sf::Sound *sfx = GameObject::prepareSfx(sfxBuffer, compTbl["path"]); fxComponents.push_back(FxManager::Fx::Component((void*)sfx, vfx, duration)); } } - return new FxManager::Fx(fxComponents, false, true); + return new FxManager::Fx(fxComponents, true); } Unit::Weapon::~Weapon(){ - //FxManager::removeFx(); + FxManager *fm = FxManager::getSingleton(); + + if(fireFx) fm->removeFx(fireFx); + if(hitFx) fm->removeFx(hitFx); } - void Unit::Weapon::update(){ - } + void Unit::Weapon::update(){} void Unit::Weapon::fire(Order order){ if(!canFire()) return; @@ -179,10 +186,10 @@ namespace battleship{ Unit::~Unit() { removeBar(hpBackgroundNode); removeBar(hpForegroundNode); + destroyWeapons(); destroySound(); destroyHitbox(); destroyModel(); - destroyWeapons(); } void Unit::initProperties(){