| [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 |  | 
|---|
| [7547] | 36 | #include <sstream> | 
|---|
| [6681] | 37 | #include "core/CoreIncludes.h" | 
|---|
|  | 38 | #include "core/XMLPort.h" | 
|---|
|  | 39 |  | 
|---|
|  | 40 | #include "items/Engine.h" | 
|---|
|  | 41 | #include "pickup/PickupIdentifier.h" | 
|---|
| [7547] | 42 | #include "worldentities/pawns/SpaceShip.h" | 
|---|
| [6681] | 43 |  | 
|---|
|  | 44 | namespace orxonox | 
|---|
|  | 45 | { | 
|---|
|  | 46 | CreateFactory(SpeedPickup); | 
|---|
|  | 47 |  | 
|---|
|  | 48 | /** | 
|---|
|  | 49 | @brief | 
|---|
|  | 50 | Constructor. Registers the object and initializes the member variables. | 
|---|
|  | 51 | */ | 
|---|
|  | 52 | SpeedPickup::SpeedPickup(BaseObject* creator) : Pickup(creator) | 
|---|
|  | 53 | { | 
|---|
|  | 54 | RegisterObject(SpeedPickup); | 
|---|
|  | 55 |  | 
|---|
|  | 56 | this->initialize(); | 
|---|
|  | 57 | } | 
|---|
|  | 58 |  | 
|---|
|  | 59 | /** | 
|---|
|  | 60 | @brief | 
|---|
|  | 61 | Destructor. | 
|---|
|  | 62 | */ | 
|---|
|  | 63 | SpeedPickup::~SpeedPickup() | 
|---|
|  | 64 | { | 
|---|
|  | 65 |  | 
|---|
|  | 66 | } | 
|---|
|  | 67 |  | 
|---|
|  | 68 | /** | 
|---|
|  | 69 | @brief | 
|---|
|  | 70 | Initializes the member variables. | 
|---|
|  | 71 | */ | 
|---|
|  | 72 | void SpeedPickup::initialize(void) | 
|---|
|  | 73 | { | 
|---|
| [6706] | 74 | this->duration_ = 0.0f; | 
|---|
|  | 75 | this->speedAdd_ = 0.0f; | 
|---|
|  | 76 | this->speedMultiply_ = 1.0f; | 
|---|
| [6681] | 77 |  | 
|---|
| [6713] | 78 | this->addTarget(ClassIdentifier<Engine>::getIdentifier()); | 
|---|
| [6681] | 79 | } | 
|---|
|  | 80 |  | 
|---|
|  | 81 | /** | 
|---|
|  | 82 | @brief | 
|---|
|  | 83 | Initializes the PickupIdentifier of this pickup. | 
|---|
|  | 84 | */ | 
|---|
|  | 85 | void SpeedPickup::initializeIdentifier(void) | 
|---|
|  | 86 | { | 
|---|
|  | 87 | std::stringstream stream; | 
|---|
|  | 88 | stream << this->getDuration(); | 
|---|
|  | 89 | std::string type1 = "duration"; | 
|---|
|  | 90 | std::string val1 = stream.str(); | 
|---|
|  | 91 | this->pickupIdentifier_->addParameter(type1, val1); | 
|---|
|  | 92 |  | 
|---|
|  | 93 | stream.clear(); | 
|---|
|  | 94 | stream << this->getSpeedAdd(); | 
|---|
|  | 95 | std::string type2 = "speedAdd"; | 
|---|
|  | 96 | std::string val2 = stream.str(); | 
|---|
|  | 97 | this->pickupIdentifier_->addParameter(type2, val2); | 
|---|
|  | 98 |  | 
|---|
|  | 99 | stream.clear(); | 
|---|
|  | 100 | stream << this->getSpeedMultiply(); | 
|---|
|  | 101 | std::string type3 = "speedMultiply"; | 
|---|
|  | 102 | std::string val3 = stream.str(); | 
|---|
|  | 103 | this->pickupIdentifier_->addParameter(type3, val3); | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
|  | 106 | /** | 
|---|
|  | 107 | @brief | 
|---|
|  | 108 | Method for creating a SpeedPickup object through XML. | 
|---|
|  | 109 | */ | 
|---|
|  | 110 | void SpeedPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) | 
|---|
|  | 111 | { | 
|---|
|  | 112 | SUPER(SpeedPickup, XMLPort, xmlelement, mode); | 
|---|
|  | 113 |  | 
|---|
|  | 114 | XMLPortParam(SpeedPickup, "duration", setDuration, getDuration, xmlelement, mode); | 
|---|
|  | 115 | XMLPortParam(SpeedPickup, "speedAdd", setSpeedAdd, getSpeedAdd, xmlelement, mode); | 
|---|
|  | 116 | XMLPortParam(SpeedPickup, "speedMultiply", setSpeedMultiply, getSpeedMultiply, xmlelement, mode); | 
|---|
|  | 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 SpeedPickup::changedUsed(void) | 
|---|
|  | 126 | { | 
|---|
|  | 127 | SUPER(SpeedPickup, changedUsed); | 
|---|
|  | 128 |  | 
|---|
| [6706] | 129 | Engine* engine = this->carrierToEngineHelper(); | 
|---|
| [7547] | 130 | if(engine == NULL) // If the PickupCarrier is no Engine, then this pickup is useless and therefore is destroyed. | 
|---|
| [7163] | 131 | this->Pickupable::destroy(); | 
|---|
|  | 132 |  | 
|---|
| [7547] | 133 | // If the pickup has transited to used. | 
|---|
| [6681] | 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()) | 
|---|
| [6728] | 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(&SpeedPickup::pickupTimerCallback, this))); | 
|---|
|  | 146 | } | 
|---|
| [6728] | 147 | } | 
|---|
| [7547] | 148 |  | 
|---|
| [6681] | 149 | engine->setSpeedAdd(this->getSpeedAdd()); | 
|---|
|  | 150 | engine->setSpeedMultiply(this->getSpeedMultiply()); | 
|---|
|  | 151 | } | 
|---|
| [6706] | 152 | else | 
|---|
|  | 153 | { | 
|---|
|  | 154 | engine->setSpeedAdd(0.0f); | 
|---|
|  | 155 | engine->setSpeedMultiply(1.0f); | 
|---|
| [7163] | 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())) | 
|---|
| [6706] | 159 | { | 
|---|
| [7547] | 160 | this->Pickupable::destroy(); | 
|---|
| [6706] | 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 | } | 
|---|
| [6706] | 167 | } | 
|---|
| [6681] | 168 | } | 
|---|
|  | 169 |  | 
|---|
|  | 170 | /** | 
|---|
|  | 171 | @brief | 
|---|
|  | 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 | Engine* SpeedPickup::carrierToEngineHelper(void) | 
|---|
|  | 177 | { | 
|---|
|  | 178 | PickupCarrier* carrier = this->getCarrier(); | 
|---|
| [6713] | 179 | Engine* engine = dynamic_cast<Engine*>(carrier); | 
|---|
| [6681] | 180 |  | 
|---|
| [6713] | 181 | if(engine == NULL) | 
|---|
| [6681] | 182 | { | 
|---|
|  | 183 | COUT(1) << "Invalid PickupCarrier in SpeedPickup." << std::endl; | 
|---|
|  | 184 | } | 
|---|
| [7163] | 185 |  | 
|---|
| [6713] | 186 | return engine; | 
|---|
| [6681] | 187 | } | 
|---|
|  | 188 |  | 
|---|
|  | 189 | /** | 
|---|
|  | 190 | @brief | 
|---|
|  | 191 | Creates a duplicate of the input OrxonoxClass. | 
|---|
|  | 192 | @param item | 
|---|
|  | 193 | A pointer to the Orxonox class. | 
|---|
|  | 194 | */ | 
|---|
|  | 195 | void SpeedPickup::clone(OrxonoxClass*& item) | 
|---|
|  | 196 | { | 
|---|
|  | 197 | if(item == NULL) | 
|---|
|  | 198 | item = new SpeedPickup(this); | 
|---|
|  | 199 |  | 
|---|
|  | 200 | SUPER(SpeedPickup, clone, item); | 
|---|
|  | 201 |  | 
|---|
|  | 202 | SpeedPickup* pickup = dynamic_cast<SpeedPickup*>(item); | 
|---|
|  | 203 | pickup->setDuration(this->getDuration()); | 
|---|
|  | 204 | pickup->setSpeedAdd(this->getSpeedAdd()); | 
|---|
|  | 205 | pickup->setSpeedMultiply(this->getSpeedMultiply()); | 
|---|
|  | 206 |  | 
|---|
|  | 207 | pickup->initializeIdentifier(); | 
|---|
|  | 208 | } | 
|---|
|  | 209 |  | 
|---|
|  | 210 | /** | 
|---|
|  | 211 | @brief | 
|---|
| [7547] | 212 | Sets the duration for which the SpeedPickup stays active. | 
|---|
| [6681] | 213 | @param duration | 
|---|
| [7547] | 214 | The duration in seconds. | 
|---|
| [6681] | 215 | */ | 
|---|
|  | 216 | void SpeedPickup::setDuration(float duration) | 
|---|
|  | 217 | { | 
|---|
|  | 218 | if(duration >= 0.0f) | 
|---|
|  | 219 | { | 
|---|
|  | 220 | this->duration_ = duration; | 
|---|
|  | 221 | } | 
|---|
|  | 222 | else | 
|---|
|  | 223 | { | 
|---|
|  | 224 | COUT(1) << "Invalid duration in SpeedPickup." << std::endl; | 
|---|
| [6694] | 225 | this->duration_ = 0.0f; | 
|---|
| [6681] | 226 | } | 
|---|
|  | 227 | } | 
|---|
|  | 228 |  | 
|---|
|  | 229 | /** | 
|---|
|  | 230 | @brief | 
|---|
| [7547] | 231 | Sets the speedAdd, the value that is added to the speed of the Pawn. | 
|---|
| [6681] | 232 | @param speedAdd | 
|---|
| [7547] | 233 | The added speed. | 
|---|
| [6681] | 234 | */ | 
|---|
|  | 235 | void SpeedPickup::setSpeedAdd(float speedAdd) | 
|---|
|  | 236 | { | 
|---|
|  | 237 | if(speedAdd >= 0.0f) | 
|---|
|  | 238 | { | 
|---|
|  | 239 | this->speedAdd_ = speedAdd; | 
|---|
|  | 240 | } | 
|---|
|  | 241 | else | 
|---|
|  | 242 | { | 
|---|
|  | 243 | COUT(1) << "Invalid speedAdd in SpeedPickup." << std::endl; | 
|---|
| [6694] | 244 | this->speedAdd_ = 0.0f; | 
|---|
| [6681] | 245 | } | 
|---|
|  | 246 | } | 
|---|
|  | 247 |  | 
|---|
|  | 248 | /** | 
|---|
|  | 249 | @brief | 
|---|
| [7547] | 250 | Sets the speedMultiply, the factor by which the speed of the Pawn is multiplied. | 
|---|
| [7401] | 251 | @param speedMultiply | 
|---|
| [7547] | 252 | The factor by which the speed is mutiplied. | 
|---|
| [6681] | 253 | */ | 
|---|
|  | 254 | void SpeedPickup::setSpeedMultiply(float speedMultiply) | 
|---|
|  | 255 | { | 
|---|
|  | 256 | if(speedMultiply != 0) | 
|---|
|  | 257 | { | 
|---|
|  | 258 | this->speedMultiply_ = speedMultiply; | 
|---|
|  | 259 | } | 
|---|
|  | 260 | else | 
|---|
|  | 261 | { | 
|---|
|  | 262 | COUT(1) << "Invalid speedMultiply in SpeedPickup." << std::endl; | 
|---|
| [6694] | 263 | this->speedMultiply_ = 1.0f; | 
|---|
| [6681] | 264 | } | 
|---|
|  | 265 | } | 
|---|
|  | 266 |  | 
|---|
| [6706] | 267 | void SpeedPickup::pickupTimerCallback(void) | 
|---|
| [7163] | 268 | { | 
|---|
| [6706] | 269 | this->setUsed(false); | 
|---|
| [6681] | 270 | } | 
|---|
|  | 271 | } | 
|---|