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