Changeset 7504 for code/trunk/src/modules/pickup/PickupManager.cc
- Timestamp:
- Oct 6, 2010, 8:00:40 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/modules/pickup/PickupManager.cc
r7494 r7504 40 40 #include "core/GUIManager.h" 41 41 #include "core/Identifier.h" 42 #include "network/Host.h" 43 #include "network/NetworkFunction.h" 42 44 #include "interfaces/PickupCarrier.h" 43 45 #include "infos/PlayerInfo.h" … … 53 55 DeclareToluaInterface(Pickup); 54 56 55 ManageScopedSingleton(PickupManager, ScopeID:: Graphics, false);57 ManageScopedSingleton(PickupManager, ScopeID::Root, false); 56 58 57 59 /*static*/ const std::string PickupManager::guiName_s = "PickupInventory"; 58 60 61 registerStaticNetworkFunction(PickupManager::pickupChangedUsedNetwork); 62 registerStaticNetworkFunction(PickupManager::pickupChangedPickedUpNetwork); 63 registerStaticNetworkFunction(PickupManager::dropPickupNetworked); 64 registerStaticNetworkFunction(PickupManager::usePickupNetworked); 65 59 66 /** 60 67 @brief 61 68 Constructor. Registers the PickupManager and creates the default PickupRepresentation. 62 69 */ 63 PickupManager::PickupManager() : defaultRepresentation_(NULL) 64 { 65 RegisterRootObject(PickupManager); 66 67 GUIManager::getInstance().loadGUI(PickupManager::guiName_s); 70 PickupManager::PickupManager() : guiLoaded_(false), pickupIndex_(0), defaultRepresentation_(NULL) 71 { 72 RegisterObject(PickupManager); 68 73 69 74 this->defaultRepresentation_ = new PickupRepresentation(); … … 83 88 84 89 this->representations_.clear(); 90 91 //TODO: Destroy properly... 85 92 86 93 COUT(3) << "PickupManager destroyed." << std::endl; … … 103 110 assert(representation); 104 111 105 if( this->representations_.find(identifier) != this->representations_.end()) // If the Pickupable already has a Representation registered.112 if(!this->representations_.empty() && this->representations_.find(identifier) != this->representations_.end()) // If the Pickupable already has a Representation registered. 106 113 return false; 107 114 … … 109 116 110 117 COUT(4) << "PickupRepresentation &" << representation << " registered with the PickupManager." << std::endl; 118 return true; 119 } 120 121 bool PickupManager::registerRepresentation(PickupRepresentation* representation) 122 { 123 assert(representation); 124 125 if(!this->representationsNetworked_.empty() && this->representationsNetworked_.find(representation->getObjectID()) != this->representationsNetworked_.end()) // If the PickupRepresentation is already registered. 126 return false; 127 128 this->representationsNetworked_[representation->getObjectID()] = representation; 111 129 return true; 112 130 } … … 137 155 } 138 156 157 bool PickupManager::unregisterRepresentation(PickupRepresentation* representation) 158 { 159 assert(representation); 160 161 std::map<uint32_t, PickupRepresentation*>::iterator it = this->representationsNetworked_.find(representation->getObjectID()); 162 if(it == this->representationsNetworked_.end()) //!< If the Pickupable is not registered in the first place. 163 return false; 164 165 this->representationsNetworked_.erase(it); 166 return true; 167 } 168 139 169 /** 140 170 @brief … … 166 196 Returns the PickupRepresentation of the input Pickupable or NULL if an error occurred. 167 197 */ 168 orxonox::PickupRepresentation* PickupManager::getPickupRepresentation(orxonox::Pickupable* pickup) 169 { 170 if(pickup != NULL) 171 return this->getRepresentation(pickup->getPickupIdentifier()); 172 173 return NULL; 198 orxonox::PickupRepresentation* PickupManager::getPickupRepresentation(uint64_t pickup) 199 { 200 this->representationsNetworked_.clear(); 201 for(ObjectList<PickupRepresentation>::iterator it = ObjectList<PickupRepresentation>::begin(); it != ObjectList<PickupRepresentation>::end(); ++it) 202 this->representationsNetworked_[it->getObjectID()] = *it; 203 204 std::map<uint64_t, PickupInventoryContainer*>::iterator it = this->pickupInventoryContainers_.find(pickup); 205 if(it == this->pickupInventoryContainers_.end()) 206 return this->defaultRepresentation_; 207 208 std::map<uint32_t, PickupRepresentation*>::iterator it2 = this->representationsNetworked_.find(it->second->representationObjectId); 209 if(it2 == this->representationsNetworked_.end()) 210 return this->defaultRepresentation_; 211 212 return it2->second; 174 213 } 175 214 … … 183 222 int PickupManager::getNumPickups(void) 184 223 { 185 this->pickupsList_.clear(); // Clear the list if Pickupables. 186 187 PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s); 188 PickupCarrier* carrier = NULL; 189 if (player != NULL) 190 carrier = orxonox_cast<PickupCarrier*>(player->getControllableEntity()); 191 else 192 return 0; 193 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. 196 for(std::vector<PickupCarrier*>::iterator it = carriers->begin(); it != carriers->end(); it++) 197 { 198 std::set<Pickupable*> pickups = (*it)->getPickups(); 199 for(std::set<Pickupable*>::iterator pickup = pickups.begin(); pickup != pickups.end(); pickup++) 200 { 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. 203 if(collectible == NULL || !collectible->isInCollection()) 204 this->pickupsList_.insert(std::pair<Pickupable*, WeakPtr<Pickupable> >(*pickup, WeakPtr<Pickupable>(*pickup))); 205 } 206 } 207 delete carriers; 208 209 this->pickupsIterator_ = this->pickupsList_.begin(); //Set the iterator to the start of the list. 210 return this->pickupsList_.size(); 211 } 212 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(). 233 for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++) 234 this->getAllCarriers(*it, carriers); 235 delete children; 236 237 return carriers; 224 this->pickupsIterator_ = this->pickupInventoryContainers_.begin(); 225 return this->pickupInventoryContainers_.size(); 238 226 } 239 227 … … 245 233 The Pickupable to be dropped. 246 234 */ 247 void PickupManager::dropPickup(orxonox::Pickupable* pickup) 248 { 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. 235 void PickupManager::dropPickup(uint64_t pickup) 236 { 237 if(GameMode::isMaster() && !this->pickups_.empty()) 238 { 239 Pickupable* pickupable = this->pickups_.find(pickup)->second->get(); 240 if(pickupable != NULL) 241 pickupable->drop(); 242 } 243 else 244 { 245 callStaticNetworkFunction(PickupManager::dropPickupNetworked, 0, pickup); 246 } 247 } 248 249 /*static*/ void PickupManager::dropPickupNetworked(uint64_t pickup) 250 { 251 if(GameMode::isServer()) 252 { 253 PickupManager& manager = PickupManager::getInstance(); 254 if(manager.pickups_.empty()) 255 return; 256 Pickupable* pickupable = manager.pickups_.find(pickup)->second->get(); 257 if(pickupable != NULL) 258 pickupable->drop(); 259 } 258 260 } 259 261 … … 267 269 If true the input Pickupable is used, if false it is unused. 268 270 */ 269 void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use) 270 { 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). 271 void PickupManager::usePickup(uint64_t pickup, bool use) 272 { 273 if(GameMode::isMaster() && !this->pickups_.empty()) 274 { 275 Pickupable* pickupable = this->pickups_.find(pickup)->second->get(); 276 if(pickupable != NULL) 277 pickupable->setUsed(use); 278 } 279 else 280 { 281 callStaticNetworkFunction(PickupManager::usePickupNetworked, 0, pickup, use); 282 } 283 } 284 285 /*static*/ void PickupManager::usePickupNetworked(uint64_t pickup, bool use) 286 { 287 if(GameMode::isServer()) 288 { 289 PickupManager& manager = PickupManager::getInstance(); 290 if(manager.pickups_.empty()) 291 return; 292 Pickupable* pickupable = manager.pickups_.find(pickup)->second->get(); 293 if(pickupable != NULL) 294 pickupable->setUsed(use); 295 } 280 296 } 281 297 … … 288 304 Returns true if the input Pickupable is still valid, false if not. 289 305 */ 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. 306 bool PickupManager::isValidPickup(uint64_t pickup) 307 { 308 return this->pickups_.find(pickup) != this->pickups_.end(); 309 } 310 311 void PickupManager::pickupChangedUsed(Pickupable* pickup, bool used) 312 { 313 assert(pickup); 314 315 if(!GameMode::isMaster()) // If this is neither standalone nor the server. 316 return; 317 318 CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(pickup); 319 // If the Picupable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is. 320 if(collectible != NULL && collectible->isInCollection()) 321 return; 322 323 PickupCarrier* carrier = pickup->getCarrier(); 324 while(carrier->getCarrierParent() != NULL) 325 carrier = carrier->getCarrierParent(); 326 Pawn* pawn = orxonox_cast<Pawn*>(carrier); 327 if(pawn == NULL) 328 return; 329 PlayerInfo* info = pawn->getPlayer(); 330 if(info == NULL) 331 return; 332 unsigned int clientId = info->getClientID(); 333 334 std::map<Pickupable*, uint64_t>::iterator it = this->indexes_.find(pickup); 335 assert(it != this->indexes_.end()); 336 337 uint64_t index = it->second; 338 if(GameMode::isStandalone() || Host::getPlayerID() == clientId) 339 { 340 PickupManager::pickupChangedUsedNetwork(index, used, pickup->isUsable(), pickup->isUnusable()); 341 } 342 else 343 { 344 callStaticNetworkFunction(PickupManager::pickupChangedUsedNetwork, clientId, index, used, pickup->isUsable(), pickup->isUnusable()); 345 } 346 } 347 348 /*static*/ void PickupManager::pickupChangedUsedNetwork(uint64_t pickup, bool inUse, bool usable, bool unusable) 349 { 350 PickupManager& manager = PickupManager::getInstance(); 351 if(manager.pickupInventoryContainers_.find(pickup) == manager.pickupInventoryContainers_.end()) 352 { 353 COUT(1) << "Error: Pickupable &(" << pickup << ") was not registered with PickupManager for the PickupInventory, when it changed used." << std::endl; 354 return; 355 } 356 357 manager.pickupInventoryContainers_[pickup]->inUse = inUse; 358 manager.pickupInventoryContainers_[pickup]->usable = usable; 359 manager.pickupInventoryContainers_[pickup]->unusable = unusable; 360 361 manager.updateGUI(); 362 } 363 364 void PickupManager::pickupChangedPickedUp(Pickupable* pickup, bool pickedUp) 365 { 366 assert(pickup); 367 368 if(!GameMode::isMaster()) 369 return; 370 371 CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(pickup); 372 // If the Picupable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is. 373 if(collectible != NULL && collectible->isInCollection()) 374 return; 375 376 PickupCarrier* carrier = pickup->getCarrier(); 377 while(carrier->getCarrierParent() != NULL) 378 carrier = carrier->getCarrierParent(); 379 Pawn* pawn = orxonox_cast<Pawn*>(carrier); 380 if(pawn == NULL) 381 return; 382 PlayerInfo* info = pawn->getPlayer(); 383 if(info == NULL) 384 return; 385 unsigned int clientId = info->getClientID(); 386 387 uint64_t index = 0; 388 if(pickedUp) 389 { 390 index = this->getPickupIndex(); 391 this->indexes_[pickup] = index; 392 this->pickups_[index] = new WeakPtr<Pickupable>(pickup); 393 } 394 else 395 { 396 std::map<Pickupable*, uint64_t>::iterator it = this->indexes_.find(pickup); 397 index = it->second; 398 WeakPtr<Pickupable>* ptr = this->pickups_[index]; 399 this->indexes_.erase(it); 400 this->pickups_.erase(index); 401 delete ptr; 402 } 403 404 if(GameMode::isStandalone() || Host::getPlayerID() == clientId) 405 { 406 PickupManager::pickupChangedPickedUpNetwork(index, pickup->isUsable(), this->representations_[pickup->getPickupIdentifier()]->getObjectID(), pickedUp); 407 } 408 else 409 { 410 callStaticNetworkFunction(PickupManager::pickupChangedPickedUpNetwork, clientId, index, pickup->isUsable(), this->representations_[pickup->getPickupIdentifier()]->getObjectID(), pickedUp); 411 } 412 413 } 414 415 /*static*/ void PickupManager::pickupChangedPickedUpNetwork(uint64_t pickup, bool usable, uint32_t representationObjectId, bool pickedUp) 416 { 417 PickupManager& manager = PickupManager::getInstance(); 418 if(pickedUp) 419 { 420 PickupInventoryContainer* container = new PickupInventoryContainer; 421 container->pickup = pickup; 422 container->inUse = false; 423 container->pickedUp = pickedUp; 424 container->usable = usable; 425 container->unusable = false; 426 container->representationObjectId = representationObjectId; 427 manager.pickupInventoryContainers_.insert(std::pair<uint64_t, PickupInventoryContainer*>(pickup, container)); 428 429 if(GameMode::showsGraphics()) 430 { 431 if(!manager.guiLoaded_) 432 { 433 GUIManager::getInstance().loadGUI(PickupManager::guiName_s); 434 manager.guiLoaded_ = true; 435 } 436 GUIManager::getInstance().getLuaState()->doString(PickupManager::guiName_s + ".update()"); 437 } 438 } 439 else 440 { 441 std::map<uint64_t, PickupInventoryContainer*>::iterator it = manager.pickupInventoryContainers_.find(pickup); 442 if(it != manager.pickupInventoryContainers_.end()) 443 delete it->second; 444 manager.pickupInventoryContainers_.erase(pickup); 445 446 manager.updateGUI(); 447 } 448 } 449 450 inline void PickupManager::updateGUI(void) 451 { 452 if(GameMode::showsGraphics()) 453 { 454 if(!this->guiLoaded_) 455 { 456 GUIManager::getInstance().loadGUI(PickupManager::guiName_s); 457 this->guiLoaded_ = true; 458 } 459 GUIManager::getInstance().getLuaState()->doString(PickupManager::guiName_s + ".update()"); 460 } 461 } 462 463 uint64_t PickupManager::getPickupIndex(void) 464 { 465 if(this->pickupIndex_ == uint64_t(~0x0)-1) 466 this->pickupIndex_ = 0; 467 return this->pickupIndex_++; 296 468 } 297 469
Note: See TracChangeset
for help on using the changeset viewer.