Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7128 was 7102, checked in by bensch, 19 years ago

power ups can only be picked up once

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