Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

updated swarm_projectile, model included in data
TE2 tests

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