Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 8, 2010, 8:53:52 PM (14 years ago)
Author:
dafrick
Message:

Significant structural changes to the pickup module. Lots of bugs found and fixed.
Introduced a new class CollectiblePickup (which is now the only kind a PickupCollection can consist of) to solve some issues cleanly.
MetaPickup received additional functionality. It can now also be set to either destroy all the pickups of a PickupCarrier or destroy the PickupCarrier itself. (This was done mainly for testing purposes)
I've done some extensive testing on the pickups, so they should really work now.

Location:
code/branches/presentation3/src/orxonox/interfaces
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation3/src/orxonox/interfaces/InterfaceCompilation.cc

    r7127 r7162  
    5959    {
    6060        RegisterRootObject(PickupCarrier);
    61 
    62         this->setCarrierName("PickupCarrier");
    6361    }
    6462
    6563    PickupCarrier::~PickupCarrier()
    6664    {
     65
     66    }
     67
     68    void PickupCarrier::preDestroy(void)
     69    {
    6770        std::set<Pickupable*>::iterator it = this->pickups_.begin();
     71        std::set<Pickupable*>::iterator temp;
    6872        while(it != this->pickups_.end())
    6973        {
    70             (*it)->destroy();
     74            (*it)->carrierDestroyed();
     75            temp = it;
    7176            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            }
    7282        }
    7383
  • code/branches/presentation3/src/orxonox/interfaces/PickupCarrier.h

    r7150 r7162  
    7979            PickupCarrier(); //!< Constructor.
    8080            virtual ~PickupCarrier(); //!< Destructor.
     81            void preDestroy(void);
    8182
    8283            /**
     
    138139            virtual const Vector3& getCarrierPosition(void) = 0;
    139140
    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_export
    145 
    146141        protected:
    147142            /**
     
    160155
    161156            /**
     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            /**
    162167            @brief Adds a Pickupable to the list of pickups that are carried by this PickupCarrier.
    163168            @param pickup A pointer to the pickup to be added.
     
    165170            */
    166171            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                }
    168176
    169177            /**
     
    173181            */
    174182            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)
    201183                {
    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;
    243186                }
    244187
  • code/branches/presentation3/src/orxonox/interfaces/Pickupable.cc

    r7150 r7162  
    5858
    5959        this->pickupIdentifier_ = new PickupIdentifier(this);
     60        this->beingDestroyed_ = false;
     61        this->enabled_ = true;
    6062    }
    6163
     
    6668    Pickupable::~Pickupable()
    6769    {
    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;
    7671        if(this->pickupIdentifier_ != NULL)
    7772            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;
    78107    }
    79108
     
    88117    bool Pickupable::setUsed(bool used)
    89118    {
    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.
    91123            return false;
    92124
     
    112144        if(carrier == NULL)
    113145            return false;
     146
    114147        return this->isTarget(carrier->getIdentifier());
    115148    }
     
    131164                return true;
    132165        }
     166
    133167        return false;
    134168    }
     
    178212            return false;
    179213
    180         if(!carrier->addPickup(this))
     214        if(!this->setCarrier(carrier))
    181215        {
    182216            COUT(3) << "A Pickupable (&" << this << ") was trying to be added to a PickupCarrier, but was already present." << std::endl;
    183217            return false;
    184218        }
    185 
     219       
     220        this->setPickedUp(true);
    186221        COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl;
    187         this->setCarrier(carrier);
    188         this->setPickedUp(true);
    189222        return true;
    190223    }
     
    194227        Can be called to drop a Pickupable.
    195228    @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.
    197230    @return
    198231        Returns true if the Pickupable has been dropped, false if not.
     
    200233    bool Pickupable::drop(bool createSpawner)
    201234    {
    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.
    206239        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
    209242        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
    210243        this->setUsed(false);
     
    217250        this->setCarrier(NULL);
    218251
    219         if(!created && createSpawner)
    220         {
     252        if(!created && createSpawner) // If a PickupSpawner should have been created but wasn't.
    221253            this->destroy();
    222         }
    223254
    224255        return true;
     
    235266    bool Pickupable::setPickedUp(bool pickedUp)
    236267    {
    237         if(this->pickedUp_ == pickedUp)
     268        if(this->pickedUp_ == pickedUp) // If the picked up status has not changed.
    238269            return false;
    239270
     
    241272
    242273        this->pickedUp_ = pickedUp;
     274        if(!pickedUp) // if the Pickupable has been dropped it unregisters itself with its PickupCarrier.
     275            this->getCarrier()->removePickup(this);
    243276        this->changedPickedUp();
    244277        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
     
    251284    @param carrier
    252285        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.
    257294            return false;
    258295
    259296        COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl;
    260297
     298        if(carrier != NULL && tell)
     299        {
     300            if(!carrier->addPickup(this))
     301                return false;
     302        }
     303       
    261304        this->carrier_ = carrier;
    262305        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();
    266316    }
    267317
  • code/branches/presentation3/src/orxonox/interfaces/Pickupable.h

    r7150 r7162  
    9595            virtual void changedPickedUp(void) {}
    9696
    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.
    99119
    100120            virtual bool isTarget(PickupCarrier* carrier) const; //!< Get whether the given PickupCarrier is a target of this pickup.
     
    115135            bool setUsed(bool used); //!< Sets the Pickupable to used or unused, depending on the input.
    116136            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.
    118144
    119145        protected:
     
    122148            */
    123149            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; }
    124159
    125160            /**
     
    136171        private:
    137172
    138             bool used_; //!< Whether the pickup is currently in use or not.
    139             bool pickedUp_; //!< Whether the pickup is 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.
    140175
    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.
    143182
    144183        // For implementing the Rewardable interface:
Note: See TracChangeset for help on using the changeset viewer.