[4597] | 1 | /* |
---|
[2077] | 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: |
---|
[6113] | 12 | main-programmer: Manuel Leuenberger |
---|
[2077] | 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[5439] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
[2077] | 17 | |
---|
[5439] | 18 | |
---|
[2077] | 19 | #include "power_up.h" |
---|
[6113] | 20 | #include "extendable.h" |
---|
| 21 | #include "primitive_model.h" |
---|
[2077] | 22 | |
---|
[7193] | 23 | #include "util/loading/resource_manager.h" |
---|
| 24 | #include "util/loading/load_param.h" |
---|
[6815] | 25 | |
---|
[2077] | 26 | using namespace std; |
---|
| 27 | |
---|
[6113] | 28 | PowerUp::PowerUp(float r, float g, float b) |
---|
| 29 | { |
---|
[6424] | 30 | this->setClassID(CL_POWER_UP, "PowerUp"); |
---|
| 31 | |
---|
[6973] | 32 | this->respawnType = RESPAWN_TIME; |
---|
[6589] | 33 | this->respawnStart = 10; |
---|
| 34 | this->model = NULL; |
---|
[7221] | 35 | /* if(!PowerUp::sphereModel) {*/ |
---|
[6282] | 36 | |
---|
| 37 | Model* sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5); |
---|
| 38 | |
---|
| 39 | this->setModel(sphereModel); |
---|
| 40 | this->buildObbTree( 4); |
---|
[6113] | 41 | this->sphereMaterial = new Material; |
---|
[6973] | 42 | this->sphereMaterial->setTransparency(.8); |
---|
[6113] | 43 | this->sphereMaterial->setDiffuse(r, g, b); |
---|
[6150] | 44 | this->toList(OM_COMMON); |
---|
[7065] | 45 | |
---|
| 46 | this->soundSource.setSourceNode(this); |
---|
[7066] | 47 | this->pickupBuffer = NULL; |
---|
| 48 | this->respawnBuffer = NULL; |
---|
[7102] | 49 | |
---|
| 50 | this->collider = NULL; |
---|
[6113] | 51 | } |
---|
[2077] | 52 | |
---|
[6113] | 53 | PowerUp::~PowerUp () |
---|
[4597] | 54 | { |
---|
[6113] | 55 | delete this->sphereMaterial; |
---|
[7066] | 56 | if (this->pickupBuffer != NULL) |
---|
| 57 | ResourceManager::getInstance()->unload(this->pickupBuffer); |
---|
| 58 | if (this->respawnBuffer != NULL) |
---|
| 59 | ResourceManager::getInstance()->unload(this->respawnBuffer); |
---|
[4597] | 60 | } |
---|
[2077] | 61 | |
---|
| 62 | |
---|
[6113] | 63 | void PowerUp::loadParams(const TiXmlElement* root) |
---|
| 64 | { |
---|
[6512] | 65 | WorldEntity::loadParams(root); |
---|
[7065] | 66 | |
---|
[6973] | 67 | LoadParam(root, "respawnType", this, PowerUp, setRespawnType); |
---|
[7065] | 68 | |
---|
[6973] | 69 | LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime); |
---|
[7065] | 70 | |
---|
| 71 | LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound); |
---|
[7066] | 72 | |
---|
| 73 | LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound); |
---|
[6113] | 74 | } |
---|
[2077] | 75 | |
---|
| 76 | |
---|
[7221] | 77 | void PowerUp::loadPickupSound(const std::string& pickupSound) |
---|
[7065] | 78 | { |
---|
[7066] | 79 | if (this->pickupBuffer != NULL) |
---|
| 80 | ResourceManager::getInstance()->unload(this->pickupBuffer); |
---|
[7065] | 81 | |
---|
[7221] | 82 | else if (!pickupSound.empty()) |
---|
[7065] | 83 | { |
---|
[7221] | 84 | this->pickupBuffer = (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 | } |
---|
[7065] | 93 | } |
---|
| 94 | else |
---|
[7066] | 95 | this->pickupBuffer = NULL; |
---|
[7065] | 96 | } |
---|
| 97 | |
---|
[7221] | 98 | void PowerUp::loadRespawnSound(const std::string& respawnSound) |
---|
[7066] | 99 | { |
---|
| 100 | if (this->respawnBuffer != NULL) |
---|
| 101 | ResourceManager::getInstance()->unload(this->respawnBuffer); |
---|
| 102 | |
---|
[7221] | 103 | else if (!respawnSound.empty()) |
---|
[7066] | 104 | { |
---|
| 105 | this->respawnBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV); |
---|
| 106 | if (this->respawnBuffer != NULL) |
---|
| 107 | { |
---|
[7221] | 108 | PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound.c_str(), this->getName()); |
---|
[7066] | 109 | } |
---|
| 110 | else |
---|
| 111 | { |
---|
[7221] | 112 | PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound.c_str(), this->getName()); |
---|
[7066] | 113 | } |
---|
| 114 | } |
---|
| 115 | else |
---|
| 116 | this->respawnBuffer = NULL; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
[6113] | 120 | void PowerUp::collidesWith (WorldEntity* entity, const Vector& location) |
---|
| 121 | { |
---|
[7103] | 122 | if(this->collider != entity && entity->isA(CL_EXTENDABLE)) |
---|
[6113] | 123 | { |
---|
[7102] | 124 | this->collider = entity; |
---|
[6113] | 125 | if(dynamic_cast<Extendable*>(entity)->pickup(this)) |
---|
| 126 | { |
---|
[7066] | 127 | if(pickupBuffer != NULL) |
---|
| 128 | this->soundSource.play(this->pickupBuffer); |
---|
[7065] | 129 | |
---|
[7221] | 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; |
---|
[6973] | 139 | } |
---|
[6113] | 140 | } |
---|
| 141 | } |
---|
| 142 | } |
---|
[2077] | 143 | |
---|
[7221] | 144 | void PowerUp::tick(float dt) |
---|
| 145 | { |
---|
| 146 | if(this->getOMListNumber() != OM_COMMON) |
---|
| 147 | { |
---|
[6589] | 148 | this->respawnTime -= dt; |
---|
[7066] | 149 | if(this->respawnTime <= 0) |
---|
| 150 | { |
---|
[6589] | 151 | this->toList(OM_COMMON); |
---|
[7102] | 152 | this->collider = NULL; |
---|
[7077] | 153 | if (likely(this->respawnBuffer != NULL)) |
---|
| 154 | this->soundSource.play(this->respawnBuffer); |
---|
[7066] | 155 | |
---|
[6589] | 156 | } |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
[6113] | 160 | void PowerUp::draw() const |
---|
[5434] | 161 | { |
---|
[7221] | 162 | if(this->model != NULL) |
---|
| 163 | { |
---|
[6589] | 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 | } |
---|
[6547] | 174 | this->sphereMaterial->select(); |
---|
| 175 | WorldEntity::draw(); |
---|
[5434] | 176 | } |
---|
| 177 | |
---|
[7221] | 178 | const char* PowerUp::respawnTypes[] = |
---|
| 179 | { |
---|
| 180 | "none", |
---|
| 181 | "time" |
---|
| 182 | }; |
---|
[6113] | 183 | |
---|
[7221] | 184 | |
---|
| 185 | void PowerUp::setRespawnType(const std::string& type) |
---|
[6113] | 186 | { |
---|
[7221] | 187 | for(int i = 0; i < RESPAWN_size; ++i) |
---|
| 188 | { |
---|
| 189 | if(type == respawnTypes[i]) |
---|
| 190 | { |
---|
[6113] | 191 | this->respawnType = (PowerUpRespawn)i; |
---|
| 192 | break; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
[6973] | 197 | void PowerUp::setRespawnTime(const float respawnTime) |
---|
| 198 | { |
---|
| 199 | this->respawnStart = respawnTime; |
---|
| 200 | } |
---|
[6498] | 201 | |
---|
| 202 | |
---|
| 203 | /******************************************************************************************** |
---|
| 204 | NETWORK STUFF |
---|
| 205 | ********************************************************************************************/ |
---|
| 206 | |
---|
| 207 | |
---|
[6424] | 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(); |
---|
[6815] | 217 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_PU_WE_STATE ); |
---|
[6424] | 218 | return SYNCHELP_WRITE_N; |
---|
| 219 | } |
---|
| 220 | |
---|
[6498] | 221 | |
---|
[6424] | 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(); |
---|
[6815] | 231 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_PU_WE_STATE ); |
---|
[6424] | 232 | return SYNCHELP_READ_N; |
---|
| 233 | } |
---|
| 234 | |
---|