[Orxonox-commit 2443] r7150 - in code/branches/presentation3/src: modules/pickup modules/pickup/items orxonox/interfaces

dafrick at orxonox.net dafrick at orxonox.net
Fri Jun 18 20:54:02 CEST 2010


Author: dafrick
Date: 2010-06-18 20:54:01 +0200 (Fri, 18 Jun 2010)
New Revision: 7150

Modified:
   code/branches/presentation3/src/modules/pickup/PickupManager.cc
   code/branches/presentation3/src/modules/pickup/PickupSpawner.cc
   code/branches/presentation3/src/modules/pickup/items/MetaPickup.cc
   code/branches/presentation3/src/orxonox/interfaces/PickupCarrier.h
   code/branches/presentation3/src/orxonox/interfaces/Pickupable.cc
   code/branches/presentation3/src/orxonox/interfaces/Pickupable.h
Log:
Untangled Pickupable and PickupCarrier a little bit.


Modified: code/branches/presentation3/src/modules/pickup/PickupManager.cc
===================================================================
--- code/branches/presentation3/src/modules/pickup/PickupManager.cc	2010-06-13 21:07:33 UTC (rev 7149)
+++ code/branches/presentation3/src/modules/pickup/PickupManager.cc	2010-06-18 18:54:01 UTC (rev 7150)
@@ -211,7 +211,7 @@
         PickupCarrier* carrier = pickup->getCarrier();
         if(pickup != NULL && carrier != NULL)
         {
-            carrier->drop(pickup);
+            pickup->drop(carrier);
         }
     }
 

Modified: code/branches/presentation3/src/modules/pickup/PickupSpawner.cc
===================================================================
--- code/branches/presentation3/src/modules/pickup/PickupSpawner.cc	2010-06-13 21:07:33 UTC (rev 7149)
+++ code/branches/presentation3/src/modules/pickup/PickupSpawner.cc	2010-06-18 18:54:01 UTC (rev 7150)
@@ -303,7 +303,7 @@
 
             if(target != NULL && pickup != NULL)
             {
-                if(target->pickup(pickup))
+                if(pickup->pickup(target))
                 {
                     this->decrementSpawnsRemaining();
                 }

Modified: code/branches/presentation3/src/modules/pickup/items/MetaPickup.cc
===================================================================
--- code/branches/presentation3/src/modules/pickup/items/MetaPickup.cc	2010-06-13 21:07:33 UTC (rev 7149)
+++ code/branches/presentation3/src/modules/pickup/items/MetaPickup.cc	2010-06-18 18:54:01 UTC (rev 7150)
@@ -135,7 +135,7 @@
                     {
                         if(pickup != NULL && pickup != this)
                         {
-                            carrier->drop(pickup);
+                            pickup->drop(carrier);
                         }
                     }
                 }

Modified: code/branches/presentation3/src/orxonox/interfaces/PickupCarrier.h
===================================================================
--- code/branches/presentation3/src/orxonox/interfaces/PickupCarrier.h	2010-06-13 21:07:33 UTC (rev 7149)
+++ code/branches/presentation3/src/orxonox/interfaces/PickupCarrier.h	2010-06-18 18:54:01 UTC (rev 7150)
@@ -80,39 +80,6 @@
             virtual ~PickupCarrier(); //!< Destructor.
 
             /**
-            @brief Can be called to pick up a Pickupable.
-            @param pickup A pointer to the Pickupable.
-            @return Returns true if the Pickupable was picked up, false if not.
-            */
-            bool pickup(Pickupable* pickup)
-                {
-                    bool pickedUp = this->pickups_.insert(pickup).second;
-                    if(pickedUp)
-                    {
-                        COUT(4) << "Picked up Pickupable " << pickup->getIdentifier()->getName() << "(&" << pickup << ")." << std::endl;
-                        pickup->pickedUp(this);
-                    }
-                    return pickedUp;
-                }
-
-            /**
-            @brief Can be called to drop a Pickupable.
-            @param pickup A pointer to the Pickupable.
-            @param drop If the Pickupable should just be removed from the PickupCarrier without further action, this can be set to false. true is default.
-            @return Returns true if the Pickupable has been dropped, false if not.
-            */
-            bool drop(Pickupable* pickup, bool drop = true)
-                {
-                    bool dropped = this->pickups_.erase(pickup) == 1;
-                    if(dropped && drop)
-                    {
-                        COUT(4) << "Dropping Pickupable " << pickup->getIdentifier()->getName() << "(&" << pickup << ")." << std::endl;
-                        pickup->dropped();
-                    }
-                    return dropped;
-                }
-
-            /**
             @brief Can be used to check whether the PickupCarrier or a child of his is a target ot the input Pickupable.
             @param pickup A pointer to the Pickupable.
             @return Returns true if the PickupCarrier or one of its children is a target, false if not.
@@ -192,6 +159,22 @@
             virtual PickupCarrier* getCarrierParent(void) = 0;
 
             /**
+            @brief Adds a Pickupable to the list of pickups that are carried by this PickupCarrier.
+            @param pickup A pointer to the pickup to be added.
+            @return Returns true if successfull, false if the Pickupable was already present.
+            */
+            bool addPickup(Pickupable* pickup)
+                { return this->pickups_.insert(pickup).second; }
+
+            /**
+            @brief Removes a Pickupable from the list of pickups that are carried by thsi PickupCarrier.
+            @param pickup A pointer to the pickup to be removed.
+            @return Returns true if successfull, false if the Pickupable was not present in the list.
+            */
+            bool removePickup(Pickupable* pickup)
+                { return this->pickups_.erase(pickup) == 1; }
+
+            /**
             @brief Get all Pickupables this PickupCarrier has.
             @return  Returns the set of all Pickupables this PickupCarrier has.
             */

