Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9290


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

Location:
code/branches/presentation2012merge/src/modules/pickup
Files:
6 edited

Legend:

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

    r7494 r9290  
    4545        Registers the object and initializes variables.
    4646    */
    47     CollectiblePickup::CollectiblePickup() : isInCollection_(false)
     47    CollectiblePickup::CollectiblePickup() : collection_(NULL)
    4848    {
    4949        RegisterObject(CollectiblePickup);
    50 
    51         this->collection_ = NULL;
    5250    }
    5351
     
    5856    CollectiblePickup::~CollectiblePickup()
    5957    {
    60 
    61     }
    62 
    63     /**
    64     @brief
    65         Is called by OrxonoxClass::destroy() before the object is actually destroyed.
    66     */
    67     void CollectiblePickup::preDestroy(void)
    68     {
    69         this->Pickupable::preDestroy();
    70 
    71         // The PickupCollection has to be destroyed as well.
    72         if(this->isInCollection())
    73             this->collection_->Pickupable::destroy();
    74     }
    75 
    76     /**
    77     @brief
    78         Destroys a Pickupable.
    79     */
    80     void CollectiblePickup::destroyPickup(void)
    81     {
    82         if(!this->isInCollection()) // If the CollectiblePickup is not in a PickupCollection the destroyPickup method of Pickupable is called.
    83             this->Pickupable::destroyPickup();
    84         else // Else the ColectiblePickup is dropped and disabled,
    85         {
    86             this->drop(false);
    87             if(this->isInCollection() && this->isEnabled()) // It is only disabled if it is enabled and still ina PickupCollection after having been dropped.
    88             {
    89                 this->setDisabled();
    90                 this->collection_->pickupDisabled();
    91             }
    92         }
     58        if (this->isInCollection())
     59            this->collection_->removePickupable(this);
    9360    }
    9461
     
    13198    /**
    13299    @brief
    133         Adds this CollectiblePickup to the input PickupCollection.
     100        Notifies this CollectiblePickup that it was added to a PickupCollection.
    134101    @param collection
    135102        A pointer to the PickupCollection to which the CollectiblePickup should be added.
    136     @return
    137         Returns true if the CollectiblePickup was successfully added to the PickupCollection.
    138103    */
    139     bool CollectiblePickup::addToCollection(PickupCollection* collection)
     104    void CollectiblePickup::wasAddedToCollection(PickupCollection* collection)
    140105    {
    141         if(this->isInCollection() || collection == NULL) //If the CollectiblePickup already is in a PickupCollection or if the input pointer is NULL.
    142             return false;
    143 
    144         this->isInCollection_ = true;
    145106        this->collection_ = collection;
    146         return true;
    147107    }
    148108
    149109    /**
    150110    @brief
    151         Removes this CollectiblePickup from its PickupCollection.
    152     @return
    153         Returns true if the CollectiblePickup was succcessfully removed.
     111        Notifies this CollectiblePickup that it was removed from its PickupCollection.
    154112    */
    155     bool CollectiblePickup::removeFromCollection(void)
     113    void CollectiblePickup::wasRemovedFromCollection(void)
    156114    {
    157         if(!this->isInCollection()) //If the CollectiblePickup is not in a PickupCollection.
    158             return false;
    159 
    160         this->isInCollection_ = false;
    161115        this->collection_ = NULL;
    162         return true;
    163116    }
    164 
    165117}
  • code/branches/presentation2012merge/src/modules/pickup/CollectiblePickup.h

    r7547 r9290  
    5555    class _PickupExport CollectiblePickup : public Pickupable
    5656    {
     57        friend class PickupCollection;
    5758
    5859        public:
     
    6869            */
    6970            bool isInCollection(void) const
    70                 { return this->isInCollection_; }
    71 
    72             bool addToCollection(PickupCollection* collection); //!< Adds this CollectiblePickup to the input PickupCollection.
    73             bool removeFromCollection(void); //!< Removes this CollectiblePickup from its PickupCollection.
     71                { return this->collection_ != NULL; }
    7472
    7573            void carrierDestroyed(void); //!< Is called by the PickupCarrier when it is being destroyed.
    7674
    77         protected:
    78             virtual void preDestroy(void); //!< Is called by OrxonoxClass::destroy() before the object is actually destroyed.
    79             virtual void destroyPickup(void); //!< Destroys a Pickupable.
     75        private:
     76            void wasAddedToCollection(PickupCollection* collection);
     77            void wasRemovedFromCollection(void);
    8078
    81         private:
    82             bool isInCollection_; //!< True if the CollectiblePickup is in a PickupCollection.
    8379            PickupCollection* collection_; //!< A pointer to the PickupCollection this CollectiblePickup is in.
    84 
    8580    };
    8681}
  • 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
  • code/branches/presentation2012merge/src/modules/pickup/PickupCollection.h

    r8351 r9290  
    8989            bool addPickupable(CollectiblePickup* pickup); //!< Add the input Pickupable to list of Pickupables combined by this PickupCollection.
    9090            const Pickupable* getPickupable(unsigned int index) const; //!< Get the Pickupable at the given index.
     91            bool removePickupable(CollectiblePickup* pickup); //!< Removes the input Pickupable from the list of Pickupables in this PickupCollection.
     92
     93            inline const std::list<CollectiblePickup*>& getPickups() const
     94                { return this->pickups_; }
    9195
    9296            void pickupChangedUsed(bool changed); //!< Informs the PickupCollection, that one of its pickups has changed its used status to the input value.
     
    9599
    96100        protected:
    97             void initializeIdentifier(void); //!< Initializes the PickupIdentifier for this pickup.
    98 
    99101            virtual bool createSpawner(void); //!< Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
    100102
     
    105107            void changedPickedUpAction(void); //!< Helper method.
    106108
    107             std::vector<CollectiblePickup*> pickups_; //!< The list of the pointers of all the Pickupables this PickupCollection consists of. They are weak pointers to facilitate testing, whether the pointers are still valid.
     109            std::list<CollectiblePickup*> pickups_; //!< The list of the pointers of all the Pickupables this PickupCollection consists of. They are weak pointers to facilitate testing, whether the pointers are still valid.
    108110
    109111            unsigned int usedCounter_; //!< Keeps track of the number of pickups of this PickupCollection, that are in use.
  • code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.cc

    r7533 r9290  
    3535
    3636#include "PickupCollectionIdentifier.h"
     37#include "PickupCollection.h"
    3738
    3839namespace orxonox
     
    4344        Constructor. Registers the object.
    4445    */
    45     PickupCollectionIdentifier::PickupCollectionIdentifier(Pickupable* pickup) : PickupIdentifier(pickup)
     46    PickupCollectionIdentifier::PickupCollectionIdentifier(PickupCollection* collection) : PickupIdentifier(collection)
    4647    {
    4748        RegisterObject(PickupCollectionIdentifier);
     49
     50        this->collection_ = collection;
    4851    }
    4952
     
    8083
    8184        // If the number of Pickupables each of the two PickupCollectionIdentifiers contain differ, the one with less is considered smaller.
    82         if(this->identifiers_.size() != collectionIdentifier->identifiers_.size())
    83             return this->identifiers_.size()-collectionIdentifier->identifiers_.size();
     85        if(this->collection_->getPickups().size() != collectionIdentifier->collection_->getPickups().size())
     86            return this->collection_->getPickups().size()-collectionIdentifier->collection_->getPickups().size();
    8487
    8588        // Compare the Pickupables of the two PickupCollectionIdentifiers one after the other. the one with the first 'smaller' one is considered smaller.
    86         std::set<const PickupIdentifier*, PickupIdentifierCompare>::const_iterator it2 = collectionIdentifier->identifiers_.begin();
    87         for(std::set<const PickupIdentifier*, PickupIdentifierCompare>::const_iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); it++)
     89        std::list<CollectiblePickup*>::const_iterator it1 = this->collection_->getPickups().begin();
     90        std::list<CollectiblePickup*>::const_iterator it2 = collectionIdentifier->collection_->getPickups().begin();
     91        for( ; it1 != this->collection_->getPickups().end(); ++it1, ++it2)
    8892        {
     93            const PickupIdentifier* id1 = (*it1)->getPickupIdentifier();
     94            const PickupIdentifier* id2 = (*it2)->getPickupIdentifier();
    8995
    90             if((*it)->compare(*it2) < 0)
     96            if(id1->compare(id2) < 0)
    9197                return -1;
    92             if((*it2)->compare(*it) < 0)
     98            if(id2->compare(id1) < 0)
    9399                return 1;
    94100        }
     
    98104    }
    99105
    100     /**
    101     @brief
    102         Add a Pickupable to the PickupCollectionIdentifier.
    103     @param identifier
    104         A pointer to the PickupIdentifier of the Pickupable to be added.
    105     */
    106     void PickupCollectionIdentifier::addPickup(const PickupIdentifier* identifier)
    107     {
    108         this->identifiers_.insert(identifier);
    109     }
    110 
    111106}
    112107
  • code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.h

    r8729 r9290  
    6161
    6262        public:
    63             PickupCollectionIdentifier(Pickupable* pickup); //!< Constructor.
     63            PickupCollectionIdentifier(PickupCollection* pickup); //!< Constructor.
    6464            ~PickupCollectionIdentifier(); //!< Destructor.
    6565
    6666            virtual int compare(const PickupIdentifier* identifier) const; //!< Compares a PickupCollectionIdentifier with a PickupIdentifier.
    6767
    68             void addPickup(const PickupIdentifier* identifier); //!< Add a @ref orxonox::Pickupable "Pickupable" to the PickupCollectionIdentifier.
    69 
    7068        private:
    71             std::set<const PickupIdentifier*, PickupIdentifierCompare> identifiers_; //!< The set of PickupIdentifiers of the @ref orxonox::Pickupable "Pickupables", the @ref orxonox::PickupCollection "PickupCollection" with this PickupCollectionIdentifier consists of, ordered by the rule set by @ref orxonox::PickupIdentifierCompare "PickupIdentifierCompare".
     69            PickupCollection* collection_;
    7270
    7371    };
Note: See TracChangeset for help on using the changeset viewer.