Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7206 for code/trunk


Ignore:
Timestamp:
Aug 23, 2010, 11:43:10 AM (14 years ago)
Author:
dafrick
Message:

Cleaning up and documenting PickupManager.
Also discovered and fixed a small bug.

Location:
code/trunk/src/modules/pickup
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/pickup/PickupManager.cc

    r7163 r7206  
    157157    }
    158158
     159    /**
     160    @brief
     161        Get the PickupRepresentation of an input Pickupable.
     162        This method spares us the hassle to export the PickupIdentifier class to lua.
     163    @param pickup
     164        The Pickupable whose PickupRepresentation should be returned.
     165    @return
     166        Returns the PickupRepresentation of the input Pickupable or NULL if an error occurred.
     167    */
     168    orxonox::PickupRepresentation* PickupManager::getPickupRepresentation(orxonox::Pickupable* pickup)
     169    {
     170        if(pickup != NULL)
     171            return this->getRepresentation(pickup->getPickupIdentifier());
     172       
     173        return NULL;
     174    }
     175
     176    /**
     177    @brief
     178        Update the list of picked up Pickupables and get the number of Pickupables in the list.
     179        This method is used in lua to populate the PickupInventory. The intended usage is to call this method to update the list of Pickupables and then use popPickup() to get the individual Pickupables.
     180    @return
     181        Returns the number of the players picked up Pickupables.
     182    */
    159183    int PickupManager::getNumPickups(void)
    160184    {
    161         this->pickupsList_.clear();
     185        this->pickupsList_.clear(); // Clear the list if Pickupables.
    162186
    163187        PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s);
    164188        PickupCarrier* carrier = NULL;
    165189        if (player != NULL)
    166             carrier =  dynamic_cast<PickupCarrier*>(player->getControllableEntity());
     190            carrier =  orxonox_cast<PickupCarrier*>(player->getControllableEntity());
    167191        else
    168192            return 0;
    169193
    170         std::vector<PickupCarrier*>* carriers = this->getAllCarriers(carrier);
     194        std::vector<PickupCarrier*>* carriers = this->getAllCarriers(carrier); // Get a list of all the entities which carry the players Pickupables.
     195        // Iterate through all the carriers and add their Pickupable to the list.
    171196        for(std::vector<PickupCarrier*>::iterator it = carriers->begin(); it != carriers->end(); it++)
    172197        {
     
    175200            {
    176201                CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(*pickup);
     202                // If the Pickupable is in a PickupCollection it is not added to the list and thus not displayed in the PickupInventory.
    177203                if(collectible == NULL || !collectible->isInCollection())
    178204                    this->pickupsList_.insert(std::pair<Pickupable*, WeakPtr<Pickupable> >(*pickup, WeakPtr<Pickupable>(*pickup)));
     
    181207        delete carriers;
    182208
    183         this->pickupsIterator_ = this->pickupsList_.begin();
     209        this->pickupsIterator_ = this->pickupsList_.begin(); //Set the iterator to the start of the list.
    184210        return this->pickupsList_.size();
    185211    }
    186212
    187     std::vector<PickupCarrier*>* PickupManager::getAllCarriers(PickupCarrier* carrier)
    188     {
    189         //TODO: More efficiently.
    190         std::vector<PickupCarrier*>* carriers = new std::vector<PickupCarrier*>();
    191         carriers->insert(carriers->end(), carrier);
    192         std::vector<PickupCarrier*>* children = carrier->getCarrierChildren();
     213    /**
     214    @brief
     215        Helper method. Get all the PickupCarriers that carry Pickupables, recursively.
     216    @param carrier
     217        The PickupCarrier whose children should be added to the list of PickupCarriers.
     218    @param carriers
     219        The list of PickupCarriers, used in "recursive-mode".
     220        For the first instance this is just NULL (which is default and can be omitted) upon which a new list is allocated.
     221    @return
     222        Returns the list of PickupCarriers.
     223    */
     224    std::vector<PickupCarrier*>* PickupManager::getAllCarriers(PickupCarrier* carrier, std::vector<PickupCarrier*>* carriers)
     225    {
     226        if(carriers == NULL) // Create a new list if no list was passed.
     227            carriers = new std::vector<PickupCarrier*>();
     228
     229        carriers->insert(carriers->end(), carrier); // Add the input PickupCarrier to the list.
     230
     231        std::vector<PickupCarrier*>* children = carrier->getCarrierChildren(); // Get the children of the input PickupCarrier.
     232        // Iterate through the children and add them (and their children) to the list by recursively executing getAllCarriers().
    193233        for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++)
    194         {
    195             std::vector<PickupCarrier*>* childrensChildren = this->getAllCarriers(*it);
    196             for(std::vector<PickupCarrier*>::iterator it2 = childrensChildren->begin(); it2 != childrensChildren->end(); it2++)
    197             {
    198                 carriers->insert(carriers->end(), *it2);
    199             }
    200             delete childrensChildren;
    201         }
     234            this->getAllCarriers(*it, carriers);
    202235        delete children;
     236
    203237        return carriers;
    204238    }
    205239
     240    /**
     241    @brief
     242        Drop the input Pickupable.
     243        This method checks whether the inout Pickupable still exists and drops it, if so.
     244    @param pickup
     245        The Pickupable to be dropped.
     246    */
    206247    void PickupManager::dropPickup(orxonox::Pickupable* pickup)
    207248    {
    208         std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup);
    209         if(pickup == NULL || it == this->pickupsList_.end() || it->second.get() == NULL)
    210             return;
    211 
    212         if(!pickup->isPickedUp())
    213             return;
    214 
    215         PickupCarrier* carrier = pickup->getCarrier();
    216         if(pickup != NULL && carrier != NULL)
    217         {
    218             pickup->drop(carrier);
    219         }
    220     }
    221 
     249        if(pickup == NULL)
     250            return;
     251
     252        std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable.
     253        // If either the input Pickupable is not in the PickupManagers list or it no longer exists, the method returns.
     254        if(it == this->pickupsList_.end() || it->second.get() == NULL)
     255            return;
     256
     257        pickup->drop(); // The Pickupable is dropped.
     258    }
     259
     260    /**
     261    @brief
     262        Use (or unuse) the input Pickupable.
     263        This method checks whether the input Pickupable still exists and uses (or unuses) it, if so,
     264    @param pickup
     265        The Pickupable to be used (or unused).
     266    @param use
     267        If true the input Pickupable is used, if false it is unused.
     268    */
    222269    void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use)
    223270    {
    224         std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup);
    225         if(pickup == NULL || it == this->pickupsList_.end() || it->second.get() == NULL)
    226             return;
    227 
    228         if(!pickup->isPickedUp())
    229             return;
    230 
    231         PickupCarrier* carrier = pickup->getCarrier();
    232         if(pickup != NULL && carrier != NULL)
    233             pickup->setUsed(use);
     271        if(pickup == NULL)
     272            return;
     273       
     274        std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable.
     275        // If either the input Pickupable is not in the PickupManagers list or it no longer exists, the method returns.
     276        if(it == this->pickupsList_.end() || it->second.get() == NULL)
     277            return;
     278
     279        pickup->setUsed(use); // The Pickupable is used (or unused).
     280    }
     281
     282    /**
     283    @brief
     284        Check whether the input Pickupable is valid, meaning that it is in the PickupManager's list and still exists.
     285    @param pickup
     286        The Pickupable.
     287    @return
     288        Returns true if the input Pickupable is still valid, false if not.
     289    */
     290    bool PickupManager::isValidPickup(orxonox::Pickupable* pickup)
     291    {
     292        std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable.
     293        if(it == this->pickupsList_.end()) // If the Pickupable is not in the PickupManager's list.
     294            return false;
     295        return it->second.get() != NULL; // Returns whether the Pickupable still exists.
    234296    }
    235297
  • code/trunk/src/modules/pickup/PickupManager.h

    r7163 r7206  
    5252        Manages Pickupables.
    5353        In essence has two tasks to fulfill. Firstly it must link Pickupables (through their PickupIdentifiers) and their PickupRepresentations. Secondly it manages the PickupInventory.
    54         //TODO: Manage Pickup GUI.
    5554    @author
    5655        Damian 'Mozork' Frick
     
    7271
    7372            // tolua_begin
    74             int getNumPickups(void);
     73            orxonox::PickupRepresentation* getPickupRepresentation(orxonox::Pickupable* pickup); //!< Get the PickupRepresentation of an input Pickupable.
     74
     75            int getNumPickups(void); //!< Update the list of picked up Pickupables and get the number of Pickupables in the list.
     76            /**
     77            @brief Get the next Pickupable in the list.
     78                   Use this, after having called getNumPickups() to access all the Pickupables individually and in succession.
     79            @return Returns the next Pickupable in the list.
     80            */
    7581            orxonox::Pickupable* popPickup(void) { return (this->pickupsIterator_++)->first; }
    76             orxonox::PickupRepresentation* getPickupRepresentation(orxonox::Pickupable* pickup) { if(pickup != NULL) return this->getRepresentation(pickup->getPickupIdentifier()); return NULL; }
    7782
    78             void dropPickup(orxonox::Pickupable* pickup);
    79             void usePickup(orxonox::Pickupable* pickup, bool use);
    80             bool isValidPickup(orxonox::Pickupable* pickup) { std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); if(it == this->pickupsList_.end()) return false; return it->second.get() != NULL; }
     83            void dropPickup(orxonox::Pickupable* pickup); //!< Drop the input Pickupable.
     84            void usePickup(orxonox::Pickupable* pickup, bool use); //!< Use (or unuse) the input Pickupable.
     85            bool isValidPickup(orxonox::Pickupable* pickup); //!< Check whether the input Pickupable is valid, meaning that it is in the PickupManager's list and still exists.
    8186            // tolua_end
    8287
    8388        private:
    8489            static PickupManager* singletonPtr_s;
    85             static const std::string guiName_s;
     90            static const std::string guiName_s; //!< The name of the PickupInventory
    8691
    8792            PickupRepresentation* defaultRepresentation_; //!< The default PickupRepresentation.
    8893            std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare> representations_; //!< Map linking PickupIdentifiers (representing types if Pickupables) and PickupRepresentations.
    8994
    90             std::map<Pickupable*, WeakPtr<Pickupable> > pickupsList_;
    91             std::map<Pickupable*, WeakPtr<Pickupable> >::iterator pickupsIterator_;
     95            std::map<Pickupable*, WeakPtr<Pickupable> > pickupsList_; //!< A list of all the picked up Pickupables.
     96            std::map<Pickupable*, WeakPtr<Pickupable> >::iterator pickupsIterator_; //!< An iterator pointing to the current Pickupable in pickupsList_.
    9297
    93             std::vector<PickupCarrier*>* getAllCarriers(PickupCarrier* carrier);
     98            std::vector<PickupCarrier*>* getAllCarriers(PickupCarrier* carrier, std::vector<PickupCarrier*>* carriers = NULL); //!< Helper method. Get all the PickupCarriers that carry Pickupables, recursively.
    9499
    95100    }; // tolua_export
Note: See TracChangeset for help on using the changeset viewer.