[6869] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Eric Beier |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file ShieldPickup.cc |
---|
| 31 | @brief Implementation of the ShieldPickup class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "ShieldPickup.h" |
---|
| 35 | |
---|
[7547] | 36 | #include <sstream> |
---|
[6869] | 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/XMLPort.h" |
---|
| 39 | |
---|
| 40 | #include "pickup/PickupIdentifier.h" |
---|
[7547] | 41 | #include "worldentities/pawns/Pawn.h" |
---|
[6869] | 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
| 45 | CreateFactory(ShieldPickup); |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | @brief |
---|
| 49 | Constructor. Registers the object and initializes the member variables. |
---|
| 50 | */ |
---|
| 51 | ShieldPickup::ShieldPickup(BaseObject* creator) : Pickup(creator) |
---|
| 52 | { |
---|
| 53 | RegisterObject(ShieldPickup); |
---|
| 54 | |
---|
| 55 | this->initialize(); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | @brief |
---|
| 60 | Destructor. |
---|
| 61 | */ |
---|
| 62 | ShieldPickup::~ShieldPickup() |
---|
| 63 | { |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | @brief |
---|
| 69 | Initializes the member variables. |
---|
| 70 | */ |
---|
| 71 | void ShieldPickup::initialize(void) |
---|
| 72 | { |
---|
| 73 | this->duration_ = 0.0f; |
---|
[6884] | 74 | this->shieldAbsorption_ = 0.0f; |
---|
| 75 | this->shieldHealth_ = 0.0f; |
---|
[6869] | 76 | |
---|
[6887] | 77 | this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); |
---|
[6869] | 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | @brief |
---|
| 82 | Initializes the PickupIdentifier of this pickup. |
---|
| 83 | */ |
---|
| 84 | void ShieldPickup::initializeIdentifier(void) |
---|
| 85 | { |
---|
| 86 | std::stringstream stream; |
---|
| 87 | stream << this->getDuration(); |
---|
| 88 | std::string type1 = "duration"; |
---|
| 89 | std::string val1 = stream.str(); |
---|
| 90 | this->pickupIdentifier_->addParameter(type1, val1); |
---|
| 91 | |
---|
[6884] | 92 | stream.clear(); |
---|
| 93 | stream << this->getShieldHealth(); |
---|
| 94 | std::string type2 = "ShieldHealth"; |
---|
| 95 | std::string val2 = stream.str(); |
---|
| 96 | this->pickupIdentifier_->addParameter(type2, val2); |
---|
[7127] | 97 | |
---|
[6884] | 98 | stream.clear(); |
---|
| 99 | stream << this->getShieldAbsorption(); |
---|
| 100 | std::string type3 = "ShieldAbsorption"; |
---|
| 101 | std::string val3 = stream.str(); |
---|
| 102 | this->pickupIdentifier_->addParameter(type3, val3); |
---|
[6869] | 103 | |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | @brief |
---|
| 108 | Method for creating a ShieldPickup object through XML. |
---|
| 109 | */ |
---|
| 110 | void ShieldPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
| 111 | { |
---|
| 112 | SUPER(ShieldPickup, XMLPort, xmlelement, mode); |
---|
| 113 | |
---|
[6884] | 114 | XMLPortParam(ShieldPickup, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode); |
---|
| 115 | XMLPortParam(ShieldPickup, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode); |
---|
[7547] | 116 | XMLPortParam(ShieldPickup, "duration", setDuration, getDuration, xmlelement, mode); |
---|
[6869] | 117 | |
---|
| 118 | this->initializeIdentifier(); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | @brief |
---|
| 123 | Is called when the pickup has transited from used to unused or the other way around. |
---|
| 124 | */ |
---|
| 125 | void ShieldPickup::changedUsed(void) |
---|
| 126 | { |
---|
| 127 | SUPER(ShieldPickup, changedUsed); |
---|
| 128 | |
---|
[7547] | 129 | // If the pickup is not picked up nothing must be done. |
---|
[6869] | 130 | if(!this->isPickedUp()) |
---|
| 131 | return; |
---|
[6884] | 132 | |
---|
| 133 | Pawn* pawn = this->carrierToPawnHelper(); |
---|
| 134 | if(pawn == NULL) |
---|
[7162] | 135 | this->Pickupable::destroy(); |
---|
[6884] | 136 | |
---|
[7547] | 137 | // If the pickup has transited to used. |
---|
[6884] | 138 | if(this->isUsed()) |
---|
| 139 | { |
---|
[7547] | 140 | // If its durationType is continuous, we set a Timer to be reminded, when the time has run out. |
---|
| 141 | if(this->isContinuous()) |
---|
[6884] | 142 | { |
---|
[7547] | 143 | if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f) |
---|
| 144 | { |
---|
| 145 | this->durationTimer_.unpauseTimer(); |
---|
| 146 | } |
---|
| 147 | else |
---|
| 148 | { |
---|
| 149 | this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&ShieldPickup::pickupTimerCallback, this))); |
---|
| 150 | } |
---|
[6884] | 151 | } |
---|
| 152 | pawn->setShieldAbsorption(this->getShieldAbsorption()); |
---|
| 153 | pawn->setShieldHealth(this->getShieldHealth()); |
---|
| 154 | } |
---|
| 155 | else |
---|
| 156 | { |
---|
| 157 | pawn->setShieldAbsorption(0.0f); |
---|
| 158 | this->setShieldHealth(pawn->getShieldHealth()); |
---|
| 159 | pawn->setShieldHealth(0.0f); |
---|
| 160 | |
---|
[7547] | 161 | // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded. |
---|
| 162 | if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration())) |
---|
[6884] | 163 | { |
---|
[7547] | 164 | this->Pickupable::destroy(); |
---|
[6884] | 165 | } |
---|
[7547] | 166 | // We pause the Timer if the pickup is continuous and the duration is not yet exceeded, |
---|
| 167 | else if(this->isContinuous() && this->durationTimer_.isActive()) |
---|
| 168 | { |
---|
| 169 | this->durationTimer_.pauseTimer(); |
---|
| 170 | } |
---|
[6884] | 171 | } |
---|
[6869] | 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | @brief |
---|
[7547] | 176 | Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. |
---|
| 177 | @return |
---|
| 178 | A pointer to the Pawn, or NULL if the conversion failed. |
---|
| 179 | */ |
---|
| 180 | Pawn* ShieldPickup::carrierToPawnHelper(void) |
---|
| 181 | { |
---|
| 182 | PickupCarrier* carrier = this->getCarrier(); |
---|
| 183 | Pawn* pawn = dynamic_cast<Pawn*>(carrier); |
---|
| 184 | |
---|
| 185 | if(pawn == NULL) |
---|
| 186 | { |
---|
| 187 | COUT(1) << "Invalid PickupCarrier in ShieldPickup." << std::endl; |
---|
| 188 | } |
---|
| 189 | return pawn; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | /** |
---|
| 193 | @brief |
---|
[6869] | 194 | Creates a duplicate of the input OrxonoxClass. |
---|
| 195 | @param item |
---|
| 196 | A pointer to the Orxonox class. |
---|
| 197 | */ |
---|
| 198 | void ShieldPickup::clone(OrxonoxClass*& item) |
---|
| 199 | { |
---|
| 200 | if(item == NULL) |
---|
| 201 | item = new ShieldPickup(this); |
---|
| 202 | |
---|
| 203 | SUPER(ShieldPickup, clone, item); |
---|
| 204 | |
---|
| 205 | ShieldPickup* pickup = dynamic_cast<ShieldPickup*>(item); |
---|
| 206 | pickup->setDuration(this->getDuration()); |
---|
[6884] | 207 | pickup->setShieldAbsorption(this->getShieldAbsorption()); |
---|
| 208 | pickup->setShieldHealth(this->getShieldHealth()); |
---|
[6869] | 209 | pickup->initializeIdentifier(); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | @brief |
---|
[7547] | 214 | Sets the duration. |
---|
| 215 | @param duration |
---|
| 216 | The duration in seconds. |
---|
[6884] | 217 | */ |
---|
[7547] | 218 | void ShieldPickup::setDuration(float duration) |
---|
[6884] | 219 | { |
---|
[7547] | 220 | if(duration >= 0.0f) |
---|
[6884] | 221 | { |
---|
[7547] | 222 | this->duration_ = duration; |
---|
[6884] | 223 | } |
---|
| 224 | else |
---|
| 225 | { |
---|
[7547] | 226 | COUT(1) << "Invalid duration in ShieldPickup." << std::endl; |
---|
| 227 | this->duration_ = 0.0f; |
---|
[6884] | 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | @brief |
---|
[7547] | 233 | Sets the health of the shield. |
---|
[6884] | 234 | @param shieldHealth |
---|
[7547] | 235 | The shieldHealth. |
---|
[6884] | 236 | */ |
---|
| 237 | void ShieldPickup::setShieldHealth(float shieldHealth) |
---|
| 238 | { |
---|
| 239 | if (shieldHealth>=0) |
---|
| 240 | { |
---|
| 241 | this->shieldHealth_=shieldHealth; |
---|
| 242 | } |
---|
| 243 | else |
---|
| 244 | { |
---|
| 245 | COUT(1) << "Invalid Shieldhealth in ShieldPickup." << std::endl; |
---|
| 246 | this->shieldHealth_=0; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | @brief |
---|
[7547] | 252 | Sets the percentage the shield absorbs of the dealt damage. |
---|
| 253 | @param shieldAbsorption |
---|
| 254 | The shieldAbsorption. Has to be between 0 and 1. |
---|
[6869] | 255 | */ |
---|
[7547] | 256 | void ShieldPickup::setShieldAbsorption(float shieldAbsorption) |
---|
[6869] | 257 | { |
---|
[7547] | 258 | if (shieldAbsorption>=0 && shieldAbsorption<=1) |
---|
[6869] | 259 | { |
---|
[7547] | 260 | this->shieldAbsorption_=shieldAbsorption; |
---|
[6869] | 261 | } |
---|
| 262 | else |
---|
| 263 | { |
---|
[7547] | 264 | COUT(1) << "Invalid Absorption in ShieldPickup." << std::endl; |
---|
| 265 | this->shieldAbsorption_=0; |
---|
[6869] | 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
[7547] | 269 | /** |
---|
| 270 | @brief |
---|
| 271 | Helper method. Is called by the Timer as soon as it expires. |
---|
| 272 | */ |
---|
[6869] | 273 | void ShieldPickup::pickupTimerCallback(void) |
---|
[7127] | 274 | { |
---|
[6869] | 275 | this->setUsed(false); |
---|
| 276 | } |
---|
| 277 | } |
---|