Changeset 7163 for code/trunk/src/orxonox/interfaces/Pickupable.cc
- Timestamp:
- Aug 11, 2010, 8:55:13 AM (14 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/interfaces/Pickupable.cc
- Property svn:eol-style set to native
r6901 r7163 34 34 #include "Pickupable.h" 35 35 36 #include "core/LuaState.h" 37 #include "core/GUIManager.h" 36 38 #include "core/Identifier.h" 37 39 #include "core/CoreIncludes.h" 40 #include "util/Convert.h" 41 #include "infos/PlayerInfo.h" 38 42 #include "pickup/PickupIdentifier.h" 43 #include "worldentities/pawns/Pawn.h" 39 44 #include "PickupCarrier.h" 40 45 41 46 namespace orxonox 42 47 { 43 48 44 49 /** 45 50 @brief … … 47 52 */ 48 53 Pickupable::Pickupable() : pickupIdentifier_(NULL), used_(false), pickedUp_(false) 49 { 54 { 50 55 RegisterRootObject(Pickupable); 51 56 52 57 this->carrier_ = NULL; 53 58 54 59 this->pickupIdentifier_ = new PickupIdentifier(this); 55 } 56 57 /** 58 @brief 59 Destructor. 60 this->beingDestroyed_ = false; 61 this->enabled_ = true; 62 } 63 64 /** 65 @brief 66 Destructor. 60 67 */ 61 68 Pickupable::~Pickupable() 62 69 { 63 if(this->isUsed()) 64 this->setUsed(false); 65 66 if(this->isPickedUp() && this->getCarrier() != NULL) 67 { 68 this->getCarrier()->drop(this, false); 69 this->setCarrier(NULL); 70 } 71 70 COUT(4) << "Pickupable (" << this->getIdentifier()->getName() << ") (&" << this << ") destroyed." << std::endl; 72 71 if(this->pickupIdentifier_ != NULL) 73 72 this->pickupIdentifier_->destroy(); 74 73 } 75 74 75 /** 76 @brief 77 A method that is called by OrxonoxClass::destroy() before the object is actually destroyed. 78 */ 79 void Pickupable::preDestroy(void) 80 { 81 this->beingDestroyed_ = true; 82 83 if(this->isPickedUp()) 84 this->drop(false); // Drops the pickup without creating a PickupSpawner. 85 } 86 87 /** 88 @brief 89 Is called internally within the pickup module to destroy pickups. 90 */ 91 void Pickupable::destroy(void) 92 { 93 this->destroyPickup(); 94 } 95 96 /** 97 @brief 98 Destroys a Pickupable. 99 If the Pickupable is already in the process of being destroyed a warning is displayed and this method is skipped. 100 */ 101 void Pickupable::destroyPickup(void) 102 { 103 if(!this->beingDestroyed_) 104 this->OrxonoxClass::destroy(); 105 else 106 COUT(2) << this->getIdentifier()->getName() << " may be unsafe. " << std::endl; 107 } 108 76 109 /** 77 110 @brief … … 84 117 bool Pickupable::setUsed(bool used) 85 118 { 86 if(this->used_ == used) 87 return false; 88 119 if(this->used_ == used || !this->isPickedUp()) // If either the used status of the Pickupable doesn't change or it isn't picked up. 120 return false; 121 122 if((!this->isUsable() && used) || (!this->isUnusable() && !used)) // If either the Pickupable is requested to be used but it is not usable or the Pickupable is requested to be unused, while it is not unusable. 123 return false; 124 89 125 COUT(4) << "Pickupable (&" << this << ") set to used " << used << "." << std::endl; 90 126 91 127 this->used_ = used; 92 128 this->changedUsed(); 93 return true; 94 } 95 129 130 GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()"); 131 return true; 132 } 133 96 134 /** 97 135 @brief … … 106 144 if(carrier == NULL) 107 145 return false; 146 108 147 return this->isTarget(carrier->getIdentifier()); 109 148 } 110 149 111 150 /** 112 151 @brief … … 125 164 return true; 126 165 } 166 127 167 return false; 128 168 } 129 169 130 170 /** 131 171 @brief … … 140 180 return this->addTarget(target->getIdentifier()); 141 181 } 142 182 143 183 /** 144 184 @brief … … 153 193 if(this->isTarget(target)) //!< If the input target is already present in the list of targets. 154 194 return false; 155 195 156 196 COUT(4) << "Target " << target->getName() << " added to Pickupable (&" << this << ")." << std::endl; 157 197 this->targets_.push_back(target); 158 198 return true; 159 199 } 160 161 /** 162 @brief 163 Sets the Pickupable to picked up. 164 This method will be called by the PickupCarrier picking the Pickupable up. 200 201 /** 202 @brief 203 Can be called to pick up a Pickupable. 165 204 @param carrier 166 The PickupCarrier that picked the Pickupable up. 167 @return 168 Returns false if, for some reason, the pickup could not be picked up, e.g. it was picked up already. 169 */ 170 bool Pickupable::pickedUp(PickupCarrier* carrier) 171 { 172 if(this->isPickedUp()) //!< If the Pickupable is already picked up. 173 return false; 205 A pointer to the PickupCarrier that picks up the Pickupable. 206 @return 207 Returns true if the Pickupable was picked up, false if not. 208 */ 209 bool Pickupable::pickup(PickupCarrier* carrier) 210 { 211 if(carrier == NULL || this->isPickedUp()) //!< If carrier is NULL or the Pickupable is already picked up. 212 return false; 213 214 if(!this->setCarrier(carrier)) 215 { 216 COUT(3) << "A Pickupable (&" << this << ") was trying to be added to a PickupCarrier, but was already present." << std::endl; 217 return false; 218 } 174 219 220 this->setPickedUp(true); 175 221 COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl; 176 this->setCarrier(carrier); 177 this->setPickedUp(true); 178 return true; 179 } 180 222 return true; 223 } 224 225 /** 226 @brief 227 Can be called to drop a Pickupable. 228 @param createSpawner 229 If true a spawner is to be created for the dropped Pickupable. True is default. 230 @return 231 Returns true if the Pickupable has been dropped, false if not. 232 */ 233 bool Pickupable::drop(bool createSpawner) 234 { 235 if(!this->isPickedUp()) // If the Pickupable is not picked up. 236 return false; 237 238 assert(this->getCarrier()); // The Carrier cannot be NULL at this point. 239 if(!this->getCarrier()->removePickup(this)) //TODO Shouldn't this be a little later? 240 COUT(2) << "Pickupable (&" << this << ", " << this->getIdentifier()->getName() << ") is being dropped, but it was not present in the PickupCarriers list of pickups." << std::endl; 241 242 COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl; 243 this->setUsed(false); 244 this->setPickedUp(false); 245 246 bool created = false; 247 if(createSpawner) 248 created = this->createSpawner(); 249 250 this->setCarrier(NULL); 251 252 if(!created && createSpawner) // If a PickupSpawner should have been created but wasn't. 253 this->destroy(); 254 255 return true; 256 } 257 181 258 /** 182 259 @brief … … 189 266 bool Pickupable::setPickedUp(bool pickedUp) 190 267 { 191 if(this->pickedUp_ == pickedUp) 192 return false; 193 268 if(this->pickedUp_ == pickedUp) // If the picked up status has not changed. 269 return false; 270 194 271 COUT(4) << "Pickupable (&" << this << ") set to pickedUp " << pickedUp << "." << std::endl; 195 272 196 273 this->pickedUp_ = pickedUp; 274 if(!pickedUp) // if the Pickupable has been dropped it unregisters itself with its PickupCarrier. 275 this->getCarrier()->removePickup(this); 197 276 this->changedPickedUp(); 198 return true; 199 } 200 277 GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()"); 278 return true; 279 } 280 201 281 /** 202 282 @brief … … 204 284 @param carrier 205 285 Sets the input PickupCarrier as the carrier of the pickup. 206 */ 207 inline bool Pickupable::setCarrier(PickupCarrier* carrier) 208 { 209 if(this->carrier_ == carrier) 210 return false; 211 286 @param tell 287 If true (default) the pickup is added to the list of pickups in the PickupCarrier. 288 @return 289 Returns true if successful, false if not. 290 */ 291 bool Pickupable::setCarrier(orxonox::PickupCarrier* carrier, bool tell) 292 { 293 if(this->carrier_ == carrier) // If the PickupCarrier doesn't change. 294 return false; 295 212 296 COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl; 297 298 if(carrier != NULL && tell) 299 { 300 if(!carrier->addPickup(this)) 301 return false; 302 } 213 303 214 304 this->carrier_ = carrier; … … 216 306 return true; 217 307 } 218 219 /** 220 @brief 221 Sets the Pickupable to not picked up or dropped. 222 This method will be called by the PickupCarrier dropping the Pickupable. 223 @return 224 Returns false if the pickup could not be dropped. 225 */ 226 bool Pickupable::dropped(void) 227 { 228 if(!this->isPickedUp()) //!< If the Pickupable is not picked up. 229 return false; 230 231 COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl; 232 this->setUsed(false); 233 this->setPickedUp(false); 234 235 bool created = this->createSpawner(); 236 237 this->setCarrier(NULL); 238 239 if(!created) 240 { 241 this->destroy(); 242 } 243 244 return true; 245 } 246 308 309 /** 310 @brief 311 Is called by the PickupCarrier when it is being destroyed. 312 */ 313 void Pickupable::carrierDestroyed(void) 314 { 315 this->destroy(); 316 } 317 247 318 /** 248 319 @brief … … 255 326 OrxonoxClass* item = NULL; 256 327 this->clone(item); 257 328 258 329 Pickupable* pickup = dynamic_cast<Pickupable*>(item); 259 330 260 331 COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl; 261 332 return pickup; 262 333 } 263 334 264 335 /** 265 336 @brief … … 273 344 SUPER(Pickupable, clone, item); 274 345 } 275 346 347 /** 348 @brief 349 Method to transcribe a Pickupable as a Rewardable to the player. 350 @param player 351 A pointer to the PlayerInfo, do whatever you want with it. 352 @return 353 Return true if successful. 354 */ 355 bool Pickupable::reward(PlayerInfo* player) 356 { 357 ControllableEntity* entity = player->getControllableEntity(); 358 Pawn* pawn = static_cast<Pawn*>(entity); 359 PickupCarrier* carrier = static_cast<PickupCarrier*>(pawn); 360 return this->pickup(carrier); 361 } 362 276 363 }
Note: See TracChangeset
for help on using the changeset viewer.