Changeset 7163 for code/trunk/src/modules/pickup/PickupManager.cc
- Timestamp:
- Aug 11, 2010, 8:55:13 AM (14 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/modules/pickup/PickupManager.cc
- Property svn:eol-style set to native
r6752 r7163 39 39 #include "core/ScopedSingletonManager.h" 40 40 #include "core/Identifier.h" 41 #include "util/Convert.h" 41 42 #include "interfaces/PickupCarrier.h" 42 43 #include "infos/PlayerInfo.h" 43 44 #include "worldentities/pawns/Pawn.h" 45 #include "CollectiblePickup.h" 44 46 #include "PickupRepresentation.h" 45 47 … … 50 52 // Register tolua_open function when loading the library 51 53 DeclareToluaInterface(Pickup); 52 53 ManageScopedSingleton(PickupManager, ScopeID:: Graphics, false);54 54 55 ManageScopedSingleton(PickupManager, ScopeID::Root, false); 56 55 57 /*static*/ const std::string PickupManager::guiName_s = "PickupInventory"; 56 58 57 59 /** 58 60 @brief … … 62 64 { 63 65 RegisterRootObject(PickupManager); 64 66 67 //TODO: This doesn't work, yet. 68 if( GameMode::showsGraphics() ) 69 { 70 GUIManager::getInstance().loadGUI(PickupManager::guiName_s); 71 } 65 72 this->defaultRepresentation_ = new PickupRepresentation(); 66 73 67 74 COUT(3) << "PickupManager created." << std::endl; 68 75 } 69 76 70 77 /** 71 78 @brief … … 77 84 if(this->defaultRepresentation_ != NULL) 78 85 this->defaultRepresentation_->destroy(); 79 86 80 87 this->representations_.clear(); 81 88 82 89 COUT(3) << "PickupManager destroyed." << std::endl; 83 90 } 84 91 85 92 /** 86 93 @brief … … 95 102 */ 96 103 bool PickupManager::registerRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation) 97 { 104 { 98 105 if(identifier == NULL || representation == NULL || this->representations_.find(identifier) != this->representations_.end()) //!< If the Pickupable already has a Representation registered. 99 106 return false; 100 107 101 108 this->representations_[identifier] = representation; 102 109 103 110 COUT(4) << "PickupRepresentation " << representation << " registered with the PickupManager." << std::endl; 104 111 return true; 105 112 } 106 113 107 114 /** 108 115 @brief … … 116 123 */ 117 124 bool PickupManager::unregisterRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation) 118 { 125 { 119 126 if(identifier == NULL || representation == NULL) 120 127 return false; 121 128 122 129 std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier); 123 130 if(it == this->representations_.end()) //!< If the Pickupable is not registered in the first place. 124 131 return false; 125 132 126 133 this->representations_.erase(it); 127 134 128 135 COUT(4) << "PickupRepresentation " << representation << " unregistered with the PickupManager." << std::endl; 129 136 return true; 130 137 } 131 138 132 139 /** 133 140 @brief … … 146 153 return this->defaultRepresentation_; 147 154 } 148 155 149 156 return it->second; 150 157 } 151 152 PickupCarrier* PickupManager::getPawn(void) 153 { 158 159 int PickupManager::getNumPickups(void) 160 { 161 this->pickupsList_.clear(); 162 154 163 PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s); 164 PickupCarrier* carrier = NULL; 155 165 if (player != NULL) 156 returndynamic_cast<PickupCarrier*>(player->getControllableEntity());166 carrier = dynamic_cast<PickupCarrier*>(player->getControllableEntity()); 157 167 else 158 return NULL;159 }160 161 int PickupManager::getNumCarrierChildren(PickupCarrier* carrier)162 {163 if(carrier == NULL)164 168 return 0; 165 return carrier->getNumCarrierChildren(); 166 } 167 168 PickupCarrier* PickupManager::getCarrierChild(int index, PickupCarrier* carrier) 169 { 170 if(carrier == NULL) 171 return NULL; 172 return carrier->getCarrierChild(index); 173 } 174 175 const std::string& PickupManager::getCarrierName(orxonox::PickupCarrier* carrier) 176 { 177 if(carrier == NULL) 178 return BLANKSTRING; 179 return carrier->getCarrierName(); 180 } 181 182 PickupRepresentation* PickupManager::getPickupRepresentation(int index, PickupCarrier* carrier) 183 { 184 Pickupable* pickup = carrier->getPickup(index); 185 if(pickup == NULL) 186 return NULL; 187 188 return this->getRepresentation(pickup->getPickupIdentifier()); 189 } 190 191 int PickupManager::getNumPickups(PickupCarrier* carrier) 192 { 193 if(carrier == NULL) 194 return 0; 195 return carrier->getNumPickups(); 196 } 197 198 void PickupManager::dropPickup(int index, PickupCarrier* carrier) 199 { 200 Pickupable* pickup = carrier->getPickup(index); 201 if(pickup != NULL) 202 carrier->drop(pickup); 203 } 204 205 void PickupManager::usePickup(int index, PickupCarrier* carrier, bool use) 206 { 207 Pickupable* pickup = carrier->getPickup(index); 208 if(pickup != NULL) 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(); 185 } 186 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(); 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; 204 } 205 206 void PickupManager::dropPickup(orxonox::Pickupable* pickup) 207 { 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 222 void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use) 223 { 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) 209 233 pickup->setUsed(use); 210 234 } 211 235 212 236 }
Note: See TracChangeset
for help on using the changeset viewer.