Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/projectiles/guided_missile.cc @ 9227

Last change on this file since 9227 was 8362, checked in by bensch, 18 years ago

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

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 "state.h"
21#include "class_list.h"
22
23#include "dot_emitter.h"
24#include "sprite_particles.h"
25
26#include "debug.h"
27
28CREATE_FAST_FACTORY_STATIC(GuidedMissile, CL_GUIDED_MISSILE);
29
30/**
31 *  standard constructor
32*/
33GuidedMissile::GuidedMissile () : Projectile()
34{
35  this->setClassID(CL_GUIDED_MISSILE, "GuidedMissile");
36
37  this->loadModel("models/projectiles/orx-rocket.obj", .3);
38  this->loadExplosionSound("sound/explosions/explosion_4.wav");
39
40
41  this->setMinEnergy(1);
42  this->setHealthMax(10);
43  this->lifeSpan = 4.0;
44  this->agility = 3.5;
45  this->maxVelocity = 75;
46
47  this->emitter = new DotEmitter(100, 5, M_2_PI);
48  this->emitter->setParent(this);
49  this->emitter->setSpread(M_PI, M_PI);
50}
51
52
53/**
54 *  standard deconstructor
55*/
56GuidedMissile::~GuidedMissile ()
57{
58  // delete this->emitter;
59
60  /* this is normaly done by World.cc by deleting the ParticleEngine */
61  if (GuidedMissile::trailParticles != NULL && ClassList::getList(CL_GUIDED_MISSILE)->size() <= 1)
62  {
63    if (ClassList::exists(GuidedMissile::trailParticles, CL_PARTICLE_SYSTEM))
64      delete GuidedMissile::trailParticles;
65    GuidedMissile::trailParticles = NULL;
66  }
67  if (GuidedMissile::explosionParticles != NULL && ClassList::getList(CL_GUIDED_MISSILE)->size() <= 1)
68  {
69    if (ClassList::exists(GuidedMissile::explosionParticles, CL_PARTICLE_SYSTEM))
70      delete GuidedMissile::explosionParticles;
71    GuidedMissile::explosionParticles = NULL;
72  }
73
74}
75
76SpriteParticles* GuidedMissile::trailParticles = NULL;
77SpriteParticles* GuidedMissile::explosionParticles = NULL;
78
79
80
81void GuidedMissile::activate()
82{
83  if (unlikely(GuidedMissile::trailParticles == NULL))
84  {
85    GuidedMissile::trailParticles = new SpriteParticles(2000);
86    GuidedMissile::trailParticles->setName("GuidedMissileTrailParticles");
87    GuidedMissile::trailParticles->setMaterialTexture("maps/radial-trans-noise.png");
88    GuidedMissile::trailParticles->setLifeSpan(1.0, .3);
89    GuidedMissile::trailParticles->setRadius(0.0, .5);
90    GuidedMissile::trailParticles->setRadius(0.2, 2.0);
91    GuidedMissile::trailParticles->setRadius(.5, .8);
92    GuidedMissile::trailParticles->setRadius(1.0, .8);
93    GuidedMissile::trailParticles->setColor(0.0, 1,0,0,.7);
94    GuidedMissile::trailParticles->setColor(0.2, .8,.8,0,.5);
95    GuidedMissile::trailParticles->setColor(0.5, .8,.8,.8,.8);
96    GuidedMissile::trailParticles->setColor(1.0, .8,.8,.8,.0);
97  }
98  if (unlikely(GuidedMissile::explosionParticles == NULL))
99  {
100    GuidedMissile::explosionParticles = new SpriteParticles(200);
101    GuidedMissile::explosionParticles->setName("GuidedMissileExplosionParticles");
102    GuidedMissile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png");
103    GuidedMissile::explosionParticles->setLifeSpan(.5, .3);
104    GuidedMissile::explosionParticles->setRadius(0.0, 10);
105    GuidedMissile::explosionParticles->setRadius(.5, 15.0);
106    GuidedMissile::explosionParticles->setRadius(1.0, 10.0);
107    GuidedMissile::explosionParticles->setColor(0.0, 0,1,0,1);
108    GuidedMissile::explosionParticles->setColor(0.5, .8,.8,0,.8);
109    GuidedMissile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
110    GuidedMissile::explosionParticles->setColor(1.0, 1,1,1,.0);
111  }
112
113  this->emitter->setSystem(GuidedMissile::trailParticles);
114
115  this->updateNode(0);
116  this->emitter->setEmissionRate(45.0);
117  this->emitter->setEmissionVelocity(0.0);
118
119  this->setHealth(10.0* (float)rand()/(float)RAND_MAX);
120}
121
122
123void GuidedMissile::deactivate()
124{
125  this->emitter->setSystem(NULL);
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)
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( (diffVector *  (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility )
159       velocity = ((diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility;
160      else if(velocity.dot(diffVector) > 0)
161        velocity += correctionVector;
162      else if (velocity.dot(diffVector) < 0)
163        velocity -= correctionVector;
164     }
165    else
166      velocity += diffVector * agility;
167
168     this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
169   }
170
171  velocity *= maxVelocity/velocity.len();
172  Vector v = this->velocity * (time);
173  this->shiftCoor(v);
174
175  if(this->tickLifeCycle(time))
176    this->deactivate();
177}
178
179/**
180 *  the function gets called, when the projectile is destroyed
181 */
182void GuidedMissile::destroy ()
183{
184  Projectile::destroy();
185  PRINTF(5)("DESTROY GuidedMissile\n");
186  this->lifeCycle = .95; //!< @todo calculate this usefully.
187  this->emitter->setSystem(GuidedMissile::explosionParticles);
188
189  this->emitter->setEmissionRate(1000.0);
190  this->emitter->setEmissionVelocity(50.0);
191//  this->deactivate();
192
193}
194
195
196void GuidedMissile::draw () const
197{
198  glMatrixMode(GL_MODELVIEW);
199  glPushMatrix();
200
201  float matrix[4][4];
202  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
203  this->getAbsDir().matrix (matrix);
204  glMultMatrixf((float*)matrix);
205  glScalef(2.0, 2.0, 2.0);
206  this->getModel()->draw();
207
208  glPopMatrix();
209
210}
211
Note: See TracBrowser for help on using the repository browser.