| 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: Marc Schaerrer | 
|---|
| 13 | co-programmer: Benjamin Grauer | 
|---|
| 14 |  | 
|---|
| 15 | */ | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON | 
|---|
| 19 |  | 
|---|
| 20 | #include "mbolt.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "state.h" | 
|---|
| 23 | #include "model.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "world_entities/npcs/npc.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include "particles/dot_emitter.h" | 
|---|
| 28 | #include "particles/sprite_particles.h" | 
|---|
| 29 |  | 
|---|
| 30 | #include "space_ships/space_ship.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include <cassert> | 
|---|
| 33 | #include "debug.h" | 
|---|
| 34 |  | 
|---|
| 35 | #include "static_model.h" | 
|---|
| 36 |  | 
|---|
| 37 | #include "effects/trail.h" | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | ObjectListDefinition(MBolt); | 
|---|
| 42 | CREATE_FAST_FACTORY_STATIC(MBolt); | 
|---|
| 43 |  | 
|---|
| 44 | /** | 
|---|
| 45 | *  standard constructor | 
|---|
| 46 | */ | 
|---|
| 47 | MBolt::MBolt () : Projectile() | 
|---|
| 48 | { | 
|---|
| 49 | this->registerObject(this, MBolt::_objectList); | 
|---|
| 50 | this->loadModel("models/projectiles/mbolt.obj",4); | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | //this->loadModel("models/projectiles/laser.obj"); | 
|---|
| 54 |  | 
|---|
| 55 | this->setMinEnergy(4); | 
|---|
| 56 | this->setHealthMax(0); | 
|---|
| 57 | this->lifeSpan = 1.5; | 
|---|
| 58 | this->angle     = 0; | 
|---|
| 59 |  | 
|---|
| 60 | //this->emitter = new DotEmitter(1000, 0, 0); | 
|---|
| 61 | this->emitter = new DotEmitter(50, 0, 0); | 
|---|
| 62 | this->emitter->setParent(this); | 
|---|
| 63 | this->emitter->setSpread(M_PI,M_PI); | 
|---|
| 64 | this->emitter->setInheritSpeed(this->velocity.len()); | 
|---|
| 65 | this->emitter->setEmissionRate(500.0); | 
|---|
| 66 | this->emitter->setEmissionVelocity(this->velocity.len()); | 
|---|
| 67 |  | 
|---|
| 68 | this->mat = new Material("mBolt"); | 
|---|
| 69 | //this->mat->setBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 70 | this->mat->setBlendFunc(GL_SRC_ALPHA,GL_ONE); | 
|---|
| 71 | this->mat->setDiffuse(1,1,1); | 
|---|
| 72 | this->mat->setDiffuseMap("laser_add.png"); | 
|---|
| 73 | this->mat->setDiffuseMap("laser.png",1); | 
|---|
| 74 |  | 
|---|
| 75 | dynamic_cast<StaticModel*>(this->getModel())->addMaterial(*this->mat); | 
|---|
| 76 | dynamic_cast<StaticModel*>(this->getModel())->finalize(); | 
|---|
| 77 |  | 
|---|
| 78 | dynamic_cast<StaticModel*>(this->getModel())->rebuild(); | 
|---|
| 79 | //this->buildObbTree(4); | 
|---|
| 80 |  | 
|---|
| 81 | this->trail = new Trail(6, 4, .1, this); | 
|---|
| 82 | //this->trail->setParent( this); | 
|---|
| 83 | this->trail->setTexture( "textures/laser.png"); | 
|---|
| 84 | this->trail->setAbsCoor(this->getAbsCoor() - Vector(.7,0,0)); | 
|---|
| 85 |  | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 | *  standard deconstructor | 
|---|
| 91 | * | 
|---|
| 92 | */ | 
|---|
| 93 | MBolt::~MBolt () | 
|---|
| 94 | { | 
|---|
| 95 |  | 
|---|
| 96 | if (MBolt::explosionParticles != NULL && MBolt::objectList().size() <= 1) | 
|---|
| 97 | { | 
|---|
| 98 | if (ParticleSystem::objectList().exists(MBolt::explosionParticles)) | 
|---|
| 99 | delete MBolt::explosionParticles; | 
|---|
| 100 | MBolt::explosionParticles = NULL; | 
|---|
| 101 | PRINTF(1)("Deleting MBolt Explosion Particles\n"); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | delete this->emitter; | 
|---|
| 105 | delete this->trail; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | SpriteParticles* MBolt::explosionParticles = NULL; | 
|---|
| 109 |  | 
|---|
| 110 | void MBolt::activate() | 
|---|
| 111 | { | 
|---|
| 112 | this->origList = this->getOMListNumber(); | 
|---|
| 113 | this->toList(OM_ENVIRON); | 
|---|
| 114 | if (unlikely(MBolt::explosionParticles == NULL)) | 
|---|
| 115 | { | 
|---|
| 116 | MBolt::explosionParticles = new SpriteParticles(1000); | 
|---|
| 117 | MBolt::explosionParticles->setName("MBoltExplosionParticles"); | 
|---|
| 118 | MBolt::explosionParticles->setLifeSpan(.2, .1); | 
|---|
| 119 | MBolt::explosionParticles->setRadius(0.0, 10.0); | 
|---|
| 120 | MBolt::explosionParticles->setRadius(.5, 6.0); | 
|---|
| 121 | MBolt::explosionParticles->setRadius(1.0, 3.0); | 
|---|
| 122 | MBolt::explosionParticles->setColor(0.0, 1,1,0,.9); | 
|---|
| 123 | MBolt::explosionParticles->setColor(0.5, .8,.8,0,.5); | 
|---|
| 124 | MBolt::explosionParticles->setColor(1.0, .8,.8,.7,.0); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | this->setPhysDamage(10); | 
|---|
| 128 | this->setElecDamage(0); | 
|---|
| 129 | this->setHealth(0); | 
|---|
| 130 |  | 
|---|
| 131 | this->emitter->setSpread(0); | 
|---|
| 132 | this->emitter->setEmissionRate(10.0); | 
|---|
| 133 | this->emitter->setEmissionVelocity(50); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 |  | 
|---|
| 137 | void MBolt::deactivate() | 
|---|
| 138 | { | 
|---|
| 139 | assert (MBolt::explosionParticles != NULL); | 
|---|
| 140 | MBolt::explosionParticles->removeEmitter(this->emitter); | 
|---|
| 141 | this->lifeCycle = 0.0; | 
|---|
| 142 |  | 
|---|
| 143 | this->lifeCycle = 0.0; | 
|---|
| 144 | this->toList(OM_NULL); | 
|---|
| 145 | //this->toList(OM_DEAD); | 
|---|
| 146 | this->removeNode(); | 
|---|
| 147 | MBolt::fastFactory->kill(this); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | void MBolt::hit (float damage, WorldEntity* entity ) | 
|---|
| 151 | { | 
|---|
| 152 |  | 
|---|
| 153 | if (this->hitEntity != entity) | 
|---|
| 154 | this->destroy( entity ); | 
|---|
| 155 | this->hitEntity = entity; | 
|---|
| 156 | // dynamic_cast<SpaceShip*>(entity)->damage(this->getPhysDamage(),this->getElecDamage()); | 
|---|
| 157 | //this->destroy(this); | 
|---|
| 158 | this->deactivate(); | 
|---|
| 159 |  | 
|---|
| 160 | return; | 
|---|
| 161 |  | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | /** | 
|---|
| 165 | *  signal tick, time dependent things will be handled here | 
|---|
| 166 | * @param dt time since last tick | 
|---|
| 167 | */ | 
|---|
| 168 | void MBolt::tick (float dt) | 
|---|
| 169 | { | 
|---|
| 170 | //Vector v = *this->flightDirection * ( this->speed * time * 1000 + 0.1); | 
|---|
| 171 | Vector v = this->velocity * dt; | 
|---|
| 172 | this->shiftCoor(v); | 
|---|
| 173 |  | 
|---|
| 174 | if (this->tickLifeCycle(dt)) | 
|---|
| 175 | this->deactivate(); | 
|---|
| 176 |  | 
|---|
| 177 | this->angle += MBolt::rotationSpeed * dt; | 
|---|
| 178 | this->trail->tick(dt); | 
|---|
| 179 |  | 
|---|
| 180 | for( ObjectList<NPC>::const_iterator eIterator = NPC::objectList().begin(); eIterator !=NPC::objectList().end(); eIterator++) | 
|---|
| 181 | { | 
|---|
| 182 | if( ((*eIterator)->getOMListNumber() != (this->origList -1))  && ((*eIterator)->getAbsCoor() - this->getAbsCoor()).len() <= 8) | 
|---|
| 183 | { | 
|---|
| 184 | (*eIterator)->destroy(this); //hit (this->getDamage(),this); | 
|---|
| 185 | this->deactivate(); | 
|---|
| 186 | PRINTF(0)("MBolt destroyed\n"); | 
|---|
| 187 | } | 
|---|
| 188 | } | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | /** | 
|---|
| 192 | *  the function gets called, when the projectile is destroyed | 
|---|
| 193 | */ | 
|---|
| 194 | void MBolt::destroy (WorldEntity* killer) | 
|---|
| 195 | { | 
|---|
| 196 | //this->deactivate(); | 
|---|
| 197 | Projectile::destroy( killer ); | 
|---|
| 198 | PRINTF(5)("DESTROY MBolt\n"); | 
|---|
| 199 | this->lifeCycle = .95; //!< @todo calculate this usefully. | 
|---|
| 200 |  | 
|---|
| 201 | this->emitter->setSystem(MBolt::explosionParticles); | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 |  | 
|---|
| 205 | void MBolt::draw () const | 
|---|
| 206 | { | 
|---|
| 207 | glPushAttrib(GL_ENABLE_BIT); | 
|---|
| 208 | glDisable(GL_LIGHTING); | 
|---|
| 209 |  | 
|---|
| 210 | glPushMatrix(); | 
|---|
| 211 | float matrix[4][4]; | 
|---|
| 212 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); | 
|---|
| 213 | glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile | 
|---|
| 214 | this->getAbsDir().matrix (matrix); | 
|---|
| 215 | glMultMatrixf((float*)matrix); | 
|---|
| 216 |  | 
|---|
| 217 | glScalef(0.75/4, 0.7/16, 0.7/16);  // no double rescale | 
|---|
| 218 |  | 
|---|
| 219 | this->mat->select(); | 
|---|
| 220 | dynamic_cast<StaticModel*>(this->getModel())->draw(); | 
|---|
| 221 | this->mat->unselect(); | 
|---|
| 222 | glScalef(4/.75,16/.7,16/.7); | 
|---|
| 223 | glTranslatef(-3,0,0); | 
|---|
| 224 | this->trail->draw(); | 
|---|
| 225 | glPopMatrix(); | 
|---|
| 226 | glPopAttrib(); | 
|---|
| 227 |  | 
|---|
| 228 | } | 
|---|