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