Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weapons/guided_missile.cc @ 6168

Last change on this file since 6168 was 6162, checked in by bensch, 19 years ago

orxonox/trunk: merged the spaceshipcontroll branche into the trunk
merged with command
svn merge -r6036:HEAD spaceshipcontrol/ ../trunk/
no conflicts

File size: 6.1 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
31using namespace std;
32
33CREATE_FAST_FACTORY_STATIC(GuidedMissile, CL_GUIDED_MISSILE);
34
35/**
36 *  standard constructor
37*/
38GuidedMissile::GuidedMissile () : Projectile()
39{
40  this->setClassID(CL_GUIDED_MISSILE, "GuidedMissile");
41
42  float modelSize = .3;
43  this->loadModel("models/projectiles/orx-rocket.obj", .3);
44
45  this->energyMin = 1;
46  this->energyMax = 10;
47  this->lifeSpan = 10.0;
48  this->agility = 4;
49  this->maxVelocity = 75;
50
51  this->emitter = new ParticleEmitter(Vector(0,1,0), M_2_PI, 100, 5);
52  this->emitter->setParent(this);
53  this->emitter->setSpread(M_PI, M_PI);
54}
55
56
57/**
58 *  standard deconstructor
59*/
60GuidedMissile::~GuidedMissile ()
61{
62  // delete this->emitter;
63
64  /* this is normaly done by World.cc by deleting the ParticleEngine */
65  if (GuidedMissile::trailParticles != NULL && ClassList::getList(CL_TEST_BULLET)->size() <= 1)
66  {
67    if (ClassList::exists(GuidedMissile::trailParticles, CL_PARTICLE_SYSTEM))
68      delete GuidedMissile::trailParticles;
69    GuidedMissile::trailParticles = NULL;
70  }
71  if (GuidedMissile::explosionParticles != NULL && ClassList::getList(CL_TEST_BULLET)->size() <= 1)
72  {
73    if (ClassList::exists(GuidedMissile::explosionParticles, CL_PARTICLE_SYSTEM))
74      delete GuidedMissile::explosionParticles;
75    GuidedMissile::explosionParticles = NULL;
76  }
77
78}
79
80ParticleSystem* GuidedMissile::trailParticles = NULL;
81ParticleSystem* GuidedMissile::explosionParticles = NULL;
82
83
84
85void GuidedMissile::activate()
86{
87  if (unlikely(GuidedMissile::trailParticles == NULL))
88  {
89    GuidedMissile::trailParticles = new ParticleSystem(2000, PARTICLE_SPRITE);
90    GuidedMissile::trailParticles->setName("GuidedMissileTrailParticles");
91    GuidedMissile::trailParticles->setMaterialTexture("maps/radial-trans-noise.png");
92    GuidedMissile::trailParticles->setLifeSpan(1.0, .3);
93    GuidedMissile::trailParticles->setRadius(0.0, .5);
94    GuidedMissile::trailParticles->setRadius(0.2, 2.0);
95    GuidedMissile::trailParticles->setRadius(.5, .8);
96    GuidedMissile::trailParticles->setRadius(1.0, .8);
97    GuidedMissile::trailParticles->setColor(0.0, 1,0,0,.7);
98    GuidedMissile::trailParticles->setColor(0.2, .8,.8,0,.5);
99    GuidedMissile::trailParticles->setColor(0.5, .8,.8,.8,.8);
100    GuidedMissile::trailParticles->setColor(1.0, .8,.8,.8,.0);
101  }
102  if (unlikely(GuidedMissile::explosionParticles == NULL))
103  {
104    GuidedMissile::explosionParticles = new ParticleSystem(200, PARTICLE_SPRITE);
105    GuidedMissile::explosionParticles->setName("GuidedMissileExplosionParticles");
106    GuidedMissile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png");
107    GuidedMissile::explosionParticles->setLifeSpan(.5, .3);
108    GuidedMissile::explosionParticles->setRadius(0.0, 10);
109    GuidedMissile::explosionParticles->setRadius(.5, 15.0);
110    GuidedMissile::explosionParticles->setRadius(1.0, 10.0);
111    GuidedMissile::explosionParticles->setColor(0.0, 0,1,0,1);
112    GuidedMissile::explosionParticles->setColor(0.5, .8,.8,0,.8);
113    GuidedMissile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
114    GuidedMissile::explosionParticles->setColor(1.0, 1,1,1,.0);
115  }
116
117  ParticleEngine::getInstance()->addConnection(this->emitter, GuidedMissile::trailParticles);
118
119  this->updateNode(0);
120  this->emitter->setEmissionRate(45.0);
121  this->emitter->setEmissionVelocity(0.0);
122}
123
124
125void GuidedMissile::deactivate()
126{
127  ParticleEngine::getInstance()->breakConnections(this->emitter);
128  this->lifeCycle = 0.0;
129
130  this->toList(OM_DEAD);
131  this->removeNode();
132  GuidedMissile::fastFactory->kill(this);
133}
134
135
136void GuidedMissile::collidesWith(WorldEntity* entity, const Vector& location)
137{
138  if (this->hitEntity != entity && entity->isA(CL_NPC))
139    this->destroy();
140  this->hitEntity = entity;
141}
142
143/**
144 *  signal tick, time dependent things will be handled here
145 * @param time since last tick
146*/
147void GuidedMissile::tick (float time)
148{
149
150  if (this->target != NULL && this->target->getParent() != PNode::getNullParent())
151   {
152    speed = velocity.len();
153    diffVector = ((target->getAbsCoor() - this->getAbsCoor()).getNormalized());
154
155    if(velocity.dot(diffVector) != 0)
156    {
157     correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
158
159      if(velocity.dot(diffVector) > 0)
160        velocity += correctionVector;
161      else if (velocity.dot(diffVector) < 0)
162        velocity -= correctionVector;
163    }
164    else
165      velocity += diffVector * agility;
166
167     this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
168   }
169
170  velocity *= maxVelocity/velocity.len();
171  Vector v = this->velocity * (time);
172  this->shiftCoor(v);
173
174  if(this->tickLifeCycle(time))
175    this->deactivate();
176}
177
178/**
179 *  the function gets called, when the projectile is destroyed
180 */
181void GuidedMissile::destroy ()
182{
183  PRINTF(5)("DESTROY GuidedMissile\n");
184  this->lifeCycle = .95; //!< @todo calculate this usefully.
185  ParticleEngine::getInstance()->breakConnection(this->emitter, GuidedMissile::trailParticles);
186  ParticleEngine::getInstance()->addConnection(this->emitter, GuidedMissile::explosionParticles);
187
188  this->emitter->setEmissionRate(1000.0);
189  this->emitter->setEmissionVelocity(50.0);
190//  this->deactivate();
191
192}
193
194
195void GuidedMissile::draw () const
196{
197  glMatrixMode(GL_MODELVIEW);
198  glPushMatrix();
199
200  float matrix[4][4];
201  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
202  this->getAbsDir().matrix (matrix);
203  glMultMatrixf((float*)matrix);
204  glScalef(2.0, 2.0, 2.0);
205  this->getModel()->draw();
206
207  glPopMatrix();
208
209}
210
Note: See TracBrowser for help on using the repository browser.