Changeset 7206 for code/trunk
- Timestamp:
- Aug 23, 2010, 11:43:10 AM (14 years ago)
- Location:
- code/trunk/src/modules/pickup
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/modules/pickup/PickupManager.cc
r7163 r7206 157 157 } 158 158 159 /** 160 @brief 161 Get the PickupRepresentation of an input Pickupable. 162 This method spares us the hassle to export the PickupIdentifier class to lua. 163 @param pickup 164 The Pickupable whose PickupRepresentation should be returned. 165 @return 166 Returns the PickupRepresentation of the input Pickupable or NULL if an error occurred. 167 */ 168 orxonox::PickupRepresentation* PickupManager::getPickupRepresentation(orxonox::Pickupable* pickup) 169 { 170 if(pickup != NULL) 171 return this->getRepresentation(pickup->getPickupIdentifier()); 172 173 return NULL; 174 } 175 176 /** 177 @brief 178 Update the list of picked up Pickupables and get the number of Pickupables in the list. 179 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. 180 @return 181 Returns the number of the players picked up Pickupables. 182 */ 159 183 int PickupManager::getNumPickups(void) 160 184 { 161 this->pickupsList_.clear(); 185 this->pickupsList_.clear(); // Clear the list if Pickupables. 162 186 163 187 PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s); 164 188 PickupCarrier* carrier = NULL; 165 189 if (player != NULL) 166 carrier = dynamic_cast<PickupCarrier*>(player->getControllableEntity());190 carrier = orxonox_cast<PickupCarrier*>(player->getControllableEntity()); 167 191 else 168 192 return 0; 169 193 170 std::vector<PickupCarrier*>* carriers = this->getAllCarriers(carrier); 194 std::vector<PickupCarrier*>* carriers = this->getAllCarriers(carrier); // Get a list of all the entities which carry the players Pickupables. 195 // Iterate through all the carriers and add their Pickupable to the list. 171 196 for(std::vector<PickupCarrier*>::iterator it = carriers->begin(); it != carriers->end(); it++) 172 197 { … … 175 200 { 176 201 CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(*pickup); 202 // If the Pickupable is in a PickupCollection it is not added to the list and thus not displayed in the PickupInventory. 177 203 if(collectible == NULL || !collectible->isInCollection()) 178 204 this->pickupsList_.insert(std::pair<Pickupable*, WeakPtr<Pickupable> >(*pickup, WeakPtr<Pickupable>(*pickup))); … … 181 207 delete carriers; 182 208 183 this->pickupsIterator_ = this->pickupsList_.begin(); 209 this->pickupsIterator_ = this->pickupsList_.begin(); //Set the iterator to the start of the list. 184 210 return this->pickupsList_.size(); 185 211 } 186 212 187 std::vector<PickupCarrier*>* PickupManager::getAllCarriers(PickupCarrier* carrier) 188 { 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(); 213 /** 214 @brief 215 Helper method. Get all the PickupCarriers that carry Pickupables, recursively. 216 @param carrier 217 The PickupCarrier whose children should be added to the list of PickupCarriers. 218 @param carriers 219 The list of PickupCarriers, used in "recursive-mode". 220 For the first instance this is just NULL (which is default and can be omitted) upon which a new list is allocated. 221 @return 222 Returns the list of PickupCarriers. 223 */ 224 std::vector<PickupCarrier*>* PickupManager::getAllCarriers(PickupCarrier* carrier, std::vector<PickupCarrier*>* carriers) 225 { 226 if(carriers == NULL) // Create a new list if no list was passed. 227 carriers = new std::vector<PickupCarrier*>(); 228 229 carriers->insert(carriers->end(), carrier); // Add the input PickupCarrier to the list. 230 231 std::vector<PickupCarrier*>* children = carrier->getCarrierChildren(); // Get the children of the input PickupCarrier. 232 // Iterate through the children and add them (and their children) to the list by recursively executing getAllCarriers(). 193 233 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 } 234 this->getAllCarriers(*it, carriers); 202 235 delete children; 236 203 237 return carriers; 204 238 } 205 239 240 /** 241 @brief 242 Drop the input Pickupable. 243 This method checks whether the inout Pickupable still exists and drops it, if so. 244 @param pickup 245 The Pickupable to be dropped. 246 */ 206 247 void PickupManager::dropPickup(orxonox::Pickupable* pickup) 207 248 { 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 } 220 } 221 249 if(pickup == NULL) 250 return; 251 252 std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable. 253 // If either the input Pickupable is not in the PickupManagers list or it no longer exists, the method returns. 254 if(it == this->pickupsList_.end() || it->second.get() == NULL) 255 return; 256 257 pickup->drop(); // The Pickupable is dropped. 258 } 259 260 /** 261 @brief 262 Use (or unuse) the input Pickupable. 263 This method checks whether the input Pickupable still exists and uses (or unuses) it, if so, 264 @param pickup 265 The Pickupable to be used (or unused). 266 @param use 267 If true the input Pickupable is used, if false it is unused. 268 */ 222 269 void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use) 223 270 { 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) 233 pickup->setUsed(use); 271 if(pickup == NULL) 272 return; 273 274 std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable. 275 // If either the input Pickupable is not in the PickupManagers list or it no longer exists, the method returns. 276 if(it == this->pickupsList_.end() || it->second.get() == NULL) 277 return; 278 279 pickup->setUsed(use); // The Pickupable is used (or unused). 280 } 281 282 /** 283 @brief 284 Check whether the input Pickupable is valid, meaning that it is in the PickupManager's list and still exists. 285 @param pickup 286 The Pickupable. 287 @return 288 Returns true if the input Pickupable is still valid, false if not. 289 */ 290 bool PickupManager::isValidPickup(orxonox::Pickupable* pickup) 291 { 292 std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); // Get the WeakPointer of the Pickupable. 293 if(it == this->pickupsList_.end()) // If the Pickupable is not in the PickupManager's list. 294 return false; 295 return it->second.get() != NULL; // Returns whether the Pickupable still exists. 234 296 } 235 297 -
code/trunk/src/modules/pickup/PickupManager.h
r7163 r7206 52 52 Manages Pickupables. 53 53 In essence has two tasks to fulfill. Firstly it must link Pickupables (through their PickupIdentifiers) and their PickupRepresentations. Secondly it manages the PickupInventory. 54 //TODO: Manage Pickup GUI.55 54 @author 56 55 Damian 'Mozork' Frick … … 72 71 73 72 // tolua_begin 74 int getNumPickups(void); 73 orxonox::PickupRepresentation* getPickupRepresentation(orxonox::Pickupable* pickup); //!< Get the PickupRepresentation of an input Pickupable. 74 75 int getNumPickups(void); //!< Update the list of picked up Pickupables and get the number of Pickupables in the list. 76 /** 77 @brief Get the next Pickupable in the list. 78 Use this, after having called getNumPickups() to access all the Pickupables individually and in succession. 79 @return Returns the next Pickupable in the list. 80 */ 75 81 orxonox::Pickupable* popPickup(void) { return (this->pickupsIterator_++)->first; } 76 orxonox::PickupRepresentation* getPickupRepresentation(orxonox::Pickupable* pickup) { if(pickup != NULL) return this->getRepresentation(pickup->getPickupIdentifier()); return NULL; }77 82 78 void dropPickup(orxonox::Pickupable* pickup); 79 void usePickup(orxonox::Pickupable* pickup, bool use); 80 bool isValidPickup(orxonox::Pickupable* pickup) { std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); if(it == this->pickupsList_.end()) return false; return it->second.get() != NULL; }83 void dropPickup(orxonox::Pickupable* pickup); //!< Drop the input Pickupable. 84 void usePickup(orxonox::Pickupable* pickup, bool use); //!< Use (or unuse) the input Pickupable. 85 bool isValidPickup(orxonox::Pickupable* pickup); //!< Check whether the input Pickupable is valid, meaning that it is in the PickupManager's list and still exists. 81 86 // tolua_end 82 87 83 88 private: 84 89 static PickupManager* singletonPtr_s; 85 static const std::string guiName_s; 90 static const std::string guiName_s; //!< The name of the PickupInventory 86 91 87 92 PickupRepresentation* defaultRepresentation_; //!< The default PickupRepresentation. 88 93 std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare> representations_; //!< Map linking PickupIdentifiers (representing types if Pickupables) and PickupRepresentations. 89 94 90 std::map<Pickupable*, WeakPtr<Pickupable> > pickupsList_; 91 std::map<Pickupable*, WeakPtr<Pickupable> >::iterator pickupsIterator_; 95 std::map<Pickupable*, WeakPtr<Pickupable> > pickupsList_; //!< A list of all the picked up Pickupables. 96 std::map<Pickupable*, WeakPtr<Pickupable> >::iterator pickupsIterator_; //!< An iterator pointing to the current Pickupable in pickupsList_. 92 97 93 std::vector<PickupCarrier*>* getAllCarriers(PickupCarrier* carrier );98 std::vector<PickupCarrier*>* getAllCarriers(PickupCarrier* carrier, std::vector<PickupCarrier*>* carriers = NULL); //!< Helper method. Get all the PickupCarriers that carry Pickupables, recursively. 94 99 95 100 }; // tolua_export
Note: See TracChangeset
for help on using the changeset viewer.