| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 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: Stefan Lienhard | 
|---|
| 13 |    co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #include "bomb.h" | 
|---|
| 17 | #include "glincl.h" | 
|---|
| 18 | #include "state.h" | 
|---|
| 19 | #include "model.h" | 
|---|
| 20 | #include "primitive_model.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "fast_factory.h" | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | #include "object_manager.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include "dot_emitter.h" | 
|---|
| 28 | #include "particle_system.h" | 
|---|
| 29 |  | 
|---|
| 30 | using namespace std; | 
|---|
| 31 |  | 
|---|
| 32 | CREATE_FAST_FACTORY_STATIC(Bomb, CL_BOMB); | 
|---|
| 33 |  | 
|---|
| 34 | /** | 
|---|
| 35 |  * constructs and loads a Bomb from a XML-element | 
|---|
| 36 |  * @param root the XML-element to load from | 
|---|
| 37 |  */ | 
|---|
| 38 | Bomb::Bomb(const TiXmlElement* root) | 
|---|
| 39 | { | 
|---|
| 40 |   this->init(); | 
|---|
| 41 |   if (root != NULL) | 
|---|
| 42 |     this->loadParams(root); | 
|---|
| 43 |  | 
|---|
| 44 |   float modelSize = 1.0; | 
|---|
| 45 |   this->loadModel("models/projectiles/RadioActiveBomb.obj", 1.0); | 
|---|
| 46 |  | 
|---|
| 47 |   this->setMinEnergy(1); | 
|---|
| 48 |   this->setHealthMax(10); | 
|---|
| 49 |  | 
|---|
| 50 |   this->lifeSpan = 15; | 
|---|
| 51 |  | 
|---|
| 52 |   this->emitter = new DotEmitter(100, 5, M_2_PI); | 
|---|
| 53 |   this->emitter->setParent(this); | 
|---|
| 54 |   this->emitter->setSpread(M_PI, M_PI); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 59 |  * standard deconstructor | 
|---|
| 60 |  */ | 
|---|
| 61 | Bomb::~Bomb () | 
|---|
| 62 | { | 
|---|
| 63 |   delete this->detonationSphere; | 
|---|
| 64 |   delete this->detonationMaterial; | 
|---|
| 65 |  | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | /** | 
|---|
| 70 |  * initializes the Bomb | 
|---|
| 71 |  * @todo change this to what you wish | 
|---|
| 72 |  */ | 
|---|
| 73 | void Bomb::init() | 
|---|
| 74 | { | 
|---|
| 75 |   this->setClassID(CL_BOMB, "Bomb"); | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 |   this->detonationSphere = new PrimitiveModel(PRIM_SPHERE); | 
|---|
| 79 |   this->detonationMaterial = new Material(); | 
|---|
| 80 |   this->detonationMaterial->setDiffuse(1, 0, 0); | 
|---|
| 81 |  | 
|---|
| 82 |   this->loadExplosionSound("sound/explosions/explosion_7_far.wav"); | 
|---|
| 83 |   //   this->detonationMaterial->setTransparency(.1); | 
|---|
| 84 |   /** | 
|---|
| 85 |    * @todo: Write CL_PROTO_WORLD_ENTITY INTO THE src/defs/class_id.h (your own definition) | 
|---|
| 86 |    */ | 
|---|
| 87 |  | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | /** | 
|---|
| 92 |  * loads a Bomb from a XML-element | 
|---|
| 93 |  * @param root the XML-element to load from | 
|---|
| 94 |  * @todo make the class Loadable | 
|---|
| 95 |  */ | 
|---|
| 96 | void Bomb::loadParams(const TiXmlElement* root) | 
|---|
| 97 | { | 
|---|
| 98 |   // all the clases this Entity is directly derived from must be called in this way, to load all settings. | 
|---|
| 99 |   Projectile::loadParams(root); | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 |   /** | 
|---|
| 103 |    * @todo: make the class Loadable | 
|---|
| 104 |    */ | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | /** | 
|---|
| 109 |  * advances the Bomb about time seconds | 
|---|
| 110 |  * @param time the Time to step | 
|---|
| 111 |  */ | 
|---|
| 112 | void Bomb::tick(float time) | 
|---|
| 113 | { | 
|---|
| 114 |   this->lifeCycle += time/this->lifeSpan; | 
|---|
| 115 |   if( this->lifeCycle >= 1.0) | 
|---|
| 116 |     { | 
|---|
| 117 |       PRINTF(5)("FINALIZE==========================\n"); | 
|---|
| 118 |       PRINTF(5)("current life cycle is: %f\n", this->lifeCycle); | 
|---|
| 119 |       PRINTF(5)("FINALIZE===========================\n"); | 
|---|
| 120 |  | 
|---|
| 121 |       this->deactivate(); | 
|---|
| 122 |     } | 
|---|
| 123 |   else if (this->lifeCycle > 0.9f) | 
|---|
| 124 |     this->detonate ((this->lifeCycle-.89) *1000.0); | 
|---|
| 125 |   else | 
|---|
| 126 |   { | 
|---|
| 127 |     Vector v = this->velocity * (time); | 
|---|
| 128 |     this->shiftCoor(v); | 
|---|
| 129 |   } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | /** | 
|---|
| 133 |  * draws this worldEntity | 
|---|
| 134 |  */ | 
|---|
| 135 | void Bomb::draw () const | 
|---|
| 136 | { | 
|---|
| 137 |   glMatrixMode(GL_MODELVIEW); | 
|---|
| 138 |   glPushMatrix(); | 
|---|
| 139 |   float matrix[4][4]; | 
|---|
| 140 |  | 
|---|
| 141 |   /* translate */ | 
|---|
| 142 |   glTranslatef (this->getAbsCoor ().x, | 
|---|
| 143 |                 this->getAbsCoor ().y, | 
|---|
| 144 |                 this->getAbsCoor ().z); | 
|---|
| 145 |   /* rotate */ | 
|---|
| 146 |   this->getAbsDir().matrix(matrix); | 
|---|
| 147 |   glMultMatrixf((float*)matrix); | 
|---|
| 148 |  | 
|---|
| 149 |   if (this->lifeCycle < .9) | 
|---|
| 150 |   { | 
|---|
| 151 |     if (this->getModel() != NULL) | 
|---|
| 152 |       this->getModel()->draw(); | 
|---|
| 153 |   } | 
|---|
| 154 |   else | 
|---|
| 155 |   { | 
|---|
| 156 |     glScalef((this->lifeCycle-.89) *1000.0, | 
|---|
| 157 |               (this->lifeCycle-.89) *1000.0, | 
|---|
| 158 |               (this->lifeCycle-.89) *1000.0); | 
|---|
| 159 |     this->detonationMaterial->select(); | 
|---|
| 160 |     this->detonationSphere->draw(); | 
|---|
| 161 |   } | 
|---|
| 162 |   glPopMatrix(); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 |  | 
|---|
| 166 | /** | 
|---|
| 167 |  * | 
|---|
| 168 |  * | 
|---|
| 169 |  */ | 
|---|
| 170 | void Bomb::collidesWith (WorldEntity* entity, const Vector& location) | 
|---|
| 171 | { | 
|---|
| 172 |   if (this->lifeCycle < .9f && entity->isA(CL_NPC)) | 
|---|
| 173 |     this->lifeCycle = 0.9f; | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | void Bomb::activate() | 
|---|
| 177 | { | 
|---|
| 178 |  | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | void Bomb::deactivate() | 
|---|
| 182 | { | 
|---|
| 183 |   this->toList(OM_DEAD); | 
|---|
| 184 |   this->lifeCycle = 0.0f; | 
|---|
| 185 |   Bomb::fastFactory->kill(this); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | void Bomb::detonate(float size) | 
|---|
| 189 | { | 
|---|
| 190 |   std::list<WorldEntity*>* detonationList = ObjectManager::distanceFromObject(*this, size, CL_NPC); | 
|---|
| 191 |   if (detonationList != NULL) | 
|---|
| 192 |   { | 
|---|
| 193 |     while( !detonationList->empty() ) | 
|---|
| 194 |     { | 
|---|
| 195 |       detonationList->front()->collidesWith(this, Vector(0,0,0)); | 
|---|
| 196 |       detonationList->pop_front(); | 
|---|
| 197 |     } | 
|---|
| 198 |     delete detonationList; | 
|---|
| 199 |   } | 
|---|
| 200 | } | 
|---|