Changeset 6574
- Timestamp:
- Mar 20, 2010, 6:36:00 PM (15 years ago)
- Location:
- code/branches/ppspickups1
- Files:
-
- 1 deleted
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ppspickups1/src/modules/pickup/items/SpeedPickup.cc
r6552 r6574 21 21 * 22 22 * Author: 23 * Damian 'Mozork' Frick23 * Eric Beier 24 24 * Co-authors: 25 25 * ... 26 26 * 27 * TODO: Implement...28 27 */ 29 /* 30 * ORXONOX - the hottest 3D action shooter ever to exist 31 * > www.orxonox.net < 32 * 33 * 34 * License notice: 35 * 36 * This program is free software; you can redistribute it and/or 37 * modify it under the terms of the GNU General Public License 38 * as published by the Free Software Foundation; either version 2 39 * of the License, or (at your option) any later version. 40 * 41 * This program is distributed in the hope that it will be useful, 42 * but WITHOUT ANY WARRANTY; without even the implied warranty of 43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 44 * GNU General Public License for more details. 45 * 46 * You should have received a copy of the GNU General Public License 47 * along with this program; if not, write to the Free Software 48 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 49 * 50 * Author: 51 * Damian 'Mozork' Frick 52 * Co-authors: 53 * ... 54 * 55 * TODO: Implement... 56 */ 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/Pawn.h" 41 #include "pickup/PickupIdentifier.h" 42 43 #include <sstream> 44 45 46 namespace orxonox 47 { 48 CreateFactory(SpeedPickup); 49 50 /** 51 @brief 52 Constructor. Registers the object and initializes the member variables. 53 */ 54 SpeedPickup::SpeedPickup(BaseObject* creator) : Pickup(creator) 55 { 56 RegisterObject(SpeedPickup); 57 58 this->initialize(); 59 } 60 61 /** 62 @brief 63 Destructor. 64 */ 65 SpeedPickup::~SpeedPickup() 66 { 67 68 } 69 70 /** 71 @brief 72 Initializes the member variables. 73 */ 74 void SpeedPickup::initialize(void) 75 { 76 this->duration_ = 0; 77 this->speedAdd_ = 0; 78 this->speedMultiply_ = 0; 79 80 this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); 81 } 82 83 /** 84 @brief 85 Initializes the PickupIdentifier of this pickup. 86 */ 87 void SpeedPickup::initializeIdentifier(void) 88 { 89 std::stringstream stream; 90 stream << this->getDuration(); 91 std::string type1 = "duration"; 92 std::string val1 = stream.str(); 93 this->pickupIdentifier_->addParameter(type1, val1); 94 95 stream.clear(); 96 stream << this->getSpeedAdd(); 97 std::string type2 = "speedAdd"; 98 std::string val2 = stream.str(); 99 this->pickupIdentifier_->addParameter(type2, val2); 100 101 stream.clear(); 102 stream << this->getSpeedMultiply(); 103 std::string type3 = "speedMultiply"; 104 std::string val3 = stream.str(); 105 this->pickupIdentifier_->addParameter(type3, val3); 106 } 107 108 /** 109 @brief 110 Method for creating a SpeedPickup object through XML. 111 */ 112 void SpeedPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) 113 { 114 SUPER(SpeedPickup, XMLPort, xmlelement, mode); 115 116 XMLPortParam(SpeedPickup, "duration", setDuration, getDuration, xmlelement, mode); 117 XMLPortParam(SpeedPickup, "speedAdd", setSpeedAdd, getSpeedAdd, xmlelement, mode); 118 XMLPortParam(SpeedPickup, "speedMultiply", setSpeedMultiply, getSpeedMultiply, xmlelement, mode); 119 120 this->initializeIdentifier(); 121 } 122 123 /** 124 @brief 125 Is called every tick. 126 Does count down the duration of the SpeedPickup. 127 @param dt 128 The duration of the last tick. 129 */ 130 void SpeedPickup::tick(float dt) 131 { 132 if(this->isUsed()) 133 { 134 Pawn* pawn = this->carrierToPawnHelper(); 135 if(pawn == NULL) //!< If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 136 this->destroy(); 137 138 //! Calculate the remaining duration of the Pickup 139 float duration=this->getDuration()-dt; 140 this->setDuration(duration); 141 142 //! If duration is over 143 if(this->getDuration() < 0) 144 { 145 this->setUsed(false); 146 } 147 } 148 } 149 150 /** 151 @brief 152 Is called when the pickup has transited from used to unused or the other way around. 153 */ 154 void SpeedPickup::changedUsed(void) 155 { 156 SUPER(SpeedPickup, changedUsed); 157 158 //! If the pickup is not picked up nothing must be done. 159 if(!this->isPickedUp()) 160 return; 161 162 //! If the pickup has transited to used. 163 if(this->isUsed()) 164 { 165 if(this->isOnce()) 166 { 167 Pawn* pawn = this->carrierToPawnHelper(); 168 if(pawn == NULL) //!< If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 169 this->destroy(); 170 171 //! The pickup has been used up. 172 this->setUsed(false); 173 } 174 } 175 else 176 { 177 //! If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused. 178 if(this->isOnce() || (this->isContinuous() && this->getDuration() < 0)) 179 { 180 this->destroy(); 181 } 182 } 183 } 184 185 /** 186 @brief 187 Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. 188 @return 189 A pointer to the Pawn, or NULL if the conversion failed. 190 */ 191 Pawn* SpeedPickup::carrierToPawnHelper(void) 192 { 193 PickupCarrier* carrier = this->getCarrier(); 194 Pawn* pawn = dynamic_cast<Pawn*>(carrier); 195 196 if(pawn == NULL) 197 { 198 COUT(1) << "Invalid PickupCarrier in SpeedPickup." << std::endl; 199 } 200 201 return pawn; 202 } 203 204 /** 205 @brief 206 Creates a duplicate of the input OrxonoxClass. 207 @param item 208 A pointer to the Orxonox class. 209 */ 210 void SpeedPickup::clone(OrxonoxClass*& item) 211 { 212 if(item == NULL) 213 item = new SpeedPickup(this); 214 215 SUPER(SpeedPickup, clone, item); 216 217 SpeedPickup* pickup = dynamic_cast<SpeedPickup*>(item); 218 pickup->setDuration(this->getDuration()); 219 pickup->setSpeedAdd(this->getSpeedAdd()); 220 pickup->setSpeedMultiply(this->getSpeedMultiply()); 221 222 pickup->initializeIdentifier(); 223 } 224 225 /** 226 @brief 227 Sets the duration. 228 @param duration 229 The duration 230 */ 231 void SpeedPickup::setDuration(float duration) 232 { 233 if(duration >= 0.0f) 234 { 235 this->duration_ = duration; 236 } 237 else 238 { 239 COUT(1) << "Invalid duration in SpeedPickup." << std::endl; 240 this->duration_ = 0.0; 241 } 242 } 243 244 /** 245 @brief 246 Sets the SpeedAdd 247 @param speedAdd 248 The added Speed 249 */ 250 void SpeedPickup::setSpeedAdd(float speedAdd) 251 { 252 if(speedAdd > 0.0f) 253 { 254 this->speedAdd_ = speedAdd; 255 } 256 else 257 { 258 COUT(1) << "Invalid speedAdd in SpeedPickup." << std::endl; 259 this->speedAdd_ = 0.0; 260 } 261 } 262 263 /** 264 @brief 265 Sets the SpeedMultiply 266 @param speedAdd 267 The multiplied Speed 268 */ 269 void SpeedPickup::setSpeedMultiply(float speedMultiply) 270 { 271 if(speedMultiply > 0.0f) 272 { 273 this->speedMultiply_ = speedMultiply; 274 } 275 else 276 { 277 COUT(1) << "Invalid speedMultiply in SpeedPickup." << std::endl; 278 this->speedMultiply_ = 0.0; 279 } 280 } 281 } -
code/branches/ppspickups1/src/modules/pickup/items/SpeedPickup.h
r6552 r6574 21 21 * 22 22 * Author: 23 * Damian 'Mozork' Frick23 * Eric Beier 24 24 * Co-authors: 25 25 * ... 26 26 * 27 * TODO: Implement...28 27 */ 29 /* 30 * ORXONOX - the hottest 3D action shooter ever to exist 31 * > www.orxonox.net < 32 * 33 * 34 * License notice: 35 * 36 * This program is free software; you can redistribute it and/or 37 * modify it under the terms of the GNU General Public License 38 * as published by the Free Software Foundation; either version 2 39 * of the License, or (at your option) any later version. 40 * 41 * This program is distributed in the hope that it will be useful, 42 * but WITHOUT ANY WARRANTY; without even the implied warranty of 43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 44 * GNU General Public License for more details. 45 * 46 * You should have received a copy of the GNU General Public License 47 * along with this program; if not, write to the Free Software 48 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 49 * 50 * Author: 51 * Damian 'Mozork' Frick 52 * Co-authors: 53 * ... 54 * 55 * TODO: Implement... 56 */ 28 29 /** 30 @file SpeedPickup.h 31 @brief Declaration of the SpeedPickup class. 32 */ 33 34 #ifndef _SpeedPickup_H__ 35 #define _SpeedPickup_H__ 36 37 #include "pickup/PickupPrereqs.h" 38 39 #include <string> 40 #include <worldentities/pawns/Pawn.h> 41 #include "worldentities/StaticEntity.h" 42 43 #include "pickup/Pickup.h" 44 45 namespace orxonox { 46 47 /** 48 @brief 49 A Pickup which can manipulate the Speed of a Pawn. 50 51 1) The speed multiplier: 52 The additional (forward) speed: 53 2) The activation type: 'immediate' or 'onUse'. defines if the item is used when it's picked up or only after the player chooses to use it. 54 4) The duration: the activation time of the pickup. 55 56 @author 57 Eric Beier 58 */ 59 class _PickupExport SpeedPickup : public Pickup, public Tickable 60 { 61 public: 62 63 SpeedPickup(BaseObject* creator); //!< Constructor. 64 virtual ~SpeedPickup(); //!< Destructor. 65 66 virtual void XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode); //!< Method for creating a HealthPickup object through XML. 67 virtual void tick(float dt); //!< Is called every tick. 68 69 virtual void changedUsed(void); //!< Is called when the pickup has transited from used to unused or the other way around. 70 virtual void clone(OrxonoxClass*& item); //!< Creates a duplicate of the input OrxonoxClass. 71 72 inline float getDuration(void) 73 { return this->duration_; } 74 inline float getSpeedAdd(void) 75 { return this->speedAdd_; } 76 inline float getSpeedMultiply(void) 77 { return this->speedMultiply_; } 78 79 protected: 80 void initializeIdentifier(void); //!< Initializes the PickupIdentifier of this pickup. 81 82 void setDuration(float duration); //!< Sets the duration 83 void setSpeedAdd(float speedAdd); 84 void setSpeedMultiply(float speedMultiply); 85 86 87 private: 88 void initialize(void); //!< Initializes the member variables. 89 Pawn* carrierToPawnHelper(void); //!< Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. 90 91 float duration_; //!< The health that is transferred to the Pawn. 92 float speedAdd_; 93 float speedMultiply_; 94 }; 95 } 96 97 #endif // _HealthPickup_H__
Note: See TracChangeset
for help on using the changeset viewer.