/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific main-programmer: Silvan Nellen co-programmer: */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON #include "swarm_projectile.h" #include "state.h" #include "particles/dot_emitter.h" #include "particles/sprite_particles.h" #include "debug.h" #include "class_id_DEPRECATED.h" ObjectListDefinitionID(SwarmProjectile, CL_SWARM_PROJECTILE); CREATE_FAST_FACTORY_STATIC(SwarmProjectile); /** * standard constructor */ SwarmProjectile::SwarmProjectile () : Projectile() { /* this->loadModel("models/projectiles/orx-rocket.obj", 0.5);*/ this->loadModel("models/projectiles/swarm_projectile.obj"); // no double rescale (see draw()) this->loadExplosionSound("sound/explosions/explosion_4.wav"); this->setMinEnergy(1); this->setHealthMax(10); this->lifeSpan = 2.0; this->agility = 3.5; this->maxVelocity = 100; this->emitter = new DotEmitter(100, 5, M_2_PI); this->emitter->setParent(this); this->emitter->setSpread(M_PI, M_PI); this->target = NULL; this->rotationSpeed = 100; this->angle = 0; } /** * standard deconstructor */ SwarmProjectile::~SwarmProjectile () { // delete this->emitter; /* this is normaly done by World.cc by deleting the ParticleEngine */ if (SwarmProjectile::trailParticles != NULL && SwarmProjectile::objectList().size() <= 1) { if (ParticleSystem::objectList().exists(SwarmProjectile::trailParticles)) delete SwarmProjectile::trailParticles; SwarmProjectile::trailParticles = NULL; } if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1) { if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles)) delete SwarmProjectile::explosionParticles; SwarmProjectile::explosionParticles = NULL; } } SpriteParticles* SwarmProjectile::trailParticles = NULL; SpriteParticles* SwarmProjectile::explosionParticles = NULL; void SwarmProjectile::activate() { if (unlikely(SwarmProjectile::trailParticles == NULL)) { SwarmProjectile::trailParticles = new SpriteParticles(2000); SwarmProjectile::trailParticles->setName("BoomerangProjectileTrailParticles"); SwarmProjectile::trailParticles->setMaterialTexture("maps/radial-trans-noise.png"); SwarmProjectile::trailParticles->setLifeSpan(1.0, .3); SwarmProjectile::trailParticles->setRadius(0.0, .5); SwarmProjectile::trailParticles->setRadius(0.2, 4.0); SwarmProjectile::trailParticles->setRadius(.5, 1.5); SwarmProjectile::trailParticles->setRadius(1.0, 1.5); SwarmProjectile::trailParticles->setColor(0.0, 1,0,0,.7); SwarmProjectile::trailParticles->setColor(0.2, .8,.8,0,.5); SwarmProjectile::trailParticles->setColor(0.5, .8,.8,.8,.8); SwarmProjectile::trailParticles->setColor(1.0, .8,.8,.8,.0); } if (unlikely(SwarmProjectile::explosionParticles == NULL)) { SwarmProjectile::explosionParticles = new SpriteParticles(200); SwarmProjectile::explosionParticles->setName("BoomerangProjectileExplosionParticles"); SwarmProjectile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png"); SwarmProjectile::explosionParticles->setLifeSpan(.5, .3); SwarmProjectile::explosionParticles->setRadius(0.0, 10); SwarmProjectile::explosionParticles->setRadius(.5, 15.0); SwarmProjectile::explosionParticles->setRadius(1.0, 10.0); SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1); SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8); SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8); SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0); } this->emitter->setSystem(SwarmProjectile::trailParticles); this->updateNode(0); this->emitter->setEmissionRate(45.0); this->emitter->setEmissionVelocity(0.0); this->setHealth(10.0* (float)rand()/(float)RAND_MAX); } void SwarmProjectile::deactivate() { this->emitter->setSystem(NULL); this->lifeCycle = 0.0; this->toList(OM_DEAD); this->removeNode(); SwarmProjectile::fastFactory->kill(this); } void SwarmProjectile::collidesWith(WorldEntity* entity, const Vector& location) { if (this->hitEntity != entity) this->destroy( entity ); this->hitEntity = entity; } /** * this function gets called by tick to calculate the new flight direction * @param curDirection direction vector * @param estTargetDir target vector, pointing to where the target will be on hit * @param angle = tick * turningSpeed * @return (new) direction vector */ Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle) { if (unlikely(curDirection.len() == 0)) return curDirection; printf("recalculating direction\n"); float tmp = angleDeg ( curDirection, estTargetDir); if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); } if( fabsf(angle) > fabsf(tmp) ) { angle = tmp; } Vector d = curDirection.cross(estTargetDir).cross(curDirection); d.normalize(); if( unlikely( angle == 90)) { return d; } //avoid complication Vector newDir = curDirection + d * curDirection.len() * tan (angle); newDir.normalize(); newDir *= curDirection.len(); return newDir; } /** * signal tick, time dependent things will be handled here * @param time since last tick */ void SwarmProjectile::tick (float time) { /* Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0); { speed = velocity.len(); diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized()); if(velocity.dot(diffVector) != 0) { correctionVector = (( ( diffVector * (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility; if( (diffVector * (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility ) velocity = ((diffVector * (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility; else if(velocity.dot(diffVector) > 0) velocity += correctionVector; else if (velocity.dot(diffVector) < 0) velocity -= correctionVector; } else velocity += diffVector * agility; this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0))); } velocity *= maxVelocity/velocity.len(); Vector v = this->velocity * (time); this->shiftCoor(v);*/ float projectileVelocity = this->velocity.len(); if (target != NULL){ float tti; //Time To Impact float targetVelocity = this->target->getVelocity().len(); if (unlikely(projectileVelocity == 0 && targetVelocity == 0)) // TODO calculate this reasonably tti = 1; // we do have a problem.... else tti = this->target->getRelCoor().len() / sqrt ( projectileVelocity * projectileVelocity + targetVelocity * targetVelocity); Vector estTargetDir = (this->target->getRelCoor() + this->target->getVelocity()) * tti; this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time ); } else if (likely(projectileVelocity != 0)) this->velocity *= maxVelocity / projectileVelocity; // set speed to max this->shiftCoor(velocity * time); if(this->tickLifeCycle(time)) this->deactivate(); this->updateAngle(time); } /** * the function gets called, when the projectile is destroyed */ void SwarmProjectile::destroy (WorldEntity* killer) { printf("THIS SHOULD WORLk\n"); Projectile::destroy( killer ); PRINTF(5)("DESTROY SwarmProjectile\n"); this->lifeCycle = .95; //!< @todo calculate this usefully. this->emitter->setSystem(SwarmProjectile::explosionParticles); this->emitter->setEmissionRate(1000.0); this->emitter->setEmissionVelocity(50.0); // this->deactivate(); } void SwarmProjectile::updateAngle(float time) { this->angle += this->rotationSpeed * time; } void SwarmProjectile::draw () const { glMatrixMode(GL_MODELVIEW); glPushMatrix(); float matrix[4][4]; glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile this->getAbsDir().matrix (matrix); glMultMatrixf((float*)matrix); //glScalef(2.0, 2.0, 2.0); // no double rescale this->getModel()->draw(); glPopMatrix(); }