/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific main-programmer: Patrick Boenzli co-programmer: Benjamin Grauer */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON #include "rocket.h" #include "fast_factory.h" #include "model.h" #include "state.h" #include "list.h" #include "class_list.h" #include "particle_engine.h" #include "particle_emitter.h" #include "particle_system.h" using namespace std; CREATE_FAST_FACTORY_STATIC(Rocket, CL_ROCKET); /** * standard constructor */ Rocket::Rocket () : Projectile() { this->setClassID(CL_TEST_BULLET, "Rocket"); float modelSize = .3; this->loadModel("models/projectiles/orx-rocket.obj", .3); this->energyMin = 1; this->energyMax = 10; this->lifeSpan = 5; this->emitter = new ParticleEmitter(Vector(0,1,0), M_2_PI, 100, 5); this->emitter->setParent(this); this->emitter->setSpread(M_PI, M_PI); } /** * standard deconstructor */ Rocket::~Rocket () { // delete this->emitter; /* this is normaly done by World.cc by deleting the ParticleEngine */ if (Rocket::trailParticles != NULL && ClassList::getList(CL_TEST_BULLET)->size() <= 1) { if (ClassList::exists(Rocket::trailParticles, CL_PARTICLE_SYSTEM)) delete Rocket::trailParticles; Rocket::trailParticles = NULL; } if (Rocket::explosionParticles != NULL && ClassList::getList(CL_TEST_BULLET)->size() <= 1) { if (ClassList::exists(Rocket::explosionParticles, CL_PARTICLE_SYSTEM)) delete Rocket::explosionParticles; Rocket::explosionParticles = NULL; } } ParticleSystem* Rocket::trailParticles = NULL; ParticleSystem* Rocket::explosionParticles = NULL; void Rocket::activate() { State::getWorldEntityList()->add(this); if (unlikely(Rocket::trailParticles == NULL)) { Rocket::trailParticles = new ParticleSystem(2000, PARTICLE_SPRITE); Rocket::trailParticles->setName("RocketTrailParticles"); Rocket::trailParticles->setMaterialTexture("maps/radial-trans-noise.png"); Rocket::trailParticles->setLifeSpan(1.0, .3); Rocket::trailParticles->setRadius(0.0, .5); Rocket::trailParticles->setRadius(0.2, 2.0); Rocket::trailParticles->setRadius(.5, .8); Rocket::trailParticles->setRadius(1.0, .8); Rocket::trailParticles->setColor(0.0, 1,0,0,.7); Rocket::trailParticles->setColor(0.2, .8,.8,0,.5); Rocket::trailParticles->setColor(0.5, .8,.8,.8,.8); Rocket::trailParticles->setColor(1.0, .8,.8,.8,.0); } if (unlikely(Rocket::explosionParticles == NULL)) { Rocket::explosionParticles = new ParticleSystem(200, PARTICLE_SPRITE); Rocket::explosionParticles->setName("RocketExplosionParticles"); Rocket::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png"); Rocket::explosionParticles->setLifeSpan(.5, .3); Rocket::explosionParticles->setRadius(0.0, 10); Rocket::explosionParticles->setRadius(.5, 15.0); Rocket::explosionParticles->setRadius(1.0, 10.0); Rocket::explosionParticles->setColor(0.0, 0,1,0,1); Rocket::explosionParticles->setColor(0.5, .8,.8,0,.8); Rocket::explosionParticles->setColor(0.8, .8,.8,.3,.8); Rocket::explosionParticles->setColor(1.0, 1,1,1,.0); } ParticleEngine::getInstance()->addConnection(this->emitter, Rocket::trailParticles); this->updateNode(0); this->emitter->setEmissionRate(45.0); this->emitter->setEmissionVelocity(0.0); } void Rocket::deactivate() { ParticleEngine::getInstance()->breakConnections(this->emitter); this->lifeCycle = 0.0; // GarbageCollector::getInstance()->collect(this); State::getWorldEntityList()->remove(this); Rocket::fastFactory->kill(this); } void Rocket::collidesWith(WorldEntity* entity, const Vector& location) { if (this->hitEntity != entity && entity->isA(CL_NPC)) this->destroy(); this->hitEntity = entity; } /** * signal tick, time dependent things will be handled here * @param time since last tick */ void Rocket::tick (float time) { //Vector v = *this->flightDirection * ( this->speed * time * 1000 + 0.1); Vector v = this->velocity * (time); this->shiftCoor(v); this->lifeCycle += time/this->lifeSpan; if( this->lifeCycle >= 1.0) { PRINTF(5)("FINALIZE==========================\n"); PRINTF(5)("current life cycle is: %f\n", this->lifeCycle); PRINTF(5)("FINALIZE===========================\n"); this->deactivate(); } } /** * the function gets called, when the projectile is destroyed */ void Rocket::destroy () { PRINTF(5)("DESTROY Rocket\n"); this->lifeCycle = .95; //!< @todo calculate this usefully. ParticleEngine::getInstance()->breakConnection(this->emitter, Rocket::trailParticles); ParticleEngine::getInstance()->addConnection(this->emitter, Rocket::explosionParticles); this->emitter->setEmissionRate(1000.0); this->emitter->setEmissionVelocity(50.0); // this->deactivate(); } void Rocket::draw () const { glMatrixMode(GL_MODELVIEW); glPushMatrix(); float matrix[4][4]; glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); this->getAbsDir().matrix (matrix); glMultMatrixf((float*)matrix); glScalef(2.0, 2.0, 2.0); this->model->draw(); glPopMatrix(); }