[4593] | 1 | /* |
---|
[3708] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific |
---|
[5759] | 12 | main-programmer: Silvan Nellen |
---|
| 13 | co-programmer: |
---|
[5443] | 14 | |
---|
[3708] | 15 | */ |
---|
[5357] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
[3708] | 17 | |
---|
[5759] | 18 | #include "guided_missile.h" |
---|
[3708] | 19 | |
---|
[5443] | 20 | #include "state.h" |
---|
[5444] | 21 | #include "class_list.h" |
---|
[5054] | 22 | |
---|
[6822] | 23 | #include "dot_emitter.h" |
---|
[6621] | 24 | #include "sprite_particles.h" |
---|
[5443] | 25 | |
---|
[8362] | 26 | #include "debug.h" |
---|
[3708] | 27 | |
---|
[5759] | 28 | CREATE_FAST_FACTORY_STATIC(GuidedMissile, CL_GUIDED_MISSILE); |
---|
[3708] | 29 | |
---|
| 30 | /** |
---|
[4836] | 31 | * standard constructor |
---|
[3708] | 32 | */ |
---|
[5759] | 33 | GuidedMissile::GuidedMissile () : Projectile() |
---|
[3755] | 34 | { |
---|
[5759] | 35 | this->setClassID(CL_GUIDED_MISSILE, "GuidedMissile"); |
---|
[4597] | 36 | |
---|
[5764] | 37 | this->loadModel("models/projectiles/orx-rocket.obj", .3); |
---|
[7086] | 38 | this->loadExplosionSound("sound/explosions/explosion_4.wav"); |
---|
[4948] | 39 | |
---|
[7086] | 40 | |
---|
[6431] | 41 | this->setMinEnergy(1); |
---|
[6700] | 42 | this->setHealthMax(10); |
---|
[7081] | 43 | this->lifeSpan = 4.0; |
---|
[7102] | 44 | this->agility = 3.5; |
---|
[6162] | 45 | this->maxVelocity = 75; |
---|
[5443] | 46 | |
---|
[6825] | 47 | this->emitter = new DotEmitter(100, 5, M_2_PI); |
---|
[5443] | 48 | this->emitter->setParent(this); |
---|
[5449] | 49 | this->emitter->setSpread(M_PI, M_PI); |
---|
[3755] | 50 | } |
---|
[3708] | 51 | |
---|
| 52 | |
---|
| 53 | /** |
---|
[4836] | 54 | * standard deconstructor |
---|
[3708] | 55 | */ |
---|
[5759] | 56 | GuidedMissile::~GuidedMissile () |
---|
[3708] | 57 | { |
---|
[5446] | 58 | // delete this->emitter; |
---|
[5444] | 59 | |
---|
[5445] | 60 | /* this is normaly done by World.cc by deleting the ParticleEngine */ |
---|
[6627] | 61 | if (GuidedMissile::trailParticles != NULL && ClassList::getList(CL_GUIDED_MISSILE)->size() <= 1) |
---|
[5447] | 62 | { |
---|
[5759] | 63 | if (ClassList::exists(GuidedMissile::trailParticles, CL_PARTICLE_SYSTEM)) |
---|
| 64 | delete GuidedMissile::trailParticles; |
---|
| 65 | GuidedMissile::trailParticles = NULL; |
---|
[5447] | 66 | } |
---|
[6627] | 67 | if (GuidedMissile::explosionParticles != NULL && ClassList::getList(CL_GUIDED_MISSILE)->size() <= 1) |
---|
[5444] | 68 | { |
---|
[5759] | 69 | if (ClassList::exists(GuidedMissile::explosionParticles, CL_PARTICLE_SYSTEM)) |
---|
| 70 | delete GuidedMissile::explosionParticles; |
---|
| 71 | GuidedMissile::explosionParticles = NULL; |
---|
[5444] | 72 | } |
---|
[5445] | 73 | |
---|
[3708] | 74 | } |
---|
| 75 | |
---|
[6622] | 76 | SpriteParticles* GuidedMissile::trailParticles = NULL; |
---|
| 77 | SpriteParticles* GuidedMissile::explosionParticles = NULL; |
---|
[5443] | 78 | |
---|
[5760] | 79 | |
---|
| 80 | |
---|
[5759] | 81 | void GuidedMissile::activate() |
---|
[5443] | 82 | { |
---|
[5759] | 83 | if (unlikely(GuidedMissile::trailParticles == NULL)) |
---|
[5447] | 84 | { |
---|
[6621] | 85 | GuidedMissile::trailParticles = new SpriteParticles(2000); |
---|
[5759] | 86 | GuidedMissile::trailParticles->setName("GuidedMissileTrailParticles"); |
---|
| 87 | GuidedMissile::trailParticles->setMaterialTexture("maps/radial-trans-noise.png"); |
---|
| 88 | GuidedMissile::trailParticles->setLifeSpan(1.0, .3); |
---|
| 89 | GuidedMissile::trailParticles->setRadius(0.0, .5); |
---|
| 90 | GuidedMissile::trailParticles->setRadius(0.2, 2.0); |
---|
| 91 | GuidedMissile::trailParticles->setRadius(.5, .8); |
---|
| 92 | GuidedMissile::trailParticles->setRadius(1.0, .8); |
---|
| 93 | GuidedMissile::trailParticles->setColor(0.0, 1,0,0,.7); |
---|
| 94 | GuidedMissile::trailParticles->setColor(0.2, .8,.8,0,.5); |
---|
| 95 | GuidedMissile::trailParticles->setColor(0.5, .8,.8,.8,.8); |
---|
| 96 | GuidedMissile::trailParticles->setColor(1.0, .8,.8,.8,.0); |
---|
[5447] | 97 | } |
---|
[5759] | 98 | if (unlikely(GuidedMissile::explosionParticles == NULL)) |
---|
[5443] | 99 | { |
---|
[6621] | 100 | GuidedMissile::explosionParticles = new SpriteParticles(200); |
---|
[5759] | 101 | GuidedMissile::explosionParticles->setName("GuidedMissileExplosionParticles"); |
---|
| 102 | GuidedMissile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png"); |
---|
| 103 | GuidedMissile::explosionParticles->setLifeSpan(.5, .3); |
---|
| 104 | GuidedMissile::explosionParticles->setRadius(0.0, 10); |
---|
| 105 | GuidedMissile::explosionParticles->setRadius(.5, 15.0); |
---|
| 106 | GuidedMissile::explosionParticles->setRadius(1.0, 10.0); |
---|
| 107 | GuidedMissile::explosionParticles->setColor(0.0, 0,1,0,1); |
---|
| 108 | GuidedMissile::explosionParticles->setColor(0.5, .8,.8,0,.8); |
---|
| 109 | GuidedMissile::explosionParticles->setColor(0.8, .8,.8,.3,.8); |
---|
| 110 | GuidedMissile::explosionParticles->setColor(1.0, 1,1,1,.0); |
---|
[5443] | 111 | } |
---|
| 112 | |
---|
[6619] | 113 | this->emitter->setSystem(GuidedMissile::trailParticles); |
---|
[5447] | 114 | |
---|
[5772] | 115 | this->updateNode(0); |
---|
[5472] | 116 | this->emitter->setEmissionRate(45.0); |
---|
[5471] | 117 | this->emitter->setEmissionVelocity(0.0); |
---|
[7072] | 118 | |
---|
| 119 | this->setHealth(10.0* (float)rand()/(float)RAND_MAX); |
---|
[5443] | 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
[5759] | 123 | void GuidedMissile::deactivate() |
---|
[5443] | 124 | { |
---|
[6619] | 125 | this->emitter->setSystem(NULL); |
---|
[5447] | 126 | this->lifeCycle = 0.0; |
---|
[5443] | 127 | |
---|
[6142] | 128 | this->toList(OM_DEAD); |
---|
[6056] | 129 | this->removeNode(); |
---|
[5759] | 130 | GuidedMissile::fastFactory->kill(this); |
---|
[5443] | 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
[5759] | 134 | void GuidedMissile::collidesWith(WorldEntity* entity, const Vector& location) |
---|
[5257] | 135 | { |
---|
[6433] | 136 | if (this->hitEntity != entity) |
---|
[5447] | 137 | this->destroy(); |
---|
| 138 | this->hitEntity = entity; |
---|
[5257] | 139 | } |
---|
[3708] | 140 | |
---|
| 141 | /** |
---|
[4836] | 142 | * signal tick, time dependent things will be handled here |
---|
| 143 | * @param time since last tick |
---|
[3708] | 144 | */ |
---|
[5759] | 145 | void GuidedMissile::tick (float time) |
---|
[3708] | 146 | { |
---|
[6162] | 147 | |
---|
[6431] | 148 | |
---|
[6241] | 149 | if (this->target != NULL && this->target->getParent() != PNode::getNullParent()) |
---|
[5764] | 150 | { |
---|
[6162] | 151 | speed = velocity.len(); |
---|
| 152 | diffVector = ((target->getAbsCoor() - this->getAbsCoor()).getNormalized()); |
---|
[5765] | 153 | |
---|
[6162] | 154 | if(velocity.dot(diffVector) != 0) |
---|
[6241] | 155 | { |
---|
[6162] | 156 | correctionVector = (( ( diffVector * (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility; |
---|
| 157 | |
---|
[6426] | 158 | if( (diffVector * (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility ) |
---|
| 159 | velocity = ((diffVector * (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility; |
---|
| 160 | else if(velocity.dot(diffVector) > 0) |
---|
[6162] | 161 | velocity += correctionVector; |
---|
| 162 | else if (velocity.dot(diffVector) < 0) |
---|
| 163 | velocity -= correctionVector; |
---|
[6241] | 164 | } |
---|
[6162] | 165 | else |
---|
| 166 | velocity += diffVector * agility; |
---|
| 167 | |
---|
[5765] | 168 | this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0))); |
---|
[5764] | 169 | } |
---|
[6162] | 170 | |
---|
| 171 | velocity *= maxVelocity/velocity.len(); |
---|
[4464] | 172 | Vector v = this->velocity * (time); |
---|
[3708] | 173 | this->shiftCoor(v); |
---|
| 174 | |
---|
[6056] | 175 | if(this->tickLifeCycle(time)) |
---|
| 176 | this->deactivate(); |
---|
[3708] | 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
[4836] | 180 | * the function gets called, when the projectile is destroyed |
---|
[5765] | 181 | */ |
---|
[5759] | 182 | void GuidedMissile::destroy () |
---|
[5257] | 183 | { |
---|
[7086] | 184 | Projectile::destroy(); |
---|
[5759] | 185 | PRINTF(5)("DESTROY GuidedMissile\n"); |
---|
[5448] | 186 | this->lifeCycle = .95; //!< @todo calculate this usefully. |
---|
[6619] | 187 | this->emitter->setSystem(GuidedMissile::explosionParticles); |
---|
[3708] | 188 | |
---|
[5465] | 189 | this->emitter->setEmissionRate(1000.0); |
---|
[5447] | 190 | this->emitter->setEmissionVelocity(50.0); |
---|
| 191 | // this->deactivate(); |
---|
[3708] | 192 | |
---|
[5257] | 193 | } |
---|
| 194 | |
---|
| 195 | |
---|
[5759] | 196 | void GuidedMissile::draw () const |
---|
[3708] | 197 | { |
---|
| 198 | glMatrixMode(GL_MODELVIEW); |
---|
| 199 | glPushMatrix(); |
---|
| 200 | |
---|
[4593] | 201 | float matrix[4][4]; |
---|
[3708] | 202 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); |
---|
| 203 | this->getAbsDir().matrix (matrix); |
---|
[4593] | 204 | glMultMatrixf((float*)matrix); |
---|
[3755] | 205 | glScalef(2.0, 2.0, 2.0); |
---|
[5994] | 206 | this->getModel()->draw(); |
---|
[3708] | 207 | |
---|
| 208 | glPopMatrix(); |
---|
[5765] | 209 | |
---|
[3708] | 210 | } |
---|
| 211 | |
---|