From d3d7daa4779baac6fbc7d1c851a7422ce4c4871b Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 1 Jun 2024 15:53:38 +0300 Subject: [PATCH] FX manager --- CMakeLists.txt | 5 ++-- fx.cpp | 21 --------------- fx.h | 25 ------------------ fxManager.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ fxManager.h | 43 ++++++++++++++++++++++++++++++ game.cpp | 32 ++++------------------ game.h | 4 --- projectile.cpp | 1 - unit.cpp | 3 +++ 9 files changed, 125 insertions(+), 81 deletions(-) delete mode 100644 fx.cpp delete mode 100644 fx.h create mode 100644 fxManager.cpp create mode 100644 fxManager.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dae23e7..1424c10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,13 +22,12 @@ set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppSt set(UTIL util.cpp binds.h) set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp) set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp addTechnologyCommand.cpp) -set(CORE gameManager.cpp game.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) +set(CORE gameManager.cpp game.cpp fxManager.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) set(PROJECTILES projectile.cpp cruiseMissile.cpp) -set(FX fx.cpp) set(VEHICLES vehicle.cpp engineer.cpp resourceRover.cpp) set(STRUCTURES structure.cpp factory.cpp pointDefense.cpp extractor.cpp researchStruct.cpp) set(UNITS gameObjectFactory.cpp unit.cpp ${STRUCTURES} ${VEHICLES}) -set(CONTENT player.cpp trader.cpp pathfinder.cpp map.cpp gameObject.cpp gameObjectFrame.h resourceDeposit.cpp ${UNITS} ${PROJECTILES} ${FX}) +set(CONTENT player.cpp trader.cpp pathfinder.cpp map.cpp gameObject.cpp gameObjectFrame.h resourceDeposit.cpp ${UNITS} ${PROJECTILES}) set(GAME_SRC ${STATES} ${GUI} ${CORE} ${CONTENT} ${UTIL}) set(SFML_DIR external/SFML) diff --git a/fx.cpp b/fx.cpp deleted file mode 100644 index 7b072ab..0000000 --- a/fx.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "fx.h" - -#include -#include - -#include - -namespace battleship{ - using namespace vb01; - - void Fx::activate(){ - if(sfx) - sfx->play(); - - if(peNode){ - - } - } -} - - diff --git a/fx.h b/fx.h deleted file mode 100644 index e9f3479..0000000 --- a/fx.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef FX_H -#define FX_H - -#include - -namespace sf{ - class Sound; -} - -namespace vb01{ - class Node; -} - -namespace battleship{ - struct Fx { - vb01::s64 initTime, vfxTime, sfxTime; - sf::Sound *sfx = nullptr; - vb01::Node *peNode = nullptr; - - Fx(vb01::s64 vt, vb01::s64 st, sf::Sound *s = nullptr, vb01::Node *n = nullptr) : vfxTime(vt), sfxTime(st), sfx(s), peNode(n), initTime(vb01::getTime()){} - void activate(); - }; -} - -#endif diff --git a/fxManager.cpp b/fxManager.cpp new file mode 100644 index 0000000..4cc36b8 --- /dev/null +++ b/fxManager.cpp @@ -0,0 +1,72 @@ +#include "fxManager.h" + +#include +#include + +#include + +namespace battleship{ + using namespace std; + using namespace vb01; + using namespace sf; + + static FxManager *fxManager = nullptr; + + FxManager* FxManager::getSingleton(){ + if(!fxManager) + fxManager = new FxManager(); + + return fxManager; + } + + void FxManager::update(){ + for(int i = 0; i < fxs.size(); i++){ + s64 currTime = getTime(); + Fx &fx = fxs[i]; + + for(int j = 0; j < fx.components.size(); j++){ + Fx::Component &comp = fx.components[j]; + + if(comp.comp && getTime() - fx.initTime > comp.offsetTime + comp.duration) + destroyFxComponent(i, j); + } + } + } + + void FxManager::destroyFxComponent(int fid, int cid){ + Fx &fx = fxs[fid]; + + if(fx.components[cid].vfx){ + Node *vfxNode = (Node*)fx.components[cid].comp, *parNode = vfxNode->getParent(); + parNode->dettachChild(vfxNode); + delete vfxNode; + } + else{ + sf::Sound *sfx = (sf::Sound*)fx.components[cid].comp; + const sf::SoundBuffer *buffer = sfx->getBuffer(); + sfx->stop(); + + delete sfx; + delete buffer; + } + + fx.components[cid].comp = nullptr; + } + + FxManager::Fx& FxManager::addFx(Fx fx){ + fxs.push_back(fx); + return fxs[fxs.size() - 1]; + } + + void FxManager::removeFx(int id){ + Fx &fx = fxs[id]; + + 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 new file mode 100644 index 0000000..fa724fc --- /dev/null +++ b/fxManager.h @@ -0,0 +1,43 @@ +#ifndef FX_MANAGER_H +#define FX_MANAGER_H + +#include + +namespace sf{ + class Sound; +} + +namespace vb01{ + class Node; +} + +namespace battleship{ + class FxManager{ + public: + struct Fx { + struct Component{ + vb01::s64 duration, offsetTime = 0; + bool vfx; + void *comp = nullptr; + + Component(void *c, bool v, vb01::s64 dur, vb01::s64 ot = 0) : comp(c), vfx(v), duration(dur), offsetTime(ot) {} + }; + + vb01::s64 initTime; + std::vector components; + + Fx(std::vector comp) : initTime(vb01::getTime()), components(comp){} + }; + + static FxManager* getSingleton(); + void update(); + Fx& addFx(Fx); + void removeFx(int); + private: + FxManager(){} + void destroyFxComponent(int, int); + std::vector fxs; + }; +} + +#endif diff --git a/game.cpp b/game.cpp index 365d4d3..452e193 100644 --- a/game.cpp +++ b/game.cpp @@ -5,6 +5,7 @@ #include "activeGameState.h" #include "inGameAppState.h" #include "concreteGuiManager.h" +#include "fxManager.h" #include "defConfigs.h" #include @@ -82,32 +83,9 @@ namespace battleship{ endGame(false); } - for(int i = 0; i < fx.size(); i++){ - if(getTime() - fx[i].initTime > fx[i].vfxTime) - removeFx(i, true); - - if(getTime() - fx[i].initTime > fx[i].sfxTime) - removeFx(i, false); - } + FxManager::getSingleton()->update(); } - //TODO remove the bool flag - void Game::removeFx(int id, bool vfx){ - const sf::SoundBuffer *buffer = fx[id].sfx->getBuffer(); - - if(!vfx){ - fx[id].sfx->stop(); - delete fx[id].sfx; - delete buffer; - fx.erase(fx.begin() + id); - } - - if(fx[id].peNode && vfx){ - Root::getSingleton()->getRootNode()->dettachChild(fx[id].peNode); - delete fx[id].peNode; - fx[id].peNode = nullptr; - } - } void Game::removeAllElements(){ resetLuaGameObjects(); @@ -195,9 +173,9 @@ namespace battleship{ node->lookAt(Vector3::VEC_J, Vector3::VEC_K); root->getRootNode()->attachChild(node); - Fx fx(50, 2500, explosionSfx, node); - fx.activate(); - addFx(fx); + typedef FxManager::Fx Fx; + typedef FxManager::Fx::Component Component; + FxManager::getSingleton()->addFx(Fx(vector{Component((void*)node, true, 50), Component((void*)explosionSfx, false, 2500)})); } void Game::changeUnitPlayer(Unit *unit, Player *newPlayer){ diff --git a/game.h b/game.h index 52bc729..5899048 100644 --- a/game.h +++ b/game.h @@ -1,7 +1,6 @@ #ifndef GAME_H #define GAME_H -#include "fx.h" #include "technology.h" #include "ability.h" #include "tradeOffer.h" @@ -22,7 +21,6 @@ namespace battleship{ static Game* getSingleton(); void update(); void togglePause(); - void removeFx(int, bool); void removeAllElements(); void explode(vb01::Vector3, int, float, sf::Sound*); void changeUnitPlayer(Unit*, Player*); @@ -31,7 +29,6 @@ namespace battleship{ bool isUnitUnlocked(std::vector, int); std::vector findTradeOffers(Player*, Player*); inline void addTradeOffer(TradeOffer *to){tradeOffers.push_back(to);} - inline void addFx(Fx f){fx.push_back(f);} inline void addPlayer(Player *pl){players.push_back(pl);} inline std::vector& getPlayers(){return players;} inline Player* getPlayer(int id){return players[id];} @@ -45,7 +42,6 @@ namespace battleship{ std::vector parseTechTable(int, std::string, std::string, std::string); bool paused = false, ended = false; - std::vector fx; std::vector technologies; std::vector abilities; std::vector players; diff --git a/projectile.cpp b/projectile.cpp index e362ca5..737eb28 100755 --- a/projectile.cpp +++ b/projectile.cpp @@ -7,7 +7,6 @@ #include -#include "fx.h" #include "game.h" #include "unit.h" #include "util.h" diff --git a/unit.cpp b/unit.cpp index 1f6866c..5997ac4 100755 --- a/unit.cpp +++ b/unit.cpp @@ -77,6 +77,9 @@ namespace battleship{ } vector Unit::Weapon::initFx(sol::table weaponTable, string vfxKey){ + if((sol::optional)weaponTable[vfxKey] == sol::nullopt) + return vector{}; + vector vfxNodes; sol::table vfxTbl = weaponTable[vfxKey];