Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/modules/pickup/PickupManager.cc @ 7161

Last change on this file since 7161 was 7150, checked in by dafrick, 14 years ago

Untangled Pickupable and PickupCarrier a little bit.

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