Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 11, 2010, 8:55:13 AM (14 years ago)
Author:
dafrick
Message:

Merged presentation3 branch into trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

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

    r6901 r7163  
    3535#include "core/XMLPort.h"
    3636#include "interfaces/PickupCarrier.h"
     37#include "CollectiblePickup.h"
    3738#include "DroppedPickup.h"
    3839#include "PickupCollectionIdentifier.h"
     
    4243namespace orxonox
    4344{
    44  
     45
    4546    CreateFactory(PickupCollection);
    4647
     
    5253    {
    5354        RegisterObject(PickupCollection);
    54        
     55
    5556        this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(this);
    56     }
    57    
     57        this->usedCounter_ = 0;
     58        this->pickedUpCounter_ = 0;
     59        this->disabledCounter_ = 0;
     60        this->processingUsed_ = false;
     61        this->processingPickedUp_ = false;
     62    }
     63
    5864    /**
    5965    @brief
     
    6268    PickupCollection::~PickupCollection()
    6369    {
    64         //! Destroy all Pickupables constructing this PickupCollection.
    65         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
    66         {
    67             if((*it).get() != NULL)
    68                 (*it).get()->destroy();
    69         }
    70     }
    71    
     70        // Destroy all Pickupables constructing this PickupCollection.
     71        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     72        {
     73            (*it)->removeFromCollection();
     74            (*it)->destroy();
     75        }
     76        this->pickups_.clear();
     77    }
     78
    7279    /**
    7380    @brief
     
    7784    {
    7885        SUPER(PickupCollection, XMLPort, xmlelement, mode);
    79        
    80         XMLPortObject(PickupCollection, Pickupable, "pickupables", addPickupable, getPickupable, xmlelement, mode);
    81        
     86
     87        XMLPortObject(PickupCollection, CollectiblePickup, "pickupables", addPickupable, getPickupable, xmlelement, mode);
     88
    8289        this->initializeIdentifier();
    8390    }
    84    
     91
    8592    /**
    8693    @brief
     
    8996    void PickupCollection::initializeIdentifier(void)
    9097    {
    91         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
    92         {
    93             this->pickupCollectionIdentifier_->addPickup((*it).get()->getPickupIdentifier());
    94         }
    95     }
    96    
     98        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     99        {
     100            this->pickupCollectionIdentifier_->addPickup((*it)->getPickupIdentifier());
     101        }
     102    }
     103
    97104    /**
    98105    @brief
     
    103110    {
    104111        SUPER(PickupCollection, changedUsed);
    105        
    106         //! Change used for all Pickupables this PickupCollection consists of.
    107         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
    108         {
    109             (*it).get()->setUsed(this->isUsed());
    110         }
    111     }
    112    
     112
     113        this->processingUsed_ = true;
     114        // Change used for all Pickupables this PickupCollection consists of.
     115        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     116        {
     117            (*it)->setUsed(this->isUsed());
     118        }
     119        this->processingUsed_ = false;
     120
     121        this->changedUsedAction();
     122    }
     123
     124    /**
     125    @brief
     126        Helper method.
     127        Checks whether due to changes in the used status of the pickups of this PickupCollection the used status of this PickupCollection has to change as well.
     128    */
     129    void PickupCollection::changedUsedAction(void)
     130    {
     131        if(this->processingUsed_)
     132            return;
     133
     134        // If all the pickups are not in use but the PickupCollection is.
     135        if(this->usedCounter_ == 0 && this->isUsed())
     136            this->setUsed(false);
     137
     138        // If all the enabled pickups are in use but the PickupCollection is not.
     139        if(this->usedCounter_ != 0 && this->usedCounter_ == this->pickups_.size()-this->disabledCounter_ && !this->isUsed())
     140            this->setUsed(true);
     141    }
     142
    113143    /**
    114144    @brief
     
    119149    {
    120150        SUPER(PickupCollection, changedCarrier);
    121        
    122         //! Change the PickupCarrier for all Pickupables this PickupCollection consists of.
    123         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
    124         {
    125             (*it).get()->setCarrier(this->getCarrier()->getTarget(*it));
    126         }
    127     }
    128    
     151
     152        // Change the PickupCarrier for all Pickupables this PickupCollection consists of.
     153        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     154        {
     155            if(this->getCarrier() == NULL)
     156                (*it)->setCarrier(NULL);
     157            else
     158                (*it)->setCarrier(this->getCarrier()->getTarget(*it));
     159        }
     160    }
     161
    129162    /**
    130163    @brief
     
    135168    {
    136169        SUPER(PickupCollection, changedPickedUp);
    137        
    138         //! Change the pickedUp status for all Pickupables this PickupCollection consists of.
    139         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
    140         {
    141             (*it).get()->setPickedUp(this->isPickedUp());
    142         }
    143     }
    144    
     170
     171        this->processingPickedUp_ = true;
     172        // Change the pickedUp status for all Pickupables this PickupCollection consists of.
     173        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     174        {
     175            (*it)->setPickedUp(this->isPickedUp());
     176        }
     177        this->processingPickedUp_ = false;
     178
     179        this->changedPickedUpAction();
     180    }
     181
     182    /**
     183    @brief
     184        Helper method.
     185        Checks whether due to changes in the picked up status of the pickups of this PickupCollection the picked up status of this PickupCollection has to change as well.
     186    */
     187    void PickupCollection::changedPickedUpAction(void)
     188    {
     189        if(this->processingPickedUp_)
     190            return;
     191
     192        // If at least all the enabled pickups of this PickupCollection are no longer picked up.
     193        if(this->pickedUpCounter_ <= this->disabledCounter_ && this->isPickedUp())
     194            this->Pickupable::destroy();
     195
     196        // If the PickupCollection is no longer picked up.
     197        if(!this->isPickedUp())
     198            this->pickedUpCounter_ = 0;
     199    }
     200
    145201    /**
    146202    @brief
     
    154210        if(item == NULL)
    155211            item = new PickupCollection(this);
    156        
     212
    157213        SUPER(PickupCollection, clone, item);
    158        
     214
    159215        PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);
    160         //! Clone all Pickupables this PickupCollection consist of.
    161         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
    162         {
    163             Pickupable* newPickup = (*it).get()->clone();
    164             pickup->addPickupable(newPickup);
     216        // Clone all Pickupables this PickupCollection consist of.
     217        for(std::vector<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     218        {
     219            Pickupable* newPickup = (*it)->clone();
     220            CollectiblePickup* collectible = static_cast<CollectiblePickup*>(newPickup);
     221            pickup->addPickupable(collectible);
    165222        }
    166223
    167224        pickup->initializeIdentifier();
    168225    }
    169    
     226
    170227    /**
    171228    @brief
     
    178235    bool PickupCollection::isTarget(PickupCarrier* carrier) const
    179236    {
    180         for(std::vector<WeakPtr<Pickupable> >::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
    181         {
    182             if(!carrier->isTarget((*it).get()))
     237        for(std::vector<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
     238        {
     239            if(!carrier->isTarget(*it))
    183240                return false;
    184241        }
    185        
     242
    186243        return true;
    187244    }
    188    
     245
    189246    /**
    190247    @brief
     
    198255        return this->pickupCollectionIdentifier_;
    199256    }
    200    
     257
    201258    /**
    202259    @brief
     
    207264        Returns true if successful,
    208265    */
    209     bool PickupCollection::addPickupable(Pickupable* pickup)
     266    bool PickupCollection::addPickupable(CollectiblePickup* pickup)
    210267    {
    211268        if(pickup == NULL)
    212269            return false;
    213        
    214         WeakPtr<Pickupable> ptr = pickup; //!< Create a weak pointer to be able to test in the constructor if the Pointer is still valid.
    215         this->pickups_.push_back(ptr);
     270
     271        pickup->addToCollection(this);
     272        this->pickups_.push_back(pickup);
    216273        return true;
    217274    }
    218    
     275
    219276    /**
    220277    @brief
     
    227284    const Pickupable* PickupCollection::getPickupable(unsigned int index)
    228285    {
    229         return this->pickups_[index].get();
    230     }
    231        
     286        return this->pickups_[index];
     287    }
     288
     289    /**
     290    @brief
     291        Informs the PickupCollection, that one of its pickups has changed its used status to the input value.
     292        This is used internally by the CollectiblePickup class.
     293    @param changed
     294        The value the used status has changed to.
     295    */
     296    void PickupCollection::pickupChangedUsed(bool changed)
     297    {
     298        if(changed)
     299            this->usedCounter_++;
     300        else
     301            this->usedCounter_--;
     302
     303        this->changedUsedAction();
     304    }
     305
     306    /**
     307    @brief
     308        Informs the PickupCollection, that one of its pickups has changed its picked up status to the input value.
     309        This is used internally by the CollectiblePickup class.
     310    @param changed
     311        The value the picked up status has changed to.
     312    */
     313    void PickupCollection::pickupChangedPickedUp(bool changed)
     314    {
     315        if(changed)
     316            this->pickedUpCounter_++;
     317        else
     318            this->pickedUpCounter_--;
     319
     320        this->changedPickedUpAction();
     321    }
     322
     323    /**
     324    @brief
     325        Informs the PickupCollection, that one of its pickups has been disabled.
     326        This is used internally by the CollectiblePickup class.
     327    */
     328    void PickupCollection::pickupDisabled(void)
     329    {
     330        this->disabledCounter_++;
     331    }
     332
    232333    /**
    233334    @brief
     
    245346        return true;
    246347    }
    247    
     348
    248349}
Note: See TracChangeset for help on using the changeset viewer.