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