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 | |
---|
48 | namespace 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 | int PickupManager::getNumCarrierChildren(PickupCarrier* carrier) |
---|
130 | { |
---|
131 | if(carrier == NULL) |
---|
132 | return 0; |
---|
133 | return carrier->getNumCarrierChildren(); |
---|
134 | } |
---|
135 | |
---|
136 | PickupCarrier* PickupManager::getCarrierChild(int index, PickupCarrier* carrier) |
---|
137 | { |
---|
138 | if(carrier == NULL) |
---|
139 | return NULL; |
---|
140 | return carrier->getCarrierChild(index); |
---|
141 | } |
---|
142 | |
---|
143 | const std::string& PickupManager::getCarrierName(orxonox::PickupCarrier* carrier) |
---|
144 | { |
---|
145 | if(carrier == NULL) |
---|
146 | return BLANKSTRING; |
---|
147 | return carrier->getCarrierName(); |
---|
148 | } |
---|
149 | |
---|
150 | PickupRepresentation* PickupManager::getPickupRepresentation(int index, PickupCarrier* carrier) |
---|
151 | { |
---|
152 | Pickupable* pickup = carrier->getPickup(index); |
---|
153 | if(pickup == NULL) |
---|
154 | return NULL; |
---|
155 | |
---|
156 | return this->getRepresentation(pickup->getPickupIdentifier()); |
---|
157 | } |
---|
158 | |
---|
159 | int PickupManager::getNumPickups(PickupCarrier* carrier) |
---|
160 | { |
---|
161 | if(carrier == NULL) |
---|
162 | return 0; |
---|
163 | return carrier->getNumPickups(); |
---|
164 | } |
---|
165 | |
---|
166 | void PickupManager::dropPickup(int index, PickupCarrier* carrier) |
---|
167 | { |
---|
168 | Pickupable* pickup = carrier->getPickup(index); |
---|
169 | carrier->drop(pickup); |
---|
170 | } |
---|
171 | |
---|
172 | void PickupManager::usePickup(int index, PickupCarrier* carrier, bool use) |
---|
173 | { |
---|
174 | Pickupable* pickup = carrier->getPickup(index); |
---|
175 | pickup->setUsed(use); |
---|
176 | } |
---|
177 | |
---|
178 | } |
---|