Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 30, 2012, 11:08:17 PM (12 years ago)
Author:
landauf
Message:

merged branch presentation2012merge back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/modules/pickup/PickupCollection.cc

    r8305 r9348  
    3838
    3939#include "CollectiblePickup.h"
    40 #include "DroppedPickup.h"
    41 #include "PickupCollectionIdentifier.h"
     40#include "PickupSpawner.h"
    4241
    4342#include "PickupCollection.h"
     
    5453        The creator of the object.
    5554    */
    56     PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator), pickupCollectionIdentifier_(NULL)
     55    PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator)
    5756    {
    5857        RegisterObject(PickupCollection);
    5958
    60         this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(this);
    61         this->usedCounter_ = 0;
    62         this->pickedUpCounter_ = 0;
    63         this->disabledCounter_ = 0;
    6459        this->processingUsed_ = false;
    6560        this->processingPickedUp_ = false;
     
    7065        Destructor. Iterates through all Pickupables this PickupCollection consists of and destroys them if they haven't been already.
    7166    */
    72     PickupCollection::~ PickupCollection()
     67    PickupCollection::~PickupCollection()
    7368    {
    7469        // 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();
    7873            (*it)->destroy();
    7974        }
    8075        this->pickups_.clear();
    81        
    82         if(this->pickupCollectionIdentifier_ != NULL)
    83             delete this->pickupCollectionIdentifier_;
    8476    }
    8577
     
    9284        SUPER(PickupCollection, XMLPort, xmlelement, mode);
    9385
     86        XMLPortParam(PickupCollection, "representation", setRepresentationName, getRepresentationName, xmlelement, mode);
    9487        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         }
    10988    }
    11089
     
    12099        this->processingUsed_ = true;
    121100        // 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)
    123102            (*it)->setUsed(this->isUsed());
    124103
     
    138117            return;
    139118
     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
    140129        // 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())
    142131            this->setUsed(false);
    143132
    144133        // 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())
    146135            this->setUsed(true);
    147136    }
     
    157146
    158147        // 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)
    160149        {
    161150            if(this->getCarrier() == NULL)
     
    177166        this->processingPickedUp_ = true;
    178167        // 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());
    181170
    182171        this->processingPickedUp_ = false;
     
    195184            return;
    196185
    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())
    199197            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     @brief
    208         Creates a duplicate of the input Pickupable.
    209         This method needs to be implemented by any Class inheriting from Pickupable.
    210     @param item
    211         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();
    230198    }
    231199
     
    240208    bool PickupCollection::isTarget(const PickupCarrier* carrier) const
    241209    {
    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)
    243211        {
    244212            if(!carrier->isTarget(*it))
     
    247215
    248216        return true;
    249     }
    250 
    251     /**
    252     @brief
    253         Get the PickupIdentifier of this PickupCollection.
    254         This is in fact the PickupCollectionIdentifier.
    255     @return
    256         Returns a pointer to the PickupIdentifier of this PickupCollection.
    257     */
    258     const PickupIdentifier* PickupCollection::getPickupIdentifier(void) const
    259     {
    260         return this->pickupCollectionIdentifier_;
    261217    }
    262218
     
    274230            return false;
    275231
    276         pickup->addToCollection(this);
    277232        this->pickups_.push_back(pickup);
     233        pickup->wasAddedToCollection(this);
     234        this->pickupsChanged();
    278235        return true;
    279236    }
     
    289246    const Pickupable* PickupCollection::getPickupable(unsigned int index) const
    290247    {
    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;
    292277    }
    293278
     
    297282        This is used internally by the CollectiblePickup class.
    298283    @param changed
    299         The value the used status has changed to. 
     284        The value the used status has changed to.
    300285    */
    301286    void PickupCollection::pickupChangedUsed(bool changed)
    302287    {
    303         if(changed)
    304             this->usedCounter_++;
    305         else
    306             this->usedCounter_--;
    307 
    308288        this->changedUsedAction();
    309289    }
     
    318298    void PickupCollection::pickupChangedPickedUp(bool changed)
    319299    {
    320         if(changed)
    321             this->pickedUpCounter_++;
    322         else
    323             this->pickedUpCounter_--;
    324 
    325300        this->changedPickedUpAction();
    326301    }
     
    333308    void PickupCollection::pickupDisabled(void)
    334309    {
    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();
    336320    }
    337321
     
    339323    @brief
    340324        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);
    343325    @return
    344326        Returns true if a spawner was created, false if not.
     
    346328    bool PickupCollection::createSpawner(void)
    347329    {
    348         new DroppedPickup(this, this, this->getCarrier());
     330        PickupSpawner::createDroppedPickup(this, this, this->getCarrier());
    349331        return true;
    350332    }
Note: See TracChangeset for help on using the changeset viewer.