Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/projectiles/swarm_projectile.cc @ 10539

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

Fixes for weapon systems

File size: 7.6 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
30
31#include "math/vector.h"
32
33ObjectListDefinition(SwarmProjectile);
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", .25); // no double rescale (see draw())
44  this->loadExplosionSound("sounds/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( "textures/laser.png");
64
65
66  this->origList = this->getOMListNumber();
67  this->toList(OM_ENVIRON);
68}
69
70
71/**
72 *  standard deconstructor
73*/
74SwarmProjectile::~SwarmProjectile ()
75{
76
77  if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1)
78  {
79    if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles))
80      delete SwarmProjectile::explosionParticles;
81    SwarmProjectile::explosionParticles = NULL;
82  }
83  // delete this->emitter;
84  delete this->trail;
85}
86
87SpriteParticles* SwarmProjectile::explosionParticles = NULL;
88
89
90
91void SwarmProjectile::activate()
92{
93  this->toList(OM_ENVIRON);
94  if (unlikely(SwarmProjectile::explosionParticles == NULL))
95  {
96    SwarmProjectile::explosionParticles = new SpriteParticles(200);
97    SwarmProjectile::explosionParticles->setName("SwarmProjectileExplosionParticles");
98    SwarmProjectile::explosionParticles->setMaterialTexture("textures/radial-trans-noise.png");
99    SwarmProjectile::explosionParticles->setLifeSpan(.5, .3);
100    SwarmProjectile::explosionParticles->setRadius(0.0, 10);
101    SwarmProjectile::explosionParticles->setRadius(.5, 15.0);
102    SwarmProjectile::explosionParticles->setRadius(1.0, 10.0);
103    SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1);
104    SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8);
105    SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8);
106    SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0);
107  }
108
109  this->emitter->setEmissionRate(50.0);
110  this->emitter->setEmissionVelocity(0.0);
111  this->emitter->setInheritSpeed(0);
112
113  this->setHealth(10.0* (float)rand()/(float)RAND_MAX);
114
115  this->maxVelocity = 300;
116
117  this->rotationSpeed = 360;
118  this->angle = 0;
119
120  this->curDir = this->lastDir = this->velocity;
121}
122
123
124void SwarmProjectile::deactivate()
125{
126  this->emitter->setSystem(NULL);
127  this->lifeCycle = 0.0;
128
129  this->toList(OM_DEAD);
130  this->removeNode();
131  SwarmProjectile::fastFactory->kill(this);
132}
133
134
135void SwarmProjectile::hit (WorldEntity* entity, float damage)
136{
137  if (this->hitEntity != entity)
138    this->destroy( entity );
139  this->hitEntity = entity;
140  //dynamic_cast<SpaceShip*>(entity)->damage(this->getPhysDamage(),this->getElecDamage());
141 // this->destroy(this);
142  this->deactivate();
143}
144
145
146void SwarmProjectile::setTarget(PNode* target)
147{
148    this->target = target;
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 = angleRad ( curDirection, estTargetDir);
166  if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); }
167//   printf("turning angle: %f\ntemp: %f\n", angle, tmp);
168
169  if( fabsf(angle) >  fabsf(tmp) )
170    angle = tmp;
171  else
172    angle *= tmp/fabsf(tmp);
173
174  Vector d = curDirection.cross(estTargetDir).cross(curDirection);
175  d.normalize();
176  if( unlikely( fabsf(angle) == 90)) { return d; } //avoid complication
177
178  Vector newDir = curDirection + d *  curDirection.len() * tan (angle);
179  newDir.normalize();
180  newDir *= curDirection.len();
181  return newDir;
182}
183
184
185
186
187/**
188 *  signal tick, time dependent things will be handled here
189 * @param time since last tick
190*/
191void SwarmProjectile::tick (float time)
192{
193  if(unlikely(this->target == NULL)) /** Check whether the target still exists*/
194    this->deactivate();
195
196
197
198/** old  guiding function*/
199
200  float projectileVelocity = this->getVelocity().len();
201  if (target != NULL){
202    Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
203    this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time );
204  }
205  else
206    if (likely(projectileVelocity != 0 || projectileVelocity != this->maxVelocity) )
207      this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max
208/*
209  printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z);
210  printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z);*/
211
212  this->shiftCoor(this->velocity * time);
213
214  if(this->tickLifeCycle(time))
215    this->deactivate();
216
217  this->trail->tick(time);
218
219  this->angle += this->rotationSpeed * time;
220  while (this->angle > 360)
221    this->angle -= 360;
222
223  this->lastDir = this->curDir;
224  this->curDir = this->velocity;
225
226  if( this->target != NULL && (this->getAbsCoor() - this->target->getAbsCoor()).len() < 3)   // FIXME  Temp fake workaround for collision :)
227  {
228    dynamic_cast<WorldEntity*>(target)->destroy(this); //hit(this->getDamage(), this);
229    this->deactivate();
230    PRINTF(0)("Target was hit by Swarm Missile!\n");
231  }
232  else if( this->target == NULL)
233    this->deactivate();
234}
235
236/**
237 *  the function gets called, when the projectile is destroyed
238 */
239void SwarmProjectile::destroy (WorldEntity* killer)
240{
241
242//   printf("THIS SHOULD WORK!\n");
243
244  Projectile::destroy( killer );
245  PRINTF(5)("DESTROY SwarmProjectile\n");
246  this->lifeCycle = .95; //!< @todo calculate this usefully.
247  this->emitter->setSystem(SwarmProjectile::explosionParticles);
248
249  this->emitter->setEmissionRate(1000.0);
250  this->emitter->setEmissionVelocity(50.0);
251  this->deactivate();
252
253}
254
255
256void SwarmProjectile::draw () const
257{
258  glMatrixMode(GL_MODELVIEW);
259  glPushMatrix();
260
261  Vector tmpDir = this->curDir; // *.7 + this->lastDir * .3;
262  tmpDir.slerpTo(this->lastDir, .4);
263
264  float matrix[4][4];
265  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
266  Vector tmpRot = this->getAbsCoor().cross(tmpDir);
267  glRotatef (angleRad ( this->getAbsCoor(), tmpDir), tmpRot.x, tmpRot.y, tmpRot.z );
268  glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile
269  this->getAbsDir().matrix (matrix);
270  glMultMatrixf((float*)matrix);
271  //glScalef(2.0, 2.0, 2.0);  // no double rescale
272  this->getModel()->draw();
273  glTranslatef(-.9,0,0);
274  this->trail->draw();
275  glPopMatrix();
276}
Note: See TracBrowser for help on using the repository browser.