[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 | |
---|
[6884] | 129 | Pawn* pawn = this->carrierToPawnHelper(); |
---|
| 130 | if(pawn == NULL) |
---|
[7162] | 131 | this->Pickupable::destroy(); |
---|
[6884] | 132 | |
---|
[7547] | 133 | // If the pickup has transited to used. |
---|
[6884] | 134 | if(this->isUsed()) |
---|
| 135 | { |
---|
[7547] | 136 | // If its durationType is continuous, we set a Timer to be reminded, when the time has run out. |
---|
| 137 | if(this->isContinuous()) |
---|
[6884] | 138 | { |
---|
[7547] | 139 | if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f) |
---|
| 140 | { |
---|
| 141 | this->durationTimer_.unpauseTimer(); |
---|
| 142 | } |
---|
| 143 | else |
---|
| 144 | { |
---|
| 145 | this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&ShieldPickup::pickupTimerCallback, this))); |
---|
| 146 | } |
---|
[6884] | 147 | } |
---|
| 148 | pawn->setShieldAbsorption(this->getShieldAbsorption()); |
---|
| 149 | pawn->setShieldHealth(this->getShieldHealth()); |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | pawn->setShieldAbsorption(0.0f); |
---|
| 154 | this->setShieldHealth(pawn->getShieldHealth()); |
---|
| 155 | pawn->setShieldHealth(0.0f); |
---|
| 156 | |
---|
[7547] | 157 | // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded. |
---|
| 158 | if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration())) |
---|
[6884] | 159 | { |
---|
[7547] | 160 | this->Pickupable::destroy(); |
---|
[6884] | 161 | } |
---|
[7547] | 162 | // We pause the Timer if the pickup is continuous and the duration is not yet exceeded, |
---|
| 163 | else if(this->isContinuous() && this->durationTimer_.isActive()) |
---|
| 164 | { |
---|
| 165 | this->durationTimer_.pauseTimer(); |
---|
| 166 | } |
---|
[6884] | 167 | } |
---|
[6869] | 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | @brief |
---|
[7547] | 172 | Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. |
---|
| 173 | @return |
---|
| 174 | A pointer to the Pawn, or NULL if the conversion failed. |
---|
| 175 | */ |
---|
| 176 | Pawn* ShieldPickup::carrierToPawnHelper(void) |
---|
| 177 | { |
---|
| 178 | PickupCarrier* carrier = this->getCarrier(); |
---|
| 179 | Pawn* pawn = dynamic_cast<Pawn*>(carrier); |
---|
| 180 | |
---|
| 181 | if(pawn == NULL) |
---|
| 182 | { |
---|
[8811] | 183 | orxout(internal_error, context::pickups) << "Invalid PickupCarrier in ShieldPickup." << endl; |
---|
[7547] | 184 | } |
---|
| 185 | return pawn; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | @brief |
---|
[6869] | 190 | Creates a duplicate of the input OrxonoxClass. |
---|
| 191 | @param item |
---|
| 192 | A pointer to the Orxonox class. |
---|
| 193 | */ |
---|
| 194 | void ShieldPickup::clone(OrxonoxClass*& item) |
---|
| 195 | { |
---|
| 196 | if(item == NULL) |
---|
| 197 | item = new ShieldPickup(this); |
---|
| 198 | |
---|
| 199 | SUPER(ShieldPickup, clone, item); |
---|
| 200 | |
---|
| 201 | ShieldPickup* pickup = dynamic_cast<ShieldPickup*>(item); |
---|
| 202 | pickup->setDuration(this->getDuration()); |
---|
[6884] | 203 | pickup->setShieldAbsorption(this->getShieldAbsorption()); |
---|
| 204 | pickup->setShieldHealth(this->getShieldHealth()); |
---|
[6869] | 205 | pickup->initializeIdentifier(); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | /** |
---|
| 209 | @brief |
---|
[7547] | 210 | Sets the duration. |
---|
| 211 | @param duration |
---|
| 212 | The duration in seconds. |
---|
[6884] | 213 | */ |
---|
[7547] | 214 | void ShieldPickup::setDuration(float duration) |
---|
[6884] | 215 | { |
---|
[7547] | 216 | if(duration >= 0.0f) |
---|
[6884] | 217 | { |
---|
[7547] | 218 | this->duration_ = duration; |
---|
[6884] | 219 | } |
---|
| 220 | else |
---|
| 221 | { |
---|
[8811] | 222 | orxout(internal_error, context::pickups) << "Invalid duration in ShieldPickup." << endl; |
---|
[7547] | 223 | this->duration_ = 0.0f; |
---|
[6884] | 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | @brief |
---|
[7547] | 229 | Sets the health of the shield. |
---|
[6884] | 230 | @param shieldHealth |
---|
[7547] | 231 | The shieldHealth. |
---|
[6884] | 232 | */ |
---|
| 233 | void ShieldPickup::setShieldHealth(float shieldHealth) |
---|
| 234 | { |
---|
| 235 | if (shieldHealth>=0) |
---|
| 236 | { |
---|
| 237 | this->shieldHealth_=shieldHealth; |
---|
| 238 | } |
---|
| 239 | else |
---|
| 240 | { |
---|
[8811] | 241 | orxout(internal_error, context::pickups) << "Invalid Shieldhealth in ShieldPickup." << endl; |
---|
[6884] | 242 | this->shieldHealth_=0; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | /** |
---|
| 247 | @brief |
---|
[7547] | 248 | Sets the percentage the shield absorbs of the dealt damage. |
---|
| 249 | @param shieldAbsorption |
---|
| 250 | The shieldAbsorption. Has to be between 0 and 1. |
---|
[6869] | 251 | */ |
---|
[7547] | 252 | void ShieldPickup::setShieldAbsorption(float shieldAbsorption) |
---|
[6869] | 253 | { |
---|
[7547] | 254 | if (shieldAbsorption>=0 && shieldAbsorption<=1) |
---|
[6869] | 255 | { |
---|
[7547] | 256 | this->shieldAbsorption_=shieldAbsorption; |
---|
[6869] | 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
[8811] | 260 | orxout(internal_error, context::pickups) << "Invalid Absorption in ShieldPickup." << endl; |
---|
[7547] | 261 | this->shieldAbsorption_=0; |
---|
[6869] | 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
[7547] | 265 | /** |
---|
| 266 | @brief |
---|
| 267 | Helper method. Is called by the Timer as soon as it expires. |
---|
| 268 | */ |
---|
[6869] | 269 | void ShieldPickup::pickupTimerCallback(void) |
---|
[7127] | 270 | { |
---|
[6869] | 271 | this->setUsed(false); |
---|
| 272 | } |
---|
| 273 | } |
---|