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