Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6123 was 6044, checked in by snellen, 19 years ago

WARNING: probably doesnt compile

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