Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/objectmanager/src/world_entities/weapons/guided_missile.cc @ 6121

Last change on this file since 6121 was 6078, checked in by bensch, 19 years ago

orxonox/trunk: PNode compiles again
PNode is now handled with the erase function of stl, rather than remove (which is totally the wrong way when iterating through a list and delete elements)

File size: 6.0 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 = 5.0;
48  this->agility = 5.0;
49  this->maxVelocity = 100;
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  State::getWorldEntityList()->add(this);
88  if (unlikely(GuidedMissile::trailParticles == NULL))
89  {
90    GuidedMissile::trailParticles = new ParticleSystem(2000, PARTICLE_SPRITE);
91    GuidedMissile::trailParticles->setName("GuidedMissileTrailParticles");
92    GuidedMissile::trailParticles->setMaterialTexture("maps/radial-trans-noise.png");
93    GuidedMissile::trailParticles->setLifeSpan(1.0, .3);
94    GuidedMissile::trailParticles->setRadius(0.0, .5);
95    GuidedMissile::trailParticles->setRadius(0.2, 2.0);
96    GuidedMissile::trailParticles->setRadius(.5, .8);
97    GuidedMissile::trailParticles->setRadius(1.0, .8);
98    GuidedMissile::trailParticles->setColor(0.0, 1,0,0,.7);
99    GuidedMissile::trailParticles->setColor(0.2, .8,.8,0,.5);
100    GuidedMissile::trailParticles->setColor(0.5, .8,.8,.8,.8);
101    GuidedMissile::trailParticles->setColor(1.0, .8,.8,.8,.0);
102  }
103  if (unlikely(GuidedMissile::explosionParticles == NULL))
104  {
105    GuidedMissile::explosionParticles = new ParticleSystem(200, PARTICLE_SPRITE);
106    GuidedMissile::explosionParticles->setName("GuidedMissileExplosionParticles");
107    GuidedMissile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png");
108    GuidedMissile::explosionParticles->setLifeSpan(.5, .3);
109    GuidedMissile::explosionParticles->setRadius(0.0, 10);
110    GuidedMissile::explosionParticles->setRadius(.5, 15.0);
111    GuidedMissile::explosionParticles->setRadius(1.0, 10.0);
112    GuidedMissile::explosionParticles->setColor(0.0, 0,1,0,1);
113    GuidedMissile::explosionParticles->setColor(0.5, .8,.8,0,.8);
114    GuidedMissile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
115    GuidedMissile::explosionParticles->setColor(1.0, 1,1,1,.0);
116  }
117
118  ParticleEngine::getInstance()->addConnection(this->emitter, GuidedMissile::trailParticles);
119
120  this->updateNode(0);
121  this->emitter->setEmissionRate(45.0);
122  this->emitter->setEmissionVelocity(0.0);
123}
124
125
126void GuidedMissile::deactivate()
127{
128  ParticleEngine::getInstance()->breakConnections(this->emitter);
129  this->lifeCycle = 0.0;
130
131//  GarbageCollector::getInstance()->collect(this);
132  State::getWorldEntityList()->remove(this);
133  this->removeNode();
134  GuidedMissile::fastFactory->kill(this);
135}
136
137
138void GuidedMissile::collidesWith(WorldEntity* entity, const Vector& location)
139{
140  if (this->hitEntity != entity && entity->isA(CL_NPC))
141    this->destroy();
142  this->hitEntity = entity;
143}
144
145/**
146 *  signal tick, time dependent things will be handled here
147 * @param time since last tick
148*/
149void GuidedMissile::tick (float time)
150{
151  //Vector v = *this->flightDirection * ( this->speed * time * 1000 + 0.1);
152  if (this->target != NULL && this->target->getParent() != PNode::getNullParent())
153   {
154     printf("========%s::%s\n", target->getParent()->getClassName(), target->getParent()->getName() );
155     velocity += ((target->getAbsCoor() - this->getAbsCoor()).getNormalized())*agility;
156     float speed = velocity.len();
157     if (speed > this->maxVelocity)
158       velocity *= maxVelocity/speed;
159
160     this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
161   }
162  Vector v = this->velocity * (time);
163  this->shiftCoor(v);
164
165  if(this->tickLifeCycle(time))
166    this->deactivate();
167}
168
169/**
170 *  the function gets called, when the projectile is destroyed
171 */
172void GuidedMissile::destroy ()
173{
174  PRINTF(5)("DESTROY GuidedMissile\n");
175  this->lifeCycle = .95; //!< @todo calculate this usefully.
176  ParticleEngine::getInstance()->breakConnection(this->emitter, GuidedMissile::trailParticles);
177  ParticleEngine::getInstance()->addConnection(this->emitter, GuidedMissile::explosionParticles);
178
179  this->emitter->setEmissionRate(1000.0);
180  this->emitter->setEmissionVelocity(50.0);
181//  this->deactivate();
182
183}
184
185
186void GuidedMissile::draw () const
187{
188  glMatrixMode(GL_MODELVIEW);
189  glPushMatrix();
190
191  float matrix[4][4];
192  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
193  this->getAbsDir().matrix (matrix);
194  glMultMatrixf((float*)matrix);
195  glScalef(2.0, 2.0, 2.0);
196  this->getModel()->draw();
197
198  glPopMatrix();
199
200}
201
Note: See TracBrowser for help on using the repository browser.