Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Firing Echo fix on WM

File size: 8.6 KB
Line 
1
2/*
3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004-2006 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: Marc Schaerrer, Nicolas Schlumberger
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#include "space_ships/space_ship.h"
26#include "effects/trail.h"
27
28#include "debug.h"
29
30#include "class_id_DEPRECATED.h"
31
32#include "math/vector.h"
33
34ObjectListDefinitionID(SwarmProjectile, CL_SWARM_PROJECTILE);
35CREATE_FAST_FACTORY_STATIC(SwarmProjectile);
36
37/**
38 *  standard constructor
39*/
40SwarmProjectile::SwarmProjectile () : Projectile()
41{
42
43/*  this->loadModel("models/projectiles/orx-rocket.obj", 0.5);*/
44  this->loadModel("models/projectiles/swarm_projectile.obj"); // no double rescale (see draw())
45  this->loadExplosionSound("sound/explosions/explosion_4.wav");
46
47
48  this->setMinEnergy(1);
49  this->setHealthMax(10);
50  this->lifeSpan = 4.0;
51  this->agility = 3.5;
52
53  this->emitter = new DotEmitter(100, 5, M_2_PI);
54  this->emitter->setParent(this);
55  this->emitter->setSpread(M_PI, M_PI);
56
57  this->turningSpeed = 10;
58
59  this->physDamage = 200;
60  this->elecDamage = 0;
61
62  this->trail = new Trail(2.5,4,.2, this);
63  //this->trail->setParent( this);
64  this->trail->setTexture( "maps/laser.png");
65}
66
67
68/**
69 *  standard deconstructor
70*/
71SwarmProjectile::~SwarmProjectile ()
72{
73
74  if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1)
75  {
76    if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles))
77      delete SwarmProjectile::explosionParticles;
78    SwarmProjectile::explosionParticles = NULL;
79  }
80  // delete this->emitter;
81  delete this->trail;
82}
83
84SpriteParticles* SwarmProjectile::explosionParticles = NULL;
85
86
87
88void SwarmProjectile::activate()
89{
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
106  this->updateNode(0);
107  this->emitter->setEmissionRate(50.0);
108  this->emitter->setEmissionVelocity(0.0);
109  this->emitter->setInheritSpeed(0);
110
111  this->setHealth(10.0* (float)rand()/(float)RAND_MAX);
112
113  this->maxVelocity = 300;
114
115  this->rotationSpeed = 360;
116  this->angle = 0;
117
118  this->curDir = this->lastDir = this->velocity;
119}
120
121
122void SwarmProjectile::deactivate()
123{
124  this->emitter->setSystem(NULL);
125  this->lifeCycle = 0.0;
126
127  this->toList(OM_DEAD);
128  this->removeNode();
129  SwarmProjectile::fastFactory->kill(this);
130}
131
132
133void SwarmProjectile::collidesWith(WorldEntity* entity, const Vector& location)
134{
135  if (this->hitEntity != entity)
136    this->destroy( entity );
137  this->hitEntity = entity;
138  //dynamic_cast<SpaceShip*>(entity)->damage(this->getPhysDamage(),this->getElecDamage());
139  this->destroy(this);
140}
141
142
143void SwarmProjectile::setTarget(PNode* target)
144{
145    this->target = target;
146}
147
148
149
150/**
151 *  this function gets called by tick to calculate the new flight direction
152 *  @param curDirection direction vector
153 *  @param estTargetDir target vector, pointing to where the target will be on hit
154 *  @param angle = tick * turningSpeed
155 *  @return (new) direction vector
156*/
157Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle)
158{
159  if (unlikely(curDirection.len() == 0))
160    return curDirection;
161  //printf("recalculating direction\n");
162  float tmp = angleRad ( curDirection, estTargetDir);
163  if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); }
164//   printf("turning angle: %f\ntemp: %f\n", angle, tmp);
165
166  if( fabsf(angle) >  fabsf(tmp) ) 
167    angle = tmp;
168  else
169    angle *= tmp/fabsf(tmp);
170
171  Vector d = curDirection.cross(estTargetDir).cross(curDirection);
172  d.normalize();
173  if( unlikely( fabsf(angle) == 90)) { return d; } //avoid complication
174
175  Vector newDir = curDirection + d *  curDirection.len() * tan (angle);
176  newDir.normalize();
177  newDir *= curDirection.len();
178  return newDir;
179}
180
181
182
183
184/**
185 *  signal tick, time dependent things will be handled here
186 * @param time since last tick
187*/
188void SwarmProjectile::tick (float time)
189{
190/*
191  Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0);
192
193  {
194    speed = velocity.len();
195    diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized());
196
197    if(velocity.dot(diffVector) != 0)
198    {
199      correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
200
201      if( (diffVector *  (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility )
202        velocity = ((diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility;
203      else if(velocity.dot(diffVector) > 0)
204        velocity += correctionVector;
205      else if (velocity.dot(diffVector) < 0)
206        velocity -= correctionVector;
207    }
208    else
209      velocity += diffVector * agility;
210
211    this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
212  }
213
214  velocity *= maxVelocity/velocity.len();
215  Vector v = this->velocity * (time);
216  this->shiftCoor(v);*/
217
218  float projectileVelocity = this->getVelocity().len();
219  if (target != NULL){
220/*    float tti;  //Time To Impact*/
221//     float targetVelocity = this->target->getVelocity().len();
222//     if (unlikely(projectileVelocity == 0 && targetVelocity == 0)) // TODO calculate this reasonably
223//       tti = 1; // we do have a problem....
224//     else
225//       tti = (this->getAbsCoor() - this->target->getAbsCoor()).len() / sqrt ( projectileVelocity * projectileVelocity + targetVelocity * targetVelocity);
226    Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
227//     estTargetDir.slerpTo(this->velocity, 1);
228    this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
229  }
230  else
231    if (likely(projectileVelocity != 0 || projectileVelocity != this->maxVelocity) )
232      this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max
233
234//   printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z);
235//   printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z);
236
237  this->shiftCoor(this->velocity * (time));
238
239  if(this->tickLifeCycle(time))
240    this->deactivate();
241
242  this->trail->tick(time);
243
244  this->angle += this->rotationSpeed * time;
245
246  this->lastDir = this->curDir;
247  this->curDir = this->velocity;
248  if( (this->getAbsCoor() - this->target->getAbsCoor()).len() < 3)   // FIXME  Temp fake workaround for collision :)
249  {
250    dynamic_cast<WorldEntity*>(target)->destroy( this);
251    this->destroy( this);
252  }
253}
254
255/**
256 *  the function gets called, when the projectile is destroyed
257 */
258void SwarmProjectile::destroy (WorldEntity* killer)
259{
260
261//   printf("THIS SHOULD WORK!\n");
262
263  Projectile::destroy( killer );
264  PRINTF(5)("DESTROY SwarmProjectile\n");
265  this->lifeCycle = .95; //!< @todo calculate this usefully.
266  this->emitter->setSystem(SwarmProjectile::explosionParticles);
267
268  this->emitter->setEmissionRate(1000.0);
269  this->emitter->setEmissionVelocity(50.0);
270  this->deactivate();
271
272}
273
274
275void SwarmProjectile::draw () const
276{
277  glMatrixMode(GL_MODELVIEW);
278  glPushMatrix();
279
280  Vector tmpDir = this->curDir; // *.7 + this->lastDir * .3;
281  tmpDir.slerpTo(this->lastDir, .4);
282
283  float matrix[4][4];
284  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
285  Vector tmpRot = this->getAbsCoor().cross(tmpDir);
286  glRotatef (angleDeg ( this->getAbsCoor(), tmpDir), tmpRot.x, tmpRot.y, tmpRot.z );
287  glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile
288  this->getAbsDir().matrix (matrix);
289  glMultMatrixf((float*)matrix);
290  //glScalef(2.0, 2.0, 2.0);  // no double rescale
291  this->getModel()->draw();
292  glTranslatef(-.9,0,0);
293  this->trail->draw();
294  glPopMatrix();
295}
Note: See TracBrowser for help on using the repository browser.