Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 10, 2012, 11:01:40 PM (12 years ago)
Author:
landauf
Message:

Fixed crash with MSVC if a PickupCollection was used
A depleted CollectiblePickup is now destroyed instead of being dropped
A destroyed CollectiblePickup removes itself from the PickupCollection
PickupCollection has to use a list instead of a vector because of this reason
Also PickupCollectionIdentifier needed to be changed because the number of pickups in a collection may now change
Probably also fixed a bug in PickupCollectionIdentifier::compare() because it2 was not incremented

not completely clean yet

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2012merge/src/modules/pickup/PickupCollection.cc

    r9279 r9290  
    7070        Destructor. Iterates through all Pickupables this PickupCollection consists of and destroys them if they haven't been already.
    7171    */
    72     PickupCollection::~ PickupCollection()
     72    PickupCollection::~PickupCollection()
    7373    {
    7474        // 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();
    78             (*it)->destroy();
     75        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
     76        {
     77            (*it)->wasRemovedFromCollection();
     78            (*it)->destroyPickup();
    7979        }
    8080        this->pickups_.clear();
     
    9393
    9494        XMLPortObject(PickupCollection, CollectiblePickup, "pickupables", addPickupable, getPickupable, xmlelement, mode);
    95 
    96         this->initializeIdentifier();
    97     }
    98 
    99     /**
    100     @brief
    101         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         }
    10995    }
    11096
     
    120106        this->processingUsed_ = true;
    121107        // Change used for all Pickupables this PickupCollection consists of.
    122         for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     108        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
    123109            (*it)->setUsed(this->isUsed());
    124110
     
    157143
    158144        // 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++)
     145        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
    160146        {
    161147            if(this->getCarrier() == NULL)
     
    177163        this->processingPickedUp_ = true;
    178164        // 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());
     165        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); )
     166            (*(it++))->setPickedUp(this->isPickedUp());
    181167
    182168        this->processingPickedUp_ = false;
     
    220206        PickupCollection* pickup = orxonox_cast<PickupCollection*>(item);
    221207        // Clone all Pickupables this PickupCollection consist of.
    222         for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     208        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
    223209        {
    224210            Pickupable* newPickup = (*it)->clone();
     
    226212            pickup->addPickupable(collectible);
    227213        }
    228 
    229         pickup->initializeIdentifier();
    230214    }
    231215
     
    240224    bool PickupCollection::isTarget(const PickupCarrier* carrier) const
    241225    {
    242         for(std::vector<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     226        for(std::list<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
    243227        {
    244228            if(!carrier->isTarget(*it))
     
    274258            return false;
    275259
    276         pickup->addToCollection(this);
    277260        this->pickups_.push_back(pickup);
     261        pickup->wasAddedToCollection(this);
    278262        return true;
    279263    }
     
    289273    const Pickupable* PickupCollection::getPickupable(unsigned int index) const
    290274    {
    291         return this->pickups_[index];
     275        unsigned int count = 0;
     276        for (std::list<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
     277        {
     278            if (count == index)
     279                return *it;
     280            else
     281                ++count;
     282        }
     283        return NULL;
     284    }
     285
     286    /**
     287    @brief
     288        Removes the Pickup from the Collection.
     289    @param pickup
     290        The Pickup to be removed.
     291    @return
     292        Returns true if the pickup was in the collection.
     293    */
     294    bool PickupCollection::removePickupable(CollectiblePickup* pickup)
     295    {
     296        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
     297        {
     298            if (*it == pickup)
     299            {
     300                this->pickups_.erase(it);
     301                pickup->wasRemovedFromCollection();
     302                return true;
     303            }
     304        }
     305        return false;
    292306    }
    293307
Note: See TracChangeset for help on using the changeset viewer.