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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.