diff --git a/Assets/Scripts/unitData.lua b/Assets/Scripts/unitData.lua index 3e22be3..39a573a 100644 --- a/Assets/Scripts/unitData.lua +++ b/Assets/Scripts/unitData.lua @@ -35,7 +35,7 @@ units = { health = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500}, cost = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500}, range = {15, 15, 14, 14, 13, 13, 12, 12, 15, 25}, - rateOfFire = {100, 100, 100, 100, 100, 100, 100}, + rateOfFire = {5000, 100, 100, 100, 100, 100, 100}, unitCornerPoints = { { @@ -299,5 +299,23 @@ units = { PATH .. 'Sounds/Units/Carriers/selection.ogg', PATH .. 'Sounds/Units/Submarines/selection.ogg', PATH .. 'Sounds/Units/Sample/selection.ogg', + }, + fireSfx = { + PATH .. 'Sounds/Units/WarMechs/fire.ogg', + PATH .. 'Sounds/Units/WarMechs/fire.ogg', + PATH .. 'Sounds/Units/WarMechs/fire.ogg', + PATH .. 'Sounds/Units/WarMechs/fire.ogg', + PATH .. 'Sounds/Units/WarMechs/fire.ogg', + PATH .. 'Sounds/Units/WarMechs/fire.ogg', + PATH .. 'Sounds/Units/WarMechs/fire.ogg', + }, + deathSfx = { + PATH .. 'Sounds/SFX/Explosions/explosion01.ogg', + PATH .. 'Sounds/SFX/Explosions/explosion01.ogg', + PATH .. 'Sounds/SFX/Explosions/explosion01.ogg', + PATH .. 'Sounds/SFX/Explosions/explosion01.ogg', + PATH .. 'Sounds/SFX/Explosions/explosion01.ogg', + PATH .. 'Sounds/SFX/Explosions/explosion01.ogg', + PATH .. 'Sounds/SFX/Explosions/explosion01.ogg', } } diff --git a/CMakeLists.txt b/CMakeLists.txt index f502891..5412af5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp) set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp) set(CORE gameManager.cpp game.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) set(PROJECTILES projectile.cpp) -set(FX explosion.cpp) +set(FX fx.cpp) set(VEHICLES vehicle.cpp engineer.cpp) set(UNITS gameObjectFactory.cpp unit.cpp structure.cpp ${VEHICLES}) set(CONTENT player.cpp pathfinder.cpp map.cpp gameObject.cpp gameObjectFrame.h resourceDeposit.cpp ${UNITS} ${PROJECTILES} ${FX}) diff --git a/explosion.cpp b/explosion.cpp index 7fd6575..53647be 100644 --- a/explosion.cpp +++ b/explosion.cpp @@ -9,7 +9,7 @@ using namespace std; using namespace vb01; namespace battleship{ - void detonate(Vector3 pos, Vector3 dir){ + void detonate(string p){ sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); sf::Sound *sfx = nullptr; string p = GameManager::getSingleton()->getPath() + "Sounds/Explosions/explosion0" + to_string(rand() % 4) + ".ogg"; @@ -19,11 +19,7 @@ namespace battleship{ sfx->play(); } - Fx fx; - fx.initTime = getTime(); - fx.time = 2500; - fx.sfx = sfx; - Game::getSingleton()->addFx(fx); + Game::getSingleton()->addFx(Fx(sfx, 2500)); } void detonateDepthCharge(){ @@ -36,11 +32,7 @@ namespace battleship{ sfx->play(); } - Fx fx; - fx.initTime = getTime(); - fx.time = 2000; - fx.sfx = sfx; - Game::getSingleton()->addFx(fx); + Game::getSingleton()->addFx(Fx(sfx, 2000)); } void detonateTorpedo(){ @@ -53,10 +45,6 @@ namespace battleship{ sfx->play(); } - Fx fx; - fx.initTime = getTime(); - fx.time = 250; - fx.sfx = sfx; - Game::getSingleton()->addFx(fx); + Game::getSingleton()->addFx(Fx(sfx, 250)); } } diff --git a/explosion.h b/explosion.h index f18c035..7dd9684 100644 --- a/explosion.h +++ b/explosion.h @@ -1,12 +1,10 @@ #ifndef EXPLOSION_H #define EXPLOSION_H -#include +#include namespace battleship{ - void detonate(vb01::Vector3, vb01::Vector3); - void detonateDepthCharge(); - void detonateTorpedo(); + void detonate(std::string); } #endif diff --git a/fx.cpp b/fx.cpp new file mode 100644 index 0000000..7b072ab --- /dev/null +++ b/fx.cpp @@ -0,0 +1,21 @@ +#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 index 03b2f12..72f7237 100644 --- a/fx.h +++ b/fx.h @@ -7,10 +7,18 @@ namespace sf{ class Sound; } +namespace vb01{ + class Node; +} + namespace battleship{ struct Fx { vb01::s64 initTime, time; sf::Sound *sfx = nullptr; + vb01::Node *peNode = nullptr; + + Fx(vb01::s64 t, sf::Sound *s = nullptr, vb01::Node *n = nullptr) : time(t), sfx(s), peNode(n), initTime(vb01::getTime()){} + void activate(); }; } diff --git a/game.cpp b/game.cpp index 5c3b159..7a76c07 100644 --- a/game.cpp +++ b/game.cpp @@ -2,7 +2,11 @@ #include "player.h" #include "projectile.h" +#include + namespace battleship{ + using namespace vb01; + static Game *game = nullptr; Game* Game::getSingleton(){ @@ -18,6 +22,24 @@ namespace battleship{ for(Projectile *proj : projectiles) proj->update(); + + for(int i = 0; i < fx.size(); i++) + if(getTime() - fx[i].initTime > fx[i].time) + removeFx(i); + } + + void Game::removeFx(int id){ + const sf::SoundBuffer *buffer = fx[id].sfx->getBuffer(); + fx[id].sfx->stop(); + delete fx[id].sfx; + delete buffer; + + if(fx[id].peNode){ + Root::getSingleton()->getRootNode()->dettachChild(fx[id].peNode); + delete fx[id].peNode; + } + + fx.erase(fx.begin() + id); } void Game::togglePause(){ diff --git a/game.h b/game.h index 9e43f08..0bbb410 100644 --- a/game.h +++ b/game.h @@ -14,6 +14,7 @@ namespace battleship{ static Game* getSingleton(); void update(); void togglePause(); + void removeFx(int); inline void addFx(Fx f){fx.push_back(f);} inline void addPlayer(Player *pl){players.push_back(pl);} inline void addProjectile(Projectile *proj){projectiles.push_back(proj);} diff --git a/gameObject.cpp b/gameObject.cpp index b31676f..f93a555 100644 --- a/gameObject.cpp +++ b/gameObject.cpp @@ -6,6 +6,8 @@ #include +#include + #include namespace battleship{ @@ -64,9 +66,27 @@ namespace battleship{ root->getRootNode()->attachChild(model); } - void GameObject::destroySound(){} + void GameObject::destroySound(){ + } - void GameObject::initSound(){} + sf::Sound* GameObject::prepareSfx(sf::SoundBuffer *buffer, string key){ + sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()]; + string sfxPath = SOL_LUA_STATE[key][id + 1]; + + sf::Sound *sfx = nullptr; + + if(buffer->loadFromFile(sfxPath.c_str())){ + sfx = new sf::Sound(*buffer); + sfx->setBuffer(*buffer); + } + + return sfx; + } + + void GameObject::initSound(){ + deathSfxBuffer = new sf::SoundBuffer(); + deathSfx = prepareSfx(deathSfxBuffer, "deathSfx"); + } string GameObject::getGameObjTableName(){ switch(type){ diff --git a/gameObject.h b/gameObject.h index 668baa1..2467345 100644 --- a/gameObject.h +++ b/gameObject.h @@ -5,6 +5,11 @@ #include #include +namespace sf{ + class SoundBuffer; + class Sound; +} + namespace battleship{ class Player; @@ -43,10 +48,13 @@ namespace battleship{ virtual void initModel(bool = true); virtual void destroySound(); virtual void initSound(); + sf::Sound* prepareSfx(sf::SoundBuffer*, std::string); Type type; int id; Player *player; + sf::SoundBuffer *deathSfxBuffer; + sf::Sound *deathSfx; vb01::Model *model = nullptr; vb01::Vector3 pos = vb01::Vector3(0, 0, 0), upVec = vb01::Vector3(0, 1, 0), dirVec = vb01::Vector3(0, 0, 1), leftVec = vb01::Vector3(1, 0, 0), corners[8]; vb01::Quaternion rot; diff --git a/player.cpp b/player.cpp index 04106ac..f6c5345 100755 --- a/player.cpp +++ b/player.cpp @@ -62,4 +62,20 @@ namespace battleship{ selectedUnits.clear(); } + + void Player::removeUnit(Unit *unit){ + for(int i = 0; i < units.size(); i++) + if(unit == units[i]){ + removeUnit(i); + break; + } + } + + void Player::removeUnit(int id){ + if(units[id]->isSelected()) + deselectUnit(id); + + delete units[id]; + units.erase(units.begin() + id); + } } diff --git a/player.h b/player.h index a92fe19..81b157b 100755 --- a/player.h +++ b/player.h @@ -18,6 +18,8 @@ namespace battleship{ void issueOrder(Order::TYPE, std::vector, bool); void deselectUnit(int); void deselectUnits(); + void removeUnit(Unit*); + void removeUnit(int); bool isThisPlayersUnit(GameObject*); const inline std::vector& getSelectedUnits(){return selectedUnits;} inline Unit* getSelectedUnit(int i){return selectedUnits[i];} diff --git a/projectile.cpp b/projectile.cpp index 8703acc..6a55adf 100755 --- a/projectile.cpp +++ b/projectile.cpp @@ -14,7 +14,7 @@ #include "projectile.h" #include "defConfigs.h" #include "inGameAppState.h" -#include "explosion.h" +#include "fx.h" using namespace std; using namespace vb01; @@ -111,13 +111,7 @@ namespace battleship{ StateManager *sm = GameManager::getSingleton()->getStateManager(); InGameAppState *inGameState = ((InGameAppState*)sm->getAppStateByType((int)AppStateType::IN_GAME_STATE)); - - if(id == 8) - detonateTorpedo(); - else if(weaponTypeId == 1 && (id == 2 || id == 3)) - detonateDepthCharge(); - else - detonate(pos, -dirVec); + string path = GameManager::getSingleton()->getPath() + "Sounds/Explosions/explosion0" + to_string(rand() % 4) + ".ogg"; } void Projectile::debug(){ diff --git a/structure.cpp b/structure.cpp index 9e9724d..a244790 100644 --- a/structure.cpp +++ b/structure.cpp @@ -13,6 +13,12 @@ namespace battleship{ buildStatusForeground = createBar(Unit::lenHpBar, Vector4(0, 0, 1, 1)); } + Structure::~Structure(){ + removeBar(buildStatusForeground); + removeBar(buildStatusBackground); + Unit::~Unit(); + } + void Structure::update(){ Unit::update(); diff --git a/structure.h b/structure.h index 412d861..37574e2 100644 --- a/structure.h +++ b/structure.h @@ -11,6 +11,7 @@ namespace battleship{ class Structure : public Unit{ public: Structure(Player*, int, vb01::Vector3, vb01::Quaternion, int = 0); + ~Structure(); inline int getBuildStatus(){return buildStatus;} inline void incrementBuildStatus(){buildStatus++;} virtual void update(); diff --git a/unit.cpp b/unit.cpp index f9dfc61..d109f8e 100755 --- a/unit.cpp +++ b/unit.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -12,6 +13,7 @@ #include "unit.h" #include "util.h" +#include "game.h" #include "inGameAppState.h" #include "defConfigs.h" #include "pathfinder.h" @@ -32,8 +34,11 @@ namespace battleship{ hpForegroundNode = createBar(lenHpBar, Vector4(0, 1, 0, 1)); } - //TODO complete implementation Unit::~Unit() { + removeBar(hpBackgroundNode); + removeBar(hpForegroundNode); + destroySound(); + destroyModel(); } void Unit::init(){ @@ -46,6 +51,7 @@ namespace battleship{ void Unit::initProperties(){ sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()]; + string name = SOL_LUA_STATE["name"][id + 1]; health = SOL_LUA_STATE["health"][id + 1]; maxHealth = health; @@ -71,16 +77,22 @@ namespace battleship{ selectionSfx->stop(); delete selectionSfx; delete selectionSfxBuffer; + + if(fireSfx){ + fireSfx->stop(); + delete fireSfx; + delete fireSfxBuffer; + } } void Unit::initSound(){ - sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()]; - string name = SOL_LUA_STATE["name"][id + 1]; - selectionSfxBuffer = new sf::SoundBuffer(); - string sfxPath = SOL_LUA_STATE["selectionSfx"][id + 1]; + GameObject::initSound(); - if(selectionSfxBuffer->loadFromFile(sfxPath.c_str())) - selectionSfx = new sf::Sound(*selectionSfxBuffer); + selectionSfxBuffer = new sf::SoundBuffer(); + selectionSfx = GameObject::prepareSfx(selectionSfxBuffer, "selectionSfx"); + + fireSfxBuffer = new sf::SoundBuffer(); + fireSfx = prepareSfx(fireSfxBuffer, "fireSfx"); } void Unit::reinit(){ @@ -110,6 +122,11 @@ namespace battleship{ return node; } + void Unit::removeBar(Node *node){ + Root::getSingleton()->getGuiNode()->dettachChild(node); + delete node; + } + void Unit::initUnitStats(){ } @@ -130,11 +147,41 @@ namespace battleship{ displayUnitStats(hpForegroundNode, hpBackgroundNode, health, maxHealth); if (health <= 0) - blowUp(); + blowUp(); } void Unit::blowUp(){ - working = false; + Root *root = Root::getSingleton(); + + const int numFrames = 1; + string p[numFrames]; + + for(int i = 0; i < numFrames; i++) + p[i] = GameManager::getSingleton()->getPath() + "Textures/Explosion/explosion0" + to_string(7) + ".png"; + + Texture *tex = new Texture(numFrames, p, false); + + Material *mat = new Material(root->getLibPath() + "particle"); + mat->addVec4Uniform("startColor", Vector4(1, 1, 1, 1)); + mat->addVec4Uniform("endColor", Vector4(1, 1, 0, 1)); + mat->addTexUniform("tex", tex, false); + + ParticleEmitter *pe = new ParticleEmitter(1); + pe->setMaterial(mat); + pe->setLowLife(3); + pe->setHighLife(3); + pe->setSize(10 * Vector2::VEC_IJ); + pe->setSpeed(0); + Node *node = new Node(pos + Vector3(0, 2, 0)); + node->attachParticleEmitter(pe); + node->lookAt(Vector3::VEC_J, Vector3::VEC_K); + root->getRootNode()->attachChild(node); + + Fx fx(2500, deathSfx, node); + fx.activate(); + Game::getSingleton()->addFx(fx); + + player->removeUnit(this); } void Unit::displayUnitStats(Node *foreground, Node *background, int currVal, int maxVal, Vector2 offset) { diff --git a/unit.h b/unit.h index 121f189..27d4d49 100755 --- a/unit.h +++ b/unit.h @@ -6,12 +6,15 @@ #include #include -#include - #include "gameManager.h" #include "gameObject.h" #include "projectile.h" +namespace sf{ + class SoundBuffer; + class Sound; +} + namespace vb01{ class Mesh; class Quad; @@ -59,7 +62,6 @@ namespace battleship{ virtual void addOrder(Order); virtual void reinit(); inline vb01::Vector3 getCorner(int i){return corners[i];} - inline bool isWorking(){return working;} inline vb01::Vector2 getScreenPos(){return screenPos;} inline vb01::Vector3* getPosPtr() {return &pos;} inline float getLineOfSight() {return lineOfSight;} @@ -83,9 +85,10 @@ namespace battleship{ vb01::Vector2 screenPos; std::vector orders; vb01::Vector3 corners[8]; + sf::SoundBuffer *fireSfxBuffer; + sf::Sound *fireSfx = nullptr; int health, maxHealth, cost, id, playerId, lenHpBar = 200, rateOfFire; s64 orderLineDispTime = 0, lastFireTime = 0; - bool working = true; float lineOfSight, range; void removeOrder(int); @@ -101,6 +104,7 @@ namespace battleship{ virtual void patrol(Order){} virtual void launch(Order){} vb01::Node* createBar(float, vb01::Vector4); + void removeBar(vb01::Node*); }; } diff --git a/vehicle.cpp b/vehicle.cpp index 2dd8cfe..e52bd18 100644 --- a/vehicle.cpp +++ b/vehicle.cpp @@ -26,6 +26,13 @@ namespace battleship{ debugMat->addVec4Uniform("diffuseColor", Vector4::VEC_IJKL); } + Vehicle::~Vehicle(){ + removeAllPathpoints(); + delete debugMat; + + Unit::~Unit(); + } + void Vehicle::halt(){ Unit::halt(); removeAllPathpoints(); @@ -221,4 +228,13 @@ namespace battleship{ fire(); } } + + void Vehicle::fire(){ + fireSfx->play(); + + Unit *targetUnit = orders[0].targets[0].unit; + + if(targetUnit) + targetUnit->takeDamage(1); + } } diff --git a/vehicle.h b/vehicle.h index 1920c23..d099c9a 100644 --- a/vehicle.h +++ b/vehicle.h @@ -11,6 +11,7 @@ namespace battleship{ class Vehicle : public Unit{ public: Vehicle(Player*, int, vb01::Vector3, vb01::Quaternion); + ~Vehicle(); void move(Order); private: bool pursuingTarget = false; @@ -34,7 +35,7 @@ namespace battleship{ void navigate(Order, float = 0.); void alignToSurface(); void attack(Order); - void fire(){} + virtual void fire(); }; }