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 "CollectiblePickup.h" |
---|
46 | #include "PickupRepresentation.h" |
---|
47 | |
---|
48 | #include "ToluaBindPickup.h" |
---|
49 | |
---|
50 | namespace orxonox |
---|
51 | { |
---|
52 | // Register tolua_open function when loading the library |
---|
53 | DeclareToluaInterface(Pickup); |
---|
54 | |
---|
55 | ManageScopedSingleton(PickupManager, ScopeID::Graphics, false); |
---|
56 | |
---|
57 | /*static*/ const std::string PickupManager::guiName_s = "PickupInventory"; |
---|
58 | |
---|
59 | /** |
---|
60 | @brief |
---|
61 | Constructor. Registers the PickupManager and creates the default PickupRepresentation. |
---|
62 | */ |
---|
63 | PickupManager::PickupManager() : defaultRepresentation_(NULL) |
---|
64 | { |
---|
65 | RegisterRootObject(PickupManager); |
---|
66 | |
---|
67 | GUIManager::getInstance().loadGUI(PickupManager::guiName_s); |
---|
68 | |
---|
69 | this->defaultRepresentation_ = new PickupRepresentation(); |
---|
70 | |
---|
71 | COUT(3) << "PickupManager created." << std::endl; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | @brief |
---|
76 | Destructor. |
---|
77 | Destroys the default PickupRepresentation. |
---|
78 | */ |
---|
79 | PickupManager::~PickupManager() |
---|
80 | { |
---|
81 | if(this->defaultRepresentation_ != NULL) |
---|
82 | this->defaultRepresentation_->destroy(); |
---|
83 | |
---|
84 | this->representations_.clear(); |
---|
85 | |
---|
86 | COUT(3) << "PickupManager destroyed." << std::endl; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | @brief |
---|
91 | Registers a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents. |
---|
92 | For every type of Pickupable (uniquely idnetified by a PickupIdentifier) there can be one (and just one) PickupRepresentation registered. |
---|
93 | @param identifier |
---|
94 | The PickupIdentifier identifying the Pickupable. |
---|
95 | @param representation |
---|
96 | A pointer to the PickupRepresentation. |
---|
97 | @return |
---|
98 | Returns true if successful and false if not. |
---|
99 | */ |
---|
100 | bool PickupManager::registerRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation) |
---|
101 | { |
---|
102 | if(identifier == NULL || representation == NULL || this->representations_.find(identifier) != this->representations_.end()) //!< If the Pickupable already has a Representation registered. |
---|
103 | return false; |
---|
104 | |
---|
105 | this->representations_[identifier] = representation; |
---|
106 | |
---|
107 | COUT(4) << "PickupRepresentation " << representation << " registered with the PickupManager." << std::endl; |
---|
108 | return true; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | @brief |
---|
113 | Unegisters a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents. |
---|
114 | @param identifier |
---|
115 | The PickupIdentifier identifying the Pickupable. |
---|
116 | @param representation |
---|
117 | A pointer to the PickupRepresentation. |
---|
118 | @return |
---|
119 | Returns true if successful and false if not. |
---|
120 | */ |
---|
121 | bool PickupManager::unregisterRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation) |
---|
122 | { |
---|
123 | if(identifier == NULL || representation == NULL) |
---|
124 | return false; |
---|
125 | |
---|
126 | std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier); |
---|
127 | if(it == this->representations_.end()) //!< If the Pickupable is not registered in the first place. |
---|
128 | return false; |
---|
129 | |
---|
130 | this->representations_.erase(it); |
---|
131 | |
---|
132 | COUT(4) << "PickupRepresentation " << representation << " unregistered with the PickupManager." << std::endl; |
---|
133 | return true; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | @brief |
---|
138 | Get the PickupRepresentation representing the Pickupable with the input PickupIdentifier. |
---|
139 | @param identifier |
---|
140 | The PickupIdentifier. |
---|
141 | @return |
---|
142 | Returns a pointer to the PickupRepresentation. |
---|
143 | */ |
---|
144 | PickupRepresentation* PickupManager::getRepresentation(const PickupIdentifier* identifier) |
---|
145 | { |
---|
146 | std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier); |
---|
147 | if(it == this->representations_.end()) |
---|
148 | { |
---|
149 | COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl; |
---|
150 | return this->defaultRepresentation_; |
---|
151 | } |
---|
152 | |
---|
153 | return it->second; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | @brief |
---|
158 | Get the PickupRepresentation of an input Pickupable. |
---|
159 | This method spares us the hassle to export the PickupIdentifier class to lua. |
---|
160 | @param pickup |
---|
161 | The Pickupable whose PickupRepresentation should be returned. |
---|
162 | @return |
---|
163 | Returns the PickupRepresentation of the input Pickupable or NULL if an error occurred. |
---|
164 | */ |
---|
165 | orxonox::PickupRepresentation* PickupManager::getPickupRepresentation(orxonox::Pickupable* pickup) |
---|
166 | { |
---|
167 | if(pickup != NULL) |
---|
168 | return this->getRepresentation(pickup->getPickupIdentifier()); |
---|
169 | |
---|
170 | return NULL; |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | @brief |
---|
175 | Update the list of picked up Pickupables and get the number of Pickupables in the list. |
---|
176 | This method is used in lua to populate the PickupInventory. The intended usage is to call this method to update the list of Pickupables and then use popPickup() to get the individual Pickupables. |
---|
177 | @return |
---|
178 | Returns the number of the players picked up Pickupables. |
---|
179 | */ |
---|
180 | int PickupManager::getNumPickups(void) |
---|
181 | { |
---|
182 | this->pickupsList_.clear(); // Clear the list if Pickupables. |
---|
183 | |
---|
184 | PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s); |
---|
185 | PickupCarrier* carrier = NULL; |
---|
186 | if (player != NULL) |
---|
187 | carrier = orxonox_cast<PickupCarrier*>(player->getControllableEntity()); |
---|
188 | else |
---|
189 | return 0; |
---|
190 | |
---|
191 | std::vector<PickupCarrier*>* carriers = this->getAllCarriers(carrier); // Get a list of all the entities which carry the players Pickupables. |
---|
192 | // Iterate through all the carriers and add their Pickupable to the list. |
---|
193 | for(std::vector<PickupCarrier*>::iterator it = carriers->begin(); it != carriers->end(); it++) |
---|
194 | { |
---|
195 | std::set<Pickupable*> pickups = (*it)->getPickups(); |
---|
196 | for(std::set<Pickupable*>::iterator pickup = pickups.begin(); pickup != pickups.end(); pickup++) |
---|
197 | { |
---|
198 | CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(*pickup); |
---|
199 | // If the Pickupable is in a PickupCollection it is not added to the list and thus not displayed in the PickupInventory. |
---|
200 | if(collectible == NULL || !collectible->isInCollection()) |
---|
201 | this->pickupsList_.insert(std::pair<Pickupable*, WeakPtr<Pickupable> >(*pickup, WeakPtr<Pickupable>(*pickup))); |
---|
202 | } |
---|
203 | } |
---|
204 | delete carriers; |
---|
205 | |
---|
206 | this->pickupsIterator_ = this->pickupsList_.begin(); //Set the iterator to the start of the list. |
---|
207 | return this->pickupsList_.size(); |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | @brief |
---|
212 | Helper method. Get all the PickupCarriers that carry Pickupables, recursively. |
---|
213 | @param carrier |
---|
214 | The PickupCarrier whose children should be added to the list of PickupCarriers. |
---|
215 | @param carriers |
---|
216 | The list of PickupCarriers, used in "recursive-mode". |
---|
217 | For the first instance this is just NULL (which is default and can be omitted) upon which a new list is allocated. |
---|
218 | @return |
---|
219 | Returns the list of PickupCarriers. |
---|
220 | */ |
---|
221 | std::vector<PickupCarrier*>* PickupManager::getAllCarriers(PickupCarrier* carrier, std::vector<PickupCarrier*>* carriers) |
---|
222 | { |
---|
223 | if(carriers == NULL) // Create a new list if no list was passed. |
---|
224 | carriers = new std::vector<PickupCarrier*>(); |
---|
225 | |
---|
226 | carriers->insert(carriers->end(), carrier); // Add the input PickupCarrier to the list. |
---|
227 | |
---|
228 | std::vector<PickupCarrier*>* children = carrier->getCarrierChildren(); // Get the children of the input PickupCarrier. |
---|
229 | // Iterate through the children and add them (and their children) to the list by recursively executing getAllCarriers(). |
---|
230 | for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++) |
---|
231 | this->getAllCarriers(*it, carriers); |
---|
232 | delete children; |
---|
233 | |
---|
234 | return carriers; |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | @brief |
---|
239 | Drop the input Pickupable. |
---|
240 | This method checks whether the inout Pickupable still exists and drops it, if so. |
---|
241 | @param pickup |
---|
242 | The Pickupable to be dropped. |
---|
243 | */ |
---|
244 | void PickupManager::dropPickup(orxonox::Pickupable* pickup) |
---|
245 | { |
---|
246 | if(pickup == NULL) |
---|
247 | return; |
---|
248 | |
---|
249 | std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable. |
---|
250 | // If either the input Pickupable is not in the PickupManagers list or it no longer exists, the method returns. |
---|
251 | if(it == this->pickupsList_.end() || it->second.get() == NULL) |
---|
252 | return; |
---|
253 | |
---|
254 | pickup->drop(); // The Pickupable is dropped. |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | @brief |
---|
259 | Use (or unuse) the input Pickupable. |
---|
260 | This method checks whether the input Pickupable still exists and uses (or unuses) it, if so, |
---|
261 | @param pickup |
---|
262 | The Pickupable to be used (or unused). |
---|
263 | @param use |
---|
264 | If true the input Pickupable is used, if false it is unused. |
---|
265 | */ |
---|
266 | void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use) |
---|
267 | { |
---|
268 | if(pickup == NULL) |
---|
269 | return; |
---|
270 | |
---|
271 | std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable. |
---|
272 | // If either the input Pickupable is not in the PickupManagers list or it no longer exists, the method returns. |
---|
273 | if(it == this->pickupsList_.end() || it->second.get() == NULL) |
---|
274 | return; |
---|
275 | |
---|
276 | pickup->setUsed(use); // The Pickupable is used (or unused). |
---|
277 | } |
---|
278 | |
---|
279 | /** |
---|
280 | @brief |
---|
281 | Check whether the input Pickupable is valid, meaning that it is in the PickupManager's list and still exists. |
---|
282 | @param pickup |
---|
283 | The Pickupable. |
---|
284 | @return |
---|
285 | Returns true if the input Pickupable is still valid, false if not. |
---|
286 | */ |
---|
287 | bool PickupManager::isValidPickup(orxonox::Pickupable* pickup) |
---|
288 | { |
---|
289 | std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable. |
---|
290 | if(it == this->pickupsList_.end()) // If the Pickupable is not in the PickupManager's list. |
---|
291 | return false; |
---|
292 | return it->second.get() != NULL; // Returns whether the Pickupable still exists. |
---|
293 | } |
---|
294 | |
---|
295 | } |
---|