- Timestamp:
- Mar 5, 2010, 6:26:54 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/pickup3/src/modules/pickup/PickupSpawner.cc
r6474 r6475 35 35 36 36 #include "core/CoreIncludes.h" 37 //#include "core/GUIManager.h" // HACK; see below38 37 #include "core/Template.h" 39 38 #include "core/XMLPort.h" … … 41 40 #include "PickupManager.h" 42 41 #include "PickupRepresentation.h" 43 //#include "PickupInventory.h" // HACK; Only for hack, remove later44 45 42 46 43 namespace orxonox 47 44 { 48 49 // const float PickupSpawner::bounceSpeed_s = 6.0f;50 // const float PickupSpawner::rotationSpeed_s = 1.0f;51 // const float PickupSpawner::bounceDistance_s = 4.0f;52 45 53 46 CreateFactory(PickupSpawner); … … 64 57 65 58 this->initialize(); 66 67 PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(NULL);68 this->attach(representation->getSpawnerRepresentation(this));69 59 } 70 60 … … 95 85 this->setMaxSpawnedItems(maxSpawnedItems); 96 86 97 PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier()); 98 this->attach(representation->getSpawnerRepresentation(this)); 87 if(this->pickup_ == NULL) 88 { 89 COUT(2) << "A PickupSpawner was created without a valid Pickupable. This won't work." << std::endl; 90 this->setActive(false); 91 } 92 else 93 { 94 PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier()); 95 this->attach(representation->getSpawnerRepresentation(this)); 96 } 99 97 } 100 98 … … 109 107 this->triggerDistance_ = 20; 110 108 this->respawnTime_ = 0; 111 this->tickSum_ = 0;112 109 this->maxSpawnedItems_ = INF; 113 110 this->spawnsRemaining_ = INF; … … 141 138 XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode); 142 139 XMLPortParam(PickupSpawner, "maxSpawnedItems", setMaxSpawnedItems, getMaxSpawnedItems, xmlelement, mode); 143 144 //TODO: Kill hack. 145 // HACKs 146 // Load the GUI image as soon as the PickupSpawner gets loaded 147 // = less delays while running 148 // BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); 149 // BaseItem* asItem = orxonox_cast<BaseItem*>(newObject); 150 // if (asItem) 151 // { 152 // asItem->addTemplate(this->itemTemplate_); 153 // PickupInventory::getImageForItem(asItem); 154 // newObject->destroy(); 155 // } 156 157 // & load the GUI itself too, along with some empty windows 158 // = even less delays 159 // GUIManager::getInstance().showGUI("PickupInventory"); 160 // GUIManager::getInstance().executeCode("hideGUI(\"PickupInventory\")"); 161 // PickupInventory::getSingleton(); 140 141 if(this->pickup_ == NULL) 142 { 143 COUT(2) << "A PickupSpawner was created without a valid Pickupable. This won't work." << std::endl; 144 this->setActive(false); 145 } 146 else 147 { 148 PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier()); 149 this->attach(representation->getSpawnerRepresentation(this)); 150 } 151 } 152 153 /** 154 @brief 155 Invoked when the activity has changed. Sets visibility of attached objects. 156 */ 157 void PickupSpawner::changedActivity() 158 { 159 SUPER(PickupSpawner, changedActivity); 160 161 for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++) 162 { 163 (*it)->setVisible(this->isActive()); 164 } 165 } 166 167 /** 168 @brief 169 Tick, checks if any Pawn is close enough to trigger. 170 @param dt 171 Time since last tick. 172 */ 173 //TODO: Replace this with a real DistanceTrigger? Or better with collisions? 174 void PickupSpawner::tick(float dt) 175 { 176 //! If the PickupSpawner is active. 177 if (this->isActive()) 178 { 179 //! Iterate trough all Pawns. 180 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) 181 { 182 Vector3 distance = it->getWorldPosition() - this->getWorldPosition(); 183 //! If a Pawn, that fits the target-range of the item spawned by this Pickup, is in trigger-distance. 184 if (distance.length() < this->triggerDistance_ && this->pickup_->isTarget(*it)) 185 { 186 this->trigger(*it); 187 } 188 } 189 } 190 } 191 192 /** 193 @brief 194 Sets the maximum number of spawned items. 195 @param items 196 The maximum number of spawned items to be set. 197 */ 198 void PickupSpawner::setMaxSpawnedItems(int items) 199 { 200 this->maxSpawnedItems_ = items; 201 this->spawnsRemaining_ = items; 202 } 203 204 /** 205 @brief 206 Decrements the number of remaining spawns. 207 Sets the PickupSpawner to inactive for the duration of the respawnTime. 208 Destroys the PickupSpawner if the number of remaining spawns has reached zero. 209 */ 210 void PickupSpawner::decrementSpawnsRemaining(void) 211 { 212 if(this->spawnsRemaining_ != INF) 213 { 214 this->spawnsRemaining_--; 215 } 216 if(this->spawnsRemaining_ != 0 && this->respawnTime_ > 0) 217 { 218 //TODO: Nicer? Does this even work? 219 this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this))); 220 221 this->setActive(false); 222 this->fireEvent(); 223 } 224 else 225 { 226 COUT(3) << "PickupSpawner empty, selfdestruct initialized." << std::endl; 227 this->setActive(false); 228 this->destroy(); 229 } 162 230 } 163 231 … … 172 240 if(this->pickup_ != NULL) 173 241 { 174 COUT(1) << " addPickupable called, with this->pickup_ already set." << std::endl;242 COUT(1) << "In PickupSpawner: setPickupable called, with this->pickup_ already set." << std::endl; 175 243 return; 176 244 } 177 245 if(pickup == NULL) 178 246 { 179 COUT(1) << " Argument of addPickupable is NULL." << std::endl;247 COUT(1) << "In PickupSpawner: Argument of setPickupable is NULL." << std::endl; 180 248 return; 181 249 } … … 197 265 /** 198 266 @brief 199 Invoked when the activity has changed. Sets visibility of attached objects.200 */201 // void PickupSpawner::changedActivity()202 // {203 // SUPER(PickupSpawner, changedActivity);204 //205 // for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++)206 // (*it)->setVisible(this->isActive());207 // }208 209 /**210 @brief211 Sets the maximum number of spawned items.212 @param items213 The maximum number of spawned items to be set.214 */215 void PickupSpawner::setMaxSpawnedItems(int items)216 {217 this->maxSpawnedItems_ = items;218 this->spawnsRemaining_ = items;219 }220 221 /**222 @brief223 Tick, checks if any Pawn is close enough to trigger.224 @param dt225 Time since last tick.226 */227 //TODO: Replace this with a real DistanceTrigger? Or better with collisions?228 void PickupSpawner::tick(float dt)229 {230 //! If the PickupSpawner is active.231 if (this->isActive())232 {233 //! Iterate trough all Pawns.234 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)235 {236 Vector3 distance = it->getWorldPosition() - this->getWorldPosition();237 //! If a Pawn, that fits the target-range of the item spawned by this Pickup, is in trigger-distance.238 if (distance.length() < this->triggerDistance_ && this->pickup_->isTarget(*it))239 {240 this->trigger(*it);241 }242 }243 244 //! Animation.245 // this->yaw(Radian(rotationSpeed_s*dt));246 // this->tickSum_ += bounceSpeed_s*dt;247 // this->translate(Vector3(0,bounceDistance_s*dt*sin(this->tickSum_),0));248 // if (this->tickSum_ > 2*Ogre::Math::PI)249 // this->tickSum_ -= 2*Ogre::Math::PI;250 }251 }252 253 /**254 @brief255 267 Trigger the PickupSpawner. 256 257 Adds the pickup to the Pawn that triggered, 258 sets the timer to re-activate and deactives the PickupSpawner. 259 268 Adds the pickup to the Pawn that triggered, sets the timer to re-activate and deactives the PickupSpawner. 260 269 @param pawn 261 270 Pawn which triggered the PickupSpawner. 262 271 */ 272 //TODO: Make more generic -> without pawn. 263 273 void PickupSpawner::trigger(Pawn* pawn) 264 274 { 265 //TODO: If private, isActive doesn't need to be tested anymore.266 275 if (this->isActive()) //!< Checks whether PickupItem is active. 267 276 { 277 PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(pawn); 278 if(carrier == NULL) 279 { 280 COUT(1) << "This is bad. Pawn isn't PickupCarrier." << std::endl; 281 return; 282 } 283 284 if(!carrier->isTarget(this->pickup_)) 285 { 286 COUT(4) << "PickupSpawner triggered but Pawn wasn't a target of the Pickupable." << std::endl; 287 return; 288 } 289 290 PickupCarrier* target = carrier->getTarget(this->pickup_); 268 291 Pickupable* pickup = this->getPickup(); 269 if (pickup != NULL) //!< If everything went ok, and pickup is not NULL. 270 { 271 //TODO: Not correct anymore. 272 PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(pawn); 273 if(carrier == NULL) 274 { 275 COUT(1) << "This is bad. Pawn isn't PickupCarrier." << std::endl; 276 return; 277 } 292 293 if(target != NULL || pickup != NULL) 294 { 295 if(carrier->pickup(pickup)) 296 { 297 this->decrementSpawnsRemaining(); 298 } 299 else 300 { 301 pickup->destroy(); 302 } 303 } 304 else 305 { 306 //TODO: Really that severe? 307 if(target == NULL) 308 COUT(1) << "PickupSpawner: Pickupable has no target." << std::endl; 278 309 279 if(carrier->pickup(pickup)) 280 { 281 COUT(3) << "Pickup got picked up." << std::endl; 282 283 this->decrementSpawnsRemaining(); 310 if(pickup == NULL) 311 { 312 COUT(1) << "PickupSpawner: getPickup produced an error, no Pickupable created." << std::endl; 284 313 } 285 314 else … … 288 317 } 289 318 } 290 }291 }292 293 /**294 @brief295 Decrements the number of remaining spawns.296 Sets the PickupSpawner to inactive for the duration of the respawnTime.297 Destroys the PickupSpawner if the number of remaining spawns has reached zero.298 299 */300 void PickupSpawner::decrementSpawnsRemaining(void)301 {302 if(this->spawnsRemaining_ != INF)303 {304 this->spawnsRemaining_--;305 }306 if(this->spawnsRemaining_ != 0 && this->respawnTime_ > 0)307 {308 //TODO: Nicer? Does this even work?309 this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this)));310 311 this->setActive(false);312 this->fireEvent();313 }314 else315 {316 COUT(3) << "PickupSpawner empty, selfdistruct initialized." << std::endl;317 this->setActive(false);318 this->destroy(); //TODO: Implement destroy().319 319 } 320 320 }
Note: See TracChangeset
for help on using the changeset viewer.