diff --git a/Assets/Scripts/GameObjects/Units/unitData.lua b/Assets/Scripts/GameObjects/Units/unitData.lua index 9fb5070..b050a92 100644 --- a/Assets/Scripts/GameObjects/Units/unitData.lua +++ b/Assets/Scripts/GameObjects/Units/unitData.lua @@ -59,19 +59,23 @@ units = { rateOfFire = 100, maxRange = 10, damage = 50, - fireSfx = PATH .. 'Sounds/Units/WarMechs/fire.ogg', - fireVfx = { - --[[ + fireFx = { { - model = '', - texture = '', - duration = 20, - pos = {x = 0, y = 0, z = 0}, - rot = {w = 0, x = 0, y = 0, z = 0}, - } - ]]-- + vfx = true, + duration = 50, + path = PATH .. 'Models/VFX/muzzleFlash.xml', + color = {x = 1, y = 1, z = 0, a = 1}, + pos = {x = 0.214, y = 2.742, z = 1.54}, + rot = {w = 1, x = 0, y = 0, z = 0}, + --scale = .5 + }, + { + vfx = false, + duration = 500, + path = PATH .. 'Sounds/Units/WarMechs/fire.ogg' + }, }, - hitVfx = { + hitFx = { } } }, diff --git a/fxManager.cpp b/fxManager.cpp index 97fe15b..64f4f6f 100644 --- a/fxManager.cpp +++ b/fxManager.cpp @@ -15,6 +15,16 @@ namespace battleship{ ((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); + } + + void FxManager::Fx::toggleComponents(bool active){ + for(Component &component : components) + component.active = active; + } + static FxManager *fxManager = nullptr; FxManager* FxManager::getSingleton(){ @@ -27,36 +37,40 @@ namespace battleship{ void FxManager::update(){ for(int i = 0; i < fxs.size(); i++){ s64 currTime = getTime(); - Fx &fx = fxs[i]; + Fx *fx = fxs[i]; - for(int j = 0; j < fx.components.size(); j++){ - Fx::Component &comp = fx.components[j]; + for(int j = 0; j < fx->components.size(); j++){ + Fx::Component &comp = fx->components[j]; - if(comp.comp && getTime() - fx.initTime > comp.offsetTime){ - if(!comp.active){ + if(comp.comp && getTime() - comp.initTime > comp.offsetTime){ + if(comp.active){ if(comp.vfx) ((Node*)comp.comp)->setVisible(true); else ((sf::Sound*)comp.comp)->play(); - comp.active = true; + comp.active = false; + comp.initTime = getTime(); } - if(getTime() - fx.initTime > comp.offsetTime + comp.duration){ - if(fx.reuse){ - if(comp.vfx) ((Node*)comp.comp)->setVisible(false); - comp.active = false; + if(!comp.active && getTime() - comp.initTime > comp.offsetTime + comp.duration){ + if(fx->reuse){ + if(comp.vfx) + ((Node*)comp.comp)->setVisible(false); + else + ((sf::Sound*)comp.comp)->stop(); } - else if(!fx.reuse){ + else{ destroyFxComponent(i, j); - fx.components.erase(fx.components.begin() + j); + fx->components.erase(fx->components.begin() + j); j--; } } } + } - if(fx.components.empty()){ + if(fx->components.empty()){ fxs.erase(fxs.begin() + i); i--; } @@ -64,15 +78,15 @@ namespace battleship{ } void FxManager::destroyFxComponent(int fid, int cid){ - Fx &fx = fxs[fid]; + Fx *fx = fxs[fid]; - if(fx.components[cid].vfx){ - Node *vfxNode = (Node*)fx.components[cid].comp, *parNode = vfxNode->getParent(); + 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; + sf::Sound *sfx = (sf::Sound*)fx->components[cid].comp; const sf::SoundBuffer *buffer = sfx->getBuffer(); sfx->stop(); @@ -80,17 +94,29 @@ namespace battleship{ delete buffer; } - fx.components[cid].comp = nullptr; + fx->components[cid].comp = nullptr; } - FxManager::Fx& FxManager::addFx(Fx fx){ + FxManager::Fx* FxManager::addFx(Fx *fx){ fxs.push_back(fx); return fxs[fxs.size() - 1]; } + //TODO remove dublicatory code void FxManager::removeFx(int id){ - Fx &fx = fxs[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); + } + + /* + void FxManager::removeFx(Fx &fx){ for(int j = 0; j < fx.components.size(); j++) destroyFxComponent(id, j); @@ -99,4 +125,5 @@ namespace battleship{ fxs.erase(fxs.begin() + id); } + */ } diff --git a/fxManager.h b/fxManager.h index 5e07e8c..7ed8c5d 100644 --- a/fxManager.h +++ b/fxManager.h @@ -8,7 +8,7 @@ namespace battleship{ public: struct Fx { struct Component{ - vb01::s64 duration, offsetTime = 0; + vb01::s64 duration, initTime = 0, offsetTime = 0; bool vfx, active = false; void *comp = nullptr; @@ -19,17 +19,19 @@ namespace battleship{ vb01::s64 initTime; std::vector components; - Fx(std::vector comp, bool re = false) : initTime(vb01::getTime()), components(comp), reuse(re){} + Fx(std::vector, bool = true, bool = false); + void toggleComponents(bool); }; static FxManager* getSingleton(); void update(); - Fx& addFx(Fx); + Fx* addFx(Fx*); void removeFx(int); + void removeFx(Fx&); private: FxManager(){} void destroyFxComponent(int, int); - std::vector fxs; + std::vector fxs; }; } diff --git a/game.cpp b/game.cpp index 452e193..c12ae47 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(Fx(vector{Component((void*)node, true, 50), Component((void*)explosionSfx, false, 2500)})); + FxManager::getSingleton()->addFx(new Fx(vector{Component((void*)node, true, 50), Component((void*)explosionSfx, false, 2500)}, true)); } void Game::changeUnitPlayer(Unit *unit, Player *newPlayer){ diff --git a/map.cpp b/map.cpp index 2516e02..6ebaae8 100755 --- a/map.cpp +++ b/map.cpp @@ -151,6 +151,7 @@ namespace battleship{ sol::state_view SOL_LUA_VIEW = generateView(); assetManager->load(path + (string)SOL_LUA_VIEW["modelPrefix"], true); + assetManager->load(path + "Models/VFX/", true); assetManager->load(path + "Textures/", true); string playerInd = "players"; diff --git a/unit.cpp b/unit.cpp index 5997ac4..8d27298 100755 --- a/unit.cpp +++ b/unit.cpp @@ -35,14 +35,14 @@ namespace battleship{ damage(weaponTable["damage"].get_or(0)) { maxRange = weaponTable["maxRange"]; - fireSfxBuffer = new sf::SoundBuffer(); - string sfxPath = weaponTable["fireSfx"]; - fireSfx = GameObject::prepareSfx(fireSfxBuffer, sfxPath); initProjectileData(weaponTable); - initFx(weaponTable, "fireVfx"); - initFx(weaponTable, "hitVfx"); + fireFx = initFx(weaponTable, "fireFx"); + if(fireFx) FxManager::getSingleton()->addFx(fireFx); + + hitFx = initFx(weaponTable, "hitFx"); + if(hitFx) FxManager::getSingleton()->addFx(hitFx); } void Unit::Weapon::initProjectileData(sol::table weaponTable){ @@ -76,54 +76,72 @@ namespace battleship{ } } - vector Unit::Weapon::initFx(sol::table weaponTable, string vfxKey){ + FxManager::Fx* Unit::Weapon::initFx(sol::table weaponTable, string vfxKey){ if((sol::optional)weaponTable[vfxKey] == sol::nullopt) - return vector{}; + return nullptr; - vector vfxNodes; + int numComponents = ((sol::table)weaponTable[vfxKey]).size(); + vector fxComponents; - sol::table vfxTbl = weaponTable[vfxKey]; - int numVfx = vfxTbl.size(); + for(int i = 0; i < numComponents; i++){ + sol::table vfxTbl = weaponTable[vfxKey][i + 1]; - for(int i = 0; i < numVfx; i++){ - Material *mat = new Material(Root::getSingleton()->getLibPath() + "texture"); + bool vfx = vfxTbl["vfx"]; + s64 duration = vfxTbl["duration"]; - if((std::optional)vfxTbl["texture"] != sol::nullopt){ - string p[]{vfxTbl["texture"]}; - Texture *tex = new Texture(p, 1, false); - mat->addBoolUniform("texturingEnabled", true); - mat->addTexUniform("diffuseMap[0]", tex, false); + if(vfx){ + Material *mat = new Material(Root::getSingleton()->getLibPath() + "texture"); + + if((sol::optional)vfxTbl["texture"] != sol::nullopt){ + string p[]{vfxTbl["texture"]}; + Texture *tex = new Texture(p, 1, false); + mat->addBoolUniform("texturingEnabled", true); + mat->addTexUniform("diffuseMap[0]", tex, false); + } + else{ + sol::table colorTable = vfxTbl["color"]; + mat->addVec4Uniform("diffuseColor", Vector4(colorTable["x"], colorTable["y"], colorTable["z"], colorTable["a"])); + mat->addBoolUniform("texturingEnabled", false); + } + + Model *flashModel = new Model((string)vfxTbl["path"]); + flashModel->setMaterial(mat); + flashModel->setVisible(false); + unit->getModel()->attachChild(flashModel); + + sol::table posTable = vfxTbl["pos"], rotTable = vfxTbl["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"]; + flashModel->setScale(Vector3(sc, sc, sc)); + } + + fxComponents.push_back(FxManager::Fx::Component((void*)flashModel, vfx, duration)); } else{ - sol::table colorTable = vfxTbl["color"]; - mat->addVec4Uniform("diffuseColor", Vector4(colorTable["x"], colorTable["y"], colorTable["z"], colorTable["a"])); - mat->addBoolUniform("texturingEnabled", false); + sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); + sf::Sound *sfx = GameObject::prepareSfx(sfxBuffer, vfxTbl["path"]); + fxComponents.push_back(FxManager::Fx::Component((void*)sfx, vfx, duration)); } - - Model *flashModel = new Model((string)vfxTbl["model"]); - flashModel->setMaterial(mat); - unit->getModel()->attachChild(flashModel); - - sol::table posTable = vfxTbl["pos"][i + 1], rotTable = vfxTbl["rot"][i + 1]; - flashModel->setPosition(Vector3(posTable["x"], posTable["y"], posTable["z"])); - flashModel->setOrientation(Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"])); } - return vfxNodes; + return new FxManager::Fx(fxComponents, false, true); } Unit::Weapon::~Weapon(){ - if(fireSfx){ - fireSfx->stop(); - delete fireSfx; - delete fireSfxBuffer; - } + //FxManager::removeFx(); + } + + void Unit::Weapon::update(){ } void Unit::Weapon::fire(Order order){ if(!canFire()) return; - fireSfx->play(); + if(fireFx)fireFx->toggleComponents(true); + Unit *targetUnit = order.targets[0].unit; if(targetUnit && projId == -1){ @@ -405,6 +423,9 @@ namespace battleship{ Game::getSingleton()->explode(pos, 0, 0, deathSfx); remove = true; } + + for(Weapon *weapon : weapons) + weapon->update(); } void Unit::displayUnitStats(Node *foreground, Node *background, int currVal, int maxVal, bool render, Vector2 offset) { diff --git a/unit.h b/unit.h index 883f588..ad8363b 100755 --- a/unit.h +++ b/unit.h @@ -10,6 +10,7 @@ #include "gameObject.h" #include "buildableUnit.h" +#include "fxManager.h" namespace sf{ class SoundBuffer; @@ -85,6 +86,7 @@ namespace battleship{ Weapon(Unit*, sol::table); ~Weapon(); + virtual void update(); virtual void fire(Order); inline Type getType(){return type;} inline int getProjectileId(){return projId;} @@ -101,12 +103,10 @@ namespace battleship{ vb01::Quaternion projRot; vb01::s64 lastFireTime = 0; Unit *unit = nullptr; - sf::SoundBuffer *fireSfxBuffer; - sf::Sound *fireSfx = nullptr; - std::vector fireVfxNodes; + FxManager::Fx *fireFx = nullptr, *hitFx = nullptr; void initProjectileData(sol::table); - std::vector initFx(sol::table, std::string); + FxManager::Fx* initFx(sol::table, std::string); inline bool canFire(){return vb01::getTime() - lastFireTime > rateOfFire;} };