Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

guiding system (somewhat not working)
minor cleanup in pnode.h
cd/cr in hbolt/lbolt

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