[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 | |
---|
[7065] | 23 | #include "resource_manager.h" |
---|
[6973] | 24 | #include "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; |
---|
[6282] | 35 | /* if(!PowerUp::sphereModel) {*/ |
---|
| 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; |
---|
[6113] | 49 | } |
---|
[2077] | 50 | |
---|
[6113] | 51 | PowerUp::~PowerUp () |
---|
[4597] | 52 | { |
---|
[6113] | 53 | delete this->sphereMaterial; |
---|
[7066] | 54 | if (this->pickupBuffer != NULL) |
---|
| 55 | ResourceManager::getInstance()->unload(this->pickupBuffer); |
---|
| 56 | if (this->respawnBuffer != NULL) |
---|
| 57 | ResourceManager::getInstance()->unload(this->respawnBuffer); |
---|
[4597] | 58 | } |
---|
[2077] | 59 | |
---|
| 60 | |
---|
[6113] | 61 | void PowerUp::loadParams(const TiXmlElement* root) |
---|
| 62 | { |
---|
[6512] | 63 | WorldEntity::loadParams(root); |
---|
[7065] | 64 | |
---|
[6973] | 65 | LoadParam(root, "respawnType", this, PowerUp, setRespawnType); |
---|
[7065] | 66 | |
---|
[6973] | 67 | LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime); |
---|
[7065] | 68 | |
---|
| 69 | LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound); |
---|
[7066] | 70 | |
---|
| 71 | LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound); |
---|
[6113] | 72 | } |
---|
[2077] | 73 | |
---|
| 74 | |
---|
[7065] | 75 | void PowerUp::loadPickupSound(const char* pickupSound) |
---|
| 76 | { |
---|
[7066] | 77 | if (this->pickupBuffer != NULL) |
---|
| 78 | ResourceManager::getInstance()->unload(this->pickupBuffer); |
---|
[7065] | 79 | |
---|
| 80 | else if (pickupSound != NULL) |
---|
| 81 | { |
---|
[7066] | 82 | this->pickupBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(pickupSound, WAV); |
---|
| 83 | if (this->pickupBuffer != NULL) |
---|
[7065] | 84 | { |
---|
| 85 | PRINTF(4)("Loaded sound %s to Pickup: %s.\n", pickupSound, this->getName()); |
---|
| 86 | } |
---|
| 87 | else |
---|
| 88 | { |
---|
| 89 | PRINTF(2)("Failed to load sound %s to pickup %s.\n.", pickupSound, this->getName()); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | else |
---|
[7066] | 93 | this->pickupBuffer = NULL; |
---|
[7065] | 94 | } |
---|
| 95 | |
---|
[7066] | 96 | void PowerUp::loadRespawnSound(const char* respawnSound) |
---|
| 97 | { |
---|
| 98 | if (this->respawnBuffer != NULL) |
---|
| 99 | ResourceManager::getInstance()->unload(this->respawnBuffer); |
---|
| 100 | |
---|
| 101 | else if (respawnSound != NULL) |
---|
| 102 | { |
---|
| 103 | this->respawnBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV); |
---|
| 104 | if (this->respawnBuffer != NULL) |
---|
| 105 | { |
---|
| 106 | PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound, this->getName()); |
---|
| 107 | } |
---|
| 108 | else |
---|
| 109 | { |
---|
| 110 | PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound, this->getName()); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | this->respawnBuffer = NULL; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
[6113] | 118 | void PowerUp::collidesWith (WorldEntity* entity, const Vector& location) |
---|
| 119 | { |
---|
[6424] | 120 | if(entity->isA(CL_EXTENDABLE)) |
---|
[6113] | 121 | { |
---|
| 122 | if(dynamic_cast<Extendable*>(entity)->pickup(this)) |
---|
| 123 | { |
---|
[7066] | 124 | if(pickupBuffer != NULL) |
---|
| 125 | this->soundSource.play(this->pickupBuffer); |
---|
[7065] | 126 | |
---|
[6973] | 127 | switch(respawnType) { |
---|
| 128 | case RESPAWN_NONE: |
---|
| 129 | this->toList(OM_DEAD); |
---|
| 130 | break; |
---|
| 131 | case RESPAWN_TIME: |
---|
| 132 | this->toList(OM_DEAD_TICK); |
---|
| 133 | this->respawnTime = this->respawnStart; |
---|
| 134 | break; |
---|
| 135 | } |
---|
[6113] | 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
[2077] | 139 | |
---|
[6589] | 140 | void PowerUp::tick(float dt) { |
---|
| 141 | if(this->getOMListNumber() != OM_COMMON) { |
---|
| 142 | this->respawnTime -= dt; |
---|
[7066] | 143 | if(this->respawnTime <= 0) |
---|
| 144 | { |
---|
[6589] | 145 | this->toList(OM_COMMON); |
---|
[7066] | 146 | this->soundSource.play(this->respawnBuffer); |
---|
| 147 | |
---|
[6589] | 148 | } |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
[6113] | 152 | void PowerUp::draw() const |
---|
[5434] | 153 | { |
---|
[6589] | 154 | if(this->model != NULL) { |
---|
| 155 | glMatrixMode(GL_MODELVIEW); |
---|
| 156 | glPushMatrix(); |
---|
| 157 | glTranslatef (this->getAbsCoor ().x, |
---|
| 158 | this->getAbsCoor ().y, |
---|
| 159 | this->getAbsCoor ().z); |
---|
| 160 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 161 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 162 | this->model->draw(); |
---|
| 163 | glPopMatrix(); |
---|
| 164 | } |
---|
[6547] | 165 | this->sphereMaterial->select(); |
---|
| 166 | WorldEntity::draw(); |
---|
[5434] | 167 | } |
---|
| 168 | |
---|
[6113] | 169 | const char* PowerUp::respawnTypes[] = { |
---|
| 170 | "none", |
---|
| 171 | "time" |
---|
| 172 | }; |
---|
| 173 | |
---|
| 174 | void PowerUp::setRespawnType(const char* type) |
---|
| 175 | { |
---|
| 176 | for(int i = 0; i < RESPAWN_size; ++i) { |
---|
[6243] | 177 | if(!strcmp(type, respawnTypes[i])) { |
---|
[6113] | 178 | this->respawnType = (PowerUpRespawn)i; |
---|
| 179 | break; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
[6973] | 184 | void PowerUp::setRespawnTime(const float respawnTime) |
---|
| 185 | { |
---|
| 186 | this->respawnStart = respawnTime; |
---|
| 187 | } |
---|
[6498] | 188 | |
---|
| 189 | |
---|
| 190 | /******************************************************************************************** |
---|
| 191 | NETWORK STUFF |
---|
| 192 | ********************************************************************************************/ |
---|
| 193 | |
---|
| 194 | |
---|
[6424] | 195 | /** |
---|
| 196 | * data copied in data will bee sent to another host |
---|
| 197 | * @param data pointer to data |
---|
| 198 | * @param maxLength max length of data |
---|
| 199 | * @return the number of bytes writen |
---|
| 200 | */ |
---|
| 201 | int PowerUp::readState( byte * data, int maxLength ) |
---|
| 202 | { |
---|
| 203 | SYNCHELP_WRITE_BEGIN(); |
---|
[6815] | 204 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_PU_WE_STATE ); |
---|
[6424] | 205 | return SYNCHELP_WRITE_N; |
---|
| 206 | } |
---|
| 207 | |
---|
[6498] | 208 | |
---|
[6424] | 209 | /** |
---|
| 210 | * Writes data from network containing information about the state |
---|
| 211 | * @param data pointer to data |
---|
| 212 | * @param length length of data |
---|
| 213 | * @param sender hostID of sender |
---|
| 214 | */ |
---|
| 215 | int PowerUp::writeState( const byte * data, int length, int sender ) |
---|
| 216 | { |
---|
| 217 | SYNCHELP_READ_BEGIN(); |
---|
[6815] | 218 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_PU_WE_STATE ); |
---|
[6424] | 219 | return SYNCHELP_READ_N; |
---|
| 220 | } |
---|
| 221 | |
---|