Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/projectiles/spike_ball.cc @ 10333

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

bump: black acid splash

File size: 6.8 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#include "class_id_DEPRECATED.h"
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    SpikeBall::explosionParticles = NULL;
85  }
86
87}
88
89SpriteParticles* SpikeBall::explosionParticles = NULL;
90
91void SpikeBall::activate()
92{
93  if (unlikely(SpikeBall::explosionParticles == NULL))
94  {
95    SpikeBall::explosionParticles = new SpriteParticles(1000);
96    SpikeBall::explosionParticles->setName("BoltExplosionParticles");
97    SpikeBall::explosionParticles->setLifeSpan(.5, .3);
98    SpikeBall::explosionParticles->setRadius(0.0, 10.0);
99    SpikeBall::explosionParticles->setRadius(.5, 6.0);
100    SpikeBall::explosionParticles->setRadius(1.0, 3.0);
101    SpikeBall::explosionParticles->setColor(0.0, 1,1,0,.9);
102    SpikeBall::explosionParticles->setColor(0.5, .8,.8,0,.5);
103    SpikeBall::explosionParticles->setColor(1.0, .8,.8,.7,.0);
104  }
105  this->setDamage(5);
106  this->setHealth(10);
107  this->setRotationAxis(VECTOR_RAND(1));
108  this->setAngle();
109
110  this->launcher[0] = Vector(1.0, 0.0, 0.0);
111  this->launcher[1] = Vector(0.0, 1.0, 0.0);
112  this->launcher[2] = Vector(0.0, 0.0, 1.0);
113
114  this->launcher[3] = Vector(1.0, 1.0, 0.0);
115  this->launcher[4] = Vector(0.0, 1.0, 1.0);
116  this->launcher[5] = Vector(1.0, 0.0, 1.0);
117  this->launcher[6] = Vector(1.0, -1.0, 0.0);
118  this->launcher[7] = Vector(0.0, 1.0, -1.0);
119  this->launcher[8] = Vector(-1.0, 0.0, 1.0);
120
121  this->launcher[9] = Vector(-1.0, 1.0, 1.0);
122  this->launcher[10] = Vector(1.0, 1.0, 1.0);
123  this->launcher[11] = Vector(1.0, -1.0, 1.0);
124  this->launcher[12] = Vector(-1.0, -1.0, 1.0);
125
126  int tmp = this->getFragments() / 2;
127  for (int i = 0; i < tmp; i++)
128  {
129    this->launcher[i].normalize();
130    this->launcher[tmp + i] =  this->launcher[i] * (-1);
131  }
132
133  this->freeMode = false;
134}
135
136
137void SpikeBall::deactivate()
138{
139  assert (SpikeBall::explosionParticles != NULL);
140  SpikeBall::explosionParticles->removeEmitter(this->emitter);
141  this->lifeCycle = 0.0;
142
143  this->toList(OM_NULL);
144  this->removeNode();
145  SpikeBall::fastFactory->kill(this);
146}
147
148
149void SpikeBall::collidesWith(WorldEntity* entity, const Vector& location)
150{
151  PRINTF(0)("Collision with SpikeBall\n");
152  if (this->hitEntity != entity && entity->isA(CL_NPC))
153    this->destroy( entity );
154  this->hitEntity = entity;
155  dynamic_cast<SpaceShip*>(entity)->damage(this->getDamage(),0);
156}
157
158
159void SpikeBall::blow()
160{
161  if ( this->freeMode )
162    updateFireDir();
163
164  Spike* pj = NULL;
165  for ( int i = 0; i < this->getFragments(); i++)
166  {
167    pj  = new Spike();
168    assert( pj );
169    pj->setParent(PNode::getNullParent());
170
171    pj->setVelocity(this->launcher[i].getNormalized() * 250.0);
172
173    pj->setParent(PNode::getNullParent());
174    pj->setAbsCoor(this->getAbsCoor() + this->launcher[i] * this->size);
175//     Quaternion q;
176    pj->setAbsDir(Quaternion(this->launcher[i], 0));
177
178    pj->toList(this->getOMListNumber());
179
180    pj->activate();
181  }
182}
183
184
185void SpikeBall::updateFireDir(){
186
187  float** m = new float* [3];
188  for( int i = 0; i < 3 ; i++)
189    m[i] = new float;
190
191  float nx, ny, nz, ca, sa;
192
193  nx = this->getRotationAxis().x;
194  ny = this->getRotationAxis().y;
195  nz = this->getRotationAxis().z;
196
197  ca = cos (this->getAngle());
198  sa = sin (this->getAngle());
199
200  m[0][0] = nx * nx * (1 - ca) + ca;
201  m[0][1] = nx * ny * (1 - ca) + nz * sa;
202  m[0][2] = nx * nz * (1 - ca) - ny * sa;
203  m[1][0] = nx * nz * (1 - ca) - nz * sa;
204  m[1][1] = ny * ny * (1 - ca) + ca;
205  m[1][2] = ny * nz * (1 - ca) + nx * sa;
206  m[2][0] = nx * nz * (1 - ca) + ny * sa;
207  m[2][1] = ny * nz * (1 - ca) - nx * sa;
208  m[2][2] = nz * nz * (1 - ca) + ca;
209
210  float x, y, z;
211  for (int i = 0; i < this->getFragments(); i++){
212    x = m[0][0] * this->launcher[i].x + m[0][1] * this->launcher[i].y + m[0][2] * this->launcher[i].z;
213    y = m[1][0] * this->launcher[i].x + m[1][1] * this->launcher[i].y + m[1][2] * this->launcher[i].z;
214    z = m[2][0] * this->launcher[i].x + m[2][1] * this->launcher[i].y + m[2][2] * this->launcher[i].z;
215
216    this->launcher[i] = Vector (x, y, z);
217  }
218
219  for( int i = 0; i < 3 ; i++)
220    delete m[i];
221  delete m;
222}
223
224
225/**
226 *  signal tick, time dependent things will be handled here
227 * @param dt time since last tick
228*/
229void SpikeBall::tick (float dt)
230{
231  Vector v = this->velocity * dt;
232  this->shiftCoor(v);
233
234  if (this->tickLifeCycle(dt)){
235    this->blow();
236    this->deactivate();
237  }
238
239  this->updateAngle( dt );
240}
241
242/**
243 *  the function gets called, when the projectile is destroyed
244*/
245void SpikeBall::destroy (WorldEntity* killer)
246{
247  ProjectileWeapon::destroy( killer );
248  PRINTF(5)("DESTROY SpikeBall\n");
249  this->lifeCycle = .95; //!< @todo calculate this usefully.
250
251  this->emitter->setSystem(SpikeBall::explosionParticles);
252}
253
254
255void SpikeBall::draw () const
256{
257  glPushAttrib(GL_ENABLE_BIT);
258  glMatrixMode(GL_MODELVIEW);
259  glPushMatrix();
260
261  float matrix[4][4];
262  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
263  this->halo->draw();
264
265  glRotatef(angle, this->getRotationAxis().x, this->getRotationAxis().y, this->getRotationAxis().z);
266  this->getAbsDir().matrix (matrix);
267  glMultMatrixf((float*)matrix);
268  this->getModel()->draw();
269
270
271  glPopMatrix();
272  glPopAttrib();
273}
274
Note: See TracBrowser for help on using the repository browser.