Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/weapons/guided_missile.cc @ 6161

Last change on this file since 6161 was 6161, checked in by snellen, 18 years ago

guided_missile.cc in /world_entities/weapons updated

File size: 6.4 KB
RevLine 
[4593]1/*
[3708]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 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
[5759]12   main-programmer: Silvan Nellen
13   co-programmer:
[5443]14
[3708]15*/
[5357]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
[3708]17
[5759]18#include "guided_missile.h"
[3708]19
[4947]20#include "fast_factory.h"
[3708]21
[5511]22#include "model.h"
[5443]23#include "state.h"
24#include "list.h"
[5444]25#include "class_list.h"
[5054]26
[5443]27#include "particle_engine.h"
28#include "particle_emitter.h"
29#include "particle_system.h"
30
31
[5765]32#include "null_parent.h"
33
[3708]34using namespace std;
35
[5759]36CREATE_FAST_FACTORY_STATIC(GuidedMissile, CL_GUIDED_MISSILE);
[3708]37
38/**
[4836]39 *  standard constructor
[3708]40*/
[5759]41GuidedMissile::GuidedMissile () : Projectile()
[3755]42{
[5759]43  this->setClassID(CL_GUIDED_MISSILE, "GuidedMissile");
[4597]44
[4948]45  float modelSize = .3;
[5764]46  this->loadModel("models/projectiles/orx-rocket.obj", .3);
[4948]47
48  this->energyMin = 1;
49  this->energyMax = 10;
[6132]50  this->lifeSpan = 10.0;
51  this->agility = 4;
[6133]52  this->maxVelocity = 75;
[5443]53
[5447]54  this->emitter = new ParticleEmitter(Vector(0,1,0), M_2_PI, 100, 5);
[5443]55  this->emitter->setParent(this);
[5449]56  this->emitter->setSpread(M_PI, M_PI);
[3755]57}
[3708]58
59
60/**
[4836]61 *  standard deconstructor
[3708]62*/
[5759]63GuidedMissile::~GuidedMissile ()
[3708]64{
[5446]65  // delete this->emitter;
[5444]66
[5445]67  /* this is normaly done by World.cc by deleting the ParticleEngine */
[5779]68  if (GuidedMissile::trailParticles != NULL && ClassList::getList(CL_TEST_BULLET)->size() <= 1)
[5447]69  {
[5759]70    if (ClassList::exists(GuidedMissile::trailParticles, CL_PARTICLE_SYSTEM))
71      delete GuidedMissile::trailParticles;
72    GuidedMissile::trailParticles = NULL;
[5447]73  }
[5779]74  if (GuidedMissile::explosionParticles != NULL && ClassList::getList(CL_TEST_BULLET)->size() <= 1)
[5444]75  {
[5759]76    if (ClassList::exists(GuidedMissile::explosionParticles, CL_PARTICLE_SYSTEM))
77      delete GuidedMissile::explosionParticles;
78    GuidedMissile::explosionParticles = NULL;
[5444]79  }
[5445]80
[3708]81}
82
[5759]83ParticleSystem* GuidedMissile::trailParticles = NULL;
84ParticleSystem* GuidedMissile::explosionParticles = NULL;
[5443]85
[5760]86
87
[5759]88void GuidedMissile::activate()
[5443]89{
90  State::getWorldEntityList()->add(this);
[5759]91  if (unlikely(GuidedMissile::trailParticles == NULL))
[5447]92  {
[5759]93    GuidedMissile::trailParticles = new ParticleSystem(2000, PARTICLE_SPRITE);
94    GuidedMissile::trailParticles->setName("GuidedMissileTrailParticles");
95    GuidedMissile::trailParticles->setMaterialTexture("maps/radial-trans-noise.png");
96    GuidedMissile::trailParticles->setLifeSpan(1.0, .3);
97    GuidedMissile::trailParticles->setRadius(0.0, .5);
98    GuidedMissile::trailParticles->setRadius(0.2, 2.0);
99    GuidedMissile::trailParticles->setRadius(.5, .8);
100    GuidedMissile::trailParticles->setRadius(1.0, .8);
101    GuidedMissile::trailParticles->setColor(0.0, 1,0,0,.7);
102    GuidedMissile::trailParticles->setColor(0.2, .8,.8,0,.5);
103    GuidedMissile::trailParticles->setColor(0.5, .8,.8,.8,.8);
104    GuidedMissile::trailParticles->setColor(1.0, .8,.8,.8,.0);
[5447]105  }
[5759]106  if (unlikely(GuidedMissile::explosionParticles == NULL))
[5443]107  {
[5759]108    GuidedMissile::explosionParticles = new ParticleSystem(200, PARTICLE_SPRITE);
109    GuidedMissile::explosionParticles->setName("GuidedMissileExplosionParticles");
110    GuidedMissile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png");
111    GuidedMissile::explosionParticles->setLifeSpan(.5, .3);
112    GuidedMissile::explosionParticles->setRadius(0.0, 10);
113    GuidedMissile::explosionParticles->setRadius(.5, 15.0);
114    GuidedMissile::explosionParticles->setRadius(1.0, 10.0);
115    GuidedMissile::explosionParticles->setColor(0.0, 0,1,0,1);
116    GuidedMissile::explosionParticles->setColor(0.5, .8,.8,0,.8);
117    GuidedMissile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
118    GuidedMissile::explosionParticles->setColor(1.0, 1,1,1,.0);
[5443]119  }
120
[5759]121  ParticleEngine::getInstance()->addConnection(this->emitter, GuidedMissile::trailParticles);
[5447]122
[5772]123  this->updateNode(0);
[5472]124  this->emitter->setEmissionRate(45.0);
[5471]125  this->emitter->setEmissionVelocity(0.0);
[5443]126}
127
128
[5759]129void GuidedMissile::deactivate()
[5443]130{
[5447]131  ParticleEngine::getInstance()->breakConnections(this->emitter);
132  this->lifeCycle = 0.0;
[5443]133
[5447]134//  GarbageCollector::getInstance()->collect(this);
135  State::getWorldEntityList()->remove(this);
[5759]136  GuidedMissile::fastFactory->kill(this);
[5443]137}
138
139
[5759]140void GuidedMissile::collidesWith(WorldEntity* entity, const Vector& location)
[5257]141{
[5447]142  if (this->hitEntity != entity && entity->isA(CL_NPC))
143    this->destroy();
144  this->hitEntity = entity;
[5257]145}
[3708]146
147/**
[4836]148 *  signal tick, time dependent things will be handled here
149 * @param time since last tick
[3708]150*/
[5759]151void GuidedMissile::tick (float time)
[6159]152{
[6044]153
[5765]154  if (target != NULL && target->getParent() != NullParent::getInstance())
[5764]155   {
[6132]156    speed = velocity.len();
157    diffVector = ((target->getAbsCoor() - this->getAbsCoor()).getNormalized());
[5765]158
[6161]159    if(velocity.dot(diffVector) != 0)
160    {
161     correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
162
[6131]163      if(velocity.dot(diffVector) > 0)
[6161]164        velocity += correctionVector;
[6159]165      else if (velocity.dot(diffVector) < 0)
[6161]166        velocity -= correctionVector;
167    }
168    else
169      velocity += diffVector * agility;
[6044]170
[5765]171     this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
[5764]172   }
[6044]173
[6131]174  velocity *= maxVelocity/velocity.len();
[4464]175  Vector v = this->velocity * (time);
[3708]176  this->shiftCoor(v);
177
[4890]178  this->lifeCycle += time/this->lifeSpan;
[5447]179  if( this->lifeCycle >= 1.0)
[3708]180    {
181      PRINTF(5)("FINALIZE==========================\n");
[4890]182      PRINTF(5)("current life cycle is: %f\n", this->lifeCycle);
[3708]183      PRINTF(5)("FINALIZE===========================\n");
[5447]184
[5443]185      this->deactivate();
[3708]186    }
187}
188
189/**
[4836]190 *  the function gets called, when the projectile is destroyed
[5765]191 */
[5759]192void GuidedMissile::destroy ()
[5257]193{
[5759]194  PRINTF(5)("DESTROY GuidedMissile\n");
[5448]195  this->lifeCycle = .95; //!< @todo calculate this usefully.
[5759]196  ParticleEngine::getInstance()->breakConnection(this->emitter, GuidedMissile::trailParticles);
197  ParticleEngine::getInstance()->addConnection(this->emitter, GuidedMissile::explosionParticles);
[3708]198
[5465]199  this->emitter->setEmissionRate(1000.0);
[5447]200  this->emitter->setEmissionVelocity(50.0);
201//  this->deactivate();
[3708]202
[5257]203}
204
205
[5759]206void GuidedMissile::draw () const
[3708]207{
208  glMatrixMode(GL_MODELVIEW);
209  glPushMatrix();
210
[4593]211  float matrix[4][4];
[3708]212  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
213  this->getAbsDir().matrix (matrix);
[4593]214  glMultMatrixf((float*)matrix);
[3755]215  glScalef(2.0, 2.0, 2.0);
[5994]216  this->getModel()->draw();
[3708]217
218  glPopMatrix();
[5765]219
[3708]220}
221
Note: See TracBrowser for help on using the repository browser.