Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6822 was 6752, checked in by rgrieder, 15 years ago

Fixed PickupManager::getPawn() method against NULL pointers.
(Doesn't fix the issue I currently have though!)

File size: 7.0 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 *      ...
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 "core/CoreIncludes.h"
37#include "core/LuaState.h"
38#include "core/GUIManager.h"
39#include "core/ScopedSingletonManager.h"
40#include "core/Identifier.h"
41#include "interfaces/PickupCarrier.h"
42#include "infos/PlayerInfo.h"
43#include "worldentities/pawns/Pawn.h"
44#include "PickupRepresentation.h"
45
46#include "ToluaBindPickup.h"
47
48namespace orxonox
49{
50    // Register tolua_open function when loading the library
51    DeclareToluaInterface(Pickup);
52   
53    ManageScopedSingleton(PickupManager, ScopeID::Graphics, false);
54   
55    /*static*/ const std::string PickupManager::guiName_s = "PickupInventory";
56   
57    /**
58    @brief
59        Constructor. Registers the PickupManager and creates the default PickupRepresentation.
60    */
61    PickupManager::PickupManager() : defaultRepresentation_(NULL)
62    {
63        RegisterRootObject(PickupManager);
64       
65        this->defaultRepresentation_ = new PickupRepresentation();
66       
67        COUT(3) << "PickupManager created." << std::endl;
68    }
69   
70    /**
71    @brief
72        Destructor.
73        Destroys the default PickupRepresentation.
74    */
75    PickupManager::~PickupManager()
76    {
77        if(this->defaultRepresentation_ != NULL)
78            this->defaultRepresentation_->destroy();
79       
80        this->representations_.clear();
81       
82        COUT(3) << "PickupManager destroyed." << std::endl;
83    }
84   
85    /**
86    @brief
87        Registers a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents.
88        For every type of Pickupable (uniquely idnetified by a PickupIdentifier) there can be one (and just one) PickupRepresentation registered.
89    @param identifier
90        The PickupIdentifier identifying the Pickupable.
91    @param representation
92        A pointer to the PickupRepresentation.
93    @return
94        Returns true if successful and false if not.
95    */
96    bool PickupManager::registerRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation)
97    {       
98        if(identifier == NULL || representation == NULL || this->representations_.find(identifier) != this->representations_.end()) //!< If the Pickupable already has a Representation registered.
99            return false;
100       
101        this->representations_[identifier] = representation;
102       
103        COUT(4) << "PickupRepresentation " << representation << " registered with the PickupManager." << std::endl;
104        return true;
105    }
106   
107    /**
108    @brief
109        Unegisters a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents.
110    @param identifier
111        The PickupIdentifier identifying the Pickupable.
112    @param representation
113        A pointer to the PickupRepresentation.
114    @return
115        Returns true if successful and false if not.
116    */
117    bool PickupManager::unregisterRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation)
118    {       
119        if(identifier == NULL || representation == NULL)
120            return false;
121       
122        std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier);
123        if(it == this->representations_.end()) //!< If the Pickupable is not registered in the first place.
124            return false;
125       
126        this->representations_.erase(it);
127       
128        COUT(4) << "PickupRepresentation " << representation << " unregistered with the PickupManager." << std::endl;
129        return true;
130    }
131   
132    /**
133    @brief
134        Get the PickupRepresentation representing the Pickupable with the input PickupIdentifier.
135    @param identifier
136        The PickupIdentifier.
137    @return
138        Returns a pointer to the PickupRepresentation.
139    */
140    PickupRepresentation* PickupManager::getRepresentation(const PickupIdentifier* identifier)
141    {
142        std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier);
143        if(it == this->representations_.end())
144        {
145            COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl;
146            return this->defaultRepresentation_;
147        }
148       
149        return it->second;
150    }
151   
152    PickupCarrier* PickupManager::getPawn(void)
153    {
154        PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s);
155        if (player != NULL)
156            return dynamic_cast<PickupCarrier*>(player->getControllableEntity());
157        else
158            return NULL;
159    }
160   
161    int PickupManager::getNumCarrierChildren(PickupCarrier* carrier)
162    {
163        if(carrier == NULL)
164            return 0;
165        return carrier->getNumCarrierChildren();
166    }
167           
168    PickupCarrier* PickupManager::getCarrierChild(int index, PickupCarrier* carrier)
169    {
170        if(carrier == NULL)
171            return NULL;
172        return carrier->getCarrierChild(index);
173    }
174   
175    const std::string& PickupManager::getCarrierName(orxonox::PickupCarrier* carrier)
176    {
177        if(carrier == NULL)
178            return BLANKSTRING;
179        return carrier->getCarrierName();
180    }
181   
182    PickupRepresentation* PickupManager::getPickupRepresentation(int index, PickupCarrier* carrier)
183    {
184        Pickupable* pickup = carrier->getPickup(index);
185        if(pickup == NULL)
186            return NULL;
187       
188        return this->getRepresentation(pickup->getPickupIdentifier());
189    }
190   
191    int PickupManager::getNumPickups(PickupCarrier* carrier)
192    {
193        if(carrier == NULL)
194            return 0;
195        return carrier->getNumPickups();
196    }
197   
198    void PickupManager::dropPickup(int index, PickupCarrier* carrier)
199    {
200        Pickupable* pickup = carrier->getPickup(index);
201        if(pickup != NULL)
202            carrier->drop(pickup);
203    }
204   
205    void PickupManager::usePickup(int index, PickupCarrier* carrier, bool use)
206    {
207        Pickupable* pickup = carrier->getPickup(index);
208        if(pickup != NULL)
209            pickup->setUsed(use);
210    }
211   
212}
Note: See TracBrowser for help on using the repository browser.