Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/PickupManager.cc @ 7497

Last change on this file since 7497 was 7494, checked in by dafrick, 14 years ago

Some documenting and cleaning up/re-organization in pickups module.

  • Property svn:eol-style set to native
File size: 11.3 KB
RevLine 
[6474]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
[7494]23 *      Damian 'Mozork' Frick
[6474]24 *   Co-authors:
25 *      ...
26 *
27*/
28
29/**
[6540]30    @file PickupManager.cc
[6474]31    @brief Implementation of the PickupManager class.
32*/
33
34#include "PickupManager.h"
35
[7284]36#include "util/Convert.h"
37#include "util/ScopedSingletonManager.h"
[6474]38#include "core/CoreIncludes.h"
[6711]39#include "core/LuaState.h"
40#include "core/GUIManager.h"
[6474]41#include "core/Identifier.h"
42#include "interfaces/PickupCarrier.h"
[6711]43#include "infos/PlayerInfo.h"
[6474]44#include "worldentities/pawns/Pawn.h"
[7163]45#include "CollectiblePickup.h"
[6474]46#include "PickupRepresentation.h"
47
[6711]48#include "ToluaBindPickup.h"
49
[6474]50namespace orxonox
51{
[6711]52    // Register tolua_open function when loading the library
53    DeclareToluaInterface(Pickup);
[7163]54
[7209]55    ManageScopedSingleton(PickupManager, ScopeID::Graphics, false);
[7163]56
[6711]57    /*static*/ const std::string PickupManager::guiName_s = "PickupInventory";
[7163]58
[6474]59    /**
60    @brief
61        Constructor. Registers the PickupManager and creates the default PickupRepresentation.
62    */
[6540]63    PickupManager::PickupManager() : defaultRepresentation_(NULL)
[6474]64    {
65        RegisterRootObject(PickupManager);
[7163]66
[7209]67        GUIManager::getInstance().loadGUI(PickupManager::guiName_s);
68
[6474]69        this->defaultRepresentation_ = new PickupRepresentation();
[7163]70
[6725]71        COUT(3) << "PickupManager created." << std::endl;
[6474]72    }
[7163]73
[6474]74    /**
75    @brief
76        Destructor.
77        Destroys the default PickupRepresentation.
78    */
79    PickupManager::~PickupManager()
80    {
81        if(this->defaultRepresentation_ != NULL)
82            this->defaultRepresentation_->destroy();
[7163]83
[6725]84        this->representations_.clear();
[7163]85
[6725]86        COUT(3) << "PickupManager destroyed." << std::endl;
[6474]87    }
[7163]88
[6474]89    /**
90    @brief
91        Registers a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents.
92        For every type of Pickupable (uniquely idnetified by a PickupIdentifier) there can be one (and just one) PickupRepresentation registered.
93    @param identifier
94        The PickupIdentifier identifying the Pickupable.
95    @param representation
96        A pointer to the PickupRepresentation.
97    @return
98        Returns true if successful and false if not.
99    */
[6475]100    bool PickupManager::registerRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation)
[7163]101    {
[7494]102        assert(identifier);
103        assert(representation);
104
105        if(this->representations_.find(identifier) != this->representations_.end()) // If the Pickupable already has a Representation registered.
[6474]106            return false;
[7163]107
[6474]108        this->representations_[identifier] = representation;
[7163]109
[7494]110        COUT(4) << "PickupRepresentation &" << representation << " registered with the PickupManager." << std::endl;
[6474]111        return true;
112    }
[7163]113
[6474]114    /**
115    @brief
[6725]116        Unegisters a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents.
117    @param identifier
118        The PickupIdentifier identifying the Pickupable.
119    @param representation
120        A pointer to the PickupRepresentation.
121    @return
122        Returns true if successful and false if not.
123    */
124    bool PickupManager::unregisterRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation)
[7163]125    {
[7494]126        assert(identifier);
127        assert(representation);
[7163]128
[6725]129        std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier);
130        if(it == this->representations_.end()) //!< If the Pickupable is not registered in the first place.
131            return false;
[7163]132
[6725]133        this->representations_.erase(it);
[7163]134
[7494]135        COUT(4) << "PickupRepresentation &" << representation << " unregistered with the PickupManager." << std::endl;
[6725]136        return true;
137    }
[7163]138
[6725]139    /**
140    @brief
[6474]141        Get the PickupRepresentation representing the Pickupable with the input PickupIdentifier.
142    @param identifier
143        The PickupIdentifier.
144    @return
145        Returns a pointer to the PickupRepresentation.
146    */
147    PickupRepresentation* PickupManager::getRepresentation(const PickupIdentifier* identifier)
148    {
[6475]149        std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier);
[6474]150        if(it == this->representations_.end())
151        {
152            COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl;
153            return this->defaultRepresentation_;
154        }
[7163]155
[6474]156        return it->second;
157    }
[7163]158
[7206]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());
[7494]172
[7206]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    */
[7163]183    int PickupManager::getNumPickups(void)
[6711]184    {
[7206]185        this->pickupsList_.clear(); // Clear the list if Pickupables.
[7163]186
[6752]187        PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s);
[7163]188        PickupCarrier* carrier = NULL;
[6752]189        if (player != NULL)
[7206]190            carrier =  orxonox_cast<PickupCarrier*>(player->getControllableEntity());
[6752]191        else
[6711]192            return 0;
[7163]193
[7206]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.
[7163]196        for(std::vector<PickupCarrier*>::iterator it = carriers->begin(); it != carriers->end(); it++)
197        {
198            std::set<Pickupable*> pickups = (*it)->getPickups();
199            for(std::set<Pickupable*>::iterator pickup = pickups.begin(); pickup != pickups.end(); pickup++)
200            {
201                CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(*pickup);
[7206]202                // If the Pickupable is in a PickupCollection it is not added to the list and thus not displayed in the PickupInventory.
[7163]203                if(collectible == NULL || !collectible->isInCollection())
204                    this->pickupsList_.insert(std::pair<Pickupable*, WeakPtr<Pickupable> >(*pickup, WeakPtr<Pickupable>(*pickup)));
205            }
206        }
207        delete carriers;
208
[7206]209        this->pickupsIterator_ = this->pickupsList_.begin(); //Set the iterator to the start of the list.
[7163]210        return this->pickupsList_.size();
[6711]211    }
[7163]212
[7206]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)
[6711]225    {
[7206]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().
[7163]233        for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++)
[7206]234            this->getAllCarriers(*it, carriers);
[7163]235        delete children;
[7206]236
[7163]237        return carriers;
[6711]238    }
[7163]239
[7206]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    */
[7163]247    void PickupManager::dropPickup(orxonox::Pickupable* pickup)
[6711]248    {
[7206]249        if(pickup == NULL)
[7163]250            return;
251
[7206]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)
[7163]255            return;
256
[7206]257        pickup->drop(); // The Pickupable is dropped.
[6711]258    }
[7163]259
[7206]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    */
[7163]269    void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use)
[6711]270    {
[7206]271        if(pickup == NULL)
[7163]272            return;
[7494]273
[7206]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)
[7163]277            return;
278
[7206]279        pickup->setUsed(use); // The Pickupable is used (or unused).
[6711]280    }
[7163]281
[7206]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.
296    }
297
[6474]298}
Note: See TracBrowser for help on using the repository browser.