| 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" |
|---|
| 35 | ObjectListDefinition(SpikeBall); |
|---|
| 36 | CREATE_FAST_FACTORY_STATIC(SpikeBall); |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * standard constructor |
|---|
| 40 | */ |
|---|
| 41 | SpikeBall::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 | */ |
|---|
| 94 | SpikeBall::~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 | |
|---|
| 109 | SpriteParticles* SpikeBall::explosionParticles = NULL; |
|---|
| 110 | |
|---|
| 111 | void 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 | |
|---|
| 155 | void 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 | |
|---|
| 167 | void 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 | |
|---|
| 178 | void SpikeBall::blow() |
|---|
| 179 | { |
|---|
| 180 | Spike* pj = NULL; |
|---|
| 181 | /* printf( "KA-" );*/ |
|---|
| 182 | for ( int i = 0; i < this->getFragments(); i++) |
|---|
| 183 | { |
|---|
| 184 | pj = new Spike(); |
|---|
| 185 | assert( pj ); |
|---|
| 186 | /* printf(" %i", i);*/ |
|---|
| 187 | pj->setParent(PNode::getNullParent()); |
|---|
| 188 | |
|---|
| 189 | pj->setVelocity(this->launcher[i].getNormalized() * 250.0); |
|---|
| 190 | |
|---|
| 191 | pj->setParent(PNode::getNullParent()); |
|---|
| 192 | pj->setAbsCoor(this->getAbsCoor() + this->launcher[i] * this->size); |
|---|
| 193 | Quaternion q; |
|---|
| 194 | pj->setAbsDir(q.lookAt(Vector(), this->launcher[i], VECTOR_RAND(1))); |
|---|
| 195 | |
|---|
| 196 | pj->toList(this->getOMListNumber()); |
|---|
| 197 | |
|---|
| 198 | /* |
|---|
| 199 | pj->setAbsCoor(this->getAbsCoor() + VECTOR_RAND(3)); |
|---|
| 200 | pj->setAbsDir(this->getAbsDir());*/ |
|---|
| 201 | pj->activate(); |
|---|
| 202 | } |
|---|
| 203 | /* printf( "BOOM\n" );*/ |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | void SpikeBall::updateFireDir(){ |
|---|
| 208 | |
|---|
| 209 | float** m = new float* [3]; |
|---|
| 210 | for( int i = 0; i < 3 ; i++) |
|---|
| 211 | m[i] = new float; |
|---|
| 212 | |
|---|
| 213 | float nx, ny, nz, ca, sa; |
|---|
| 214 | |
|---|
| 215 | nx = this->getRotationAxis().x; |
|---|
| 216 | ny = this->getRotationAxis().y; |
|---|
| 217 | nz = this->getRotationAxis().z; |
|---|
| 218 | |
|---|
| 219 | ca = cos (this->getAngle()); |
|---|
| 220 | sa = sin (this->getAngle()); |
|---|
| 221 | // final version below... easier to to cheat with the one above. |
|---|
| 222 | |
|---|
| 223 | m[0][0] = nx * nx * (1 - ca) + ca; |
|---|
| 224 | m[0][1] = nx * ny * (1 - ca) + nz * sa; |
|---|
| 225 | m[0][2] = nx * nz * (1 - ca) - ny * sa; |
|---|
| 226 | m[1][0] = nx * nz * (1 - ca) - nz * sa; |
|---|
| 227 | m[1][1] = ny * ny * (1 - ca) + ca; |
|---|
| 228 | m[1][2] = ny * nz * (1 - ca) + nx * sa; |
|---|
| 229 | m[2][0] = nx * nz * (1 - ca) + ny * sa; |
|---|
| 230 | m[2][1] = ny * nz * (1 - ca) - nx * sa; |
|---|
| 231 | m[2][2] = nz * nz * (1 - ca) + ca; |
|---|
| 232 | |
|---|
| 233 | float x, y, z; |
|---|
| 234 | for (int i = 0; i < this->getFragments(); i++){ |
|---|
| 235 | // printf("%i ", i); |
|---|
| 236 | x = m[0][0] * this->launcher[i].x + m[0][1] * this->launcher[i].y + m[0][2] * this->launcher[i].z; |
|---|
| 237 | y = m[1][0] * this->launcher[i].x + m[1][1] * this->launcher[i].y + m[1][2] * this->launcher[i].z; |
|---|
| 238 | z = m[2][0] * this->launcher[i].x + m[2][1] * this->launcher[i].y + m[2][2] * this->launcher[i].z; |
|---|
| 239 | |
|---|
| 240 | this->launcher[i] = Vector (x, y, z); |
|---|
| 241 | } |
|---|
| 242 | // printf("\n"); |
|---|
| 243 | |
|---|
| 244 | for( int i = 0; i < 3 ; i++) |
|---|
| 245 | delete m[i]; |
|---|
| 246 | delete m; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | /** |
|---|
| 251 | * signal tick, time dependent things will be handled here |
|---|
| 252 | * @param dt time since last tick |
|---|
| 253 | */ |
|---|
| 254 | void SpikeBall::tick (float dt) |
|---|
| 255 | { |
|---|
| 256 | //Vector v = *this->flightDirection * ( this->speed * time * 1000 + 0.1); |
|---|
| 257 | Vector v = this->getVelocity() * dt; |
|---|
| 258 | this->shiftCoor(v); |
|---|
| 259 | |
|---|
| 260 | // if(this->lifeCycle > .9){ |
|---|
| 261 | // /* printf("time to blow: ");*/ |
|---|
| 262 | // // this->weaponMan->fire(); |
|---|
| 263 | // /* this->blow();*/ |
|---|
| 264 | // } |
|---|
| 265 | |
|---|
| 266 | if (this->tickLifeCycle(dt)){ |
|---|
| 267 | this->blow(); |
|---|
| 268 | this->deactivate(); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | this->updateAngle( dt ); |
|---|
| 272 | // angle += rotationSpeed * dt; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | /** |
|---|
| 276 | * the function gets called, when the projectile is destroyed |
|---|
| 277 | */ |
|---|
| 278 | void SpikeBall::destroy (WorldEntity* killer) |
|---|
| 279 | { |
|---|
| 280 | ProjectileWeapon::destroy( killer ); |
|---|
| 281 | PRINTF(5)("DESTROY SpikeBall\n"); |
|---|
| 282 | this->lifeCycle = .95; //!< @todo calculate this usefully. |
|---|
| 283 | |
|---|
| 284 | this->emitter->setSystem(SpikeBall::explosionParticles); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | void SpikeBall::draw () const |
|---|
| 289 | { |
|---|
| 290 | glPushAttrib(GL_ENABLE_BIT); |
|---|
| 291 | //glDisable(GL_LIGHTING); |
|---|
| 292 | |
|---|
| 293 | glMatrixMode(GL_MODELVIEW); |
|---|
| 294 | glPushMatrix(); |
|---|
| 295 | |
|---|
| 296 | float matrix[4][4]; |
|---|
| 297 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); |
|---|
| 298 | |
|---|
| 299 | glRotatef(angle, this->getRotationAxis().x, this->getRotationAxis().y, this->getRotationAxis().z); |
|---|
| 300 | this->getAbsDir().matrix (matrix); |
|---|
| 301 | glMultMatrixf((float*)matrix); |
|---|
| 302 | this->getModel()->draw(); |
|---|
| 303 | |
|---|
| 304 | this->halo->draw(); |
|---|
| 305 | |
|---|
| 306 | glPopMatrix(); |
|---|
| 307 | |
|---|
| 308 | glPopAttrib(); |
|---|
| 309 | } |
|---|
| 310 | |
|---|