Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6233 was 6222, checked in by bensch, 19 years ago

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