Changeset 7162 for code/branches/presentation3/src/orxonox/interfaces
- Timestamp:
- Aug 8, 2010, 8:53:52 PM (14 years ago)
- Location:
- code/branches/presentation3/src/orxonox/interfaces
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation3/src/orxonox/interfaces/InterfaceCompilation.cc
r7127 r7162 59 59 { 60 60 RegisterRootObject(PickupCarrier); 61 62 this->setCarrierName("PickupCarrier");63 61 } 64 62 65 63 PickupCarrier::~PickupCarrier() 66 64 { 65 66 } 67 68 void PickupCarrier::preDestroy(void) 69 { 67 70 std::set<Pickupable*>::iterator it = this->pickups_.begin(); 71 std::set<Pickupable*>::iterator temp; 68 72 while(it != this->pickups_.end()) 69 73 { 70 (*it)->destroy(); 74 (*it)->carrierDestroyed(); 75 temp = it; 71 76 it = this->pickups_.begin(); 77 if(it == temp) // Infinite loop avoidance, in case the pickup wasn't removed from the carrier somewhere in the carrierDestroy() procedure. 78 { 79 COUT(2) << "Oops. In a PickupCarrier, while cleaning up, a Pickupable (&" << (*temp) << ") didn't unregister itself as it should have." << std::endl;; 80 it++; 81 } 72 82 } 73 83 -
code/branches/presentation3/src/orxonox/interfaces/PickupCarrier.h
r7150 r7162 79 79 PickupCarrier(); //!< Constructor. 80 80 virtual ~PickupCarrier(); //!< Destructor. 81 void preDestroy(void); 81 82 82 83 /** … … 138 139 virtual const Vector3& getCarrierPosition(void) = 0; 139 140 140 /**141 @brief Get the name of this PickupCarrier.142 @return Returns the name as a string.143 */144 const std::string& getCarrierName(void) { return this->carrierName_; } // tolua_export145 146 141 protected: 147 142 /** … … 160 155 161 156 /** 157 @brief Get all Pickupables this PickupCarrier has. 158 @return Returns the set of all Pickupables this PickupCarrier has. 159 */ 160 std::set<Pickupable*>& getPickups(void) 161 { return this->pickups_; } 162 163 private: 164 std::set<Pickupable*> pickups_; //!< The list of Pickupables carried by this PickupCarrier. 165 166 /** 162 167 @brief Adds a Pickupable to the list of pickups that are carried by this PickupCarrier. 163 168 @param pickup A pointer to the pickup to be added. … … 165 170 */ 166 171 bool addPickup(Pickupable* pickup) 167 { return this->pickups_.insert(pickup).second; } 172 { 173 COUT(4) << "Adding Pickupable (&" << pickup << ") to PickupCarrier (&" << this << ")" << std::endl; 174 return this->pickups_.insert(pickup).second; 175 } 168 176 169 177 /** … … 173 181 */ 174 182 bool removePickup(Pickupable* pickup) 175 { return this->pickups_.erase(pickup) == 1; }176 177 /**178 @brief Get all Pickupables this PickupCarrier has.179 @return Returns the set of all Pickupables this PickupCarrier has.180 */181 std::set<Pickupable*>& getPickups(void)182 { return this->pickups_; }183 184 /**185 @brief Set the name of this PickupCarrier.186 The name needs to be set in the constructor of every class inheriting from PickupCarrier, by calling setCarrierName().187 @param name The name to be set.188 */189 void setCarrierName(const std::string& name)190 { this->carrierName_ = name; }191 192 private:193 std::set<Pickupable*> pickups_; //!< The list of Pickupables carried by this PickupCarrier.194 std::string carrierName_; //!< The name of the PickupCarrier, as displayed in the PickupInventory.195 196 /**197 @brief Get the number of carrier children this PickupCarrier has.198 @return Returns the number of carrier children.199 */200 unsigned int getNumCarrierChildren(void)201 183 { 202 std::vector<PickupCarrier*>* list = this->getCarrierChildren(); 203 unsigned int size = list->size(); 204 delete list; 205 return size; 206 } 207 208 /** 209 @brief Get the index-th child of this PickupCarrier. 210 @param index The index of the child to return. 211 @return Returns the index-th child. 212 */ 213 PickupCarrier* getCarrierChild(unsigned int index) 214 { 215 std::vector<PickupCarrier*>* list = this->getCarrierChildren(); 216 if(list->size() < index) 217 return NULL; 218 PickupCarrier* carrier = (*list)[index]; 219 delete list; 220 return carrier; 221 } 222 223 /** 224 @brief Get the number of Pickupables this PickupCarrier carries. 225 @return returns the number of pickups. 226 */ 227 unsigned int getNumPickups(void) 228 { return this->pickups_.size(); } 229 230 /** 231 @brief Get the index-th Pickupable of this PickupCarrier. 232 @param index The index of the Pickupable to return. 233 @return Returns the index-th pickup. 234 */ 235 Pickupable* getPickup(unsigned int index) 236 { 237 std::set<Pickupable*>::iterator it; 238 for(it = this->pickups_.begin(); index != 0 && it != this->pickups_.end(); it++) 239 index--; 240 if(it == this->pickups_.end()) 241 return NULL; 242 return *it; 184 COUT(4) << "Removing Pickupable (&" << pickup << ") from PickupCarrier (&" << this << ")" << std::endl; 185 return this->pickups_.erase(pickup) == 1; 243 186 } 244 187 -
code/branches/presentation3/src/orxonox/interfaces/Pickupable.cc
r7150 r7162 58 58 59 59 this->pickupIdentifier_ = new PickupIdentifier(this); 60 this->beingDestroyed_ = false; 61 this->enabled_ = true; 60 62 } 61 63 … … 66 68 Pickupable::~Pickupable() 67 69 { 68 if(this->isUsed()) 69 this->setUsed(false); 70 71 if(this->isPickedUp()) 72 { 73 this->drop(false); 74 } 75 70 COUT(4) << "Pickupable (" << this->getIdentifier()->getName() << ") (&" << this << ") destroyed." << std::endl; 76 71 if(this->pickupIdentifier_ != NULL) 77 72 this->pickupIdentifier_->destroy(); 73 } 74 75 /** 76 @brief 77 A method that is called by OrxonoxClass::destroy() before the object is actually destroyed. 78 */ 79 void Pickupable::preDestroy(void) 80 { 81 this->beingDestroyed_ = true; 82 83 if(this->isPickedUp()) 84 this->drop(false); // Drops the pickup without creating a PickupSpawner. 85 } 86 87 /** 88 @brief 89 Is called internally within the pickup module to destroy pickups. 90 */ 91 void Pickupable::destroy(void) 92 { 93 this->destroyPickup(); 94 } 95 96 /** 97 @brief 98 Destroys a Pickupable. 99 If the Pickupable is already in the process of being destroyed a warning is displayed and this method is skipped. 100 */ 101 void Pickupable::destroyPickup(void) 102 { 103 if(!this->beingDestroyed_) 104 this->OrxonoxClass::destroy(); 105 else 106 COUT(2) << this->getIdentifier()->getName() << " may be unsafe. " << std::endl; 78 107 } 79 108 … … 88 117 bool Pickupable::setUsed(bool used) 89 118 { 90 if(this->used_ == used) 119 if(this->used_ == used || !this->isPickedUp()) // If either the used status of the Pickupable doesn't change or it isn't picked up. 120 return false; 121 122 if((!this->isUsable() && used) || (!this->isUnusable() && !used)) // If either the Pickupable is requested to be used but it is not usable or the Pickupable is requested to be unused, while it is not unusable. 91 123 return false; 92 124 … … 112 144 if(carrier == NULL) 113 145 return false; 146 114 147 return this->isTarget(carrier->getIdentifier()); 115 148 } … … 131 164 return true; 132 165 } 166 133 167 return false; 134 168 } … … 178 212 return false; 179 213 180 if(! carrier->addPickup(this))214 if(!this->setCarrier(carrier)) 181 215 { 182 216 COUT(3) << "A Pickupable (&" << this << ") was trying to be added to a PickupCarrier, but was already present." << std::endl; 183 217 return false; 184 218 } 185 219 220 this->setPickedUp(true); 186 221 COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl; 187 this->setCarrier(carrier);188 this->setPickedUp(true);189 222 return true; 190 223 } … … 194 227 Can be called to drop a Pickupable. 195 228 @param createSpawner 196 If true a spawner is be created for the dropped Pickupable. True is default.229 If true a spawner is to be created for the dropped Pickupable. True is default. 197 230 @return 198 231 Returns true if the Pickupable has been dropped, false if not. … … 200 233 bool Pickupable::drop(bool createSpawner) 201 234 { 202 if(!this->isPickedUp()) // !<If the Pickupable is not picked up.203 return false; 204 205 assert(this->getCarrier()); // !> The Carrier cannot be NULL at this point. //TODO: Too conservative?235 if(!this->isPickedUp()) // If the Pickupable is not picked up. 236 return false; 237 238 assert(this->getCarrier()); // The Carrier cannot be NULL at this point. 206 239 if(!this->getCarrier()->removePickup(this)) //TODO Shouldn't this be a little later? 207 COUT(2) << "Pickupable (&" << this << " ) is being dropped, but it was not present in the PickupCarriers list of pickups." << std::endl;208 240 COUT(2) << "Pickupable (&" << this << ", " << this->getIdentifier()->getName() << ") is being dropped, but it was not present in the PickupCarriers list of pickups." << std::endl; 241 209 242 COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl; 210 243 this->setUsed(false); … … 217 250 this->setCarrier(NULL); 218 251 219 if(!created && createSpawner) 220 { 252 if(!created && createSpawner) // If a PickupSpawner should have been created but wasn't. 221 253 this->destroy(); 222 }223 254 224 255 return true; … … 235 266 bool Pickupable::setPickedUp(bool pickedUp) 236 267 { 237 if(this->pickedUp_ == pickedUp) 268 if(this->pickedUp_ == pickedUp) // If the picked up status has not changed. 238 269 return false; 239 270 … … 241 272 242 273 this->pickedUp_ = pickedUp; 274 if(!pickedUp) // if the Pickupable has been dropped it unregisters itself with its PickupCarrier. 275 this->getCarrier()->removePickup(this); 243 276 this->changedPickedUp(); 244 277 GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()"); … … 251 284 @param carrier 252 285 Sets the input PickupCarrier as the carrier of the pickup. 253 */ 254 inline bool Pickupable::setCarrier(PickupCarrier* carrier, bool tell) 255 { 256 if(this->carrier_ == carrier) 286 @param tell 287 If true (default) the pickup is added to the list of pickups in the PickupCarrier. 288 @return 289 Returns true if successful, false if not. 290 */ 291 bool Pickupable::setCarrier(orxonox::PickupCarrier* carrier, bool tell) 292 { 293 if(this->carrier_ == carrier) // If the PickupCarrier doesn't change. 257 294 return false; 258 295 259 296 COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl; 260 297 298 if(carrier != NULL && tell) 299 { 300 if(!carrier->addPickup(this)) 301 return false; 302 } 303 261 304 this->carrier_ = carrier; 262 305 this->changedCarrier(); 263 if(tell && carrier != NULL) 264 this->carrier_->pickups_.insert(this); 265 return true; 306 return true; 307 } 308 309 /** 310 @brief 311 Is called by the PickupCarrier when it is being destroyed. 312 */ 313 void Pickupable::carrierDestroyed(void) 314 { 315 this->destroy(); 266 316 } 267 317 -
code/branches/presentation3/src/orxonox/interfaces/Pickupable.h
r7150 r7162 95 95 virtual void changedPickedUp(void) {} 96 96 97 bool pickup(PickupCarrier* carrier); 98 bool drop(bool createSpawner = true); 97 /** 98 @brief Returns whether the Pickupable can be used. 99 @return Returns true if it can be used. 100 */ 101 inline bool isUsable(void) { return this->enabled_; } // tolua_export 102 103 /** 104 @brief Returns whether the Pickupable can be unused. 105 @return Returns true if it can be unused. 106 */ 107 inline bool isUnusable(void) { return this->enabled_; } // tolua_export 108 109 /** 110 @brief Returns whether the Pickupable is enabled. 111 Once a Pickupable is disabled it cannot be enabled again. A Pickupable that is disabled can neither be used nor unused. 112 @return Returns true if the Pickupable is enabled. 113 */ 114 inline bool isEnabled(void) 115 { return this->enabled_; } 116 117 bool pickup(PickupCarrier* carrier); //!< Can be called to pick up a Pickupable. 118 bool drop(bool createSpawner = true); //!< Can be called to drop a Pickupable. 99 119 100 120 virtual bool isTarget(PickupCarrier* carrier) const; //!< Get whether the given PickupCarrier is a target of this pickup. … … 115 135 bool setUsed(bool used); //!< Sets the Pickupable to used or unused, depending on the input. 116 136 bool setPickedUp(bool pickedUp); //!< Helper method to set the Pickupable to either picked up or not picked up. 117 bool setCarrier(PickupCarrier* carrier, bool tell = false); //!< Sets the carrier of the pickup. 137 //TODO: private? 138 bool setCarrier(PickupCarrier* carrier, bool tell = true); //!< Sets the carrier of the pickup. 139 140 //TODO: private? 141 virtual void carrierDestroyed(void); //!< Is called by the PickupCarrier when it is being destroyed. 142 143 void destroy(void); //!< Is called internally within the pickup module to destroy pickups. 118 144 119 145 protected: … … 122 148 */ 123 149 void initializeIdentifier(void) {} 150 151 virtual void preDestroy(void); //!< A method that is called by OrxonoxClass::destroy() before the object is actually destroyed. 152 virtual void destroyPickup(void); //!< Destroys a Pickupable. 153 154 /** 155 @brief Sets the Pickuapble to disabled. 156 */ 157 inline void setDisabled(void) 158 { this->enabled_ = false; } 124 159 125 160 /** … … 136 171 private: 137 172 138 bool used_; //!< Whether the pickupis currently in use or not.139 bool pickedUp_; //!< Whether the pickupis currently picked up or not.173 bool used_; //!< Whether the Pickupable is currently in use or not. 174 bool pickedUp_; //!< Whether the Pickupable is currently picked up or not. 140 175 141 PickupCarrier* carrier_; //!< The carrier of the pickup. 142 std::list<Identifier*> targets_; //!< The possible targets of this pickup. 176 bool enabled_; //!< Whether the Pickupable is enabled or not. 177 178 PickupCarrier* carrier_; //!< The PickupCarrier of the Pickupable. 179 std::list<Identifier*> targets_; //!< The possible targets of this Pickupable. 180 181 bool beingDestroyed_; //!< Is true if the Pickupable is in the process of being destroyed. 143 182 144 183 // For implementing the Rewardable interface:
Note: See TracChangeset
for help on using the changeset viewer.