[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 | |
---|
[9727] | 26 | #include "debug.h" |
---|
[2077] | 27 | |
---|
[9715] | 28 | ObjectListDefinition(PowerUp); |
---|
[9406] | 29 | |
---|
[6113] | 30 | PowerUp::PowerUp(float r, float g, float b) |
---|
| 31 | { |
---|
[9705] | 32 | this->registerObject(this, PowerUp::_objectList); |
---|
[6424] | 33 | |
---|
[6973] | 34 | this->respawnType = RESPAWN_TIME; |
---|
[6589] | 35 | this->respawnStart = 10; |
---|
| 36 | this->model = NULL; |
---|
[7221] | 37 | /* if(!PowerUp::sphereModel) {*/ |
---|
[6282] | 38 | |
---|
| 39 | Model* sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5); |
---|
| 40 | |
---|
| 41 | this->setModel(sphereModel); |
---|
| 42 | this->buildObbTree( 4); |
---|
[6113] | 43 | this->sphereMaterial = new Material; |
---|
[6973] | 44 | this->sphereMaterial->setTransparency(.8); |
---|
[6113] | 45 | this->sphereMaterial->setDiffuse(r, g, b); |
---|
[6150] | 46 | this->toList(OM_COMMON); |
---|
[7065] | 47 | |
---|
| 48 | this->soundSource.setSourceNode(this); |
---|
[7066] | 49 | this->pickupBuffer = NULL; |
---|
| 50 | this->respawnBuffer = NULL; |
---|
[7102] | 51 | |
---|
| 52 | this->collider = NULL; |
---|
[6113] | 53 | } |
---|
[2077] | 54 | |
---|
[6113] | 55 | PowerUp::~PowerUp () |
---|
[4597] | 56 | { |
---|
[6113] | 57 | delete this->sphereMaterial; |
---|
[7066] | 58 | if (this->pickupBuffer != NULL) |
---|
| 59 | ResourceManager::getInstance()->unload(this->pickupBuffer); |
---|
| 60 | if (this->respawnBuffer != NULL) |
---|
| 61 | ResourceManager::getInstance()->unload(this->respawnBuffer); |
---|
[4597] | 62 | } |
---|
[2077] | 63 | |
---|
| 64 | |
---|
[6113] | 65 | void PowerUp::loadParams(const TiXmlElement* root) |
---|
| 66 | { |
---|
[6512] | 67 | WorldEntity::loadParams(root); |
---|
[7065] | 68 | |
---|
[6973] | 69 | LoadParam(root, "respawnType", this, PowerUp, setRespawnType); |
---|
[7065] | 70 | |
---|
[6973] | 71 | LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime); |
---|
[7065] | 72 | |
---|
| 73 | LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound); |
---|
[7066] | 74 | |
---|
| 75 | LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound); |
---|
[6113] | 76 | } |
---|
[2077] | 77 | |
---|
| 78 | |
---|
[7221] | 79 | void PowerUp::loadPickupSound(const std::string& pickupSound) |
---|
[7065] | 80 | { |
---|
[7066] | 81 | if (this->pickupBuffer != NULL) |
---|
| 82 | ResourceManager::getInstance()->unload(this->pickupBuffer); |
---|
[7065] | 83 | |
---|
[7221] | 84 | else if (!pickupSound.empty()) |
---|
[7065] | 85 | { |
---|
[7460] | 86 | this->pickupBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(pickupSound, WAV); |
---|
[7221] | 87 | if (this->pickupBuffer != NULL) |
---|
| 88 | { |
---|
[9406] | 89 | PRINTF(4)("Loaded sound %s to Pickup: %s.\n", pickupSound.c_str(), this->getCName()); |
---|
[7221] | 90 | } |
---|
| 91 | else |
---|
| 92 | { |
---|
[9406] | 93 | PRINTF(2)("Failed to load sound %s to pickup %s.\n.", pickupSound.c_str(), this->getCName()); |
---|
[7221] | 94 | } |
---|
[7065] | 95 | } |
---|
| 96 | else |
---|
[7066] | 97 | this->pickupBuffer = NULL; |
---|
[7065] | 98 | } |
---|
| 99 | |
---|
[7221] | 100 | void PowerUp::loadRespawnSound(const std::string& respawnSound) |
---|
[7066] | 101 | { |
---|
| 102 | if (this->respawnBuffer != NULL) |
---|
| 103 | ResourceManager::getInstance()->unload(this->respawnBuffer); |
---|
| 104 | |
---|
[7221] | 105 | else if (!respawnSound.empty()) |
---|
[7066] | 106 | { |
---|
[7460] | 107 | this->respawnBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV); |
---|
[7066] | 108 | if (this->respawnBuffer != NULL) |
---|
| 109 | { |
---|
[9406] | 110 | PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound.c_str(), this->getCName()); |
---|
[7066] | 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
[9406] | 114 | PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound.c_str(), this->getCName()); |
---|
[7066] | 115 | } |
---|
| 116 | } |
---|
| 117 | else |
---|
| 118 | this->respawnBuffer = NULL; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
[6113] | 122 | void PowerUp::collidesWith (WorldEntity* entity, const Vector& location) |
---|
| 123 | { |
---|
[9705] | 124 | if(this->collider != entity && entity->isA(Extendable::classID())) |
---|
[6113] | 125 | { |
---|
[7102] | 126 | this->collider = entity; |
---|
[6113] | 127 | if(dynamic_cast<Extendable*>(entity)->pickup(this)) |
---|
| 128 | { |
---|
[7066] | 129 | if(pickupBuffer != NULL) |
---|
| 130 | this->soundSource.play(this->pickupBuffer); |
---|
[7065] | 131 | |
---|
[7221] | 132 | switch(respawnType) |
---|
| 133 | { |
---|
| 134 | case RESPAWN_NONE: |
---|
| 135 | this->toList(OM_DEAD); |
---|
| 136 | break; |
---|
| 137 | case RESPAWN_TIME: |
---|
| 138 | this->toList(OM_DEAD_TICK); |
---|
| 139 | this->respawnTime = this->respawnStart; |
---|
| 140 | break; |
---|
[8350] | 141 | default: |
---|
| 142 | /* NOT HANDLED */ |
---|
| 143 | break; |
---|
[6973] | 144 | } |
---|
[6113] | 145 | } |
---|
| 146 | } |
---|
| 147 | } |
---|
[2077] | 148 | |
---|
[7221] | 149 | void PowerUp::tick(float dt) |
---|
| 150 | { |
---|
| 151 | if(this->getOMListNumber() != OM_COMMON) |
---|
| 152 | { |
---|
[6589] | 153 | this->respawnTime -= dt; |
---|
[7066] | 154 | if(this->respawnTime <= 0) |
---|
| 155 | { |
---|
[6589] | 156 | this->toList(OM_COMMON); |
---|
[7102] | 157 | this->collider = NULL; |
---|
[7077] | 158 | if (likely(this->respawnBuffer != NULL)) |
---|
| 159 | this->soundSource.play(this->respawnBuffer); |
---|
[7066] | 160 | |
---|
[6589] | 161 | } |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
[6113] | 165 | void PowerUp::draw() const |
---|
[5434] | 166 | { |
---|
[7221] | 167 | if(this->model != NULL) |
---|
| 168 | { |
---|
[6589] | 169 | glMatrixMode(GL_MODELVIEW); |
---|
| 170 | glPushMatrix(); |
---|
| 171 | glTranslatef (this->getAbsCoor ().x, |
---|
| 172 | this->getAbsCoor ().y, |
---|
| 173 | this->getAbsCoor ().z); |
---|
| 174 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 175 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 176 | this->model->draw(); |
---|
| 177 | glPopMatrix(); |
---|
| 178 | } |
---|
[6547] | 179 | this->sphereMaterial->select(); |
---|
| 180 | WorldEntity::draw(); |
---|
[5434] | 181 | } |
---|
| 182 | |
---|
[7221] | 183 | const char* PowerUp::respawnTypes[] = |
---|
| 184 | { |
---|
| 185 | "none", |
---|
| 186 | "time" |
---|
| 187 | }; |
---|
[6113] | 188 | |
---|
[7221] | 189 | |
---|
| 190 | void PowerUp::setRespawnType(const std::string& type) |
---|
[6113] | 191 | { |
---|
[7221] | 192 | for(int i = 0; i < RESPAWN_size; ++i) |
---|
| 193 | { |
---|
| 194 | if(type == respawnTypes[i]) |
---|
| 195 | { |
---|
[6113] | 196 | this->respawnType = (PowerUpRespawn)i; |
---|
| 197 | break; |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[6973] | 202 | void PowerUp::setRespawnTime(const float respawnTime) |
---|
| 203 | { |
---|
| 204 | this->respawnStart = respawnTime; |
---|
| 205 | } |
---|
[6498] | 206 | |
---|
| 207 | |
---|