[10004] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
[10104] | 4 | Copyright (C) 2004-2006 orx |
---|
[10004] | 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 |
---|
[10104] | 12 | main-programmer: Marc Schaerrer, Nicolas Schlumberger |
---|
[10004] | 13 | co-programmer: |
---|
| 14 | |
---|
| 15 | */ |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
| 17 | |
---|
| 18 | #include "swarm_projectile.h" |
---|
| 19 | |
---|
| 20 | #include "state.h" |
---|
| 21 | |
---|
| 22 | #include "particles/dot_emitter.h" |
---|
| 23 | #include "particles/sprite_particles.h" |
---|
[10064] | 24 | #include "space_ships/space_ship.h" |
---|
[10081] | 25 | #include "effects/trail.h" |
---|
[10004] | 26 | |
---|
| 27 | #include "debug.h" |
---|
| 28 | |
---|
| 29 | |
---|
[10366] | 30 | |
---|
[10079] | 31 | #include "math/vector.h" |
---|
| 32 | |
---|
[10366] | 33 | ObjectListDefinition(SwarmProjectile); |
---|
[10004] | 34 | CREATE_FAST_FACTORY_STATIC(SwarmProjectile); |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * standard constructor |
---|
| 38 | */ |
---|
| 39 | SwarmProjectile::SwarmProjectile () : Projectile() |
---|
| 40 | { |
---|
| 41 | |
---|
[10035] | 42 | /* this->loadModel("models/projectiles/orx-rocket.obj", 0.5);*/ |
---|
[10037] | 43 | this->loadModel("models/projectiles/swarm_projectile.obj"); // no double rescale (see draw()) |
---|
[10415] | 44 | this->loadExplosionSound("sounds/explosions/explosion_4.wav"); |
---|
[10004] | 45 | |
---|
| 46 | |
---|
| 47 | this->setMinEnergy(1); |
---|
| 48 | this->setHealthMax(10); |
---|
[10064] | 49 | this->lifeSpan = 4.0; |
---|
[10004] | 50 | this->agility = 3.5; |
---|
| 51 | |
---|
| 52 | this->emitter = new DotEmitter(100, 5, M_2_PI); |
---|
| 53 | this->emitter->setParent(this); |
---|
| 54 | this->emitter->setSpread(M_PI, M_PI); |
---|
[10037] | 55 | |
---|
[10132] | 56 | this->turningSpeed = 10; |
---|
[10080] | 57 | |
---|
| 58 | this->physDamage = 200; |
---|
[10104] | 59 | this->elecDamage = 0; |
---|
[10081] | 60 | |
---|
[10095] | 61 | this->trail = new Trail(2.5,4,.2, this); |
---|
| 62 | //this->trail->setParent( this); |
---|
[10081] | 63 | this->trail->setTexture( "maps/laser.png"); |
---|
[10345] | 64 | |
---|
| 65 | |
---|
| 66 | this->origList = this->getOMListNumber(); |
---|
| 67 | this->toList(OM_ENVIRON); |
---|
[10004] | 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * standard deconstructor |
---|
| 73 | */ |
---|
| 74 | SwarmProjectile::~SwarmProjectile () |
---|
| 75 | { |
---|
| 76 | |
---|
| 77 | if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1) |
---|
| 78 | { |
---|
| 79 | if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles)) |
---|
| 80 | delete SwarmProjectile::explosionParticles; |
---|
| 81 | SwarmProjectile::explosionParticles = NULL; |
---|
| 82 | } |
---|
[10081] | 83 | // delete this->emitter; |
---|
| 84 | delete this->trail; |
---|
[10004] | 85 | } |
---|
| 86 | |
---|
| 87 | SpriteParticles* SwarmProjectile::explosionParticles = NULL; |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | void SwarmProjectile::activate() |
---|
| 92 | { |
---|
[10168] | 93 | this->toList(OM_ENVIRON); |
---|
[10004] | 94 | if (unlikely(SwarmProjectile::explosionParticles == NULL)) |
---|
| 95 | { |
---|
| 96 | SwarmProjectile::explosionParticles = new SpriteParticles(200); |
---|
[10073] | 97 | SwarmProjectile::explosionParticles->setName("SwarmProjectileExplosionParticles"); |
---|
[10004] | 98 | SwarmProjectile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png"); |
---|
| 99 | SwarmProjectile::explosionParticles->setLifeSpan(.5, .3); |
---|
| 100 | SwarmProjectile::explosionParticles->setRadius(0.0, 10); |
---|
| 101 | SwarmProjectile::explosionParticles->setRadius(.5, 15.0); |
---|
| 102 | SwarmProjectile::explosionParticles->setRadius(1.0, 10.0); |
---|
| 103 | SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1); |
---|
| 104 | SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8); |
---|
| 105 | SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8); |
---|
| 106 | SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0); |
---|
| 107 | } |
---|
| 108 | |
---|
[10078] | 109 | this->emitter->setEmissionRate(50.0); |
---|
[10004] | 110 | this->emitter->setEmissionVelocity(0.0); |
---|
[10078] | 111 | this->emitter->setInheritSpeed(0); |
---|
[10004] | 112 | |
---|
| 113 | this->setHealth(10.0* (float)rand()/(float)RAND_MAX); |
---|
[10079] | 114 | |
---|
| 115 | this->maxVelocity = 300; |
---|
| 116 | |
---|
[10086] | 117 | this->rotationSpeed = 360; |
---|
[10079] | 118 | this->angle = 0; |
---|
[10087] | 119 | |
---|
| 120 | this->curDir = this->lastDir = this->velocity; |
---|
[10004] | 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | void SwarmProjectile::deactivate() |
---|
| 125 | { |
---|
| 126 | this->emitter->setSystem(NULL); |
---|
| 127 | this->lifeCycle = 0.0; |
---|
| 128 | |
---|
| 129 | this->toList(OM_DEAD); |
---|
| 130 | this->removeNode(); |
---|
| 131 | SwarmProjectile::fastFactory->kill(this); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
[10261] | 135 | void SwarmProjectile::hit (WorldEntity* entity, float damage) |
---|
[10004] | 136 | { |
---|
| 137 | if (this->hitEntity != entity) |
---|
| 138 | this->destroy( entity ); |
---|
| 139 | this->hitEntity = entity; |
---|
[10117] | 140 | //dynamic_cast<SpaceShip*>(entity)->damage(this->getPhysDamage(),this->getElecDamage()); |
---|
[10261] | 141 | // this->destroy(this); |
---|
| 142 | this->deactivate(); |
---|
[10004] | 143 | } |
---|
| 144 | |
---|
[10035] | 145 | |
---|
[10080] | 146 | void SwarmProjectile::setTarget(PNode* target) |
---|
| 147 | { |
---|
| 148 | this->target = target; |
---|
| 149 | } |
---|
[10035] | 150 | |
---|
[10080] | 151 | |
---|
| 152 | |
---|
[10004] | 153 | /** |
---|
[10035] | 154 | * this function gets called by tick to calculate the new flight direction |
---|
| 155 | * @param curDirection direction vector |
---|
| 156 | * @param estTargetDir target vector, pointing to where the target will be on hit |
---|
| 157 | * @param angle = tick * turningSpeed |
---|
| 158 | * @return (new) direction vector |
---|
| 159 | */ |
---|
| 160 | Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle) |
---|
| 161 | { |
---|
[10038] | 162 | if (unlikely(curDirection.len() == 0)) |
---|
| 163 | return curDirection; |
---|
[10080] | 164 | //printf("recalculating direction\n"); |
---|
| 165 | float tmp = angleRad ( curDirection, estTargetDir); |
---|
[10035] | 166 | if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); } |
---|
[10080] | 167 | // printf("turning angle: %f\ntemp: %f\n", angle, tmp); |
---|
[10035] | 168 | |
---|
[10366] | 169 | if( fabsf(angle) > fabsf(tmp) ) |
---|
[10132] | 170 | angle = tmp; |
---|
| 171 | else |
---|
| 172 | angle *= tmp/fabsf(tmp); |
---|
[10035] | 173 | |
---|
| 174 | Vector d = curDirection.cross(estTargetDir).cross(curDirection); |
---|
| 175 | d.normalize(); |
---|
[10132] | 176 | if( unlikely( fabsf(angle) == 90)) { return d; } //avoid complication |
---|
[10035] | 177 | |
---|
| 178 | Vector newDir = curDirection + d * curDirection.len() * tan (angle); |
---|
| 179 | newDir.normalize(); |
---|
| 180 | newDir *= curDirection.len(); |
---|
| 181 | return newDir; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | /** |
---|
[10004] | 188 | * signal tick, time dependent things will be handled here |
---|
| 189 | * @param time since last tick |
---|
| 190 | */ |
---|
| 191 | void SwarmProjectile::tick (float time) |
---|
| 192 | { |
---|
[10196] | 193 | if(unlikely(this->target == NULL)) /** Check whether the target still exists*/ |
---|
| 194 | this->deactivate(); |
---|
| 195 | |
---|
[10004] | 196 | |
---|
| 197 | |
---|
[10271] | 198 | /** old guiding function*/ |
---|
[10153] | 199 | |
---|
[10080] | 200 | float projectileVelocity = this->getVelocity().len(); |
---|
[10087] | 201 | if (target != NULL){ |
---|
[10132] | 202 | Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
---|
[10080] | 203 | this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time ); |
---|
[10087] | 204 | } |
---|
| 205 | else |
---|
[10104] | 206 | if (likely(projectileVelocity != 0 || projectileVelocity != this->maxVelocity) ) |
---|
[10087] | 207 | this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max |
---|
[10271] | 208 | /* |
---|
| 209 | printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); |
---|
| 210 | printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z);*/ |
---|
[10035] | 211 | |
---|
[10153] | 212 | this->shiftCoor(this->velocity * time); |
---|
[10080] | 213 | |
---|
[10004] | 214 | if(this->tickLifeCycle(time)) |
---|
| 215 | this->deactivate(); |
---|
[10037] | 216 | |
---|
[10087] | 217 | this->trail->tick(time); |
---|
[10081] | 218 | |
---|
[10087] | 219 | this->angle += this->rotationSpeed * time; |
---|
[10188] | 220 | while (this->angle > 360) |
---|
| 221 | this->angle -= 360; |
---|
[10087] | 222 | |
---|
| 223 | this->lastDir = this->curDir; |
---|
| 224 | this->curDir = this->velocity; |
---|
[10345] | 225 | |
---|
| 226 | if( this->target != NULL && (this->getAbsCoor() - this->target->getAbsCoor()).len() < 3) // FIXME Temp fake workaround for collision :) |
---|
[10095] | 227 | { |
---|
[10345] | 228 | dynamic_cast<WorldEntity*>(target)->hit(this->getDamage(), this); |
---|
| 229 | this->deactivate(); |
---|
[10168] | 230 | } |
---|
[10004] | 231 | } |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * the function gets called, when the projectile is destroyed |
---|
| 235 | */ |
---|
| 236 | void SwarmProjectile::destroy (WorldEntity* killer) |
---|
| 237 | { |
---|
| 238 | |
---|
[10109] | 239 | // printf("THIS SHOULD WORK!\n"); |
---|
[10004] | 240 | |
---|
| 241 | Projectile::destroy( killer ); |
---|
| 242 | PRINTF(5)("DESTROY SwarmProjectile\n"); |
---|
| 243 | this->lifeCycle = .95; //!< @todo calculate this usefully. |
---|
| 244 | this->emitter->setSystem(SwarmProjectile::explosionParticles); |
---|
| 245 | |
---|
| 246 | this->emitter->setEmissionRate(1000.0); |
---|
| 247 | this->emitter->setEmissionVelocity(50.0); |
---|
[10073] | 248 | this->deactivate(); |
---|
[10004] | 249 | |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | void SwarmProjectile::draw () const |
---|
| 254 | { |
---|
| 255 | glMatrixMode(GL_MODELVIEW); |
---|
| 256 | glPushMatrix(); |
---|
| 257 | |
---|
[10132] | 258 | Vector tmpDir = this->curDir; // *.7 + this->lastDir * .3; |
---|
| 259 | tmpDir.slerpTo(this->lastDir, .4); |
---|
[10087] | 260 | |
---|
[10004] | 261 | float matrix[4][4]; |
---|
| 262 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); |
---|
[10087] | 263 | Vector tmpRot = this->getAbsCoor().cross(tmpDir); |
---|
[10188] | 264 | glRotatef (angleRad ( this->getAbsCoor(), tmpDir), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[10037] | 265 | glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile |
---|
[10004] | 266 | this->getAbsDir().matrix (matrix); |
---|
| 267 | glMultMatrixf((float*)matrix); |
---|
[10037] | 268 | //glScalef(2.0, 2.0, 2.0); // no double rescale |
---|
[10004] | 269 | this->getModel()->draw(); |
---|
[10081] | 270 | glTranslatef(-.9,0,0); |
---|
| 271 | this->trail->draw(); |
---|
[10004] | 272 | glPopMatrix(); |
---|
| 273 | } |
---|