[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 | #include "class_id_DEPRECATED.h" |
---|
| 30 | |
---|
[10079] | 31 | #include "math/vector.h" |
---|
| 32 | |
---|
[10004] | 33 | ObjectListDefinitionID(SwarmProjectile, CL_SWARM_PROJECTILE); |
---|
| 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()) |
---|
[10004] | 44 | this->loadExplosionSound("sound/explosions/explosion_4.wav"); |
---|
| 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"); |
---|
[10004] | 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * standard deconstructor |
---|
| 69 | */ |
---|
| 70 | SwarmProjectile::~SwarmProjectile () |
---|
| 71 | { |
---|
| 72 | |
---|
| 73 | if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1) |
---|
| 74 | { |
---|
| 75 | if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles)) |
---|
| 76 | delete SwarmProjectile::explosionParticles; |
---|
| 77 | SwarmProjectile::explosionParticles = NULL; |
---|
| 78 | } |
---|
[10081] | 79 | // delete this->emitter; |
---|
| 80 | delete this->trail; |
---|
[10004] | 81 | } |
---|
| 82 | |
---|
| 83 | SpriteParticles* SwarmProjectile::explosionParticles = NULL; |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | void SwarmProjectile::activate() |
---|
| 88 | { |
---|
[10168] | 89 | this->toList(OM_ENVIRON); |
---|
[10004] | 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 | |
---|
[10078] | 105 | this->emitter->setEmissionRate(50.0); |
---|
[10004] | 106 | this->emitter->setEmissionVelocity(0.0); |
---|
[10078] | 107 | this->emitter->setInheritSpeed(0); |
---|
[10004] | 108 | |
---|
| 109 | this->setHealth(10.0* (float)rand()/(float)RAND_MAX); |
---|
[10079] | 110 | |
---|
| 111 | this->maxVelocity = 300; |
---|
| 112 | |
---|
[10086] | 113 | this->rotationSpeed = 360; |
---|
[10079] | 114 | this->angle = 0; |
---|
[10087] | 115 | |
---|
| 116 | this->curDir = this->lastDir = this->velocity; |
---|
[10004] | 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | void SwarmProjectile::deactivate() |
---|
| 121 | { |
---|
| 122 | this->emitter->setSystem(NULL); |
---|
| 123 | this->lifeCycle = 0.0; |
---|
| 124 | |
---|
| 125 | this->toList(OM_DEAD); |
---|
| 126 | this->removeNode(); |
---|
| 127 | SwarmProjectile::fastFactory->kill(this); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
[10261] | 131 | void SwarmProjectile::hit (WorldEntity* entity, float damage) |
---|
[10004] | 132 | { |
---|
| 133 | if (this->hitEntity != entity) |
---|
| 134 | this->destroy( entity ); |
---|
| 135 | this->hitEntity = entity; |
---|
[10117] | 136 | //dynamic_cast<SpaceShip*>(entity)->damage(this->getPhysDamage(),this->getElecDamage()); |
---|
[10261] | 137 | // this->destroy(this); |
---|
| 138 | this->deactivate(); |
---|
[10004] | 139 | } |
---|
| 140 | |
---|
[10035] | 141 | |
---|
[10080] | 142 | void SwarmProjectile::setTarget(PNode* target) |
---|
| 143 | { |
---|
| 144 | this->target = target; |
---|
| 145 | } |
---|
[10035] | 146 | |
---|
[10080] | 147 | |
---|
| 148 | |
---|
[10004] | 149 | /** |
---|
[10035] | 150 | * this function gets called by tick to calculate the new flight direction |
---|
| 151 | * @param curDirection direction vector |
---|
| 152 | * @param estTargetDir target vector, pointing to where the target will be on hit |
---|
| 153 | * @param angle = tick * turningSpeed |
---|
| 154 | * @return (new) direction vector |
---|
| 155 | */ |
---|
| 156 | Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle) |
---|
| 157 | { |
---|
[10038] | 158 | if (unlikely(curDirection.len() == 0)) |
---|
| 159 | return curDirection; |
---|
[10080] | 160 | //printf("recalculating direction\n"); |
---|
| 161 | float tmp = angleRad ( curDirection, estTargetDir); |
---|
[10035] | 162 | if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); } |
---|
[10080] | 163 | // printf("turning angle: %f\ntemp: %f\n", angle, tmp); |
---|
[10035] | 164 | |
---|
[10132] | 165 | if( fabsf(angle) > fabsf(tmp) ) |
---|
| 166 | angle = tmp; |
---|
| 167 | else |
---|
| 168 | angle *= tmp/fabsf(tmp); |
---|
[10035] | 169 | |
---|
| 170 | Vector d = curDirection.cross(estTargetDir).cross(curDirection); |
---|
| 171 | d.normalize(); |
---|
[10132] | 172 | if( unlikely( fabsf(angle) == 90)) { return d; } //avoid complication |
---|
[10035] | 173 | |
---|
| 174 | Vector newDir = curDirection + d * curDirection.len() * tan (angle); |
---|
| 175 | newDir.normalize(); |
---|
| 176 | newDir *= curDirection.len(); |
---|
| 177 | return newDir; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | /** |
---|
[10004] | 184 | * signal tick, time dependent things will be handled here |
---|
| 185 | * @param time since last tick |
---|
| 186 | */ |
---|
| 187 | void SwarmProjectile::tick (float time) |
---|
| 188 | { |
---|
[10196] | 189 | if(unlikely(this->target == NULL)) /** Check whether the target still exists*/ |
---|
| 190 | this->deactivate(); |
---|
| 191 | |
---|
[10035] | 192 | /* |
---|
[10004] | 193 | Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0); |
---|
| 194 | |
---|
| 195 | { |
---|
| 196 | speed = velocity.len(); |
---|
| 197 | diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized()); |
---|
| 198 | |
---|
| 199 | if(velocity.dot(diffVector) != 0) |
---|
| 200 | { |
---|
| 201 | correctionVector = (( ( diffVector * (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility; |
---|
| 202 | |
---|
| 203 | if( (diffVector * (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility ) |
---|
| 204 | velocity = ((diffVector * (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility; |
---|
| 205 | else if(velocity.dot(diffVector) > 0) |
---|
| 206 | velocity += correctionVector; |
---|
| 207 | else if (velocity.dot(diffVector) < 0) |
---|
| 208 | velocity -= correctionVector; |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | velocity += diffVector * agility; |
---|
| 212 | |
---|
| 213 | this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0))); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | velocity *= maxVelocity/velocity.len(); |
---|
| 217 | Vector v = this->velocity * (time); |
---|
[10035] | 218 | this->shiftCoor(v);*/ |
---|
[10004] | 219 | |
---|
[10153] | 220 | |
---|
| 221 | /** old guiding functuion*/ |
---|
| 222 | |
---|
[10080] | 223 | float projectileVelocity = this->getVelocity().len(); |
---|
[10087] | 224 | if (target != NULL){ |
---|
[10132] | 225 | Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
---|
[10080] | 226 | this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time ); |
---|
[10087] | 227 | } |
---|
| 228 | else |
---|
[10104] | 229 | if (likely(projectileVelocity != 0 || projectileVelocity != this->maxVelocity) ) |
---|
[10087] | 230 | this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max |
---|
[10035] | 231 | |
---|
[10080] | 232 | // printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); |
---|
| 233 | // printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z); |
---|
| 234 | |
---|
[10153] | 235 | this->shiftCoor(this->velocity * time); |
---|
[10080] | 236 | |
---|
[10153] | 237 | |
---|
| 238 | /* |
---|
| 239 | Vector pjV = this->getVelocity(); |
---|
| 240 | Vector tV = this->target->getVelocity(); |
---|
| 241 | Vector pT = this->target->getAbsCoor() - this->getAbsCoor(); // vector projectile target |
---|
| 242 | |
---|
| 243 | Vector a = tV.getNormalized() * pT.dot(tV.getNormalized()); |
---|
| 244 | |
---|
| 245 | float A = 2 * pT.len() * pT.len(); |
---|
| 246 | float B = 2 * a.len() * tV.len(); |
---|
| 247 | float D = 2 * sqrt(B * B - 4 * pT.len() * pT.len() *(tV.len() * tV.len() - pjV.len() * pjV.len())); |
---|
| 248 | float tti; |
---|
| 249 | |
---|
| 250 | if (A != 0){ |
---|
| 251 | if ( B < D ) tti = ( B + D ) / A; |
---|
| 252 | else tti = ( B + D ) / A; |
---|
| 253 | } |
---|
| 254 | else tti = 0; |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | Vector estTargetDir; |
---|
| 258 | if (tti == 0) |
---|
| 259 | estTargetDir = pT.getNormalized() * pjV.len(); |
---|
| 260 | else |
---|
| 261 | estTargetDir = pT / tti + pjV; |
---|
| 262 | |
---|
| 263 | this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time ); |
---|
| 264 | |
---|
| 265 | this->shiftCoor(this->velocity * (this->maxVelocity * time));*/ |
---|
| 266 | |
---|
[10004] | 267 | if(this->tickLifeCycle(time)) |
---|
| 268 | this->deactivate(); |
---|
[10037] | 269 | |
---|
[10087] | 270 | this->trail->tick(time); |
---|
[10081] | 271 | |
---|
[10087] | 272 | this->angle += this->rotationSpeed * time; |
---|
[10188] | 273 | while (this->angle > 360) |
---|
| 274 | this->angle -= 360; |
---|
[10087] | 275 | |
---|
| 276 | this->lastDir = this->curDir; |
---|
| 277 | this->curDir = this->velocity; |
---|
[10109] | 278 | if( (this->getAbsCoor() - this->target->getAbsCoor()).len() < 3) // FIXME Temp fake workaround for collision :) |
---|
[10095] | 279 | { |
---|
| 280 | dynamic_cast<WorldEntity*>(target)->destroy( this); |
---|
| 281 | this->destroy( this); |
---|
[10168] | 282 | } |
---|
[10004] | 283 | } |
---|
| 284 | |
---|
| 285 | /** |
---|
| 286 | * the function gets called, when the projectile is destroyed |
---|
| 287 | */ |
---|
| 288 | void SwarmProjectile::destroy (WorldEntity* killer) |
---|
| 289 | { |
---|
| 290 | |
---|
[10109] | 291 | // printf("THIS SHOULD WORK!\n"); |
---|
[10004] | 292 | |
---|
| 293 | Projectile::destroy( killer ); |
---|
| 294 | PRINTF(5)("DESTROY SwarmProjectile\n"); |
---|
| 295 | this->lifeCycle = .95; //!< @todo calculate this usefully. |
---|
| 296 | this->emitter->setSystem(SwarmProjectile::explosionParticles); |
---|
| 297 | |
---|
| 298 | this->emitter->setEmissionRate(1000.0); |
---|
| 299 | this->emitter->setEmissionVelocity(50.0); |
---|
[10073] | 300 | this->deactivate(); |
---|
[10004] | 301 | |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | |
---|
| 305 | void SwarmProjectile::draw () const |
---|
| 306 | { |
---|
| 307 | glMatrixMode(GL_MODELVIEW); |
---|
| 308 | glPushMatrix(); |
---|
| 309 | |
---|
[10132] | 310 | Vector tmpDir = this->curDir; // *.7 + this->lastDir * .3; |
---|
| 311 | tmpDir.slerpTo(this->lastDir, .4); |
---|
[10087] | 312 | |
---|
[10004] | 313 | float matrix[4][4]; |
---|
| 314 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); |
---|
[10087] | 315 | Vector tmpRot = this->getAbsCoor().cross(tmpDir); |
---|
[10188] | 316 | glRotatef (angleRad ( this->getAbsCoor(), tmpDir), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[10037] | 317 | glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile |
---|
[10004] | 318 | this->getAbsDir().matrix (matrix); |
---|
| 319 | glMultMatrixf((float*)matrix); |
---|
[10037] | 320 | //glScalef(2.0, 2.0, 2.0); // no double rescale |
---|
[10004] | 321 | this->getModel()->draw(); |
---|
[10081] | 322 | glTranslatef(-.9,0,0); |
---|
| 323 | this->trail->draw(); |
---|
[10004] | 324 | glPopMatrix(); |
---|
| 325 | } |
---|