- Timestamp:
- Mar 6, 2010, 7:44:04 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/pickup3/src/modules/pickup/items/HealthPickup.cc
r6475 r6477 31 31 #include "core/CoreIncludes.h" 32 32 #include "core/XMLPort.h" 33 #include "util/StringUtils.h" 33 34 34 35 #include "worldentities/pawns/Pawn.h" … … 43 44 /*static*/ const std::string HealthPickup::healthTypeTemporary_s = "temporary"; 44 45 /*static*/ const std::string HealthPickup::healthTypePermanent_s = "permanent"; 45 /*static*/ const std::string HealthPickup::blankString_s = "";46 46 47 47 CreateFactory(HealthPickup); 48 48 49 /** 50 @brief 51 Constructor. Registers the object and initializes the member variables. 52 */ 49 53 HealthPickup::HealthPickup(BaseObject* creator) : Pickup(creator) 50 54 { … … 54 58 } 55 59 60 /** 61 @brief 62 Destructor. 63 */ 56 64 HealthPickup::~HealthPickup() 57 65 { … … 59 67 } 60 68 69 /** 70 @brief 71 Initializes the member variables. 72 */ 61 73 void HealthPickup::initialize(void) 62 74 { … … 64 76 65 77 this->health_ = 0; 66 this->health Speed_ = 0;78 this->healthRate_ = 0; 67 79 this->healthType_ = pickupHealthType::limited; 68 69 } 70 80 this->maxHealthSave_ = 0; 81 this->maxHealthOverwrite_ = 0; 82 83 } 84 85 /** 86 @brief 87 Initializes the PickupIdentifier of this pickup. 88 */ 71 89 void HealthPickup::initializeIdentifier(void) 72 90 { … … 83 101 std::string type2 = "healthType"; 84 102 this->pickupIdentifier_->addParameter(type2, val2); 85 } 86 103 104 stream.clear(); 105 stream << this->getHealthRate(); 106 std::string val3 = stream.str(); 107 std::string type3 = "healthRate"; 108 this->pickupIdentifier_->addParameter(type3, val3); 109 } 110 111 /** 112 @brief 113 Method for creating a HealthPickup object through XML. 114 */ 87 115 void HealthPickup::HealthPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) 88 116 { … … 90 118 91 119 XMLPortParam(HealthPickup, "health", setHealth, getHealth, xmlelement, mode); 92 XMLPortParam(HealthPickup, "health Speed", setHealthSpeed, getHealthSpeed, xmlelement, mode);120 XMLPortParam(HealthPickup, "healthRate", setHealthRate, getHealthRate, xmlelement, mode); 93 121 XMLPortParam(HealthPickup, "healthType", setHealthType, getHealthType, xmlelement, mode); 94 122 95 123 if(!this->isContinuous()) 96 this->health Speed_ = 0.0;124 this->healthRate_ = 0.0; 97 125 98 126 this->initializeIdentifier(); 99 127 } 100 128 101 void HealthPickup::setHealth(float health) 102 { 103 if(health > 0.0f) 104 { 105 this->health_ = health; 106 } 107 else 108 { 109 COUT(1) << "Invalid health in HealthPickup." << std::endl; 110 } 111 } 112 113 void HealthPickup::setHealthSpeed(float speed) 114 { 115 if(speed >= 0) 116 { 117 this->healthSpeed_ = speed; 118 } 119 else 120 { 121 COUT(1) << "Invalid healthSpeed in HealthPickup." << std::endl; 122 } 123 } 124 125 void HealthPickup::setHealthType(std::string type) 126 { 127 if(type == HealthPickup::healthTypeLimited_s) 128 { 129 this->setHealthTypeDirect(pickupHealthType::limited); 130 } 131 else if(type == HealthPickup::healthTypeTemporary_s) 132 { 133 this->setHealthTypeDirect(pickupHealthType::temporary); 134 } 135 else if(type == HealthPickup::healthTypePermanent_s) 136 { 137 this->setHealthTypeDirect(pickupHealthType::permanent); 138 } 139 else 140 { 141 COUT(1) << "Invalid healthType in HealthPickup." << std::endl; 142 } 143 } 144 129 /** 130 @brief 131 Is called every tick. 132 Does all the continuous stuff of this HealthPickup. 133 @param dt 134 The duration of the last tick. 135 */ 145 136 void HealthPickup::tick(float dt) 146 137 { 147 138 if(this->isContinuous() && this->isUsed()) 148 139 { 149 PickupCarrier* carrier = this->getCarrier(); 150 Pawn* pawn = dynamic_cast<Pawn*>(carrier); 140 Pawn* pawn = this->carrierToPawnHelper(); 141 if(pawn == NULL) //!< If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 142 this->destroy(); 151 143 152 if(pawn == NULL) 153 { 154 COUT(1) << "Invalid PickupCarrier in HealthPickup." << std::endl; 155 return; 156 } 157 158 float health = dt*this->getHealthSpeed(); 144 //! Calculate the health that is added this tick. 145 float health = dt*this->getHealthRate(); 159 146 if(health > this->getHealth()) 160 147 health = this->getHealth(); 148 //! Calculate the health the Pawn will have once the health is added. 161 149 float fullHealth = pawn->getHealth() + health; 162 150 this->setHealth(this->getHealth()-health); … … 171 159 break; 172 160 case pickupHealthType::temporary: 173 //TODO: How? 161 if(pawn->getMaxHealth() > fullHealth) 162 { 163 this->maxHealthSave_ = pawn->getMaxHealth(); 164 this->maxHealthOverwrite_ = fullHealth; 165 pawn->setMaxHealth(fullHealth); 166 } 167 pawn->addHealth(health); 174 168 break; 175 169 default: … … 177 171 } 178 172 173 //! If all health has been transfered. 179 174 if(this->getHealth() == 0) 180 175 { 181 //TODO: Destroy 182 } 183 } 184 } 185 186 const std::string& HealthPickup::getHealthType(void) 187 { 188 switch(this->getHealthTypeDirect()) 189 { 190 case pickupHealthType::limited: 191 return HealthPickup::healthTypeLimited_s; 192 case pickupHealthType::temporary: 193 return HealthPickup::healthTypeTemporary_s; 194 case pickupHealthType::permanent: 195 return HealthPickup::healthTypePermanent_s; 196 default: 197 COUT(1) << "Invalid healthType in HealthPickup." << std::endl; 198 return HealthPickup::blankString_s; 199 } 200 } 201 202 void HealthPickup::clone(OrxonoxClass* item) 203 { 204 if(item == NULL) 205 item = new HealthPickup(this); 206 207 SUPER(HealthPickup, clone, item); 208 209 //TODO: No temp needed? 210 HealthPickup* pickup = dynamic_cast<HealthPickup*>(item); 211 pickup->setHealth(this->getHealth()); 212 pickup->setHealthSpeed(this->getHealthSpeed()); 213 pickup->setHealthTypeDirect(this->getHealthTypeDirect()); 214 215 pickup->initializeIdentifier(); 216 } 217 218 //TODO: Does this work even if Pickup doesn't implement it? 176 this->setUsed(false); 177 } 178 } 179 } 180 181 /** 182 @brief 183 Is called when the pickup has transited from used to unused or the other way around. 184 */ 219 185 void HealthPickup::changedUsed(void) 220 186 { 221 187 SUPER(HealthPickup, changedUsed); 222 188 189 //! If the pickup is not picked up nothing must be done. 190 if(!this->isPickedUp()) 191 return; 192 193 //! If the pickup has transited to used. 223 194 if(this->isUsed()) 224 195 { 225 PickupCarrier* carrier = this->getCarrier();226 Pawn* pawn = dynamic_cast<Pawn*>(carrier);227 228 if(pawn == NULL)229 {230 COUT(1) << "Invalid PickupCarrier in HealthPickup." << std::endl;231 return;232 }233 234 196 if(this->isOnce()) 235 197 { 198 Pawn* pawn = this->carrierToPawnHelper(); 199 if(pawn == NULL) //!< If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 200 this->destroy(); 201 236 202 float health = 0; 237 203 switch(this->getHealthTypeDirect()) … … 245 211 break; 246 212 case pickupHealthType::temporary: 247 //TODO: How? 213 health = pawn->getHealth()+this->getHealth(); 214 if(pawn->getMaxHealth() < health) 215 { 216 this->maxHealthSave_ = pawn->getMaxHealth(); 217 this->maxHealthOverwrite_ = health; 218 pawn->setMaxHealth(health); 219 } 220 pawn->addHealth(this->getHealth()); 248 221 break; 249 222 default: … … 251 224 } 252 225 253 //TODO: Destroy. 226 //! The pickup has been used up. 227 this->setUsed(false); 254 228 } 255 229 } 256 230 else 257 231 { 258 //TODO: Destroy, but be careful to not destroy before being used. 232 if(this->getHealthTypeDirect() == pickupHealthType::temporary) 233 { 234 PickupCarrier* carrier = this->getCarrier(); 235 Pawn* pawn = dynamic_cast<Pawn*>(carrier); 236 237 if(pawn == NULL) 238 { 239 COUT(1) << "Something went horribly wrong in Health Pickup. PickupCarrier is no Pawn." << std::endl; 240 this->destroy(); 241 return; 242 } 243 244 if(pawn->getMaxHealth() == this->maxHealthOverwrite_) 245 { 246 pawn->setMaxHealth(this->maxHealthSave_); 247 this->maxHealthOverwrite_ = 0; 248 this->maxHealthSave_ = 0; 249 } 250 } 251 252 //! If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused. 253 if(this->isOnce() || (this->isContinuous() && this->getHealth() == 0)) 254 { 255 this->destroy(); 256 } 257 } 258 } 259 260 /** 261 @brief 262 Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. 263 @return 264 A pointer to the Pawn, or NULL if the conversion failed. 265 */ 266 Pawn* HealthPickup::carrierToPawnHelper(void) 267 { 268 PickupCarrier* carrier = this->getCarrier(); 269 Pawn* pawn = dynamic_cast<Pawn*>(carrier); 270 271 if(pawn == NULL) 272 { 273 COUT(1) << "Invalid PickupCarrier in HealthPickup." << std::endl; 274 } 275 276 return pawn; 277 } 278 279 /** 280 @brief 281 Creates a duplicate of the input OrxonoxClass. 282 @param item 283 A pointer to the Orxonox class. 284 */ 285 void HealthPickup::clone(OrxonoxClass* item) 286 { 287 if(item == NULL) 288 item = new HealthPickup(this); 289 290 SUPER(HealthPickup, clone, item); 291 292 HealthPickup* pickup = dynamic_cast<HealthPickup*>(item); 293 pickup->setHealth(this->getHealth()); 294 pickup->setHealthRate(this->getHealthRate()); 295 pickup->setHealthTypeDirect(this->getHealthTypeDirect()); 296 297 pickup->initializeIdentifier(); 298 } 299 300 /** 301 @brief 302 Get the health type of this pickup. 303 @return 304 Returns the health type as a string. 305 */ 306 const std::string& HealthPickup::getHealthType(void) 307 { 308 switch(this->getHealthTypeDirect()) 309 { 310 case pickupHealthType::limited: 311 return HealthPickup::healthTypeLimited_s; 312 case pickupHealthType::temporary: 313 return HealthPickup::healthTypeTemporary_s; 314 case pickupHealthType::permanent: 315 return HealthPickup::healthTypePermanent_s; 316 default: 317 COUT(1) << "Invalid healthType in HealthPickup." << std::endl; 318 return BLANKSTRING; 319 } 320 } 321 322 /** 323 @brief 324 Sets the health. 325 @param health 326 The health. 327 */ 328 void HealthPickup::setHealth(float health) 329 { 330 if(health > 0.0f) 331 { 332 this->health_ = health; 333 } 334 else 335 { 336 COUT(1) << "Invalid health in HealthPickup." << std::endl; 337 this->health_ = 0.0; 338 } 339 } 340 341 /** 342 @brief 343 Set the rate at which health is transferred if the pickup is continuous. 344 @param rate 345 The rate. 346 */ 347 void HealthPickup::setHealthRate(float rate) 348 { 349 if(rate >= 0) 350 { 351 this->healthRate_ = rate; 352 } 353 else 354 { 355 COUT(1) << "Invalid healthSpeed in HealthPickup." << std::endl; 356 } 357 } 358 359 /** 360 @brief 361 Set the type of the HealthPickup. 362 @param type 363 The type as a string. 364 */ 365 void HealthPickup::setHealthType(std::string type) 366 { 367 if(type == HealthPickup::healthTypeLimited_s) 368 { 369 this->setHealthTypeDirect(pickupHealthType::limited); 370 } 371 else if(type == HealthPickup::healthTypeTemporary_s) 372 { 373 this->setHealthTypeDirect(pickupHealthType::temporary); 374 } 375 else if(type == HealthPickup::healthTypePermanent_s) 376 { 377 this->setHealthTypeDirect(pickupHealthType::permanent); 378 } 379 else 380 { 381 COUT(1) << "Invalid healthType in HealthPickup." << std::endl; 259 382 } 260 383 }
Note: See TracChangeset
for help on using the changeset viewer.