Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6741 was 6728, checked in by dafrick, 15 years ago

No more seg faults with pickups (at least that's what I hope. ;))

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        Pawn* pawn = dynamic_cast<Pawn*>(GUIManager::getInstancePtr()->getPlayer(PickupManager::guiName_s)->getControllableEntity());
155        if(pawn == NULL)
156            return NULL;
157        return dynamic_cast<PickupCarrier*>(pawn);
158    }
159   
160    int PickupManager::getNumCarrierChildren(PickupCarrier* carrier)
161    {
162        if(carrier == NULL)
163            return 0;
164        return carrier->getNumCarrierChildren();
165    }
166           
167    PickupCarrier* PickupManager::getCarrierChild(int index, PickupCarrier* carrier)
168    {
169        if(carrier == NULL)
170            return NULL;
171        return carrier->getCarrierChild(index);
172    }
173   
174    const std::string& PickupManager::getCarrierName(orxonox::PickupCarrier* carrier)
175    {
176        if(carrier == NULL)
177            return BLANKSTRING;
178        return carrier->getCarrierName();
179    }
180   
181    PickupRepresentation* PickupManager::getPickupRepresentation(int index, PickupCarrier* carrier)
182    {
183        Pickupable* pickup = carrier->getPickup(index);
184        if(pickup == NULL)
185            return NULL;
186       
187        return this->getRepresentation(pickup->getPickupIdentifier());
188    }
189   
190    int PickupManager::getNumPickups(PickupCarrier* carrier)
191    {
192        if(carrier == NULL)
193            return 0;
194        return carrier->getNumPickups();
195    }
196   
197    void PickupManager::dropPickup(int index, PickupCarrier* carrier)
198    {
199        Pickupable* pickup = carrier->getPickup(index);
200        if(pickup != NULL)
201            carrier->drop(pickup);
202    }
203   
204    void PickupManager::usePickup(int index, PickupCarrier* carrier, bool use)
205    {
206        Pickupable* pickup = carrier->getPickup(index);
207        if(pickup != NULL)
208            pickup->setUsed(use);
209    }
210   
211}
Note: See TracBrowser for help on using the repository browser.