[10004] | 1 | |
---|
| 2 | /* |
---|
| 3 | orxonox - the future of 3D-vertical-scrollers |
---|
| 4 | |
---|
| 5 | Copyright (C) 2004 orx |
---|
| 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 |
---|
| 13 | main-programmer: Silvan Nellen |
---|
| 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; |
---|
[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 | { |
---|
| 89 | if (unlikely(SwarmProjectile::explosionParticles == NULL)) |
---|
| 90 | { |
---|
| 91 | SwarmProjectile::explosionParticles = new SpriteParticles(200); |
---|
[10073] | 92 | SwarmProjectile::explosionParticles->setName("SwarmProjectileExplosionParticles"); |
---|
[10004] | 93 | SwarmProjectile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png"); |
---|
| 94 | SwarmProjectile::explosionParticles->setLifeSpan(.5, .3); |
---|
| 95 | SwarmProjectile::explosionParticles->setRadius(0.0, 10); |
---|
| 96 | SwarmProjectile::explosionParticles->setRadius(.5, 15.0); |
---|
| 97 | SwarmProjectile::explosionParticles->setRadius(1.0, 10.0); |
---|
| 98 | SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1); |
---|
| 99 | SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8); |
---|
| 100 | SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8); |
---|
| 101 | SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | this->updateNode(0); |
---|
[10078] | 106 | this->emitter->setEmissionRate(50.0); |
---|
[10004] | 107 | this->emitter->setEmissionVelocity(0.0); |
---|
[10078] | 108 | this->emitter->setInheritSpeed(0); |
---|
[10004] | 109 | |
---|
| 110 | this->setHealth(10.0* (float)rand()/(float)RAND_MAX); |
---|
[10079] | 111 | |
---|
| 112 | this->maxVelocity = 300; |
---|
| 113 | |
---|
[10086] | 114 | this->rotationSpeed = 360; |
---|
[10079] | 115 | this->angle = 0; |
---|
[10087] | 116 | |
---|
| 117 | this->curDir = this->lastDir = this->velocity; |
---|
[10004] | 118 | } |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | void SwarmProjectile::deactivate() |
---|
| 122 | { |
---|
| 123 | this->emitter->setSystem(NULL); |
---|
| 124 | this->lifeCycle = 0.0; |
---|
| 125 | |
---|
| 126 | this->toList(OM_DEAD); |
---|
| 127 | this->removeNode(); |
---|
| 128 | SwarmProjectile::fastFactory->kill(this); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | void SwarmProjectile::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 133 | { |
---|
| 134 | if (this->hitEntity != entity) |
---|
| 135 | this->destroy( entity ); |
---|
| 136 | this->hitEntity = entity; |
---|
[10080] | 137 | dynamic_cast<SpaceShip*>(entity)->damage(this->getPhysDamage(),0); |
---|
[10004] | 138 | } |
---|
| 139 | |
---|
[10035] | 140 | |
---|
[10080] | 141 | void SwarmProjectile::setTarget(PNode* target) |
---|
| 142 | { |
---|
| 143 | this->target = target; |
---|
| 144 | } |
---|
[10035] | 145 | |
---|
[10080] | 146 | |
---|
| 147 | |
---|
[10004] | 148 | /** |
---|
[10035] | 149 | * this function gets called by tick to calculate the new flight direction |
---|
| 150 | * @param curDirection direction vector |
---|
| 151 | * @param estTargetDir target vector, pointing to where the target will be on hit |
---|
| 152 | * @param angle = tick * turningSpeed |
---|
| 153 | * @return (new) direction vector |
---|
| 154 | */ |
---|
| 155 | Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle) |
---|
| 156 | { |
---|
[10038] | 157 | if (unlikely(curDirection.len() == 0)) |
---|
| 158 | return curDirection; |
---|
[10080] | 159 | //printf("recalculating direction\n"); |
---|
| 160 | float tmp = angleRad ( curDirection, estTargetDir); |
---|
[10035] | 161 | if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); } |
---|
[10080] | 162 | // printf("turning angle: %f\ntemp: %f\n", angle, tmp); |
---|
[10035] | 163 | |
---|
| 164 | if( fabsf(angle) > fabsf(tmp) ) { angle = tmp; } |
---|
| 165 | |
---|
| 166 | Vector d = curDirection.cross(estTargetDir).cross(curDirection); |
---|
| 167 | d.normalize(); |
---|
| 168 | if( unlikely( angle == 90)) { return d; } //avoid complication |
---|
| 169 | |
---|
| 170 | Vector newDir = curDirection + d * curDirection.len() * tan (angle); |
---|
| 171 | newDir.normalize(); |
---|
| 172 | newDir *= curDirection.len(); |
---|
| 173 | return newDir; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | /** |
---|
[10004] | 180 | * signal tick, time dependent things will be handled here |
---|
| 181 | * @param time since last tick |
---|
| 182 | */ |
---|
| 183 | void SwarmProjectile::tick (float time) |
---|
| 184 | { |
---|
[10035] | 185 | /* |
---|
[10004] | 186 | Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0); |
---|
| 187 | |
---|
| 188 | { |
---|
| 189 | speed = velocity.len(); |
---|
| 190 | diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized()); |
---|
| 191 | |
---|
| 192 | if(velocity.dot(diffVector) != 0) |
---|
| 193 | { |
---|
| 194 | correctionVector = (( ( diffVector * (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility; |
---|
| 195 | |
---|
| 196 | if( (diffVector * (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility ) |
---|
| 197 | velocity = ((diffVector * (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility; |
---|
| 198 | else if(velocity.dot(diffVector) > 0) |
---|
| 199 | velocity += correctionVector; |
---|
| 200 | else if (velocity.dot(diffVector) < 0) |
---|
| 201 | velocity -= correctionVector; |
---|
| 202 | } |
---|
| 203 | else |
---|
| 204 | velocity += diffVector * agility; |
---|
| 205 | |
---|
| 206 | this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0))); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | velocity *= maxVelocity/velocity.len(); |
---|
| 210 | Vector v = this->velocity * (time); |
---|
[10035] | 211 | this->shiftCoor(v);*/ |
---|
[10004] | 212 | |
---|
[10080] | 213 | float projectileVelocity = this->getVelocity().len(); |
---|
[10087] | 214 | if (target != NULL){ |
---|
| 215 | /* float tti; //Time To Impact*/ |
---|
| 216 | // float targetVelocity = this->target->getVelocity().len(); |
---|
| 217 | // if (unlikely(projectileVelocity == 0 && targetVelocity == 0)) // TODO calculate this reasonably |
---|
| 218 | // tti = 1; // we do have a problem.... |
---|
| 219 | // else |
---|
| 220 | // tti = (this->getAbsCoor() - this->target->getAbsCoor()).len() / sqrt ( projectileVelocity * projectileVelocity + targetVelocity * targetVelocity); |
---|
[10080] | 221 | Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized() * maxVelocity; |
---|
| 222 | this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time ); |
---|
[10087] | 223 | } |
---|
| 224 | else |
---|
| 225 | if (likely(projectileVelocity != 0)) |
---|
| 226 | this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max |
---|
[10035] | 227 | |
---|
[10080] | 228 | // printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); |
---|
| 229 | // printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z); |
---|
| 230 | |
---|
| 231 | this->shiftCoor(this->velocity * (time)); |
---|
| 232 | |
---|
[10004] | 233 | if(this->tickLifeCycle(time)) |
---|
| 234 | this->deactivate(); |
---|
[10037] | 235 | |
---|
[10087] | 236 | this->trail->tick(time); |
---|
[10081] | 237 | |
---|
[10087] | 238 | this->angle += this->rotationSpeed * time; |
---|
| 239 | |
---|
| 240 | this->lastDir = this->curDir; |
---|
| 241 | this->curDir = this->velocity; |
---|
[10095] | 242 | if( (this->getAbsCoor() - this->target->getAbsCoor()).len() < 4) // FIXME Temp fake workaround for collision :) |
---|
| 243 | { |
---|
| 244 | dynamic_cast<WorldEntity*>(target)->destroy( this); |
---|
| 245 | this->destroy( this); |
---|
| 246 | } |
---|
[10004] | 247 | } |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | * the function gets called, when the projectile is destroyed |
---|
| 251 | */ |
---|
| 252 | void SwarmProjectile::destroy (WorldEntity* killer) |
---|
| 253 | { |
---|
| 254 | |
---|
| 255 | printf("THIS SHOULD WORLk\n"); |
---|
| 256 | |
---|
| 257 | Projectile::destroy( killer ); |
---|
| 258 | PRINTF(5)("DESTROY SwarmProjectile\n"); |
---|
| 259 | this->lifeCycle = .95; //!< @todo calculate this usefully. |
---|
| 260 | this->emitter->setSystem(SwarmProjectile::explosionParticles); |
---|
| 261 | |
---|
| 262 | this->emitter->setEmissionRate(1000.0); |
---|
| 263 | this->emitter->setEmissionVelocity(50.0); |
---|
[10073] | 264 | this->deactivate(); |
---|
[10004] | 265 | |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | |
---|
| 269 | void SwarmProjectile::draw () const |
---|
| 270 | { |
---|
| 271 | glMatrixMode(GL_MODELVIEW); |
---|
| 272 | glPushMatrix(); |
---|
| 273 | |
---|
[10087] | 274 | Vector tmpDir = this->curDir *.5 + this->lastDir * .5; |
---|
| 275 | |
---|
[10004] | 276 | float matrix[4][4]; |
---|
| 277 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); |
---|
[10087] | 278 | Vector tmpRot = this->getAbsCoor().cross(tmpDir); |
---|
| 279 | glRotatef (angleDeg ( this->getAbsCoor(), tmpDir), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[10037] | 280 | glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile |
---|
[10004] | 281 | this->getAbsDir().matrix (matrix); |
---|
| 282 | glMultMatrixf((float*)matrix); |
---|
[10037] | 283 | //glScalef(2.0, 2.0, 2.0); // no double rescale |
---|
[10004] | 284 | this->getModel()->draw(); |
---|
[10081] | 285 | glTranslatef(-.9,0,0); |
---|
| 286 | this->trail->draw(); |
---|
[10004] | 287 | glPopMatrix(); |
---|
| 288 | } |
---|