From dbbd06b16d230fc13cfe43390feefd501effe67e Mon Sep 17 00:00:00 2001 From: devZoGok Date: Mon, 22 Jan 2024 11:15:46 +0200 Subject: [PATCH] factored out explosion behaviour --- .../Projectiles/projectileData.lua | 3 +- cruiseMissile.cpp | 8 ++- game.cpp | 42 +++++++++++++ game.h | 5 ++ projectile.cpp | 59 +++++-------------- projectile.h | 5 +- unit.cpp | 39 ++---------- unit.h | 1 - 8 files changed, 77 insertions(+), 85 deletions(-) diff --git a/Assets/Scripts/GameObjects/Projectiles/projectileData.lua b/Assets/Scripts/GameObjects/Projectiles/projectileData.lua index 46e09f0..d55ae22 100644 --- a/Assets/Scripts/GameObjects/Projectiles/projectileData.lua +++ b/Assets/Scripts/GameObjects/Projectiles/projectileData.lua @@ -6,8 +6,9 @@ projectiles = { ProjectileClass.TORPEDO, }, speed = {.5, .06}, + explosion = {{damage = 100, radius = 20}, {damage = 20, radius = 3}}, rayLength = {1.25, .75}, - damage = {450, 300}, + directHitDamage = {450, 300}, name = { "Cruise missile", "Torpedo" diff --git a/cruiseMissile.cpp b/cruiseMissile.cpp index c4f52b3..90bc4d3 100644 --- a/cruiseMissile.cpp +++ b/cruiseMissile.cpp @@ -2,6 +2,7 @@ #include "player.h" #include "unit.h" #include "map.h" +#include "game.h" #include @@ -50,8 +51,11 @@ namespace battleship{ Map *map = Map::getSingleton(); int cellId = map->getCellId(pos, false); - if((pos + dirVec * rayLength).y <= map->getCells()[cellId].pos.y) - explode(); + if((pos + dirVec * rayLength).y <= map->getCells()[cellId].pos.y){ + exploded = true; + Game::getSingleton()->explode(pos, explosionDamage, explosionRadius, explosionSfx); + player->removeProjectile(this); + } } void CruiseMissile::update(){ diff --git a/game.cpp b/game.cpp index f988d8a..4f7970d 100644 --- a/game.cpp +++ b/game.cpp @@ -8,6 +8,7 @@ #include "defConfigs.h" #include +#include #include @@ -143,4 +144,45 @@ namespace battleship{ paused = !paused; } + + void Game::explode(Vector3 pos, int damage, float radius, sf::Sound *explosionSfx){ + if(!(damage == 0 || radius == 0)) + for(Player *pl : players){ + for(Unit *un : pl->getUnits()){ + float distance = un->getPos().getDistanceFrom(pos); + + if(distance < radius) + un->takeDamage(int(damage * (1.f - distance / radius))); + } + } + + 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/explosion07.png"; + + Texture *tex = new Texture(p, numFrames, false); + + Material *mat = new Material(root->getLibPath() + "particle"); + mat->addTexUniform("tex", tex, true); + + 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(50, 2500, explosionSfx, node); + fx.activate(); + addFx(fx); + } } diff --git a/game.h b/game.h index bd20c49..445ec22 100644 --- a/game.h +++ b/game.h @@ -5,6 +5,10 @@ #include +namespace sf{ + class Sound; +} + namespace battleship{ class Player; class Projectile; @@ -16,6 +20,7 @@ namespace battleship{ void togglePause(); void removeFx(int, bool); void removeAllElements(); + void explode(vb01::Vector3, int, float, sf::Sound*); inline void addFx(Fx f){fx.push_back(f);} inline void addPlayer(Player *pl){players.push_back(pl);} inline std::vector& getPlayers(){return players;} diff --git a/projectile.cpp b/projectile.cpp index b93450e..9afccfe 100755 --- a/projectile.cpp +++ b/projectile.cpp @@ -39,10 +39,13 @@ namespace battleship{ void Projectile::initProperties(){ GameObject::initProperties(); - sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()]; - rayLength = SOL_LUA_STATE["rayLength"][id + 1]; - damage = SOL_LUA_STATE["damage"][id + 1]; - speed = SOL_LUA_STATE["speed"][id + 1]; + sol::table projTable = generateView()[GameObject::getGameObjTableName()]; + rayLength = projTable["rayLength"][id + 1]; + directHitDamage = projTable["directHitDamage"][id + 1]; + string explKey = "explosion"; + explosionDamage = projTable[explKey][id + 1]["damage"]; + explosionRadius = projTable[explKey][id + 1]["radius"]; + speed = projTable["speed"][id + 1]; } void Projectile::initSound(){ @@ -84,8 +87,12 @@ namespace battleship{ if(!results.empty()){ for(int i = 0; i < targetNodes.size(); i++) - if(targetNodes[i]->getMesh(0) == results[0].mesh) - explode(); + if(targetNodes[i]->getMesh(0) == results[0].mesh){ + exploded = true; + targetUnits[i]->takeDamage(directHitDamage); + Game::getSingleton()->explode(pos, explosionDamage, explosionRadius, explosionSfx); + player->removeProjectile(this); + } } } @@ -93,46 +100,12 @@ namespace battleship{ Map *map = Map::getSingleton(); int cellId = map->getCellId(pos, false); - if(map->getCells()[cellId].type == Map::Cell::LAND) + if(map->getCells()[cellId].type == Map::Cell::LAND){ + exploded = true; player->removeProjectile(this); + } } - //TODO implement splash damage to units - void Projectile::explode(){ - exploded = true; - //target->takeDamage(damage); - 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/explosion07.png"; - - Texture *tex = new Texture(p, numFrames, false); - - Material *mat = new Material(root->getLibPath() + "particle"); - mat->addTexUniform("tex", tex, true); - - 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(50, 2500, explosionSfx, node); - fx.activate(); - Game::getSingleton()->addFx(fx); - - player->removeProjectile(this); - } - void Projectile::debug(){ } } diff --git a/projectile.h b/projectile.h index 6e0cb93..2a4b9fe 100755 --- a/projectile.h +++ b/projectile.h @@ -27,13 +27,12 @@ namespace battleship { void initSound(); void checkCollision(); protected: - void explode(); virtual void checkUnitCollision(); virtual void checkSurfaceCollision(); bool exploded = false; - float speed, rayLength; - int damage; + float speed, rayLength, explosionRadius; + int directHitDamage, explosionDamage; vb01::Vector3 initPos; Unit *unit = nullptr; sf::SoundBuffer *shotSfxBuffer, *explosionSfxBuffer; diff --git a/unit.cpp b/unit.cpp index f212a50..8c29ab7 100755 --- a/unit.cpp +++ b/unit.cpp @@ -274,41 +274,10 @@ namespace battleship{ for(GarrisonSlot &slot : garrisonSlots) displayUnitStats(slot.foreground, slot.background, (int)((bool)slot.vehicle), (int)true, renderSelectables, slot.offset); - if (health <= DEATH_HP) - blowUp(); - } - - void Unit::blowUp(){ - 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/explosion07.png"; - - Texture *tex = new Texture(p, numFrames, false); - - Material *mat = new Material(root->getLibPath() + "particle"); - mat->addTexUniform("tex", tex, true); - - 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(50, 2500, deathSfx, node); - fx.activate(); - Game::getSingleton()->addFx(fx); - - player->removeUnit(this); + if (health <= DEATH_HP){ + Game::getSingleton()->explode(pos, 0, 0, deathSfx); + player->removeUnit(this); + } } void Unit::displayUnitStats(Node *foreground, Node *background, int currVal, int maxVal, bool render, Vector2 offset) { diff --git a/unit.h b/unit.h index 7bf3588..ef1d96a 100755 --- a/unit.h +++ b/unit.h @@ -100,7 +100,6 @@ namespace battleship{ Unit(Player*, int, vb01::Vector3, vb01::Quaternion); virtual ~Unit(); virtual void update(); - virtual void blowUp(); virtual void halt(); void updateGarrison(Vehicle*, bool); virtual void select();