Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/projectiles/swarm_projectile.cc @ 10037

Last change on this file since 10037 was 10037, checked in by nicolasc, 17 years ago

added some comments
made swarm missile spin

File size: 7.9 KB
Line 
1
2/*
3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004 orx
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
12   ### File Specific
13   main-programmer: Silvan Nellen
14   co-programmer:
15
16*/
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
18
19#include "swarm_projectile.h"
20
21#include "state.h"
22
23#include "particles/dot_emitter.h"
24#include "particles/sprite_particles.h"
25
26#include "debug.h"
27
28#include "class_id_DEPRECATED.h"
29
30ObjectListDefinitionID(SwarmProjectile, CL_SWARM_PROJECTILE);
31CREATE_FAST_FACTORY_STATIC(SwarmProjectile);
32
33/**
34 *  standard constructor
35*/
36SwarmProjectile::SwarmProjectile () : Projectile()
37{
38
39/*  this->loadModel("models/projectiles/orx-rocket.obj", 0.5);*/
40  this->loadModel("models/projectiles/swarm_projectile.obj"); // no double rescale (see draw())
41  this->loadExplosionSound("sound/explosions/explosion_4.wav");
42
43
44  this->setMinEnergy(1);
45  this->setHealthMax(10);
46  this->lifeSpan = 2.0;
47  this->agility = 3.5;
48  this->maxVelocity = 100;
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  this->rotationSpeed = 100;
55  this->angle = 0;
56}
57
58
59/**
60 *  standard deconstructor
61*/
62SwarmProjectile::~SwarmProjectile ()
63{
64  // delete this->emitter;
65
66  /* this is normaly done by World.cc by deleting the ParticleEngine */
67  if (SwarmProjectile::trailParticles != NULL && SwarmProjectile::objectList().size() <= 1)
68  {
69    if (ParticleSystem::objectList().exists(SwarmProjectile::trailParticles))
70      delete SwarmProjectile::trailParticles;
71    SwarmProjectile::trailParticles = NULL;
72  }
73  if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1)
74  {
75    if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles))
76      delete SwarmProjectile::explosionParticles;
77    SwarmProjectile::explosionParticles = NULL;
78  }
79
80}
81
82SpriteParticles* SwarmProjectile::trailParticles = NULL;
83SpriteParticles* SwarmProjectile::explosionParticles = NULL;
84
85
86
87void SwarmProjectile::activate()
88{
89  if (unlikely(SwarmProjectile::trailParticles == NULL))
90  {
91    SwarmProjectile::trailParticles = new SpriteParticles(2000);
92    SwarmProjectile::trailParticles->setName("BoomerangProjectileTrailParticles");
93    SwarmProjectile::trailParticles->setMaterialTexture("maps/radial-trans-noise.png");
94    SwarmProjectile::trailParticles->setLifeSpan(1.0, .3);
95    SwarmProjectile::trailParticles->setRadius(0.0, .5);
96    SwarmProjectile::trailParticles->setRadius(0.2, 4.0);
97    SwarmProjectile::trailParticles->setRadius(.5, 1.5);
98    SwarmProjectile::trailParticles->setRadius(1.0, 1.5);
99    SwarmProjectile::trailParticles->setColor(0.0, 1,0,0,.7);
100    SwarmProjectile::trailParticles->setColor(0.2, .8,.8,0,.5);
101    SwarmProjectile::trailParticles->setColor(0.5, .8,.8,.8,.8);
102    SwarmProjectile::trailParticles->setColor(1.0, .8,.8,.8,.0);
103  }
104  if (unlikely(SwarmProjectile::explosionParticles == NULL))
105  {
106    SwarmProjectile::explosionParticles = new SpriteParticles(200);
107    SwarmProjectile::explosionParticles->setName("BoomerangProjectileExplosionParticles");
108    SwarmProjectile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png");
109    SwarmProjectile::explosionParticles->setLifeSpan(.5, .3);
110    SwarmProjectile::explosionParticles->setRadius(0.0, 10);
111    SwarmProjectile::explosionParticles->setRadius(.5, 15.0);
112    SwarmProjectile::explosionParticles->setRadius(1.0, 10.0);
113    SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1);
114    SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8);
115    SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
116    SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0);
117  }
118
119  this->emitter->setSystem(SwarmProjectile::trailParticles);
120
121  this->updateNode(0);
122  this->emitter->setEmissionRate(45.0);
123  this->emitter->setEmissionVelocity(0.0);
124
125  this->setHealth(10.0* (float)rand()/(float)RAND_MAX);
126}
127
128
129void SwarmProjectile::deactivate()
130{
131  this->emitter->setSystem(NULL);
132  this->lifeCycle = 0.0;
133
134  this->toList(OM_DEAD);
135  this->removeNode();
136  SwarmProjectile::fastFactory->kill(this);
137}
138
139
140void SwarmProjectile::collidesWith(WorldEntity* entity, const Vector& location)
141{
142  if (this->hitEntity != entity)
143    this->destroy( entity );
144  this->hitEntity = entity;
145}
146
147
148
149/**
150 *  this function gets called by tick to calculate the new flight direction
151 *  @param curDirection direction vector
152 *  @param estTargetDir target vector, pointing to where the target will be on hit
153 *  @param angle = tick * turningSpeed
154 *  @return (new) direction vector
155*/
156Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle)
157{
158  printf("recalculating direction\n");
159  float tmp = angleDeg ( curDirection, estTargetDir);
160  if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); }
161
162  if( fabsf(angle) >  fabsf(tmp) ) { angle = tmp; }
163
164  Vector d = curDirection.cross(estTargetDir).cross(curDirection);
165  d.normalize();
166  if( unlikely( angle == 90)) { return d; } //avoid complication
167
168  Vector newDir = curDirection + d *  curDirection.len() * tan (angle);
169  newDir.normalize();
170  newDir *= curDirection.len();
171  return newDir;
172}
173
174
175
176
177/**
178 *  signal tick, time dependent things will be handled here
179 * @param time since last tick
180*/
181void SwarmProjectile::tick (float time)
182{
183/*
184  Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0);
185
186  {
187    speed = velocity.len();
188    diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized());
189
190    if(velocity.dot(diffVector) != 0)
191    {
192      correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
193
194      if( (diffVector *  (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility )
195        velocity = ((diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility;
196      else if(velocity.dot(diffVector) > 0)
197        velocity += correctionVector;
198      else if (velocity.dot(diffVector) < 0)
199        velocity -= correctionVector;
200    }
201    else
202      velocity += diffVector * agility;
203
204    this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
205  }
206
207  velocity *= maxVelocity/velocity.len();
208  Vector v = this->velocity * (time);
209  this->shiftCoor(v);*/
210
211  if (target != NULL){
212    float tti = this->target->getRelCoor().len() / this->getVelocity().len();
213    Vector estTargetDir = (this->target->getRelCoor() + this->target->getVelocity()) * tti;
214    this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
215  }
216  else
217    velocity *= maxVelocity / velocity.len(); // set speed to max
218
219
220  this->shiftCoor(velocity * time);
221
222  if(this->tickLifeCycle(time))
223    this->deactivate();
224
225  this->updateAngle(time);
226}
227
228/**
229 *  the function gets called, when the projectile is destroyed
230 */
231void SwarmProjectile::destroy (WorldEntity* killer)
232{
233
234  printf("THIS SHOULD WORLk\n");
235
236  Projectile::destroy( killer );
237  PRINTF(5)("DESTROY SwarmProjectile\n");
238  this->lifeCycle = .95; //!< @todo calculate this usefully.
239  this->emitter->setSystem(SwarmProjectile::explosionParticles);
240
241  this->emitter->setEmissionRate(1000.0);
242  this->emitter->setEmissionVelocity(50.0);
243  //  this->deactivate();
244
245}
246
247void SwarmProjectile::updateAngle(float time)
248{
249  this->angle += this->rotationSpeed * time;
250}
251
252void SwarmProjectile::draw () const
253{
254  glMatrixMode(GL_MODELVIEW);
255  glPushMatrix();
256
257  float matrix[4][4];
258  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
259  glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile
260  this->getAbsDir().matrix (matrix);
261  glMultMatrixf((float*)matrix);
262  //glScalef(2.0, 2.0, 2.0);  // no double rescale
263  this->getModel()->draw();
264
265  glPopMatrix();
266
267}
268
Note: See TracBrowser for help on using the repository browser.