Files
PlanetFleet/projectile.cpp
T
2024-01-16 20:01:19 +02:00

106 lines
2.8 KiB
C++
Executable File

#include <algorithm>
#include <node.h>
#include <model.h>
#include <material.h>
#include <particleEmitter.h>
#include <quaternion.h>
#include <stateManager.h>
#include "game.h"
#include "unit.h"
#include "util.h"
#include "projectile.h"
#include "defConfigs.h"
#include "inGameAppState.h"
#include "fx.h"
using namespace std;
using namespace vb01;
namespace battleship{
using namespace vb01;
using namespace configData;
using namespace gameBase;
Projectile::Projectile(Unit *un, int id, Vector3 pos, Quaternion rot) : GameObject(GameObject::Type::PROJECTILE, id, un->getPlayer(), pos, rot), unit(un){
initProperties();
initModel();
initSound();
placeAt(pos);
orientAt(rot);
}
Projectile::~Projectile(){}
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];
}
void Projectile::initSound(){
GameManager *gm = GameManager::getSingleton();
explosionSfxBuffer = new sf::SoundBuffer();
string p2 = gm->getPath() + "Sounds/Explosions/explosion0" + to_string(rand() % 4) + ".ogg";
if(explosionSfxBuffer->loadFromFile(p2.c_str()))
explosionSfx = new sf::Sound(*explosionSfxBuffer);
}
void Projectile::update() {
GameObject::update();
placeAt(pos + speed * dirVec);
checkForCollision();
}
void Projectile::checkForCollision() {
}
void Projectile::explode(Node *collNode) {
exploded = true;
for (Player *p : Game::getSingleton()->getPlayers()) {
for (Unit *u : p->getUnits())
if (collNode == u->getNode())
u->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);
}
void Projectile::debug(){
}
}