[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 "pickup/PickupIdentifier.h" |
---|
[7547] | 41 | #include "worldentities/pawns/SpaceShip.h" |
---|
[6681] | 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 | { |
---|
[6706] | 73 | this->duration_ = 0.0f; |
---|
| 74 | this->speedAdd_ = 0.0f; |
---|
| 75 | this->speedMultiply_ = 1.0f; |
---|
[6681] | 76 | |
---|
[8727] | 77 | this->addTarget(ClassIdentifier<SpaceShip>::getIdentifier()); |
---|
[6681] | 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 | |
---|
[8727] | 128 | SpaceShip* ship = this->carrierToSpaceShipHelper(); |
---|
| 129 | if(ship == NULL) // If the PickupCarrier is no SpaceShip, then this pickup is useless and therefore is destroyed. |
---|
[7163] | 130 | this->Pickupable::destroy(); |
---|
| 131 | |
---|
[7547] | 132 | // If the pickup has transited to used. |
---|
[6681] | 133 | if(this->isUsed()) |
---|
| 134 | { |
---|
[7547] | 135 | // If its durationType is continuous, we set a Timer to be reminded, when the time has run out. |
---|
| 136 | if(this->isContinuous()) |
---|
[6728] | 137 | { |
---|
[7547] | 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 | } |
---|
[6728] | 146 | } |
---|
[7547] | 147 | |
---|
[8727] | 148 | ship->addSpeed(this->getSpeedAdd()); |
---|
| 149 | ship->addSpeedFactor(this->getSpeedMultiply()); |
---|
[6681] | 150 | } |
---|
[6706] | 151 | else |
---|
| 152 | { |
---|
[8727] | 153 | ship->addSpeed(-this->getSpeedAdd()); |
---|
| 154 | ship->addSpeedFactor(1.0f/this->getSpeedMultiply()); |
---|
[7163] | 155 | |
---|
[7547] | 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())) |
---|
[6706] | 158 | { |
---|
[7547] | 159 | this->Pickupable::destroy(); |
---|
[6706] | 160 | } |
---|
[7547] | 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 | } |
---|
[6706] | 166 | } |
---|
[6681] | 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | @brief |
---|
[8727] | 171 | Helper to transform the PickupCarrier to a SpaceShip, and throw an error message if the conversion fails. |
---|
[6681] | 172 | @return |
---|
[8727] | 173 | A pointer to the SpaceShip, or NULL if the conversion failed. |
---|
[6681] | 174 | */ |
---|
[8727] | 175 | SpaceShip* SpeedPickup::carrierToSpaceShipHelper(void) |
---|
[6681] | 176 | { |
---|
| 177 | PickupCarrier* carrier = this->getCarrier(); |
---|
[8727] | 178 | SpaceShip* ship = dynamic_cast<SpaceShip*>(carrier); |
---|
[6681] | 179 | |
---|
[8727] | 180 | if(ship == NULL) |
---|
[6681] | 181 | { |
---|
[8858] | 182 | orxout(internal_error, context::pickups) << "Invalid PickupCarrier in SpeedPickup." << endl; |
---|
[6681] | 183 | } |
---|
[7163] | 184 | |
---|
[8727] | 185 | return ship; |
---|
[6681] | 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 |
---|
[7547] | 211 | Sets the duration for which the SpeedPickup stays active. |
---|
[6681] | 212 | @param duration |
---|
[7547] | 213 | The duration in seconds. |
---|
[6681] | 214 | */ |
---|
| 215 | void SpeedPickup::setDuration(float duration) |
---|
| 216 | { |
---|
| 217 | if(duration >= 0.0f) |
---|
| 218 | { |
---|
| 219 | this->duration_ = duration; |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | { |
---|
[8858] | 223 | orxout(internal_error, context::pickups) << "Invalid duration in SpeedPickup." << endl; |
---|
[6694] | 224 | this->duration_ = 0.0f; |
---|
[6681] | 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | @brief |
---|
[7547] | 230 | Sets the speedAdd, the value that is added to the speed of the Pawn. |
---|
[6681] | 231 | @param speedAdd |
---|
[7547] | 232 | The added speed. |
---|
[6681] | 233 | */ |
---|
| 234 | void SpeedPickup::setSpeedAdd(float speedAdd) |
---|
| 235 | { |
---|
| 236 | if(speedAdd >= 0.0f) |
---|
| 237 | { |
---|
| 238 | this->speedAdd_ = speedAdd; |
---|
| 239 | } |
---|
| 240 | else |
---|
| 241 | { |
---|
[8858] | 242 | orxout(internal_error, context::pickups) << "Invalid speedAdd in SpeedPickup." << endl; |
---|
[6694] | 243 | this->speedAdd_ = 0.0f; |
---|
[6681] | 244 | } |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | @brief |
---|
[7547] | 249 | Sets the speedMultiply, the factor by which the speed of the Pawn is multiplied. |
---|
[7401] | 250 | @param speedMultiply |
---|
[7547] | 251 | The factor by which the speed is mutiplied. |
---|
[6681] | 252 | */ |
---|
| 253 | void SpeedPickup::setSpeedMultiply(float speedMultiply) |
---|
| 254 | { |
---|
| 255 | if(speedMultiply != 0) |
---|
| 256 | { |
---|
| 257 | this->speedMultiply_ = speedMultiply; |
---|
| 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
[8858] | 261 | orxout(internal_error, context::pickups) << "Invalid speedMultiply in SpeedPickup." << endl; |
---|
[6694] | 262 | this->speedMultiply_ = 1.0f; |
---|
[6681] | 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
[6706] | 266 | void SpeedPickup::pickupTimerCallback(void) |
---|
[7163] | 267 | { |
---|
[6706] | 268 | this->setUsed(false); |
---|
[6681] | 269 | } |
---|
| 270 | } |
---|