[6552] | 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: |
---|
[6574] | 23 | * Eric Beier |
---|
[6552] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
[6681] | 28 | |
---|
| 29 | /** |
---|
| 30 | @file SpeedPickup.cc |
---|
| 31 | @brief Implementation of the SpeedPickup class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "SpeedPickup.h" |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/XMLPort.h" |
---|
| 38 | #include "util/StringUtils.h" |
---|
| 39 | |
---|
| 40 | #include "worldentities/pawns/SpaceShip.h" |
---|
| 41 | #include "items/Engine.h" |
---|
| 42 | #include "pickup/PickupIdentifier.h" |
---|
| 43 | |
---|
| 44 | #include <sstream> |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | namespace orxonox |
---|
| 48 | { |
---|
| 49 | CreateFactory(SpeedPickup); |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | @brief |
---|
| 53 | Constructor. Registers the object and initializes the member variables. |
---|
| 54 | */ |
---|
| 55 | SpeedPickup::SpeedPickup(BaseObject* creator) : Pickup(creator) |
---|
| 56 | { |
---|
| 57 | RegisterObject(SpeedPickup); |
---|
| 58 | |
---|
| 59 | this->initialize(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | @brief |
---|
| 64 | Destructor. |
---|
| 65 | */ |
---|
| 66 | SpeedPickup::~SpeedPickup() |
---|
| 67 | { |
---|
| 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | @brief |
---|
| 73 | Initializes the member variables. |
---|
| 74 | */ |
---|
| 75 | void SpeedPickup::initialize(void) |
---|
| 76 | { |
---|
[6706] | 77 | this->duration_ = 0.0f; |
---|
| 78 | this->speedAdd_ = 0.0f; |
---|
| 79 | this->speedMultiply_ = 1.0f; |
---|
[6681] | 80 | |
---|
[6713] | 81 | this->addTarget(ClassIdentifier<Engine>::getIdentifier()); |
---|
[6681] | 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | @brief |
---|
| 86 | Initializes the PickupIdentifier of this pickup. |
---|
| 87 | */ |
---|
| 88 | void SpeedPickup::initializeIdentifier(void) |
---|
| 89 | { |
---|
| 90 | std::stringstream stream; |
---|
| 91 | stream << this->getDuration(); |
---|
| 92 | std::string type1 = "duration"; |
---|
| 93 | std::string val1 = stream.str(); |
---|
| 94 | this->pickupIdentifier_->addParameter(type1, val1); |
---|
| 95 | |
---|
| 96 | stream.clear(); |
---|
| 97 | stream << this->getSpeedAdd(); |
---|
| 98 | std::string type2 = "speedAdd"; |
---|
| 99 | std::string val2 = stream.str(); |
---|
| 100 | this->pickupIdentifier_->addParameter(type2, val2); |
---|
| 101 | |
---|
| 102 | stream.clear(); |
---|
| 103 | stream << this->getSpeedMultiply(); |
---|
| 104 | std::string type3 = "speedMultiply"; |
---|
| 105 | std::string val3 = stream.str(); |
---|
| 106 | this->pickupIdentifier_->addParameter(type3, val3); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | @brief |
---|
| 111 | Method for creating a SpeedPickup object through XML. |
---|
| 112 | */ |
---|
| 113 | void SpeedPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
| 114 | { |
---|
| 115 | SUPER(SpeedPickup, XMLPort, xmlelement, mode); |
---|
| 116 | |
---|
| 117 | XMLPortParam(SpeedPickup, "duration", setDuration, getDuration, xmlelement, mode); |
---|
| 118 | XMLPortParam(SpeedPickup, "speedAdd", setSpeedAdd, getSpeedAdd, xmlelement, mode); |
---|
| 119 | XMLPortParam(SpeedPickup, "speedMultiply", setSpeedMultiply, getSpeedMultiply, xmlelement, mode); |
---|
| 120 | |
---|
| 121 | this->initializeIdentifier(); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | @brief |
---|
| 126 | Is called when the pickup has transited from used to unused or the other way around. |
---|
| 127 | */ |
---|
| 128 | void SpeedPickup::changedUsed(void) |
---|
| 129 | { |
---|
| 130 | SUPER(SpeedPickup, changedUsed); |
---|
| 131 | |
---|
| 132 | //! If the pickup is not picked up nothing must be done. |
---|
| 133 | if(!this->isPickedUp()) |
---|
| 134 | return; |
---|
| 135 | |
---|
[6706] | 136 | Engine* engine = this->carrierToEngineHelper(); |
---|
| 137 | if(engine == NULL) //!< If the PickupCarrier is no Engine, then this pickup is useless and therefore is destroyed. |
---|
[7163] | 138 | this->Pickupable::destroy(); |
---|
| 139 | |
---|
[6681] | 140 | //! If the pickup has transited to used. |
---|
| 141 | if(this->isUsed()) |
---|
| 142 | { |
---|
[7208] | 143 | if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f) |
---|
[6728] | 144 | { |
---|
[7208] | 145 | this->durationTimer_.unpauseTimer(); |
---|
[6728] | 146 | } |
---|
| 147 | else |
---|
| 148 | { |
---|
[7208] | 149 | this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&SpeedPickup::pickupTimerCallback, this))); |
---|
[6728] | 150 | } |
---|
[6681] | 151 | engine->setSpeedAdd(this->getSpeedAdd()); |
---|
| 152 | engine->setSpeedMultiply(this->getSpeedMultiply()); |
---|
| 153 | } |
---|
[6706] | 154 | else |
---|
| 155 | { |
---|
| 156 | engine->setSpeedAdd(0.0f); |
---|
| 157 | engine->setSpeedMultiply(1.0f); |
---|
[7163] | 158 | |
---|
[6706] | 159 | if(this->isOnce()) |
---|
| 160 | { |
---|
[7208] | 161 | if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()) |
---|
[6728] | 162 | { |
---|
[7163] | 163 | this->Pickupable::destroy(); |
---|
[6728] | 164 | } |
---|
| 165 | else |
---|
| 166 | { |
---|
[7208] | 167 | this->durationTimer_.pauseTimer(); |
---|
[6728] | 168 | } |
---|
[6706] | 169 | } |
---|
| 170 | } |
---|
[6681] | 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | @brief |
---|
| 175 | Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. |
---|
| 176 | @return |
---|
| 177 | A pointer to the Pawn, or NULL if the conversion failed. |
---|
| 178 | */ |
---|
| 179 | Engine* SpeedPickup::carrierToEngineHelper(void) |
---|
| 180 | { |
---|
| 181 | PickupCarrier* carrier = this->getCarrier(); |
---|
[6713] | 182 | Engine* engine = dynamic_cast<Engine*>(carrier); |
---|
[6681] | 183 | |
---|
[6713] | 184 | if(engine == NULL) |
---|
[6681] | 185 | { |
---|
| 186 | COUT(1) << "Invalid PickupCarrier in SpeedPickup." << std::endl; |
---|
| 187 | } |
---|
[7163] | 188 | |
---|
[6713] | 189 | return engine; |
---|
[6681] | 190 | } |
---|
| 191 | |
---|
| 192 | /** |
---|
| 193 | @brief |
---|
| 194 | Creates a duplicate of the input OrxonoxClass. |
---|
| 195 | @param item |
---|
| 196 | A pointer to the Orxonox class. |
---|
| 197 | */ |
---|
| 198 | void SpeedPickup::clone(OrxonoxClass*& item) |
---|
| 199 | { |
---|
| 200 | if(item == NULL) |
---|
| 201 | item = new SpeedPickup(this); |
---|
| 202 | |
---|
| 203 | SUPER(SpeedPickup, clone, item); |
---|
| 204 | |
---|
| 205 | SpeedPickup* pickup = dynamic_cast<SpeedPickup*>(item); |
---|
| 206 | pickup->setDuration(this->getDuration()); |
---|
| 207 | pickup->setSpeedAdd(this->getSpeedAdd()); |
---|
| 208 | pickup->setSpeedMultiply(this->getSpeedMultiply()); |
---|
| 209 | |
---|
| 210 | pickup->initializeIdentifier(); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | @brief |
---|
| 215 | Sets the duration. |
---|
| 216 | @param duration |
---|
| 217 | The duration |
---|
| 218 | */ |
---|
| 219 | void SpeedPickup::setDuration(float duration) |
---|
| 220 | { |
---|
| 221 | if(duration >= 0.0f) |
---|
| 222 | { |
---|
| 223 | this->duration_ = duration; |
---|
| 224 | } |
---|
| 225 | else |
---|
| 226 | { |
---|
| 227 | COUT(1) << "Invalid duration in SpeedPickup." << std::endl; |
---|
[6694] | 228 | this->duration_ = 0.0f; |
---|
[6681] | 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | @brief |
---|
| 234 | Sets the SpeedAdd |
---|
| 235 | @param speedAdd |
---|
| 236 | The added Speed |
---|
| 237 | */ |
---|
| 238 | void SpeedPickup::setSpeedAdd(float speedAdd) |
---|
| 239 | { |
---|
| 240 | if(speedAdd >= 0.0f) |
---|
| 241 | { |
---|
| 242 | this->speedAdd_ = speedAdd; |
---|
| 243 | } |
---|
| 244 | else |
---|
| 245 | { |
---|
| 246 | COUT(1) << "Invalid speedAdd in SpeedPickup." << std::endl; |
---|
[6694] | 247 | this->speedAdd_ = 0.0f; |
---|
[6681] | 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | @brief |
---|
| 253 | Sets the SpeedMultiply |
---|
| 254 | @param speedAdd |
---|
| 255 | The multiplied Speed |
---|
| 256 | */ |
---|
| 257 | void SpeedPickup::setSpeedMultiply(float speedMultiply) |
---|
| 258 | { |
---|
| 259 | if(speedMultiply != 0) |
---|
| 260 | { |
---|
| 261 | this->speedMultiply_ = speedMultiply; |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | COUT(1) << "Invalid speedMultiply in SpeedPickup." << std::endl; |
---|
[6694] | 266 | this->speedMultiply_ = 1.0f; |
---|
[6681] | 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
[6706] | 270 | void SpeedPickup::pickupTimerCallback(void) |
---|
[7163] | 271 | { |
---|
[6706] | 272 | this->setUsed(false); |
---|
[6681] | 273 | } |
---|
| 274 | } |
---|