Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 18, 2010, 8:54:01 PM (14 years ago)
Author:
dafrick
Message:

Untangled Pickupable and PickupCarrier a little bit.

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

Legend:

Unmodified
Added
Removed
  • code/branches/presentation3/src/orxonox/interfaces/PickupCarrier.h

    r7129 r7150  
    8181
    8282            /**
    83             @brief Can be called to pick up a Pickupable.
    84             @param pickup A pointer to the Pickupable.
    85             @return Returns true if the Pickupable was picked up, false if not.
    86             */
    87             bool pickup(Pickupable* pickup)
    88                 {
    89                     bool pickedUp = this->pickups_.insert(pickup).second;
    90                     if(pickedUp)
    91                     {
    92                         COUT(4) << "Picked up Pickupable " << pickup->getIdentifier()->getName() << "(&" << pickup << ")." << std::endl;
    93                         pickup->pickedUp(this);
    94                     }
    95                     return pickedUp;
    96                 }
    97 
    98             /**
    99             @brief Can be called to drop a Pickupable.
    100             @param pickup A pointer to the Pickupable.
    101             @param drop If the Pickupable should just be removed from the PickupCarrier without further action, this can be set to false. true is default.
    102             @return Returns true if the Pickupable has been dropped, false if not.
    103             */
    104             bool drop(Pickupable* pickup, bool drop = true)
    105                 {
    106                     bool dropped = this->pickups_.erase(pickup) == 1;
    107                     if(dropped && drop)
    108                     {
    109                         COUT(4) << "Dropping Pickupable " << pickup->getIdentifier()->getName() << "(&" << pickup << ")." << std::endl;
    110                         pickup->dropped();
    111                     }
    112                     return dropped;
    113                 }
    114 
    115             /**
    11683            @brief Can be used to check whether the PickupCarrier or a child of his is a target ot the input Pickupable.
    11784            @param pickup A pointer to the Pickupable.
     
    193160
    194161            /**
     162            @brief Adds a Pickupable to the list of pickups that are carried by this PickupCarrier.
     163            @param pickup A pointer to the pickup to be added.
     164            @return Returns true if successfull, false if the Pickupable was already present.
     165            */
     166            bool addPickup(Pickupable* pickup)
     167                { return this->pickups_.insert(pickup).second; }
     168
     169            /**
     170            @brief Removes a Pickupable from the list of pickups that are carried by thsi PickupCarrier.
     171            @param pickup A pointer to the pickup to be removed.
     172            @return Returns true if successfull, false if the Pickupable was not present in the list.
     173            */
     174            bool removePickup(Pickupable* pickup)
     175                { return this->pickups_.erase(pickup) == 1; }
     176
     177            /**
    195178            @brief Get all Pickupables this PickupCarrier has.
    196179            @return  Returns the set of all Pickupables this PickupCarrier has.
  • code/branches/presentation3/src/orxonox/interfaces/Pickupable.cc

    r7129 r7150  
    6969            this->setUsed(false);
    7070
    71         if(this->isPickedUp() && this->getCarrier() != NULL)
     71        if(this->isPickedUp())
    7272        {
    73             this->getCarrier()->drop(this, false);
    74             this->setCarrier(NULL);
     73            this->drop(false);
    7574        }
    7675
     
    168167    /**
    169168    @brief
    170         Sets the Pickupable to picked up.
    171         This method will be called by the PickupCarrier picking the Pickupable up.
     169        Can be called to pick up a Pickupable.
    172170    @param carrier
    173         The PickupCarrier that picked the Pickupable up.
    174     @return
    175         Returns false if, for some reason, the pickup could not be picked up, e.g. it was picked up already.
    176     */
    177     bool Pickupable::pickedUp(PickupCarrier* carrier)
    178     {
    179         if(this->isPickedUp()) //!< If the Pickupable is already picked up.
    180             return false;
     171        A pointer to the PickupCarrier that picks up the Pickupable.
     172    @return
     173        Returns true if the Pickupable was picked up, false if not.
     174    */
     175    bool Pickupable::pickup(PickupCarrier* carrier)
     176    {
     177        if(carrier == NULL || this->isPickedUp()) //!< If carrier is NULL or the Pickupable is already picked up.
     178            return false;
     179
     180        if(!carrier->addPickup(this))
     181        {
     182            COUT(3) << "A Pickupable (&" << this << ") was trying to be added to a PickupCarrier, but was already present." << std::endl;
     183            return false;
     184        }
    181185
    182186        COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl;
    183187        this->setCarrier(carrier);
    184188        this->setPickedUp(true);
     189        return true;
     190    }
     191
     192    /**
     193    @brief
     194        Can be called to drop a Pickupable.
     195    @param createSpawner
     196        If true a spawner is be created for the dropped Pickupable. True is default.
     197    @return
     198        Returns true if the Pickupable has been dropped, false if not.
     199    */
     200    bool Pickupable::drop(bool createSpawner)
     201    {
     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?
     206        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       
     209        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
     210        this->setUsed(false);
     211        this->setPickedUp(false);
     212
     213        bool created = false;
     214        if(createSpawner)
     215            created = this->createSpawner();
     216
     217        this->setCarrier(NULL);
     218
     219        if(!created && createSpawner)
     220        {
     221            this->destroy();
     222        }
     223
    185224        return true;
    186225    }
     
    229268    /**
    230269    @brief
    231         Sets the Pickupable to not picked up or dropped.
    232         This method will be called by the PickupCarrier dropping the Pickupable.
    233     @return
    234         Returns false if the pickup could not be dropped.
    235     */
    236     bool Pickupable::dropped(void)
    237     {
    238         if(!this->isPickedUp()) //!< If the Pickupable is not picked up.
    239             return false;
    240 
    241         COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
    242         this->setUsed(false);
    243         this->setPickedUp(false);
    244 
    245         bool created = this->createSpawner();
    246 
    247         this->setCarrier(NULL);
    248 
    249         if(!created)
    250         {
    251             this->destroy();
    252         }
    253 
    254         return true;
    255     }
    256 
    257     /**
    258     @brief
    259270        Creates a duplicate of the Pickupable.
    260271    @return
     
    297308        Pawn* pawn = static_cast<Pawn*>(entity);
    298309        PickupCarrier* carrier = static_cast<PickupCarrier*>(pawn);
    299         return carrier->pickup(this);
     310        return this->pickup(carrier);
    300311    }
    301312
  • code/branches/presentation3/src/orxonox/interfaces/Pickupable.h

    r7129 r7150  
    9595            virtual void changedPickedUp(void) {}
    9696
    97             bool pickedUp(PickupCarrier* carrier); //!< Sets the Pickupable to picked up.
    98             bool dropped(void); //!< Sets the Pickupable to not picked up or dropped.
     97            bool pickup(PickupCarrier* carrier);
     98            bool drop(bool createSpawner = true);
    9999
    100100            virtual bool isTarget(PickupCarrier* carrier) const; //!< Get whether the given PickupCarrier is a target of this pickup.
Note: See TracChangeset for help on using the changeset viewer.