Modified: code/branches/presentation3/src/orxonox/interfaces/Pickupable.cc
===================================================================
--- code/branches/presentation3/src/orxonox/interfaces/Pickupable.cc	2010-06-13 21:07:33 UTC (rev 7149)
+++ code/branches/presentation3/src/orxonox/interfaces/Pickupable.cc	2010-06-18 18:54:01 UTC (rev 7150)
@@ -68,10 +68,9 @@
         if(this->isUsed())
             this->setUsed(false);
 
-        if(this->isPickedUp() && this->getCarrier() != NULL)
+        if(this->isPickedUp())
         {
-            this->getCarrier()->drop(this, false);
-            this->setCarrier(NULL);
+            this->drop(false);
         }
 
         if(this->pickupIdentifier_ != NULL)
@@ -167,18 +166,23 @@
 
     /**
     @brief
-        Sets the Pickupable to picked up.
-        This method will be called by the PickupCarrier picking the Pickupable up.
+        Can be called to pick up a Pickupable.
     @param carrier
-        The PickupCarrier that picked the Pickupable up.
+        A pointer to the PickupCarrier that picks up the Pickupable.
     @return
-        Returns false if, for some reason, the pickup could not be picked up, e.g. it was picked up already.
+        Returns true if the Pickupable was picked up, false if not.
     */
-    bool Pickupable::pickedUp(PickupCarrier* carrier)
+    bool Pickupable::pickup(PickupCarrier* carrier)
     {
-        if(this->isPickedUp()) //!< If the Pickupable is already picked up.
+        if(carrier == NULL || this->isPickedUp()) //!< If carrier is NULL or the Pickupable is already picked up.
             return false;
 
+        if(!carrier->addPickup(this))
+        {
+            COUT(3) << "A Pickupable (&" << this << ") was trying to be added to a PickupCarrier, but was already present." << std::endl;
+            return false;
+        }
+
         COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl;
         this->setCarrier(carrier);
         this->setPickedUp(true);
@@ -187,6 +191,41 @@
 
     /**
     @brief
+        Can be called to drop a Pickupable.
+    @param createSpawner
+        If true a spawner is be created for the dropped Pickupable. True is default.
+    @return
+        Returns true if the Pickupable has been dropped, false if not.
+    */
+    bool Pickupable::drop(bool createSpawner)
+    {
+        if(!this->isPickedUp()) //!< If the Pickupable is not picked up.
+            return false;
+
+        assert(this->getCarrier()); //!> The Carrier cannot be NULL at this point. //TODO: Too conservative?
+        if(!this->getCarrier()->removePickup(this)) //TODO Shouldn't this be a little later?
+            COUT(2) << "Pickupable (&" << this << ") is being dropped, but it was not present in the PickupCarriers list of pickups." << std::endl;
+        
+        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
+        this->setUsed(false);
+        this->setPickedUp(false);
+
+        bool created = false;
+        if(createSpawner)
+            created = this->createSpawner();
+
+        this->setCarrier(NULL);
+
+        if(!created && createSpawner)
+        {
+            this->destroy();
+        }
+
+        return true;
+    }
+
+    /**
+    @brief
         Helper method to set the Pickupable to either picked up or not picked up.
     @param pickedUp
         The value this->pickedUp_ should be set to.
@@ -228,34 +267,6 @@
 
     /**
     @brief
-        Sets the Pickupable to not picked up or dropped.
-        This method will be called by the PickupCarrier dropping the Pickupable.
-    @return
-        Returns false if the pickup could not be dropped.
-    */
-    bool Pickupable::dropped(void)
-    {
-        if(!this->isPickedUp()) //!< If the Pickupable is not picked up.
-            return false;
-
-        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
-        this->setUsed(false);
-        this->setPickedUp(false);
-
-        bool created = this->createSpawner();
-
-        this->setCarrier(NULL);
-
-        if(!created)
-        {
-            this->destroy();
-        }
-
-        return true;
-    }
-
-    /**
-    @brief
         Creates a duplicate of the Pickupable.
     @return
         Returns the clone of this pickup as a pointer to a Pickupable.
@@ -296,7 +307,7 @@
         ControllableEntity* entity = player->getControllableEntity();
         Pawn* pawn = static_cast<Pawn*>(entity);
         PickupCarrier* carrier = static_cast<PickupCarrier*>(pawn);
-        return carrier->pickup(this);
+        return this->pickup(carrier);
     }
 
 }

Modified: code/branches/presentation3/src/orxonox/interfaces/Pickupable.h
===================================================================
--- code/branches/presentation3/src/orxonox/interfaces/Pickupable.h	2010-06-13 21:07:33 UTC (rev 7149)
+++ code/branches/presentation3/src/orxonox/interfaces/Pickupable.h	2010-06-18 18:54:01 UTC (rev 7150)
@@ -94,8 +94,8 @@
             */
             virtual void changedPickedUp(void) {}
 
-            bool pickedUp(PickupCarrier* carrier); //!< Sets the Pickupable to picked up.
-            bool dropped(void); //!< Sets the Pickupable to not picked up or dropped.
+            bool pickup(PickupCarrier* carrier);
+            bool drop(bool createSpawner = true);
 
             virtual bool isTarget(PickupCarrier* carrier) const; //!< Get whether the given PickupCarrier is a target of this pickup.
             bool isTarget(const Identifier* identifier) const; //!< Get whether a given class, represented by the input Identifier, is a target of this Pickupable.




More information about the Orxonox-commit mailing list