Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

guided_missile.cc in /world_entities/weapons updated

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