Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/blink/src/world_entities/projectiles/spike_ball.cc @ 10772

Last change on this file since 10772 was 10368, checked in by patrick, 18 years ago

merged the branche playability into the trunk

File size: 7.0 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: Nicolas Schlumberger, Marc Schaerrer
13   co-programmer: Benjamin Grauer
14
15*/
16
17
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
19
20#include "spike_ball.h"
21
22#include "state.h"
23#include "model.h"
24
25#include "particles/dot_emitter.h"
26#include "particles/sprite_particles.h"
27
28#include <cassert>
29#include "debug.h"
30
31#include "space_ships/space_ship.h"
32
33
34
35ObjectListDefinition(SpikeBall);
36CREATE_FAST_FACTORY_STATIC(SpikeBall);
37
38/**
39 *  standard constructor
40*/
41SpikeBall::SpikeBall () : ProjectileWeapon()
42{
43  this->registerObject(this, SpikeBall::_objectList);
44
45  this->loadModel("models/projectiles/spike_ball.obj", .25);
46
47  this->setMinEnergy(1);
48  this->setHealthMax(1);
49  this->lifeSpan = 1.0;
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  this->emitter->setEmissionRate(300.0);
55  this->emitter->setEmissionVelocity(50.0);
56
57  this->setRotationSpeed(130);
58
59  this->halo = new Billboard();
60  this->halo->setSize(2, 2);
61  this->halo->setTexture("hbolt_halo.png");
62
63  this->setFragments(26);
64
65  this->size = 4;
66
67  this->launcher = new Vector [this->getFragments()];
68}
69
70
71/**
72 *  standard deconstructor
73*/
74SpikeBall::~SpikeBall ()
75{
76  // delete this->emitter;
77
78  /* this is normaly done by World.cc by deleting the ParticleEngine */
79  if (SpikeBall::explosionParticles != NULL && SpikeBall::objectList().size() <= 1)
80  {
81    //if (ClassList::exists(SpikeBall::explosionParticles, CL_PARTICLE_SYSTEM))
82    //  delete SpikeBall::explosionParticles;
83    PRINTF(1)("Deleting SpikeBall Particles\n");
84//    if (SpikeBall::objectList().exists(SpikeBall::explosionParticles))
85//      delete SpikeBall::explosionParticles;
86    SpikeBall::explosionParticles = NULL;
87  }
88
89}
90
91SpriteParticles* SpikeBall::explosionParticles = NULL;
92
93void SpikeBall::activate()
94{
95  if (unlikely(SpikeBall::explosionParticles == NULL))
96  {
97    SpikeBall::explosionParticles = new SpriteParticles(1000);
98    SpikeBall::explosionParticles->setName("SpikeBallExplosionParticles");
99    SpikeBall::explosionParticles->setLifeSpan(.5, .3);
100    SpikeBall::explosionParticles->setRadius(0.0, 10.0);
101    SpikeBall::explosionParticles->setRadius(.5, 6.0);
102    SpikeBall::explosionParticles->setRadius(1.0, 3.0);
103    SpikeBall::explosionParticles->setColor(0.0, 1,1,0,.9);
104    SpikeBall::explosionParticles->setColor(0.5, .8,.8,0,.5);
105    SpikeBall::explosionParticles->setColor(1.0, .8,.8,.7,.0);
106  }
107  this->setDamage(5);
108  this->setHealth(10);
109  this->setRotationAxis(VECTOR_RAND(1));
110  this->setAngle();
111
112  this->launcher[0] = Vector(1.0, 0.0, 0.0);
113  this->launcher[1] = Vector(0.0, 1.0, 0.0);
114  this->launcher[2] = Vector(0.0, 0.0, 1.0);
115
116  this->launcher[3] = Vector(1.0, 1.0, 0.0);
117  this->launcher[4] = Vector(0.0, 1.0, 1.0);
118  this->launcher[5] = Vector(1.0, 0.0, 1.0);
119  this->launcher[6] = Vector(1.0, -1.0, 0.0);
120  this->launcher[7] = Vector(0.0, 1.0, -1.0);
121  this->launcher[8] = Vector(-1.0, 0.0, 1.0);
122
123  this->launcher[9] = Vector(-1.0, 1.0, 1.0);
124  this->launcher[10] = Vector(1.0, 1.0, 1.0);
125  this->launcher[11] = Vector(1.0, -1.0, 1.0);
126  this->launcher[12] = Vector(-1.0, -1.0, 1.0);
127
128  int tmp = this->getFragments() / 2;
129  for (int i = 0; i < tmp; i++)
130  {
131    this->launcher[i].normalize();
132    this->launcher[tmp + i] =  this->launcher[i] * (-1);
133  }
134
135  this->freeMode = false;
136}
137
138
139void SpikeBall::deactivate()
140{
141  assert (SpikeBall::explosionParticles != NULL);
142  //SpikeBall::explosionParticles->removeEmitter(this->emitter);
143  this->emitter->setSystem(NULL);
144  this->lifeCycle = 0.0;
145
146  this->toList(OM_NULL);
147  this->removeNode();
148  SpikeBall::fastFactory->kill(this);
149}
150
151/*
152void SpikeBall::collidesWith(WorldEntity* entity, const Vector& location)
153{
154  PRINTF(0)("Collision with SpikeBall\n");
155  if (this->hitEntity != entity && entity->isA(CL_NPC))
156    this->destroy( entity );
157  this->hitEntity = entity;
158  dynamic_cast<SpaceShip*>(entity)->damage(this->getDamage(),0);
159}*/
160
161
162void SpikeBall::blow()
163{
164  if ( this->freeMode )
165    updateFireDir();
166
167  Spike* pj = NULL;
168  for ( int i = 0; i < this->getFragments(); i++)
169  {
170    pj  = new Spike();
171    assert( pj );
172    pj->setParent(PNode::getNullParent());
173
174    pj->setVelocity(this->launcher[i].getNormalized() * 250.0);
175
176    pj->setParent(PNode::getNullParent());
177    pj->setAbsCoor(this->getAbsCoor() + this->launcher[i] * this->size);
178//     Quaternion q;
179    pj->setAbsDir(Quaternion(this->launcher[i], 0));
180
181    pj->toList(this->getOMListNumber());
182
183    pj->activate();
184  }
185}
186
187
188void SpikeBall::updateFireDir(){
189
190  float** m = new float* [3];
191  for( int i = 0; i < 3 ; i++)
192    m[i] = new float;
193
194  float nx, ny, nz, ca, sa;
195
196  nx = this->getRotationAxis().x;
197  ny = this->getRotationAxis().y;
198  nz = this->getRotationAxis().z;
199
200  ca = cos (this->getAngle());
201  sa = sin (this->getAngle());
202
203  m[0][0] = nx * nx * (1 - ca) + ca;
204  m[0][1] = nx * ny * (1 - ca) + nz * sa;
205  m[0][2] = nx * nz * (1 - ca) - ny * sa;
206  m[1][0] = nx * nz * (1 - ca) - nz * sa;
207  m[1][1] = ny * ny * (1 - ca) + ca;
208  m[1][2] = ny * nz * (1 - ca) + nx * sa;
209  m[2][0] = nx * nz * (1 - ca) + ny * sa;
210  m[2][1] = ny * nz * (1 - ca) - nx * sa;
211  m[2][2] = nz * nz * (1 - ca) + ca;
212
213  float x, y, z;
214  for (int i = 0; i < this->getFragments(); i++){
215    x = m[0][0] * this->launcher[i].x + m[0][1] * this->launcher[i].y + m[0][2] * this->launcher[i].z;
216    y = m[1][0] * this->launcher[i].x + m[1][1] * this->launcher[i].y + m[1][2] * this->launcher[i].z;
217    z = m[2][0] * this->launcher[i].x + m[2][1] * this->launcher[i].y + m[2][2] * this->launcher[i].z;
218
219    this->launcher[i] = Vector (x, y, z);
220  }
221
222  for( int i = 0; i < 3 ; i++)
223    delete m[i];
224  delete m;
225}
226
227
228/**
229 *  signal tick, time dependent things will be handled here
230 * @param dt time since last tick
231*/
232void SpikeBall::tick (float dt)
233{
234  Vector v = this->velocity * dt;
235  this->shiftCoor(v);
236
237  if (this->tickLifeCycle(dt)){
238    this->blow();
239    this->deactivate();
240  }
241
242  this->updateAngle( dt );
243}
244
245/**
246 *  the function gets called, when the projectile is destroyed
247*/
248void SpikeBall::destroy (WorldEntity* killer)
249{
250  ProjectileWeapon::destroy( killer );
251  PRINTF(5)("DESTROY SpikeBall\n");
252  this->lifeCycle = .95; //!< @todo calculate this usefully.
253
254  this->emitter->setSystem(SpikeBall::explosionParticles);
255}
256
257
258void SpikeBall::draw () const
259{
260  glPushAttrib(GL_ENABLE_BIT);
261  glMatrixMode(GL_MODELVIEW);
262  glPushMatrix();
263
264  float matrix[4][4];
265  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
266  this->halo->draw();
267
268  glRotatef(angle, this->getRotationAxis().x, this->getRotationAxis().y, this->getRotationAxis().z);
269  this->getAbsDir().matrix (matrix);
270  glMultMatrixf((float*)matrix);
271  this->getModel()->draw();
272
273
274  glPopMatrix();
275  glPopAttrib();
276}
277
Note: See TracBrowser for help on using the repository browser.