[Orxonox-commit 4624] r9295 - code/branches/presentation2012merge/src/modules/pickup

landauf at orxonox.net landauf at orxonox.net
Tue Jun 12 23:42:43 CEST 2012


Author: landauf
Date: 2012-06-12 23:42:43 +0200 (Tue, 12 Jun 2012)
New Revision: 9295

Modified:
   code/branches/presentation2012merge/src/modules/pickup/PickupCollection.cc
   code/branches/presentation2012merge/src/modules/pickup/PickupCollection.h
Log:
removed fancy counters because they will get out of sync way too easily if pickups are added or removed
performance is not really an issue here and not worth the hassle


Modified: code/branches/presentation2012merge/src/modules/pickup/PickupCollection.cc
===================================================================
--- code/branches/presentation2012merge/src/modules/pickup/PickupCollection.cc	2012-06-12 21:05:15 UTC (rev 9294)
+++ code/branches/presentation2012merge/src/modules/pickup/PickupCollection.cc	2012-06-12 21:42:43 UTC (rev 9295)
@@ -58,9 +58,6 @@
         RegisterObject(PickupCollection);
 
         this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(this);
-        this->usedCounter_ = 0;
-        this->pickedUpCounter_ = 0;
-        this->disabledCounter_ = 0;
         this->processingUsed_ = false;
         this->processingPickedUp_ = false;
     }
@@ -123,12 +120,22 @@
         if(this->processingUsed_)
             return;
 
+        size_t numPickupsEnabled = 0;
+        size_t numPickupsInUse = 0;
+        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
+        {
+            if ((*it)->isEnabled())
+                ++numPickupsEnabled;
+            if ((*it)->isUsed())
+                ++numPickupsInUse;
+        }
+
         // If all the pickups are not in use but the PickupCollection is.
-        if(this->usedCounter_ == 0 && this->isUsed())
+        if(numPickupsInUse == 0 && this->isUsed())
             this->setUsed(false);
 
         // If all the enabled pickups are in use but the PickupCollection is not.
-        if(this->usedCounter_ != 0 && this->usedCounter_ == this->pickups_.size()-this->disabledCounter_ && !this->isUsed())
+        if(numPickupsInUse > 0 && numPickupsInUse == numPickupsEnabled && !this->isUsed())
             this->setUsed(true);
     }
 
@@ -181,10 +188,17 @@
             return;
 
         // If at least all the enabled pickups of this PickupCollection are no longer picked up.
-        if(this->pickedUpCounter_ <= this->disabledCounter_ && this->isPickedUp())
+        bool isOnePickupEnabledAndPickedUp = false;
+        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
+        {
+            if ((*it)->isEnabled() && (*it)->isPickedUp())
+            {
+                isOnePickupEnabledAndPickedUp = true;
+                break;
+            }
+        }
+        if(!isOnePickupEnabledAndPickedUp && this->isPickedUp())
             this->Pickupable::destroy();
-        else if(!this->isPickedUp()) // If the PickupCollection is no longer picked up.
-            this->pickedUpCounter_ = 0;
     }
 
     /**
@@ -257,6 +271,7 @@
 
         this->pickups_.push_back(pickup);
         pickup->wasAddedToCollection(this);
+        this->pickupsChanged();
         return true;
     }
 
@@ -294,6 +309,7 @@
             {
                 this->pickups_.erase(it);
                 pickup->wasRemovedFromCollection();
+                this->pickupsChanged();
                 return true;
             }
         }
@@ -309,11 +325,6 @@
     */
     void PickupCollection::pickupChangedUsed(bool changed)
     {
-        if(changed)
-            this->usedCounter_++;
-        else
-            this->usedCounter_--;
-
         this->changedUsedAction();
     }
 
@@ -326,11 +337,6 @@
     */
     void PickupCollection::pickupChangedPickedUp(bool changed)
     {
-        if(changed)
-            this->pickedUpCounter_++;
-        else
-            this->pickedUpCounter_--;
-
         this->changedPickedUpAction();
     }
 
@@ -341,11 +347,20 @@
     */
     void PickupCollection::pickupDisabled(void)
     {
-        this->disabledCounter_++;
     }
 
     /**
     @brief
+        Helpfer function if the number of pickups in this collection has changed.
+    */
+    void PickupCollection::pickupsChanged(void)
+    {
+        this->changedUsedAction();
+        this->changedPickedUpAction();
+    }
+
+    /**
+    @brief
         Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
         This method must be implemented by any class directly inheriting from Pickupable. It is most easily done by just creating a new DroppedPickup, e.g.:
         DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position);

Modified: code/branches/presentation2012merge/src/modules/pickup/PickupCollection.h
===================================================================
--- code/branches/presentation2012merge/src/modules/pickup/PickupCollection.h	2012-06-12 21:05:15 UTC (rev 9294)
+++ code/branches/presentation2012merge/src/modules/pickup/PickupCollection.h	2012-06-12 21:42:43 UTC (rev 9295)
@@ -105,13 +105,10 @@
         private:
             void changedUsedAction(void); //!< Helper method.
             void changedPickedUpAction(void); //!< Helper method.
+            void pickupsChanged(void); //!< Helper method.
 
             std::list<CollectiblePickup*> pickups_; //!< The list of the pointers of all the Pickupables this PickupCollection consists of. They are weak pointers to facilitate testing, whether the pointers are still valid.
 
-            unsigned int usedCounter_; //!< Keeps track of the number of pickups of this PickupCollection, that are in use.
-            unsigned int pickedUpCounter_; //!< Keeps track of the number of pickups of this PickupCollection, that are picked up.
-            unsigned int disabledCounter_; //!< Keeps track of the number of pickups of this PickupCollection, that are disabled.
-
             bool processingUsed_; //!< Boolean to ensure, that the PickupCollection doesn't update its used status while its internal state is inconsistent.
             bool processingPickedUp_; //!< Boolean to ensure, that the PickupCollection doesn't update its picked upp status while its internal state is inconsistent.
 




More information about the Orxonox-commit mailing list