Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

weapon, collision, particles

File size: 8.5 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#include "space_ships/space_ship.h"
26
27#include "debug.h"
28
29#include "class_id_DEPRECATED.h"
30
31ObjectListDefinitionID(SwarmProjectile, CL_SWARM_PROJECTILE);
32CREATE_FAST_FACTORY_STATIC(SwarmProjectile);
33
34/**
35 *  standard constructor
36*/
37SwarmProjectile::SwarmProjectile () : Projectile()
38{
39
40/*  this->loadModel("models/projectiles/orx-rocket.obj", 0.5);*/
41  this->loadModel("models/projectiles/swarm_projectile.obj"); // no double rescale (see draw())
42  this->loadExplosionSound("sound/explosions/explosion_4.wav");
43
44
45  this->setMinEnergy(1);
46  this->setHealthMax(10);
47  this->lifeSpan = 4.0;
48  this->agility = 3.5;
49  this->maxVelocity = 100;
50
51  this->emitter = new DotEmitter(100, 5, M_2_PI);
52  this->emitter->setParent(this);
53  this->emitter->setSpread(M_PI, M_PI);
54
55  this->target = NULL;
56
57  this->rotationSpeed = 100;
58  this->angle = 0;
59}
60
61
62/**
63 *  standard deconstructor
64*/
65SwarmProjectile::~SwarmProjectile ()
66{
67
68
69  /* this is normaly done by World.cc by deleting the ParticleEngine */
70  if (SwarmProjectile::trailParticles != NULL && SwarmProjectile::objectList().size() <= 1)
71  {
72    if (ParticleSystem::objectList().exists(SwarmProjectile::trailParticles))
73      delete SwarmProjectile::trailParticles;
74    SwarmProjectile::trailParticles = NULL;
75  }
76  if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1)
77  {
78    if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles))
79      delete SwarmProjectile::explosionParticles;
80    SwarmProjectile::explosionParticles = NULL;
81  }
82   delete this->emitter;
83}
84
85SpriteParticles* SwarmProjectile::trailParticles = NULL;
86SpriteParticles* SwarmProjectile::explosionParticles = NULL;
87
88
89
90void SwarmProjectile::activate()
91{
92  if (unlikely(SwarmProjectile::trailParticles == NULL))
93  {
94    SwarmProjectile::trailParticles = new SpriteParticles(2000);
95    SwarmProjectile::trailParticles->setName("BoomerangProjectileTrailParticles");
96    SwarmProjectile::trailParticles->setMaterialTexture("maps/radial-trans-noise.png");
97    SwarmProjectile::trailParticles->setLifeSpan(1.0, .3);
98    SwarmProjectile::trailParticles->setRadius(0.0, .5);
99    SwarmProjectile::trailParticles->setRadius(0.2, 2.0);
100    SwarmProjectile::trailParticles->setRadius(.5, 1.0);
101    SwarmProjectile::trailParticles->setRadius(1.0, 0.6);
102    SwarmProjectile::trailParticles->setColor(0.0, 1,0,0,.7);
103    SwarmProjectile::trailParticles->setColor(0.2, .8,.8,0,.5);
104    SwarmProjectile::trailParticles->setColor(0.5, .8,.8,.8,.8);
105    SwarmProjectile::trailParticles->setColor(1.0, .8,.8,.8,.0);
106  }
107  if (unlikely(SwarmProjectile::explosionParticles == NULL))
108  {
109    SwarmProjectile::explosionParticles = new SpriteParticles(200);
110    SwarmProjectile::explosionParticles->setName("BoomerangProjectileExplosionParticles");
111    SwarmProjectile::explosionParticles->setMaterialTexture("maps/radial-trans-noise.png");
112    SwarmProjectile::explosionParticles->setLifeSpan(.5, .3);
113    SwarmProjectile::explosionParticles->setRadius(0.0, 10);
114    SwarmProjectile::explosionParticles->setRadius(.5, 15.0);
115    SwarmProjectile::explosionParticles->setRadius(1.0, 10.0);
116    SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1);
117    SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8);
118    SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
119    SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0);
120  }
121
122  this->emitter->setSystem(SwarmProjectile::trailParticles);
123
124  this->updateNode(0);
125  this->emitter->setEmissionRate(45.0);
126  this->emitter->setEmissionVelocity(0.0);
127
128  this->setHealth(10.0* (float)rand()/(float)RAND_MAX);
129}
130
131
132void SwarmProjectile::deactivate()
133{
134  this->emitter->setSystem(NULL);
135  this->lifeCycle = 0.0;
136
137  this->toList(OM_DEAD);
138  this->removeNode();
139  SwarmProjectile::fastFactory->kill(this);
140}
141
142
143void SwarmProjectile::collidesWith(WorldEntity* entity, const Vector& location)
144{
145  if (this->hitEntity != entity)
146    this->destroy( entity );
147  this->hitEntity = entity;
148  dynamic_cast<SpaceShip*>(entity)->damage(this->getDamage(),0);
149}
150
151
152
153/**
154 *  this function gets called by tick to calculate the new flight direction
155 *  @param curDirection direction vector
156 *  @param estTargetDir target vector, pointing to where the target will be on hit
157 *  @param angle = tick * turningSpeed
158 *  @return (new) direction vector
159*/
160Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle)
161{
162  if (unlikely(curDirection.len() == 0))
163    return curDirection;
164  printf("recalculating direction\n");
165  float tmp = angleDeg ( curDirection, estTargetDir);
166  if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); }
167
168  if( fabsf(angle) >  fabsf(tmp) ) { angle = tmp; }
169
170  Vector d = curDirection.cross(estTargetDir).cross(curDirection);
171  d.normalize();
172  if( unlikely( 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/*
190  Vector targetFarFarAway = this->getAbsCoor() + Vector(100000, 0, 0);
191
192  {
193    speed = velocity.len();
194    diffVector = ((targetFarFarAway - this->getAbsCoor()).getNormalized());
195
196    if(velocity.dot(diffVector) != 0)
197    {
198      correctionVector = (( ( diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility;
199
200      if( (diffVector *  (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility )
201        velocity = ((diffVector *  (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility;
202      else if(velocity.dot(diffVector) > 0)
203        velocity += correctionVector;
204      else if (velocity.dot(diffVector) < 0)
205        velocity -= correctionVector;
206    }
207    else
208      velocity += diffVector * agility;
209
210    this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)));
211  }
212
213  velocity *= maxVelocity/velocity.len();
214  Vector v = this->velocity * (time);
215  this->shiftCoor(v);*/
216
217  float projectileVelocity = this->velocity.len();
218  if (target != NULL){
219    float tti;  //Time To Impact
220    float targetVelocity = this->target->getVelocity().len();
221    if (unlikely(projectileVelocity == 0 && targetVelocity == 0)) // TODO calculate this reasonably
222      tti = 1; // we do have a problem....
223    else
224      tti = this->target->getRelCoor().len() / sqrt ( projectileVelocity * projectileVelocity + targetVelocity * targetVelocity);
225    Vector estTargetDir = (this->target->getRelCoor() + this->target->getVelocity()) * tti;
226    this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
227  }
228  else
229    if (likely(projectileVelocity != 0))
230      this->velocity *= maxVelocity / projectileVelocity; // set speed to max
231
232
233  this->shiftCoor(velocity * time);
234
235  if(this->tickLifeCycle(time))
236    this->deactivate();
237
238  this->updateAngle(time);
239}
240
241/**
242 *  the function gets called, when the projectile is destroyed
243 */
244void SwarmProjectile::destroy (WorldEntity* killer)
245{
246
247  printf("THIS SHOULD WORLk\n");
248
249  Projectile::destroy( killer );
250  PRINTF(5)("DESTROY SwarmProjectile\n");
251  this->lifeCycle = .95; //!< @todo calculate this usefully.
252  this->emitter->setSystem(SwarmProjectile::explosionParticles);
253
254  this->emitter->setEmissionRate(1000.0);
255  this->emitter->setEmissionVelocity(50.0);
256  //  this->deactivate();
257
258}
259
260void SwarmProjectile::updateAngle(float time)
261{
262  this->angle += this->rotationSpeed * time;
263}
264
265void SwarmProjectile::draw () const
266{
267  glMatrixMode(GL_MODELVIEW);
268  glPushMatrix();
269
270  float matrix[4][4];
271  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
272  glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile
273  this->getAbsDir().matrix (matrix);
274  glMultMatrixf((float*)matrix);
275  //glScalef(2.0, 2.0, 2.0);  // no double rescale
276  this->getModel()->draw();
277
278  glPopMatrix();
279
280}
281
Note: See TracBrowser for help on using the repository browser.