[Orxonox-commit 2846] r7549 - code/trunk/src/modules/pickup

dafrick at orxonox.net dafrick at orxonox.net
Sat Oct 16 14:18:45 CEST 2010


Author: dafrick
Date: 2010-10-16 14:18:45 +0200 (Sat, 16 Oct 2010)
New Revision: 7549

Modified:
   code/trunk/src/modules/pickup/DroppedPickup.cc
   code/trunk/src/modules/pickup/DroppedPickup.h
   code/trunk/src/modules/pickup/PickupManager.cc
   code/trunk/src/modules/pickup/PickupSpawner.cc
   code/trunk/src/modules/pickup/PickupSpawner.h
Log:
Resolving some more TODO's.


Modified: code/trunk/src/modules/pickup/DroppedPickup.cc
===================================================================
--- code/trunk/src/modules/pickup/DroppedPickup.cc	2010-10-16 11:22:15 UTC (rev 7548)
+++ code/trunk/src/modules/pickup/DroppedPickup.cc	2010-10-16 12:18:45 UTC (rev 7549)
@@ -69,10 +69,7 @@
         RegisterObject(DroppedPickup);
 
         this->setPosition(carrier->getCarrierPosition());
-        this->setActive(false);
-
-        //TODO: Do more elegantly.
-        this->startRespawnTimer();
+        this->block(carrier, DEFAULT_BLOCKED_TIME);
     }
 
     /**

Modified: code/trunk/src/modules/pickup/DroppedPickup.h
===================================================================
--- code/trunk/src/modules/pickup/DroppedPickup.h	2010-10-16 11:22:15 UTC (rev 7548)
+++ code/trunk/src/modules/pickup/DroppedPickup.h	2010-10-16 12:18:45 UTC (rev 7549)
@@ -63,6 +63,9 @@
         protected:
             virtual Pickupable* getPickup(void); //!< Creates the Pickupable that is going to get picked up.
 
+        private:
+            static const unsigned int DEFAULT_BLOCKED_TIME = 10; //!< The default time a PickupCarrier is blocked from picking up the pickupable again, after it has dropped it.
+
     };
 }
 

Modified: code/trunk/src/modules/pickup/PickupManager.cc
===================================================================
--- code/trunk/src/modules/pickup/PickupManager.cc	2010-10-16 11:22:15 UTC (rev 7548)
+++ code/trunk/src/modules/pickup/PickupManager.cc	2010-10-16 12:18:45 UTC (rev 7549)
@@ -74,7 +74,6 @@
     {
         RegisterObject(PickupManager);
 
-        //TODO: Only create if isMaster().
         this->defaultRepresentation_ = new PickupRepresentation();
 
         COUT(3) << "PickupManager created." << std::endl;
@@ -355,7 +354,6 @@
         }
 
         // If we're either in standalone mode or this is the host whom the change of the pickup's status concerns.
-        //TODO: Needs to be added to server even if is was not picked up by it?
         if(GameMode::isStandalone() || Host::getPlayerID() == clientId)
         {
             // If there is no PickupRepresentation registered the default representation is used.

Modified: code/trunk/src/modules/pickup/PickupSpawner.cc
===================================================================
--- code/trunk/src/modules/pickup/PickupSpawner.cc	2010-10-16 11:22:15 UTC (rev 7548)
+++ code/trunk/src/modules/pickup/PickupSpawner.cc	2010-10-16 12:18:45 UTC (rev 7549)
@@ -107,7 +107,7 @@
     void PickupSpawner::initialize(void)
     {
         this->triggerDistance_ = 10;
-        this->respawnTime_ = 0; //TODO: Smart? Shouldn't we have a better mechanism to prevent unwanted multiple pickups?
+        this->respawnTime_ = 5.0f;
         this->maxSpawnedItems_ = INF;
         this->spawnsRemaining_ = INF;
         this->selfDestruct_ = false;
@@ -150,7 +150,7 @@
         {
             PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier());
             this->attach(representation->getSpawnerRepresentation(this));
-            this->setActive(true); //TODO: Needed?
+            this->setActive(true);
         }
     }
 
@@ -182,13 +182,22 @@
         {
             SmartPtr<PickupSpawner> temp = this; //Create a smart pointer to keep the PickupSpawner alive until we iterated through all Pawns (in case a Pawn takes the last pickup)
 
+            // Remove PickupCarriers from the blocked list if they have exceeded their time.
+            for(std::map<PickupCarrier*, std::time_t>::iterator it = this->blocked_.begin(); it != this->blocked_.end(); )
+            {
+                std::map<PickupCarrier*, std::time_t>::iterator temp = it;
+                it++;
+                if(temp->second < std::time(0))
+                    this->blocked_.erase(temp);
+            }
+
             // Iterate trough all Pawns.
-            for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
+            for(ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
             {
                 Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
                 PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(*it);
-                // If a Pawn, that fits the target-range of the item spawned by this Pickup, is in trigger-distance.
-                if (distance.length() < this->triggerDistance_ && carrier != NULL)
+                // If a PickupCarrier, that fits the target-range of the Pickupable spawned by this PickupSpawnder, is in trigger-distance and the carrier is not blocked.
+                if(distance.length() < this->triggerDistance_ && carrier != NULL && this->blocked_.find(carrier) == this->blocked_.end())
                 {
                     if(carrier->isTarget(this->pickup_))
                         this->trigger(*it);
@@ -291,12 +300,9 @@
             COUT(4) << "PickupSpawner (&" << this << ") triggered and active." << std::endl;
 
             PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(pawn);
-            if(carrier == NULL)
-            {
-                COUT(1) << "This is bad. Pawn isn't PickupCarrier." << std::endl;
-                return;
-            }
+            assert(carrier);
 
+            // If the Pawn isn't a target of the Pickupable.
             if(!carrier->isTarget(this->pickup_))
             {
                 COUT(4) << "PickupSpawner (&" << this << ") triggered but Pawn wasn't a target of the Pickupable." << std::endl;
@@ -306,6 +312,8 @@
             PickupCarrier* target = carrier->getTarget(this->pickup_);
             Pickupable* pickup = this->getPickup();
 
+            this->block(carrier);
+
             assert(pickup);
             assert(target);
             assert(pickup->pickup(target));

Modified: code/trunk/src/modules/pickup/PickupSpawner.h
===================================================================
--- code/trunk/src/modules/pickup/PickupSpawner.h	2010-10-16 11:22:15 UTC (rev 7548)
+++ code/trunk/src/modules/pickup/PickupSpawner.h	2010-10-16 12:18:45 UTC (rev 7549)
@@ -37,6 +37,7 @@
 
 #include "PickupPrereqs.h"
 
+#include <map>
 #include <string>
 #include "tools/Timer.h"
 
@@ -101,13 +102,20 @@
             */
             inline int getMaxSpawnedItems(void) const
                 { return this->maxSpawnedItems_; }
