Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

working spikeballs

File size: 7.5 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->speed = 150;
58/*
59  this->angle = 0;
60  this->rotationSpeed = 130;*/
61  this->setRotationSpeed(130);
62
63  this->halo = new Billboard();
64  this->halo->setSize(2, 2);
65  this->halo->setTexture("hbolt_halo.png");
66
67  this->setFragments(26);
68
69  this->size = 4;
70
71  this->launcher = new Vector [this->getFragments()];
72
73//   this->fastFactory = tFastFactory<Spike>::getFastFactory(CL_SPIKE, "Spike");
74
75/*
76  this->weaponMan = new WeaponManager(dynamic_cast<WorldEntity*>(this));
77  this->weaponMan->setParentEntity(this);
78  this->weaponMan->setSlotCount(1);
79  this->weaponMan->setSlotPosition(0, Vector(0, 0, 0));
80  this->weaponMan->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
81
82  Weapon* cannon = new SpikeLauncher();
83  cannon->setName( "SpikeLauncher");
84  this->weaponMan->addWeapon(cannon, 0, 0);
85  this->weaponMan->changeWeaponConfig(0);
86
87  this->weaponMan->getWeapon(0)->increaseEnergy(50);*/
88}
89
90
91/**
92 *  standard deconstructor
93*/
94SpikeBall::~SpikeBall ()
95{
96  // delete this->emitter;
97
98  /* this is normaly done by World.cc by deleting the ParticleEngine */
99  if (SpikeBall::explosionParticles != NULL && SpikeBall::objectList().size() <= 1)
100  {
101    //if (ClassList::exists(SpikeBall::explosionParticles, CL_PARTICLE_SYSTEM))
102    //  delete SpikeBall::explosionParticles;
103    PRINTF(1)("Deleting SpikeBall Particles\n");
104    SpikeBall::explosionParticles = NULL;
105  }
106
107}
108
109SpriteParticles* SpikeBall::explosionParticles = NULL;
110
111void SpikeBall::activate()
112{
113  if (unlikely(SpikeBall::explosionParticles == NULL))
114  {
115    SpikeBall::explosionParticles = new SpriteParticles(1000);
116    SpikeBall::explosionParticles->setName("BoltExplosionParticles");
117    SpikeBall::explosionParticles->setLifeSpan(.5, .3);
118    SpikeBall::explosionParticles->setRadius(0.0, 10.0);
119    SpikeBall::explosionParticles->setRadius(.5, 6.0);
120    SpikeBall::explosionParticles->setRadius(1.0, 3.0);
121    SpikeBall::explosionParticles->setColor(0.0, 1,1,0,.9);
122    SpikeBall::explosionParticles->setColor(0.5, .8,.8,0,.5);
123    SpikeBall::explosionParticles->setColor(1.0, .8,.8,.7,.0);
124  }
125  this->setDamage(5);
126  this->setHealth(10);
127  this->setRotationAxis(VECTOR_RAND(1));
128  this->setAngle();
129
130  this->launcher[0] = Vector(1.0, 0.0, 0.0);
131  this->launcher[1] = Vector(0.0, 1.0, 0.0);
132  this->launcher[2] = Vector(0.0, 0.0, 1.0);
133
134  this->launcher[3] = Vector(1.0, 1.0, 0.0);
135  this->launcher[4] = Vector(0.0, 1.0, 1.0);
136  this->launcher[5] = Vector(1.0, 0.0, 1.0);
137  this->launcher[6] = Vector(1.0, -1.0, 0.0);
138  this->launcher[7] = Vector(0.0, 1.0, -1.0);
139  this->launcher[8] = Vector(-1.0, 0.0, 1.0);
140
141  this->launcher[9] = Vector(-1.0, 1.0, 1.0);
142  this->launcher[10] = Vector(1.0, 1.0, 1.0);
143  this->launcher[11] = Vector(1.0, -1.0, 1.0);
144  this->launcher[12] = Vector(-1.0, -1.0, 1.0);
145
146  int tmp = this->getFragments() / 2;
147  for (int i = 0; i < tmp; i++)
148  {
149    this->launcher[i].normalize();
150    this->launcher[tmp + i] =  this->launcher[i] * (-1);
151  }
152}
153
154
155void SpikeBall::deactivate()
156{
157  assert (SpikeBall::explosionParticles != NULL);
158  SpikeBall::explosionParticles->removeEmitter(this->emitter);
159  this->lifeCycle = 0.0;
160
161  this->toList(OM_NULL);
162  this->removeNode();
163  SpikeBall::fastFactory->kill(this);
164}
165
166
167void SpikeBall::collidesWith(WorldEntity* entity, const Vector& location)
168{
169  PRINTF(0)("Collision with SpikeBall\n");
170  if (this->hitEntity != entity && entity->isA(CL_NPC))
171    this->destroy( entity );
172  this->hitEntity = entity;
173  dynamic_cast<SpaceShip*>(entity)->damage(this->getDamage(),0);
174//   this->deactivate();
175}
176
177
178void SpikeBall::blow()
179{
180  Spike* pj = NULL;
181
182  updateFireDir();
183
184  for ( int i = 0; i < this->getFragments(); i++)
185  {
186    pj  = new Spike();
187    assert( pj );
188    pj->setParent(PNode::getNullParent());
189
190    dynamic_cast<Spike*>(pj)->setVelocity(this->launcher[i].getNormalized() * 250.0);
191
192    pj->setParent(PNode::getNullParent());
193    pj->setAbsCoor(this->getAbsCoor() + this->launcher[i] * this->size);
194//     Quaternion q;
195//     pj->setAbsDir(q.lookAt(Vector(), this->launcher[i], VECTOR_RAND(1)));
196    pj->setRelDir(Quaternion(0, this->launcher[i]));
197
198    pj->toList(this->getOMListNumber());
199
200    pj->activate();
201  }
202}
203
204
205void SpikeBall::updateFireDir(){
206
207  float** m = new float* [3];
208  for( int i = 0; i < 3 ; i++)
209    m[i] = new float;
210
211  float nx, ny, nz, ca, sa;
212
213  nx = this->getRotationAxis().x;
214  ny = this->getRotationAxis().y;
215  nz = this->getRotationAxis().z;
216
217  ca = cos (this->getAngle());
218  sa = sin (this->getAngle());
219
220  m[0][0] = nx * nx * (1 - ca) + ca;
221  m[0][1] = nx * ny * (1 - ca) + nz * sa;
222  m[0][2] = nx * nz * (1 - ca) - ny * sa;
223  m[1][0] = nx * nz * (1 - ca) - nz * sa;
224  m[1][1] = ny * ny * (1 - ca) + ca;
225  m[1][2] = ny * nz * (1 - ca) + nx * sa;
226  m[2][0] = nx * nz * (1 - ca) + ny * sa;
227  m[2][1] = ny * nz * (1 - ca) - nx * sa;
228  m[2][2] = nz * nz * (1 - ca) + ca;
229
230  float x, y, z;
231  for (int i = 0; i < this->getFragments(); i++){
232    x = m[0][0] * this->launcher[i].x + m[0][1] * this->launcher[i].y + m[0][2] * this->launcher[i].z;
233    y = m[1][0] * this->launcher[i].x + m[1][1] * this->launcher[i].y + m[1][2] * this->launcher[i].z;
234    z = m[2][0] * this->launcher[i].x + m[2][1] * this->launcher[i].y + m[2][2] * this->launcher[i].z;
235
236    this->launcher[i] = Vector (x, y, z);
237  }
238
239  for( int i = 0; i < 3 ; i++)
240    delete m[i];
241  delete m;
242}
243
244
245/**
246 *  signal tick, time dependent things will be handled here
247 * @param dt time since last tick
248*/
249void SpikeBall::tick (float dt)
250{
251  Vector v = this->velocity * dt;
252  this->shiftCoor(v);
253
254
255  if (this->tickLifeCycle(dt)){
256    this->blow();
257    this->deactivate();
258  }
259
260  this->updateAngle( dt );
261}
262
263/**
264 *  the function gets called, when the projectile is destroyed
265*/
266void SpikeBall::destroy (WorldEntity* killer)
267{
268  ProjectileWeapon::destroy( killer );
269  PRINTF(5)("DESTROY SpikeBall\n");
270  this->lifeCycle = .95; //!< @todo calculate this usefully.
271
272  this->emitter->setSystem(SpikeBall::explosionParticles);
273}
274
275
276void SpikeBall::draw () const
277{
278  glPushAttrib(GL_ENABLE_BIT);
279  glMatrixMode(GL_MODELVIEW);
280  glPushMatrix();
281
282  float matrix[4][4];
283  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
284
285  this->halo->draw();
286
287  glRotatef(angle, this->getRotationAxis().x, this->getRotationAxis().y, this->getRotationAxis().z);
288  this->getAbsDir().matrix (matrix);
289  glMultMatrixf((float*)matrix);
290  this->getModel()->draw();
291
292  glPopMatrix();
293  glPopAttrib();
294}
295
Note: See TracBrowser for help on using the repository browser.