Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10261 was 10261, checked in by marcscha, 17 years ago

Fixes and cleanups

File size: 9.2 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004-2006 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: Marc Schaerrer, Nicolas Schlumberger
13   co-programmer:
14
15*/
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
17
18#include "swarm_projectile.h"
19
20#include "state.h"
21
22#include "particles/dot_emitter.h"
23#include "particles/sprite_particles.h"
24#include "space_ships/space_ship.h"
25#include "effects/trail.h"
26
27#include "debug.h"
28
29#include "class_id_DEPRECATED.h"
30
31#include "math/vector.h"
32
33ObjectListDefinitionID(SwarmProjectile, CL_SWARM_PROJECTILE);
34CREATE_FAST_FACTORY_STATIC(SwarmProjectile);
35
36/**
37 *  standard constructor
38*/
39SwarmProjectile::SwarmProjectile () : Projectile()
40{
41
42/*  this->loadModel("models/projectiles/orx-rocket.obj", 0.5);*/
43  this->loadModel("models/projectiles/swarm_projectile.obj"); // no double rescale (see draw())
44  this->loadExplosionSound("sound/explosions/explosion_4.wav");
45
46
47  this->setMinEnergy(1);
48  this->setHealthMax(10);
49  this->lifeSpan = 4.0;
50  this->agility = 3.5;
51
52  this->emitter = new DotEmitter(100, 5, M_2_PI);
53  this->emitter->setParent(this);
54  this->emitter->setSpread(M_PI, M_PI);
55
56  this->turningSpeed = 10;
57
58  this->physDamage = 200;
59  this->elecDamage = 0;
60
61  this->trail = new Trail(2.5,4,.2, this);
62  //this->trail->setParent( this);
63  this->trail->setTexture( "maps/laser.png");
64}
65
66
67/**
68 *  standard deconstructor
69*/
70SwarmProjectile::~SwarmProjectile ()
71{
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  // delete this->emitter;
80  delete this->trail;
81}
82
83SpriteParticles* SwarmProjectile::explosionParticles = NULL;
84
85
86
87void SwarmProjectile::activate()
88{
89  this->toList(OM_ENVIRON);
90  if (unlikely(SwarmProjectile::explosionParticles == NULL))
91  {
92    SwarmProjectile::explosionParticles = new SpriteParticles(200);
93    SwarmProjectile::explosionParticles->setName("SwarmProjectileExplosionParticles");
94    SwarmProjectile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png");
95    SwarmProjectile::explosionParticles->setLifeSpan(.5, .3);
96    SwarmProjectile::explosionParticles->setRadius(0.0, 10);
97    SwarmProjectile::explosionParticles->setRadius(.5, 15.0);
98    SwarmProjectile::explosionParticles->setRadius(1.0, 10.0);
99    SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1);
100    SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8);
101    SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
102    SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0);
103  }
104
105  this->emitter->setEmissionRate(50.0);
106  this->emitter->setEmissionVelocity(0.0);
107  this->emitter->setInheritSpeed(0);
108
109  this->setHealth(10.0* (float)rand()/(float)RAND_MAX);
110
111  this->maxVelocity = 300;
112
113  this->rotationSpeed = 360;
114  this->angle = 0;
115
116  this->curDir = this->lastDir = this->velocity;
117}
118
119
120void SwarmProjectile::deactivate()
121{
122  this->emitter->setSystem(NULL);
123  this->lifeCycle = 0.0;
124
125  this->toList(OM_DEAD);
126  this->removeNode();
127  SwarmProjectile::fastFactory->kill(this);
128}
129
130
131void SwarmProjectile::hit (WorldEntity* entity, float damage)
132{
133  if (this->hitEntity != entity)
134    this->destroy( entity );
135  this->hitEntity = entity;
136  //dynamic_cast<SpaceShip*>(entity)->damage(this->getPhysDamage(),this->getElecDamage());
137 // this->destroy(this);
138  this->deactivate();
139}
140
141
142void SwarmProjectile::setTarget(PNode* target)
143{
144    this->target = target;
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  if (unlikely(curDirection.len() == 0))
159    return curDirection;
160  //printf("recalculating direction\n");
161  float tmp = angleRad ( curDirection, estTargetDir);
162  if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); }
163//   printf("turning angle: %f\ntemp: %f\n", angle, tmp);
164
165  if( fabsf(angle) >  fabsf(tmp) ) 
166    angle = tmp;
167  else
168    angle *= tmp/fabsf(tmp);
169
170  Vector d = curDirection.cross(estTargetDir).cross(curDirection);
171  d.normalize();
172  if( unlikely( fabsf(angle) == 90)) { return d; } //avoid complication
173
174  Vector newDir = curDirection + d *  curDirection.len() * tan (angle);
175  newDir.normalize();
176  newDir *= curDirection.len();
177  return newDir;
178}
179
180
181
182
183/**
184 *  signal tick, time dependent things will be handled here
185 * @param time since last tick
186*/
187void SwarmProjectile::tick (float time)
188{
189  if(unlikely(this->target == NULL)) /** Check whether the target still exists*/
190    this->deactivate();
191
192/*
193  Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0);
194
195  {
196    speed = velocity.len();
197    diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized());
198
199    if(velocity.dot(diffVector) != 0)
200    {
201      correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
202
203      if( (diffVector *  (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility )
204        velocity = ((diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility;
205      else if(velocity.dot(diffVector) > 0)
206        velocity += correctionVector;
207      else if (velocity.dot(diffVector) < 0)
208        velocity -= correctionVector;
209    }
210    else
211      velocity += diffVector * agility;
212
213    this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
214  }
215
216  velocity *= maxVelocity/velocity.len();
217  Vector v = this->velocity * (time);
218  this->shiftCoor(v);*/
219
220
221/** old  guiding functuion*/
222
223  float projectileVelocity = this->getVelocity().len();
224  if (target != NULL){
225    Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
226    this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
227  }
228  else
229    if (likely(projectileVelocity != 0 || projectileVelocity != this->maxVelocity) )
230      this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max
231
232//   printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z);
233//   printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z);
234
235  this->shiftCoor(this->velocity * time);
236
237
238/*
239  Vector pjV = this->getVelocity();
240  Vector tV = this->target->getVelocity();
241  Vector pT = this->target->getAbsCoor() - this->getAbsCoor(); // vector projectile target
242
243  Vector a = tV.getNormalized() * pT.dot(tV.getNormalized());
244
245  float A = 2 * pT.len() * pT.len();
246  float B = 2 * a.len() * tV.len();
247  float D = 2 * sqrt(B * B - 4 * pT.len() * pT.len() *(tV.len() * tV.len() - pjV.len() * pjV.len()));
248  float tti;
249
250  if (A != 0){
251    if ( B < D ) tti = ( B + D ) / A;
252    else tti = ( B + D ) / A;
253  }
254  else tti = 0;
255
256
257  Vector estTargetDir;
258  if (tti == 0)
259    estTargetDir = pT.getNormalized() * pjV.len();
260  else
261    estTargetDir = pT / tti + pjV;
262
263  this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
264
265  this->shiftCoor(this->velocity * (this->maxVelocity * time));*/
266
267  if(this->tickLifeCycle(time))
268    this->deactivate();
269
270  this->trail->tick(time);
271
272  this->angle += this->rotationSpeed * time;
273  while (this->angle > 360)
274    this->angle -= 360;
275
276  this->lastDir = this->curDir;
277  this->curDir = this->velocity;
278  if( (this->getAbsCoor() - this->target->getAbsCoor()).len() < 3)   // FIXME  Temp fake workaround for collision :)
279  {
280    dynamic_cast<WorldEntity*>(target)->destroy( this);
281    this->destroy( this);
282  }
283}
284
285/**
286 *  the function gets called, when the projectile is destroyed
287 */
288void SwarmProjectile::destroy (WorldEntity* killer)
289{
290
291//   printf("THIS SHOULD WORK!\n");
292
293  Projectile::destroy( killer );
294  PRINTF(5)("DESTROY SwarmProjectile\n");
295  this->lifeCycle = .95; //!< @todo calculate this usefully.
296  this->emitter->setSystem(SwarmProjectile::explosionParticles);
297
298  this->emitter->setEmissionRate(1000.0);
299  this->emitter->setEmissionVelocity(50.0);
300  this->deactivate();
301
302}
303
304
305void SwarmProjectile::draw () const
306{
307  glMatrixMode(GL_MODELVIEW);
308  glPushMatrix();
309
310  Vector tmpDir = this->curDir; // *.7 + this->lastDir * .3;
311  tmpDir.slerpTo(this->lastDir, .4);
312
313  float matrix[4][4];
314  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
315  Vector tmpRot = this->getAbsCoor().cross(tmpDir);
316  glRotatef (angleRad ( this->getAbsCoor(), tmpDir), tmpRot.x, tmpRot.y, tmpRot.z );
317  glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile
318  this->getAbsDir().matrix (matrix);
319  glMultMatrixf((float*)matrix);
320  //glScalef(2.0, 2.0, 2.0);  // no double rescale
321  this->getModel()->draw();
322  glTranslatef(-.9,0,0);
323  this->trail->draw();
324  glPopMatrix();
325}
Note: See TracBrowser for help on using the repository browser.