-            
 
         protected:
             void decrementSpawnsRemaining(void); //!< Decrements the number of remaining spawns.
-            void startRespawnTimer(void);
+            void startRespawnTimer(void); //!< Invoked by the timer, re-activates the PickupSpawner.
 
             /**
+            @brief Helper method. Adds a PickupCarrier to the list of PickupCarrier that are blocked form getting a Pickupable from the PickupSpawner for a specified time.
+            @param carrier A pointer to the PickupCarrier to be blocked.
+            @param time The time for which the Pawn is blocked. Default is 5.
+            */
+            void block(PickupCarrier* carrier, unsigned int time = DEFAULT_BLOCKED_TIME)
+                { this->blocked_.insert(std::pair<PickupCarrier*, std::time_t>(carrier, std::time(0)+time)); }
+
+            /**
             @brief Set the distance in which to trigger.
             @param value The new distance in which to trigger.
             */
@@ -141,10 +149,12 @@
 
             float respawnTime_; //!< Time after which this gets re-actived.
             Timer respawnTimer_; //!< Timer used for re-activating.
+            std::map<PickupCarrier*, std::time_t> blocked_;
 
             bool selfDestruct_; //!< True if the PickupSpawner is selfdestructing.
 
             static const int INF = -1; //!< Constant for infinity.
+            static const unsigned int DEFAULT_BLOCKED_TIME = 5; //!< The default time a PickupCarrier is blocked after picking up a Pickupable.
     };
 }
 




More information about the Orxonox-commit mailing list