Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/modules/pickup/PickupManager.cc @ 7335

Last change on this file since 7335 was 7207, checked in by landauf, 14 years ago

moved ScopedSingletonManager to util

  • Property svn:eol-style set to native
File size: 8.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
[7207]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
55    ManageScopedSingleton(PickupManager, ScopeID::Root, false);
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
67        //TODO: This doesn't work, yet.
68        if( GameMode::showsGraphics() )
69        {
70            GUIManager::getInstance().loadGUI(PickupManager::guiName_s);
71        }
[6474]72        this->defaultRepresentation_ = new PickupRepresentation();
[7163]73
[6725]74        COUT(3) << "PickupManager created." << std::endl;
[6474]75    }
[7163]76
[6474]77    /**
78    @brief
79        Destructor.
80        Destroys the default PickupRepresentation.
81    */
82    PickupManager::~PickupManager()
83    {
84        if(this->defaultRepresentation_ != NULL)
85            this->defaultRepresentation_->destroy();
[7163]86
[6725]87        this->representations_.clear();
[7163]88
[6725]89        COUT(3) << "PickupManager destroyed." << std::endl;
[6474]90    }
[7163]91
[6474]92    /**
93    @brief
94        Registers a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents.
95        For every type of Pickupable (uniquely idnetified by a PickupIdentifier) there can be one (and just one) PickupRepresentation registered.
96    @param identifier
97        The PickupIdentifier identifying the Pickupable.
98    @param representation
99        A pointer to the PickupRepresentation.
100    @return
101        Returns true if successful and false if not.
102    */
[6475]103    bool PickupManager::registerRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation)
[7163]104    {
[6725]105        if(identifier == NULL || representation == NULL || this->representations_.find(identifier) != this->representations_.end()) //!< If the Pickupable already has a Representation registered.
[6474]106            return false;
[7163]107
[6474]108        this->representations_[identifier] = representation;
[7163]109
[6474]110        COUT(4) << "PickupRepresentation " << representation << " registered with the PickupManager." << std::endl;
111        return true;
112    }
[7163]113
[6474]114    /**
115    @brief
[6725]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)
[7163]125    {
[6725]126        if(identifier == NULL || representation == NULL)
127            return false;
[7163]128
[6725]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;
[7163]132
[6725]133        this->representations_.erase(it);
[7163]134
[6725]135        COUT(4) << "PickupRepresentation " << representation << " unregistered with the PickupManager." << std::endl;
136        return true;
137    }
[7163]138
[6725]139    /**
140    @brief
[6474]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    {
[6475]149        std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier);
[6474]150        if(it == this->representations_.end())
151        {
152            COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl;
153            return this->defaultRepresentation_;
154        }
[7163]155
[6474]156        return it->second;
157    }
[7163]158
159    int PickupManager::getNumPickups(void)
[6711]160    {
[7163]161        this->pickupsList_.clear();
162
[6752]163        PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s);
[7163]164        PickupCarrier* carrier = NULL;
[6752]165        if (player != NULL)
[7163]166            carrier =  dynamic_cast<PickupCarrier*>(player->getControllableEntity());
[6752]167        else
[6711]168            return 0;
[7163]169
170        std::vector<PickupCarrier*>* carriers = this->getAllCarriers(carrier);
171        for(std::vector<PickupCarrier*>::iterator it = carriers->begin(); it != carriers->end(); it++)
172        {
173            std::set<Pickupable*> pickups = (*it)->getPickups();
174            for(std::set<Pickupable*>::iterator pickup = pickups.begin(); pickup != pickups.end(); pickup++)
175            {
176                CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(*pickup);
177                if(collectible == NULL || !collectible->isInCollection())
178                    this->pickupsList_.insert(std::pair<Pickupable*, WeakPtr<Pickupable> >(*pickup, WeakPtr<Pickupable>(*pickup)));
179            }
180        }
181        delete carriers;
182
183        this->pickupsIterator_ = this->pickupsList_.begin();
184        return this->pickupsList_.size();
[6711]185    }
[7163]186
187    std::vector<PickupCarrier*>* PickupManager::getAllCarriers(PickupCarrier* carrier)
[6711]188    {
[7163]189        //TODO: More efficiently.
190        std::vector<PickupCarrier*>* carriers = new std::vector<PickupCarrier*>();
191        carriers->insert(carriers->end(), carrier);
192        std::vector<PickupCarrier*>* children = carrier->getCarrierChildren();
193        for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++)
194        {
195            std::vector<PickupCarrier*>* childrensChildren = this->getAllCarriers(*it);
196            for(std::vector<PickupCarrier*>::iterator it2 = childrensChildren->begin(); it2 != childrensChildren->end(); it2++)
197            {
198                carriers->insert(carriers->end(), *it2);
199            }
200            delete childrensChildren;
201        }
202        delete children;
203        return carriers;
[6711]204    }
[7163]205
206    void PickupManager::dropPickup(orxonox::Pickupable* pickup)
[6711]207    {
[7163]208        std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup);
209        if(pickup == NULL || it == this->pickupsList_.end() || it->second.get() == NULL)
210            return;
211
212        if(!pickup->isPickedUp())
213            return;
214
215        PickupCarrier* carrier = pickup->getCarrier();
216        if(pickup != NULL && carrier != NULL)
217        {
218            pickup->drop(carrier);
219        }
[6711]220    }
[7163]221
222    void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use)
[6711]223    {
[7163]224        std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup);
225        if(pickup == NULL || it == this->pickupsList_.end() || it->second.get() == NULL)
226            return;
227
228        if(!pickup->isPickedUp())
229            return;
230
231        PickupCarrier* carrier = pickup->getCarrier();
232        if(pickup != NULL && carrier != NULL)
[6728]233            pickup->setUsed(use);
[6711]234    }
[7163]235
[6474]236}
Note: See TracBrowser for help on using the repository browser.