Changeset 9348 for code/trunk/src/modules/pickup/PickupCollection.cc
- Timestamp:
- Aug 30, 2012, 11:08:17 PM (12 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:ignore
-
old new 1 .project 1 2 build 2 3 codeblocks 4 dependencies 3 5 vs 4 dependencies
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
code/trunk/src/modules/pickup/PickupCollection.cc
r8305 r9348 38 38 39 39 #include "CollectiblePickup.h" 40 #include "DroppedPickup.h" 41 #include "PickupCollectionIdentifier.h" 40 #include "PickupSpawner.h" 42 41 43 42 #include "PickupCollection.h" … … 54 53 The creator of the object. 55 54 */ 56 PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator) , pickupCollectionIdentifier_(NULL)55 PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator) 57 56 { 58 57 RegisterObject(PickupCollection); 59 58 60 this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(this);61 this->usedCounter_ = 0;62 this->pickedUpCounter_ = 0;63 this->disabledCounter_ = 0;64 59 this->processingUsed_ = false; 65 60 this->processingPickedUp_ = false; … … 70 65 Destructor. Iterates through all Pickupables this PickupCollection consists of and destroys them if they haven't been already. 71 66 */ 72 PickupCollection::~ 67 PickupCollection::~PickupCollection() 73 68 { 74 69 // Destroy all Pickupables constructing this PickupCollection. 75 for(std:: vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)76 { 77 (*it)-> removeFromCollection();70 for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) 71 { 72 (*it)->wasRemovedFromCollection(); 78 73 (*it)->destroy(); 79 74 } 80 75 this->pickups_.clear(); 81 82 if(this->pickupCollectionIdentifier_ != NULL)83 delete this->pickupCollectionIdentifier_;84 76 } 85 77 … … 92 84 SUPER(PickupCollection, XMLPort, xmlelement, mode); 93 85 86 XMLPortParam(PickupCollection, "representation", setRepresentationName, getRepresentationName, xmlelement, mode); 94 87 XMLPortObject(PickupCollection, CollectiblePickup, "pickupables", addPickupable, getPickupable, xmlelement, mode); 95 96 this->initializeIdentifier();97 }98 99 /**100 @brief101 Initializes the PickupIdentifier for this pickup.102 */103 void PickupCollection::initializeIdentifier(void)104 {105 for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)106 {107 this->pickupCollectionIdentifier_->addPickup((*it)->getPickupIdentifier());108 }109 88 } 110 89 … … 120 99 this->processingUsed_ = true; 121 100 // Change used for all Pickupables this PickupCollection consists of. 122 for(std:: vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)101 for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) 123 102 (*it)->setUsed(this->isUsed()); 124 103 … … 138 117 return; 139 118 119 size_t numPickupsEnabled = 0; 120 size_t numPickupsInUse = 0; 121 for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) 122 { 123 if ((*it)->isEnabled()) 124 ++numPickupsEnabled; 125 if ((*it)->isUsed()) 126 ++numPickupsInUse; 127 } 128 140 129 // If all the pickups are not in use but the PickupCollection is. 141 if( this->usedCounter_== 0 && this->isUsed())130 if(numPickupsInUse == 0 && this->isUsed()) 142 131 this->setUsed(false); 143 132 144 133 // If all the enabled pickups are in use but the PickupCollection is not. 145 if( this->usedCounter_ != 0 && this->usedCounter_ == this->pickups_.size()-this->disabledCounter_&& !this->isUsed())134 if(numPickupsInUse > 0 && numPickupsInUse == numPickupsEnabled && !this->isUsed()) 146 135 this->setUsed(true); 147 136 } … … 157 146 158 147 // Change the PickupCarrier for all Pickupables this PickupCollection consists of. 159 for(std:: vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)148 for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) 160 149 { 161 150 if(this->getCarrier() == NULL) … … 177 166 this->processingPickedUp_ = true; 178 167 // Change the pickedUp status for all Pickupables this PickupCollection consists of. 179 for(std:: vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)180 (* it)->setPickedUp(this->isPickedUp());168 for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ) 169 (*(it++))->setPickedUp(this->isPickedUp()); 181 170 182 171 this->processingPickedUp_ = false; … … 195 184 return; 196 185 197 // If at least all the enabled pickups of this PickupCollection are no longer picked up. 198 if(this->pickedUpCounter_ <= this->disabledCounter_ && this->isPickedUp()) 186 // If at least all the enabled pickups of this PickupCollection are no longer picked up. 187 bool isOnePickupEnabledAndPickedUp = false; 188 for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) 189 { 190 if ((*it)->isEnabled() && (*it)->isPickedUp()) 191 { 192 isOnePickupEnabledAndPickedUp = true; 193 break; 194 } 195 } 196 if(!isOnePickupEnabledAndPickedUp && this->isPickedUp()) 199 197 this->Pickupable::destroy(); 200 201 // If the PickupCollection is no longer picked up.202 if(!this->isPickedUp())203 this->pickedUpCounter_ = 0;204 }205 206 /**207 @brief208 Creates a duplicate of the input Pickupable.209 This method needs to be implemented by any Class inheriting from Pickupable.210 @param item211 A reference to a pointer to the OrxonoxClass that is to be duplicated.212 */213 void PickupCollection::clone(OrxonoxClass*& item)214 {215 if(item == NULL)216 item = new PickupCollection(this);217 218 SUPER(PickupCollection, clone, item);219 220 PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);221 // Clone all Pickupables this PickupCollection consist of.222 for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)223 {224 Pickupable* newPickup = (*it)->clone();225 CollectiblePickup* collectible = static_cast<CollectiblePickup*>(newPickup);226 pickup->addPickupable(collectible);227 }228 229 pickup->initializeIdentifier();230 198 } 231 199 … … 240 208 bool PickupCollection::isTarget(const PickupCarrier* carrier) const 241 209 { 242 for(std:: vector<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)210 for(std::list<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) 243 211 { 244 212 if(!carrier->isTarget(*it)) … … 247 215 248 216 return true; 249 }250 251 /**252 @brief253 Get the PickupIdentifier of this PickupCollection.254 This is in fact the PickupCollectionIdentifier.255 @return256 Returns a pointer to the PickupIdentifier of this PickupCollection.257 */258 const PickupIdentifier* PickupCollection::getPickupIdentifier(void) const259 {260 return this->pickupCollectionIdentifier_;261 217 } 262 218 … … 274 230 return false; 275 231 276 pickup->addToCollection(this);277 232 this->pickups_.push_back(pickup); 233 pickup->wasAddedToCollection(this); 234 this->pickupsChanged(); 278 235 return true; 279 236 } … … 289 246 const Pickupable* PickupCollection::getPickupable(unsigned int index) const 290 247 { 291 return this->pickups_[index]; 248 if(this->pickups_.size() >= index) 249 return NULL; 250 251 std::list<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); 252 std::advance(it, index); 253 return *it; 254 } 255 256 /** 257 @brief 258 Removes the Pickup from the Collection. 259 @param pickup 260 The Pickup to be removed. 261 @return 262 Returns true if the pickup was in the collection. 263 */ 264 bool PickupCollection::removePickupable(CollectiblePickup* pickup) 265 { 266 for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) 267 { 268 if (*it == pickup) 269 { 270 this->pickups_.erase(it); 271 pickup->wasRemovedFromCollection(); 272 this->pickupsChanged(); 273 return true; 274 } 275 } 276 return false; 292 277 } 293 278 … … 297 282 This is used internally by the CollectiblePickup class. 298 283 @param changed 299 The value the used status has changed to. 284 The value the used status has changed to. 300 285 */ 301 286 void PickupCollection::pickupChangedUsed(bool changed) 302 287 { 303 if(changed)304 this->usedCounter_++;305 else306 this->usedCounter_--;307 308 288 this->changedUsedAction(); 309 289 } … … 318 298 void PickupCollection::pickupChangedPickedUp(bool changed) 319 299 { 320 if(changed)321 this->pickedUpCounter_++;322 else323 this->pickedUpCounter_--;324 325 300 this->changedPickedUpAction(); 326 301 } … … 333 308 void PickupCollection::pickupDisabled(void) 334 309 { 335 this->disabledCounter_++; 310 } 311 312 /** 313 @brief 314 Helpfer function if the number of pickups in this collection has changed. 315 */ 316 void PickupCollection::pickupsChanged(void) 317 { 318 this->changedUsedAction(); 319 this->changedPickedUpAction(); 336 320 } 337 321 … … 339 323 @brief 340 324 Facilitates the creation of a PickupSpawner upon dropping of the Pickupable. 341 This method must be implemented by any class directly inheriting from Pickupable. It is most easily done by just creating a new DroppedPickup, e.g.:342 DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position);343 325 @return 344 326 Returns true if a spawner was created, false if not. … … 346 328 bool PickupCollection::createSpawner(void) 347 329 { 348 newDroppedPickup(this, this, this->getCarrier());330 PickupSpawner::createDroppedPickup(this, this, this->getCarrier()); 349 331 return true; 350 332 }
Note: See TracChangeset
for help on using the changeset viewer.