[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 | |
---|
[7547] | 40 | #include "worldentities/pawns/SpaceShip.h" |
---|
[6681] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[9667] | 44 | RegisterClass(SpeedPickup); |
---|
[6681] | 45 | |
---|
| 46 | /** |
---|
| 47 | @brief |
---|
| 48 | Constructor. Registers the object and initializes the member variables. |
---|
| 49 | */ |
---|
[9667] | 50 | SpeedPickup::SpeedPickup(Context* context) : Pickup(context) |
---|
[6681] | 51 | { |
---|
| 52 | RegisterObject(SpeedPickup); |
---|
| 53 | |
---|
| 54 | this->initialize(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | @brief |
---|
| 59 | Destructor. |
---|
| 60 | */ |
---|
| 61 | SpeedPickup::~SpeedPickup() |
---|
| 62 | { |
---|
| 63 | |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | @brief |
---|
| 68 | Initializes the member variables. |
---|
| 69 | */ |
---|
| 70 | void SpeedPickup::initialize(void) |
---|
| 71 | { |
---|
[6706] | 72 | this->duration_ = 0.0f; |
---|
| 73 | this->speedAdd_ = 0.0f; |
---|
| 74 | this->speedMultiply_ = 1.0f; |
---|
[6681] | 75 | |
---|
[8727] | 76 | this->addTarget(ClassIdentifier<SpaceShip>::getIdentifier()); |
---|
[6681] | 77 | } |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | @brief |
---|
| 81 | Method for creating a SpeedPickup object through XML. |
---|
| 82 | */ |
---|
| 83 | void SpeedPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
| 84 | { |
---|
| 85 | SUPER(SpeedPickup, XMLPort, xmlelement, mode); |
---|
| 86 | |
---|
| 87 | XMLPortParam(SpeedPickup, "duration", setDuration, getDuration, xmlelement, mode); |
---|
| 88 | XMLPortParam(SpeedPickup, "speedAdd", setSpeedAdd, getSpeedAdd, xmlelement, mode); |
---|
| 89 | XMLPortParam(SpeedPickup, "speedMultiply", setSpeedMultiply, getSpeedMultiply, xmlelement, mode); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | @brief |
---|
| 94 | Is called when the pickup has transited from used to unused or the other way around. |
---|
| 95 | */ |
---|
| 96 | void SpeedPickup::changedUsed(void) |
---|
| 97 | { |
---|
| 98 | SUPER(SpeedPickup, changedUsed); |
---|
| 99 | |
---|
[8727] | 100 | SpaceShip* ship = this->carrierToSpaceShipHelper(); |
---|
[11071] | 101 | if(ship == nullptr) // If the PickupCarrier is no SpaceShip, then this pickup is useless and therefore is destroyed. |
---|
[7163] | 102 | this->Pickupable::destroy(); |
---|
| 103 | |
---|
[7547] | 104 | // If the pickup has transited to used. |
---|
[6681] | 105 | if(this->isUsed()) |
---|
| 106 | { |
---|
[7547] | 107 | // If its durationType is continuous, we set a Timer to be reminded, when the time has run out. |
---|
| 108 | if(this->isContinuous()) |
---|
[6728] | 109 | { |
---|
[7547] | 110 | if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f) |
---|
| 111 | { |
---|
| 112 | this->durationTimer_.unpauseTimer(); |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&SpeedPickup::pickupTimerCallback, this))); |
---|
| 117 | } |
---|
[6728] | 118 | } |
---|
[7547] | 119 | |
---|
[8727] | 120 | ship->addSpeed(this->getSpeedAdd()); |
---|
| 121 | ship->addSpeedFactor(this->getSpeedMultiply()); |
---|
[6681] | 122 | } |
---|
[6706] | 123 | else |
---|
| 124 | { |
---|
[8727] | 125 | ship->addSpeed(-this->getSpeedAdd()); |
---|
| 126 | ship->addSpeedFactor(1.0f/this->getSpeedMultiply()); |
---|
[7163] | 127 | |
---|
[7547] | 128 | // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded. |
---|
| 129 | if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration())) |
---|
[6706] | 130 | { |
---|
[7547] | 131 | this->Pickupable::destroy(); |
---|
[6706] | 132 | } |
---|
[7547] | 133 | // We pause the Timer if the pickup is continuous and the duration is not yet exceeded, |
---|
| 134 | else if(this->isContinuous() && this->durationTimer_.isActive()) |
---|
| 135 | { |
---|
| 136 | this->durationTimer_.pauseTimer(); |
---|
| 137 | } |
---|
[6706] | 138 | } |
---|
[6681] | 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | @brief |
---|
[8727] | 143 | Helper to transform the PickupCarrier to a SpaceShip, and throw an error message if the conversion fails. |
---|
[6681] | 144 | @return |
---|
[11071] | 145 | A pointer to the SpaceShip, or nullptr if the conversion failed. |
---|
[6681] | 146 | */ |
---|
[8727] | 147 | SpaceShip* SpeedPickup::carrierToSpaceShipHelper(void) |
---|
[6681] | 148 | { |
---|
| 149 | PickupCarrier* carrier = this->getCarrier(); |
---|
[9348] | 150 | SpaceShip* ship = orxonox_cast<SpaceShip*>(carrier); |
---|
[6681] | 151 | |
---|
[11071] | 152 | if(ship == nullptr) |
---|
[6681] | 153 | { |
---|
[8858] | 154 | orxout(internal_error, context::pickups) << "Invalid PickupCarrier in SpeedPickup." << endl; |
---|
[6681] | 155 | } |
---|
[7163] | 156 | |
---|
[8727] | 157 | return ship; |
---|
[6681] | 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | @brief |
---|
[7547] | 162 | Sets the duration for which the SpeedPickup stays active. |
---|
[6681] | 163 | @param duration |
---|
[7547] | 164 | The duration in seconds. |
---|
[6681] | 165 | */ |
---|
| 166 | void SpeedPickup::setDuration(float duration) |
---|
| 167 | { |
---|
| 168 | if(duration >= 0.0f) |
---|
| 169 | { |
---|
| 170 | this->duration_ = duration; |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
[8858] | 174 | orxout(internal_error, context::pickups) << "Invalid duration in SpeedPickup." << endl; |
---|
[6694] | 175 | this->duration_ = 0.0f; |
---|
[6681] | 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | @brief |
---|
[7547] | 181 | Sets the speedAdd, the value that is added to the speed of the Pawn. |
---|
[6681] | 182 | @param speedAdd |
---|
[7547] | 183 | The added speed. |
---|
[6681] | 184 | */ |
---|
| 185 | void SpeedPickup::setSpeedAdd(float speedAdd) |
---|
| 186 | { |
---|
| 187 | if(speedAdd >= 0.0f) |
---|
| 188 | { |
---|
| 189 | this->speedAdd_ = speedAdd; |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
[8858] | 193 | orxout(internal_error, context::pickups) << "Invalid speedAdd in SpeedPickup." << endl; |
---|
[6694] | 194 | this->speedAdd_ = 0.0f; |
---|
[6681] | 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | @brief |
---|
[7547] | 200 | Sets the speedMultiply, the factor by which the speed of the Pawn is multiplied. |
---|
[7401] | 201 | @param speedMultiply |
---|
[7547] | 202 | The factor by which the speed is mutiplied. |
---|
[6681] | 203 | */ |
---|
| 204 | void SpeedPickup::setSpeedMultiply(float speedMultiply) |
---|
| 205 | { |
---|
| 206 | if(speedMultiply != 0) |
---|
| 207 | { |
---|
| 208 | this->speedMultiply_ = speedMultiply; |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
[8858] | 212 | orxout(internal_error, context::pickups) << "Invalid speedMultiply in SpeedPickup." << endl; |
---|
[6694] | 213 | this->speedMultiply_ = 1.0f; |
---|
[6681] | 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
[6706] | 217 | void SpeedPickup::pickupTimerCallback(void) |
---|
[7163] | 218 | { |
---|
[6706] | 219 | this->setUsed(false); |
---|
[6681] | 220 | } |
---|
| 221 | } |
---|