| 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: Manuel Leuenberger | 
|---|
| 13 |    co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY | 
|---|
| 17 |  | 
|---|
| 18 |  | 
|---|
| 19 | #include "power_up.h" | 
|---|
| 20 | #include "extendable.h" | 
|---|
| 21 | #include "primitive_model.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include "util/loading/resource_manager.h" | 
|---|
| 24 | #include "util/loading/load_param.h" | 
|---|
| 25 |  | 
|---|
| 26 | using namespace std; | 
|---|
| 27 |  | 
|---|
| 28 | PowerUp::PowerUp(float r, float g, float b) | 
|---|
| 29 | { | 
|---|
| 30 |   this->setClassID(CL_POWER_UP, "PowerUp"); | 
|---|
| 31 |  | 
|---|
| 32 |   this->respawnType = RESPAWN_TIME; | 
|---|
| 33 |   this->respawnStart = 10; | 
|---|
| 34 |   this->model = NULL; | 
|---|
| 35 |   /*  if(!PowerUp::sphereModel) {*/ | 
|---|
| 36 |  | 
|---|
| 37 |   Model* sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5); | 
|---|
| 38 |  | 
|---|
| 39 |   this->setModel(sphereModel); | 
|---|
| 40 |   this->buildObbTree( 4); | 
|---|
| 41 |   this->sphereMaterial = new Material; | 
|---|
| 42 |   this->sphereMaterial->setTransparency(.8); | 
|---|
| 43 |   this->sphereMaterial->setDiffuse(r, g, b); | 
|---|
| 44 |   this->toList(OM_COMMON); | 
|---|
| 45 |  | 
|---|
| 46 |   this->soundSource.setSourceNode(this); | 
|---|
| 47 |   this->pickupBuffer = NULL; | 
|---|
| 48 |   this->respawnBuffer = NULL; | 
|---|
| 49 |  | 
|---|
| 50 |   this->collider = NULL; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | PowerUp::~PowerUp () | 
|---|
| 54 | { | 
|---|
| 55 |   delete this->sphereMaterial; | 
|---|
| 56 |   if (this->pickupBuffer != NULL) | 
|---|
| 57 |     ResourceManager::getInstance()->unload(this->pickupBuffer); | 
|---|
| 58 |   if (this->respawnBuffer != NULL) | 
|---|
| 59 |     ResourceManager::getInstance()->unload(this->respawnBuffer); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | void PowerUp::loadParams(const TiXmlElement* root) | 
|---|
| 64 | { | 
|---|
| 65 |   WorldEntity::loadParams(root); | 
|---|
| 66 |  | 
|---|
| 67 |   LoadParam(root, "respawnType", this, PowerUp, setRespawnType); | 
|---|
| 68 |  | 
|---|
| 69 |   LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime); | 
|---|
| 70 |  | 
|---|
| 71 |   LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound); | 
|---|
| 72 |  | 
|---|
| 73 |   LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | void PowerUp::loadPickupSound(const std::string& pickupSound) | 
|---|
| 78 | { | 
|---|
| 79 |   if (this->pickupBuffer != NULL) | 
|---|
| 80 |     ResourceManager::getInstance()->unload(this->pickupBuffer); | 
|---|
| 81 |  | 
|---|
| 82 |   else if (!pickupSound.empty()) | 
|---|
| 83 |   { | 
|---|
| 84 |     this->pickupBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(pickupSound, WAV); | 
|---|
| 85 |     if (this->pickupBuffer != NULL) | 
|---|
| 86 |     { | 
|---|
| 87 |       PRINTF(4)("Loaded sound %s to Pickup: %s.\n", pickupSound.c_str(), this->getName()); | 
|---|
| 88 |     } | 
|---|
| 89 |     else | 
|---|
| 90 |     { | 
|---|
| 91 |       PRINTF(2)("Failed to load sound %s to pickup %s.\n.", pickupSound.c_str(), this->getName()); | 
|---|
| 92 |     } | 
|---|
| 93 |   } | 
|---|
| 94 |   else | 
|---|
| 95 |     this->pickupBuffer = NULL; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void PowerUp::loadRespawnSound(const std::string& respawnSound) | 
|---|
| 99 | { | 
|---|
| 100 |   if (this->respawnBuffer != NULL) | 
|---|
| 101 |     ResourceManager::getInstance()->unload(this->respawnBuffer); | 
|---|
| 102 |  | 
|---|
| 103 |   else if (!respawnSound.empty()) | 
|---|
| 104 |   { | 
|---|
| 105 |     this->respawnBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV); | 
|---|
| 106 |     if (this->respawnBuffer != NULL) | 
|---|
| 107 |     { | 
|---|
| 108 |       PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound.c_str(), this->getName()); | 
|---|
| 109 |     } | 
|---|
| 110 |     else | 
|---|
| 111 |     { | 
|---|
| 112 |       PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound.c_str(), this->getName()); | 
|---|
| 113 |     } | 
|---|
| 114 |   } | 
|---|
| 115 |   else | 
|---|
| 116 |     this->respawnBuffer = NULL; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 |  | 
|---|
| 120 | void PowerUp::collidesWith (WorldEntity* entity, const Vector& location) | 
|---|
| 121 | { | 
|---|
| 122 |   if(this->collider != entity && entity->isA(CL_EXTENDABLE)) | 
|---|
| 123 |   { | 
|---|
| 124 |     this->collider = entity; | 
|---|
| 125 |     if(dynamic_cast<Extendable*>(entity)->pickup(this)) | 
|---|
| 126 |     { | 
|---|
| 127 |       if(pickupBuffer != NULL) | 
|---|
| 128 |         this->soundSource.play(this->pickupBuffer); | 
|---|
| 129 |  | 
|---|
| 130 |       switch(respawnType) | 
|---|
| 131 |       { | 
|---|
| 132 |       case RESPAWN_NONE: | 
|---|
| 133 |         this->toList(OM_DEAD); | 
|---|
| 134 |         break; | 
|---|
| 135 |       case RESPAWN_TIME: | 
|---|
| 136 |         this->toList(OM_DEAD_TICK); | 
|---|
| 137 |         this->respawnTime = this->respawnStart; | 
|---|
| 138 |         break; | 
|---|
| 139 |       } | 
|---|
| 140 |     } | 
|---|
| 141 |   } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | void PowerUp::tick(float dt) | 
|---|
| 145 | { | 
|---|
| 146 |   if(this->getOMListNumber() != OM_COMMON) | 
|---|
| 147 |   { | 
|---|
| 148 |     this->respawnTime -= dt; | 
|---|
| 149 |     if(this->respawnTime <= 0) | 
|---|
| 150 |     { | 
|---|
| 151 |       this->toList(OM_COMMON); | 
|---|
| 152 |       this->collider = NULL; | 
|---|
| 153 |       if (likely(this->respawnBuffer != NULL)) | 
|---|
| 154 |         this->soundSource.play(this->respawnBuffer); | 
|---|
| 155 |  | 
|---|
| 156 |     } | 
|---|
| 157 |   } | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | void PowerUp::draw() const | 
|---|
| 161 | { | 
|---|
| 162 |   if(this->model != NULL) | 
|---|
| 163 |   { | 
|---|
| 164 |     glMatrixMode(GL_MODELVIEW); | 
|---|
| 165 |     glPushMatrix(); | 
|---|
| 166 |     glTranslatef (this->getAbsCoor ().x, | 
|---|
| 167 |                   this->getAbsCoor ().y, | 
|---|
| 168 |                   this->getAbsCoor ().z); | 
|---|
| 169 |     Vector tmpRot = this->getAbsDir().getSpacialAxis(); | 
|---|
| 170 |     glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); | 
|---|
| 171 |     this->model->draw(); | 
|---|
| 172 |     glPopMatrix(); | 
|---|
| 173 |   } | 
|---|
| 174 |   this->sphereMaterial->select(); | 
|---|
| 175 |   WorldEntity::draw(); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | const char* PowerUp::respawnTypes[] = | 
|---|
| 179 |   { | 
|---|
| 180 |     "none", | 
|---|
| 181 |     "time" | 
|---|
| 182 |   }; | 
|---|
| 183 |  | 
|---|
| 184 |  | 
|---|
| 185 | void PowerUp::setRespawnType(const std::string& type) | 
|---|
| 186 | { | 
|---|
| 187 |   for(int i = 0; i < RESPAWN_size; ++i) | 
|---|
| 188 |   { | 
|---|
| 189 |     if(type == respawnTypes[i]) | 
|---|
| 190 |     { | 
|---|
| 191 |       this->respawnType = (PowerUpRespawn)i; | 
|---|
| 192 |       break; | 
|---|
| 193 |     } | 
|---|
| 194 |   } | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | void PowerUp::setRespawnTime(const float respawnTime) | 
|---|
| 198 | { | 
|---|
| 199 |   this->respawnStart = respawnTime; | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 |  | 
|---|
| 203 | /******************************************************************************************** | 
|---|
| 204 |  NETWORK STUFF | 
|---|
| 205 |  ********************************************************************************************/ | 
|---|
| 206 |  | 
|---|
| 207 |  | 
|---|
| 208 | /** | 
|---|
| 209 |  * data copied in data will bee sent to another host | 
|---|
| 210 |  * @param data pointer to data | 
|---|
| 211 |  * @param maxLength max length of data | 
|---|
| 212 |  * @return the number of bytes writen | 
|---|
| 213 |  */ | 
|---|
| 214 | int PowerUp::readState( byte * data, int maxLength ) | 
|---|
| 215 | { | 
|---|
| 216 |   SYNCHELP_WRITE_BEGIN(); | 
|---|
| 217 |   SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_PU_WE_STATE ); | 
|---|
| 218 |   return SYNCHELP_WRITE_N; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 |  | 
|---|
| 222 | /** | 
|---|
| 223 |  * Writes data from network containing information about the state | 
|---|
| 224 |  * @param data pointer to data | 
|---|
| 225 |  * @param length length of data | 
|---|
| 226 |  * @param sender hostID of sender | 
|---|
| 227 |  */ | 
|---|
| 228 | int PowerUp::writeState( const byte * data, int length, int sender ) | 
|---|
| 229 | { | 
|---|
| 230 |   SYNCHELP_READ_BEGIN(); | 
|---|
| 231 |   SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_PU_WE_STATE ); | 
|---|
| 232 |   return SYNCHELP_READ_N; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|