Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6413 was 6241, checked in by bensch, 19 years ago

trunk: merged the spaceshipcontrol back to the trunk

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 
149if (this->target != NULL && this->target->getParent() != PNode::getNullParent())
150   {
151    speed = velocity.len();
152    diffVector = ((target->getAbsCoor() - this->getAbsCoor()).getNormalized());
153
154    if(velocity.dot(diffVector) != 0)
155     {
156     correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
157
158      if(velocity.dot(diffVector) > 0)
159        velocity += correctionVector;
160      else if (velocity.dot(diffVector) < 0)
161        velocity -= correctionVector;
162     }
163    else
164      velocity += diffVector * agility;
165
166     this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
167   }
168
169  velocity *= maxVelocity/velocity.len();
170  Vector v = this->velocity * (time);
171  this->shiftCoor(v);
172
173  if(this->tickLifeCycle(time))
174    this->deactivate();
175}
176
177/**
178 *  the function gets called, when the projectile is destroyed
179 */
180void GuidedMissile::destroy ()
181{
182  PRINTF(5)("DESTROY GuidedMissile\n");
183  this->lifeCycle = .95; //!< @todo calculate this usefully.
184  ParticleEngine::getInstance()->breakConnection(this->emitter, GuidedMissile::trailParticles);
185  ParticleEngine::getInstance()->addConnection(this->emitter, GuidedMissile::explosionParticles);
186
187  this->emitter->setEmissionRate(1000.0);
188  this->emitter->setEmissionVelocity(50.0);
189//  this->deactivate();
190
191}
192
193
194void GuidedMissile::draw () const
195{
196  glMatrixMode(GL_MODELVIEW);
197  glPushMatrix();
198
199  float matrix[4][4];
200  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
201  this->getAbsDir().matrix (matrix);
202  glMultMatrixf((float*)matrix);
203  glScalef(2.0, 2.0, 2.0);
204  this->getModel()->draw();
205
206  glPopMatrix();
207
208}
209
Note: See TracBrowser for help on using the repository browser.