Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bump

File size: 8.5 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 = 30;
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) ) { angle = tmp; }
167
168  Vector d = curDirection.cross(estTargetDir).cross(curDirection);
169  d.normalize();
170  if( unlikely( angle == 90)) { return d; } //avoid complication
171
172  Vector newDir = curDirection + d *  curDirection.len() * tan (angle);
173  newDir.normalize();
174  newDir *= curDirection.len();
175  return newDir;
176}
177
178
179
180
181/**
182 *  signal tick, time dependent things will be handled here
183 * @param time since last tick
184*/
185void SwarmProjectile::tick (float time)
186{
187/*
188  Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0);
189
190  {
191    speed = velocity.len();
192    diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized());
193
194    if(velocity.dot(diffVector) != 0)
195    {
196      correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
197
198      if( (diffVector *  (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility )
199        velocity = ((diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility;
200      else if(velocity.dot(diffVector) > 0)
201        velocity += correctionVector;
202      else if (velocity.dot(diffVector) < 0)
203        velocity -= correctionVector;
204    }
205    else
206      velocity += diffVector * agility;
207
208    this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
209  }
210
211  velocity *= maxVelocity/velocity.len();
212  Vector v = this->velocity * (time);
213  this->shiftCoor(v);*/
214
215  float projectileVelocity = this->getVelocity().len();
216  if (target != NULL){
217/*    float tti;  //Time To Impact*/
218//     float targetVelocity = this->target->getVelocity().len();
219//     if (unlikely(projectileVelocity == 0 && targetVelocity == 0)) // TODO calculate this reasonably
220//       tti = 1; // we do have a problem....
221//     else
222//       tti = (this->getAbsCoor() - this->target->getAbsCoor()).len() / sqrt ( projectileVelocity * projectileVelocity + targetVelocity * targetVelocity);
223    Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor());
224//     estTargetDir.slerpTo(this->velocity, 1);
225    this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
226  }
227  else
228    if (likely(projectileVelocity != 0 || projectileVelocity != this->maxVelocity) )
229      this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max
230
231//   printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z);
232//   printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z);
233
234  this->shiftCoor(this->velocity * (time));
235
236  if(this->tickLifeCycle(time))
237    this->deactivate();
238
239  this->trail->tick(time);
240
241  this->angle += this->rotationSpeed * time;
242
243  this->lastDir = this->curDir;
244  this->curDir = this->velocity;
245  if( (this->getAbsCoor() - this->target->getAbsCoor()).len() < 3)   // FIXME  Temp fake workaround for collision :)
246  {
247    dynamic_cast<WorldEntity*>(target)->destroy( this);
248    this->destroy( this);
249  }
250}
251
252/**
253 *  the function gets called, when the projectile is destroyed
254 */
255void SwarmProjectile::destroy (WorldEntity* killer)
256{
257
258//   printf("THIS SHOULD WORK!\n");
259
260  Projectile::destroy( killer );
261  PRINTF(5)("DESTROY SwarmProjectile\n");
262  this->lifeCycle = .95; //!< @todo calculate this usefully.
263  this->emitter->setSystem(SwarmProjectile::explosionParticles);
264
265  this->emitter->setEmissionRate(1000.0);
266  this->emitter->setEmissionVelocity(50.0);
267  this->deactivate();
268
269}
270
271
272void SwarmProjectile::draw () const
273{
274  glMatrixMode(GL_MODELVIEW);
275  glPushMatrix();
276
277  Vector tmpDir = this->curDir *.7 + this->lastDir * .3;
278
279  float matrix[4][4];
280  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
281  Vector tmpRot = this->getAbsCoor().cross(tmpDir);
282  glRotatef (angleDeg ( this->getAbsCoor(), tmpDir), tmpRot.x, tmpRot.y, tmpRot.z );
283  glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile
284  this->getAbsDir().matrix (matrix);
285  glMultMatrixf((float*)matrix);
286  //glScalef(2.0, 2.0, 2.0);  // no double rescale
287  this->getModel()->draw();
288  glTranslatef(-.9,0,0);
289  this->trail->draw();
290  glPopMatrix();
291}
Note: See TracBrowser for help on using the repository browser.