Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup4/src/modules/pickup/PickupManager.cc @ 6669

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

Committing recent changes in PickupInventory, to be able to try out some things and easily revert if they don't work out.

File size: 5.4 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::Root, 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   
68    /**
69    @brief
70        Destructor.
71        Destroys the default PickupRepresentation.
72    */
73    PickupManager::~PickupManager()
74    {
75        if(this->defaultRepresentation_ != NULL)
76            this->defaultRepresentation_->destroy();
77    }
78   
79    /**
80    @brief
81        Registers a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents.
82        For every type of Pickupable (uniquely idnetified by a PickupIdentifier) there can be one (and just one) PickupRepresentation registered.
83    @param identifier
84        The PickupIdentifier identifying the Pickupable.
85    @param representation
86        A pointer to the PickupRepresentation.
87    @return
88        Returns true if successful and false if not.
89    */
90    bool PickupManager::registerRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation)
91    {
92        if(this->representations_.find(identifier) != this->representations_.end()) //!< If the Pickupable already has a RepresentationRegistered.
93            return false;
94       
95        this->representations_[identifier] = representation;
96       
97        COUT(4) << "PickupRepresentation " << representation << " registered with the PickupManager." << std::endl;
98        return true;
99    }
100   
101    /**
102    @brief
103        Get the PickupRepresentation representing the Pickupable with the input PickupIdentifier.
104    @param identifier
105        The PickupIdentifier.
106    @return
107        Returns a pointer to the PickupRepresentation.
108    */
109    PickupRepresentation* PickupManager::getRepresentation(const PickupIdentifier* identifier)
110    {
111        std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier);
112        if(it == this->representations_.end())
113        {
114            COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl;
115            return this->defaultRepresentation_;
116        }
117       
118        return it->second;
119    }
120   
121    PickupCarrier* PickupManager::getPawn(void)
122    {
123        Pawn* pawn = dynamic_cast<Pawn*>(GUIManager::getInstancePtr()->getPlayer(PickupManager::guiName_s)->getControllableEntity());
124        if(pawn == NULL)
125            return NULL;
126        return dynamic_cast<PickupCarrier*>(pawn);
127    }
128   
129    unsigned int PickupManager::getNumCarrierChildren(PickupCarrier* carrier)
130    {
131        if(carrier == NULL)
132            return 0;
133        return carrier->getNumCarrierChildren();
134    }
135           
136    PickupCarrier* PickupManager::getCarrierChild(unsigned int index, PickupCarrier* carrier)
137    {
138        return carrier->getCarrierChild(index);
139    }
140   
141    PickupRepresentation* PickupManager::getPickupRepresentation(unsigned int index, PickupCarrier* carrier)
142    {
143        Pickupable* pickup = carrier->getPickup(index);
144        if(pickup == NULL)
145            return NULL;
146       
147        return this->getRepresentation(pickup->getPickupIdentifier());
148    }
149   
150
151    unsigned int PickupManager::getNumPickups(PickupCarrier* carrier)
152    {
153        return carrier->getNumPickups();
154    }
155   
156    void PickupManager::dropPickup(unsigned int index, PickupCarrier* carrier)
157    {
158        Pickupable* pickup = carrier->getPickup(index);
159        carrier->drop(pickup);
160    }
161   
162    void PickupManager::usePickup(unsigned int index, PickupCarrier* carrier, bool use)
163    {
164        Pickupable* pickup = carrier->getPickup(index);
165        pickup->setUsed(use);
166    }
167   
168}
Note: See TracBrowser for help on using the repository browser.