/* 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 "space_ships/space_ship.h" #include "effects/trail.h" #include "debug.h" #include "class_id_DEPRECATED.h" #include "math/vector.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 = 4.0; this->agility = 3.5; this->emitter = new DotEmitter(100, 5, M_2_PI); this->emitter->setParent(this); this->emitter->setSpread(M_PI, M_PI); this->turningSpeed = 30; this->physDamage = 200; this->trail = new Trail(2.5,4,.2); this->trail->setParent( this); this->trail->setTexture( "maps/laser.png"); } /** * standard deconstructor */ SwarmProjectile::~SwarmProjectile () { if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1) { if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles)) delete SwarmProjectile::explosionParticles; SwarmProjectile::explosionParticles = NULL; } // delete this->emitter; delete this->trail; } SpriteParticles* SwarmProjectile::explosionParticles = NULL; void SwarmProjectile::activate() { if (unlikely(SwarmProjectile::explosionParticles == NULL)) { SwarmProjectile::explosionParticles = new SpriteParticles(200); SwarmProjectile::explosionParticles->setName("SwarmProjectileExplosionParticles"); 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->updateNode(0); this->emitter->setEmissionRate(50.0); this->emitter->setEmissionVelocity(0.0); this->emitter->setInheritSpeed(0); this->setHealth(10.0* (float)rand()/(float)RAND_MAX); this->maxVelocity = 300; this->rotationSpeed = 360; this->angle = 0; } 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; dynamic_cast(entity)->damage(this->getPhysDamage(),0); } void SwarmProjectile::setTarget(PNode* target) { this->target = target; } /** * 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 = angleRad ( curDirection, estTargetDir); if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); } // printf("turning angle: %f\ntemp: %f\n", angle, tmp); 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->getVelocity().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->getAbsCoor() - this->target->getAbsCoor()).len() / sqrt ( projectileVelocity * projectileVelocity + targetVelocity * targetVelocity); Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized() * maxVelocity; // Vector estTargetDir = (this->target->getAbsCoor() - (Vector(2000, 30, 300) + VECTOR_RAND(20))).getNormalized() * projectileVelocity; this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time ); // } // else // if (likely(projectileVelocity != 0)) // this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max // printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); // printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z); this->shiftCoor(this->velocity * (time)); if(this->tickLifeCycle(time)) this->deactivate(); this->updateAngle(time); this->trail->tick(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); Vector tmpRot = this->getAbsCoor().cross(this->velocity); glRotatef (angleRad ( this->getAbsCoor(), this->velocity), tmpRot.x, tmpRot.y, tmpRot.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(); glTranslatef(-.9,0,0); this->trail->draw(); glPopMatrix(); }