Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network4/src/modules/pickup/PickupManager.cc @ 7633

Last change on this file since 7633 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
Line 
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 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27*/
28
29/**
30    @file PickupManager.cc
31    @brief Implementation of the PickupManager class.
32*/
33
34#include "PickupManager.h"
35
36#include "util/Convert.h"
37#include "util/ScopedSingletonManager.h"
38#include "core/CoreIncludes.h"
39#include "core/LuaState.h"
40#include "core/GUIManager.h"
41#include "core/Identifier.h"
42#include "interfaces/PickupCarrier.h"
43#include "infos/PlayerInfo.h"
44#include "worldentities/pawns/Pawn.h"
45#include "CollectiblePickup.h"
46#include "PickupRepresentation.h"
47
48#include "ToluaBindPickup.h"
49
50namespace orxonox
51{
52    // Register tolua_open function when loading the library
53    DeclareToluaInterface(Pickup);
54
55    ManageScopedSingleton(PickupManager, ScopeID::Graphics, false);
56
57    /*static*/ const std::string PickupManager::guiName_s = "PickupInventory";
58
59    /**
60    @brief
61        Constructor. Registers the PickupManager and creates the default PickupRepresentation.
62    */
63    PickupManager::PickupManager() : defaultRepresentation_(NULL)
64    {
65        RegisterRootObject(PickupManager);
66
67        GUIManager::getInstance().loadGUI(PickupManager::guiName_s);
68
69        this->defaultRepresentation_ = new PickupRepresentation();
70
71        COUT(3) << "PickupManager created." << std::endl;
72    }
73
74    /**
75    @brief
76        Destructor.
77        Destroys the default PickupRepresentation.
78    */
79    PickupManager::~PickupManager()
80    {
81        if(this->defaultRepresentation_ != NULL)
82            this->defaultRepresentation_->destroy();
83
84        this->representations_.clear();
85
86        COUT(3) << "PickupManager destroyed." << std::endl;
87    }
88
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    */
100    bool PickupManager::registerRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation)
101    {
102        assert(identifier);
103        assert(representation);
104
105        if(this->representations_.find(identifier) != this->representations_.end()) // If the Pickupable already has a Representation registered.
106            return false;
107
108        this->representations_[identifier] = representation;
109
110        COUT(4) << "PickupRepresentation &" << representation << " registered with the PickupManager." << std::endl;
111        return true;
112    }
113
114    /**
115    @brief
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)
125    {
126        assert(identifier);
127        assert(representation);
128
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;
132
133        this->representations_.erase(it);
134
135        COUT(4) << "PickupRepresentation &" << representation << " unregistered with the PickupManager." << std::endl;
136        return true;
137    }
138
139    /**
140    @brief
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    {
149        std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier);
150        if(it == this->representations_.end())
151        {
152            COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl;
153            return this->defaultRepresentation_;
154        }
155
156        return it->second;
157    }
158
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    */
183    int PickupManager::getNumPickups(void)
184    {
185        this->pickupsList_.clear(); // Clear the list if Pickupables.
186
187        PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s);
188        PickupCarrier* carrier = NULL;
189        if (player != NULL)
190            carrier =  orxonox_cast<PickupCarrier*>(player->getControllableEntity());
191        else
192            return 0;
193
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.
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);
202                // If the Pickupable is in a PickupCollection it is not added to the list and thus not displayed in the PickupInventory.
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
209        this->pickupsIterator_ = this->pickupsList_.begin(); //Set the iterator to the start of the list.
210        return this->pickupsList_.size();
211    }
212
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().
233        for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++)
234            this->getAllCarriers(*it, carriers);
235        delete children;
236
237        return carriers;
238    }
239
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    */
247    void PickupManager::dropPickup(orxonox::Pickupable* pickup)
248    {
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    */
269    void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use)
270    {
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.
296    }
297
298}
Note: See TracBrowser for help on using the repository browser.