Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7394 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • 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:
23 *      ...
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    {
[6725]102        if(identifier == NULL || representation == NULL || this->representations_.find(identifier) != this->representations_.end()) //!< If the Pickupable already has a Representation registered.
[6474]103            return false;
[7163]104
[6474]105        this->representations_[identifier] = representation;
[7163]106
[6474]107        COUT(4) << "PickupRepresentation " << representation << " registered with the PickupManager." << std::endl;
108        return true;
109    }
[7163]110
[6474]111    /**
112    @brief
[6725]113        Unegisters a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents.
114    @param identifier
115        The PickupIdentifier identifying the Pickupable.
116    @param representation
117        A pointer to the PickupRepresentation.
118    @return
119        Returns true if successful and false if not.
120    */
121    bool PickupManager::unregisterRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation)
[7163]122    {
[6725]123        if(identifier == NULL || representation == NULL)
124            return false;
[7163]125
[6725]126        std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier);
127        if(it == this->representations_.end()) //!< If the Pickupable is not registered in the first place.
128            return false;
[7163]129
[6725]130        this->representations_.erase(it);
[7163]131
[6725]132        COUT(4) << "PickupRepresentation " << representation << " unregistered with the PickupManager." << std::endl;
133        return true;
134    }
[7163]135
[6725]136    /**
137    @brief
[6474]138        Get the PickupRepresentation representing the Pickupable with the input PickupIdentifier.
139    @param identifier
140        The PickupIdentifier.
141    @return
142        Returns a pointer to the PickupRepresentation.
143    */
144    PickupRepresentation* PickupManager::getRepresentation(const PickupIdentifier* identifier)
145    {
[6475]146        std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier);
[6474]147        if(it == this->representations_.end())
148        {
149            COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl;
150            return this->defaultRepresentation_;
151        }
[7163]152
[6474]153        return it->second;
154    }
[7163]155
[7206]156    /**
157    @brief
158        Get the PickupRepresentation of an input Pickupable.
159        This method spares us the hassle to export the PickupIdentifier class to lua.
160    @param pickup
161        The Pickupable whose PickupRepresentation should be returned.
162    @return
163        Returns the PickupRepresentation of the input Pickupable or NULL if an error occurred.
164    */
165    orxonox::PickupRepresentation* PickupManager::getPickupRepresentation(orxonox::Pickupable* pickup)
166    {
167        if(pickup != NULL)
168            return this->getRepresentation(pickup->getPickupIdentifier());
169       
170        return NULL;
171    }
172
173    /**
174    @brief
175        Update the list of picked up Pickupables and get the number of Pickupables in the list.
176        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.
177    @return
178        Returns the number of the players picked up Pickupables.
179    */
[7163]180    int PickupManager::getNumPickups(void)
[6711]181    {
[7206]182        this->pickupsList_.clear(); // Clear the list if Pickupables.
[7163]183
[6752]184        PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s);
[7163]185        PickupCarrier* carrier = NULL;
[6752]186        if (player != NULL)
[7206]187            carrier =  orxonox_cast<PickupCarrier*>(player->getControllableEntity());
[6752]188        else
[6711]189            return 0;
[7163]190
[7206]191        std::vector<PickupCarrier*>* carriers = this->getAllCarriers(carrier); // Get a list of all the entities which carry the players Pickupables.
192        // Iterate through all the carriers and add their Pickupable to the list.
[7163]193        for(std::vector<PickupCarrier*>::iterator it = carriers->begin(); it != carriers->end(); it++)
194        {
195            std::set<Pickupable*> pickups = (*it)->getPickups();
196            for(std::set<Pickupable*>::iterator pickup = pickups.begin(); pickup != pickups.end(); pickup++)
197            {
198                CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(*pickup);
[7206]199                // If the Pickupable is in a PickupCollection it is not added to the list and thus not displayed in the PickupInventory.
[7163]200                if(collectible == NULL || !collectible->isInCollection())
201                    this->pickupsList_.insert(std::pair<Pickupable*, WeakPtr<Pickupable> >(*pickup, WeakPtr<Pickupable>(*pickup)));
202            }
203        }
204        delete carriers;
205
[7206]206        this->pickupsIterator_ = this->pickupsList_.begin(); //Set the iterator to the start of the list.
[7163]207        return this->pickupsList_.size();
[6711]208    }
[7163]209
[7206]210    /**
211    @brief
212        Helper method. Get all the PickupCarriers that carry Pickupables, recursively.
213    @param carrier
214        The PickupCarrier whose children should be added to the list of PickupCarriers.
215    @param carriers
216        The list of PickupCarriers, used in "recursive-mode".
217        For the first instance this is just NULL (which is default and can be omitted) upon which a new list is allocated.
218    @return
219        Returns the list of PickupCarriers.
220    */
221    std::vector<PickupCarrier*>* PickupManager::getAllCarriers(PickupCarrier* carrier, std::vector<PickupCarrier*>* carriers)
[6711]222    {
[7206]223        if(carriers == NULL) // Create a new list if no list was passed.
224            carriers = new std::vector<PickupCarrier*>();
225
226        carriers->insert(carriers->end(), carrier); // Add the input PickupCarrier to the list.
227
228        std::vector<PickupCarrier*>* children = carrier->getCarrierChildren(); // Get the children of the input PickupCarrier.
229        // Iterate through the children and add them (and their children) to the list by recursively executing getAllCarriers().
[7163]230        for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++)
[7206]231            this->getAllCarriers(*it, carriers);
[7163]232        delete children;
[7206]233
[7163]234        return carriers;
[6711]235    }
[7163]236
[7206]237    /**
238    @brief
239        Drop the input Pickupable.
240        This method checks whether the inout Pickupable still exists and drops it, if so.
241    @param pickup
242        The Pickupable to be dropped.
243    */
[7163]244    void PickupManager::dropPickup(orxonox::Pickupable* pickup)
[6711]245    {
[7206]246        if(pickup == NULL)
[7163]247            return;
248
[7206]249        std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable.
250        // If either the input Pickupable is not in the PickupManagers list or it no longer exists, the method returns.
251        if(it == this->pickupsList_.end() || it->second.get() == NULL)
[7163]252            return;
253
[7206]254        pickup->drop(); // The Pickupable is dropped.
[6711]255    }
[7163]256
[7206]257    /**
258    @brief
259        Use (or unuse) the input Pickupable.
260        This method checks whether the input Pickupable still exists and uses (or unuses) it, if so,
261    @param pickup
262        The Pickupable to be used (or unused).
263    @param use
264        If true the input Pickupable is used, if false it is unused.
265    */
[7163]266    void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use)
[6711]267    {
[7206]268        if(pickup == NULL)
[7163]269            return;
[7206]270       
271        std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable.
272        // If either the input Pickupable is not in the PickupManagers list or it no longer exists, the method returns.
273        if(it == this->pickupsList_.end() || it->second.get() == NULL)
[7163]274            return;
275
[7206]276        pickup->setUsed(use); // The Pickupable is used (or unused).
[6711]277    }
[7163]278
[7206]279    /**
280    @brief
281        Check whether the input Pickupable is valid, meaning that it is in the PickupManager's list and still exists.
282    @param pickup
283        The Pickupable.
284    @return
285        Returns true if the input Pickupable is still valid, false if not.
286    */
287    bool PickupManager::isValidPickup(orxonox::Pickupable* pickup)
288    {
289        std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable.
290        if(it == this->pickupsList_.end()) // If the Pickupable is not in the PickupManager's list.
291            return false;
292        return it->second.get() != NULL; // Returns whether the Pickupable still exists.
293    }
294
[6474]295}
Note: See TracBrowser for help on using the repository browser.