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