Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bump

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