Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

code cleanup, rebuilt spikeball

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
134
135void SpikeBall::deactivate()
136{
137  assert (SpikeBall::explosionParticles != NULL);
138  SpikeBall::explosionParticles->removeEmitter(this->emitter);
139  this->lifeCycle = 0.0;
140
141  this->toList(OM_NULL);
142  this->removeNode();
143  SpikeBall::fastFactory->kill(this);
144}
145
146
147void SpikeBall::collidesWith(WorldEntity* entity, const Vector& location)
148{
149  PRINTF(0)("Collision with SpikeBall\n");
150  if (this->hitEntity != entity && entity->isA(CL_NPC))
151    this->destroy( entity );
152  this->hitEntity = entity;
153  dynamic_cast<SpaceShip*>(entity)->damage(this->getDamage(),0);
154}
155
156
157void SpikeBall::blow()
158{
159  updateFireDir();
160
161  Spike* pj = NULL;
162  for ( int i = 0; i < this->getFragments(); i++)
163  {
164    pj  = new Spike();
165    assert( pj );
166    pj->setParent(PNode::getNullParent());
167
168    pj->setVelocity(this->launcher[i].getNormalized() * 250.0);
169
170    pj->setParent(PNode::getNullParent());
171    pj->setAbsCoor(this->getAbsCoor() + this->launcher[i] * this->size);
172//     Quaternion q;
173    pj->setAbsDir(Quaternion(this->launcher[i], 0));
174
175    pj->toList(this->getOMListNumber());
176
177    pj->activate();
178  }
179}
180
181
182void SpikeBall::updateFireDir(){
183
184  float** m = new float* [3];
185  for( int i = 0; i < 3 ; i++)
186    m[i] = new float;
187
188  float nx, ny, nz, ca, sa;
189
190  nx = this->getRotationAxis().x;
191  ny = this->getRotationAxis().y;
192  nz = this->getRotationAxis().z;
193
194  ca = cos (this->getAngle());
195  sa = sin (this->getAngle());
196
197  m[0][0] = nx * nx * (1 - ca) + ca;
198  m[0][1] = nx * ny * (1 - ca) + nz * sa;
199  m[0][2] = nx * nz * (1 - ca) - ny * sa;
200  m[1][0] = nx * nz * (1 - ca) - nz * sa;
201  m[1][1] = ny * ny * (1 - ca) + ca;
202  m[1][2] = ny * nz * (1 - ca) + nx * sa;
203  m[2][0] = nx * nz * (1 - ca) + ny * sa;
204  m[2][1] = ny * nz * (1 - ca) - nx * sa;
205  m[2][2] = nz * nz * (1 - ca) + ca;
206
207  float x, y, z;
208  for (int i = 0; i < this->getFragments(); i++){
209    x = m[0][0] * this->launcher[i].x + m[0][1] * this->launcher[i].y + m[0][2] * this->launcher[i].z;
210    y = m[1][0] * this->launcher[i].x + m[1][1] * this->launcher[i].y + m[1][2] * this->launcher[i].z;
211    z = m[2][0] * this->launcher[i].x + m[2][1] * this->launcher[i].y + m[2][2] * this->launcher[i].z;
212
213    this->launcher[i] = Vector (x, y, z);
214  }
215
216  for( int i = 0; i < 3 ; i++)
217    delete m[i];
218  delete m;
219}
220
221
222/**
223 *  signal tick, time dependent things will be handled here
224 * @param dt time since last tick
225*/
226void SpikeBall::tick (float dt)
227{
228  Vector v = this->velocity * dt;
229  this->shiftCoor(v);
230
231  if (this->tickLifeCycle(dt)){
232    this->blow();
233    this->deactivate();
234  }
235
236  this->updateAngle( dt );
237}
238
239/**
240 *  the function gets called, when the projectile is destroyed
241*/
242void SpikeBall::destroy (WorldEntity* killer)
243{
244  ProjectileWeapon::destroy( killer );
245  PRINTF(5)("DESTROY SpikeBall\n");
246  this->lifeCycle = .95; //!< @todo calculate this usefully.
247
248  this->emitter->setSystem(SpikeBall::explosionParticles);
249}
250
251
252void SpikeBall::draw () const
253{
254  glPushAttrib(GL_ENABLE_BIT);
255  glMatrixMode(GL_MODELVIEW);
256  glPushMatrix();
257
258  float matrix[4][4];
259  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
260
261  glRotatef(angle, this->getRotationAxis().x, this->getRotationAxis().y, this->getRotationAxis().z);
262  this->getAbsDir().matrix (matrix);
263  glMultMatrixf((float*)matrix);
264  this->getModel()->draw();
265
266  this->halo->draw();
267
268  glPopMatrix();
269  glPopAttrib();
270}
271
Note: See TracBrowser for help on using the repository browser.