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