Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 11, 2010, 8:55:13 AM (14 years ago)
Author:
dafrick
Message:

Merged presentation3 branch into trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/orxonox/interfaces/Pickupable.cc

    • Property svn:eol-style set to native
    r6901 r7163  
    3434#include "Pickupable.h"
    3535
     36#include "core/LuaState.h"
     37#include "core/GUIManager.h"
    3638#include "core/Identifier.h"
    3739#include "core/CoreIncludes.h"
     40#include "util/Convert.h"
     41#include "infos/PlayerInfo.h"
    3842#include "pickup/PickupIdentifier.h"
     43#include "worldentities/pawns/Pawn.h"
    3944#include "PickupCarrier.h"
    4045
    4146namespace orxonox
    4247{
    43    
     48
    4449    /**
    4550    @brief
     
    4752    */
    4853    Pickupable::Pickupable() : pickupIdentifier_(NULL), used_(false), pickedUp_(false)
    49     {       
     54    {
    5055        RegisterRootObject(Pickupable);
    51        
     56
    5257        this->carrier_ = NULL;
    53        
     58
    5459        this->pickupIdentifier_ = new PickupIdentifier(this);
    55     }
    56    
    57     /**
    58     @brief
    59         Destructor.
     60        this->beingDestroyed_ = false;
     61        this->enabled_ = true;
     62    }
     63
     64    /**
     65    @brief
     66        Destructor.
    6067    */
    6168    Pickupable::~Pickupable()
    6269    {
    63         if(this->isUsed())
    64             this->setUsed(false);
    65        
    66         if(this->isPickedUp() && this->getCarrier() != NULL)
    67         {
    68             this->getCarrier()->drop(this, false);
    69             this->setCarrier(NULL);
    70         }
    71        
     70        COUT(4) << "Pickupable (" << this->getIdentifier()->getName() << ") (&" << this << ") destroyed." << std::endl;
    7271        if(this->pickupIdentifier_ != NULL)
    7372            this->pickupIdentifier_->destroy();
    7473    }
    75    
     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;
     107    }
     108
    76109    /**
    77110    @brief
     
    84117    bool Pickupable::setUsed(bool used)
    85118    {
    86         if(this->used_ == used)
    87             return false;
    88        
     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.
     123            return false;
     124
    89125        COUT(4) << "Pickupable (&" << this << ") set to used " << used << "." << std::endl;
    90        
     126
    91127        this->used_ = used;
    92128        this->changedUsed();
    93         return true;
    94     }
    95    
     129
     130        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
     131        return true;
     132    }
     133
    96134    /**
    97135    @brief
     
    106144        if(carrier == NULL)
    107145            return false;
     146
    108147        return this->isTarget(carrier->getIdentifier());
    109148    }
    110    
     149
    111150    /**
    112151    @brief
     
    125164                return true;
    126165        }
     166
    127167        return false;
    128168    }
    129        
     169
    130170    /**
    131171    @brief
     
    140180        return this->addTarget(target->getIdentifier());
    141181    }
    142    
     182
    143183    /**
    144184    @brief
     
    153193        if(this->isTarget(target)) //!< If the input target is already present in the list of targets.
    154194            return false;
    155        
     195
    156196        COUT(4) << "Target " << target->getName() << " added to Pickupable (&" << this << ")." << std::endl;
    157197        this->targets_.push_back(target);
    158198        return true;
    159199    }
    160    
    161     /**
    162     @brief 
    163         Sets the Pickupable to picked up.
    164         This method will be called by the PickupCarrier picking the Pickupable up.
     200
     201    /**
     202    @brief
     203        Can be called to pick up a Pickupable.
    165204    @param carrier
    166         The PickupCarrier that picked the Pickupable up.
    167     @return
    168         Returns false if, for some reason, the pickup could not be picked up, e.g. it was picked up already.
    169     */
    170     bool Pickupable::pickedUp(PickupCarrier* carrier)
    171     {
    172         if(this->isPickedUp()) //!< If the Pickupable is already picked up.
    173             return false;
     205        A pointer to the PickupCarrier that picks up the Pickupable.
     206    @return
     207        Returns true if the Pickupable was picked up, false if not.
     208    */
     209    bool Pickupable::pickup(PickupCarrier* carrier)
     210    {
     211        if(carrier == NULL || this->isPickedUp()) //!< If carrier is NULL or the Pickupable is already picked up.
     212            return false;
     213
     214        if(!this->setCarrier(carrier))
     215        {
     216            COUT(3) << "A Pickupable (&" << this << ") was trying to be added to a PickupCarrier, but was already present." << std::endl;
     217            return false;
     218        }
    174219       
     220        this->setPickedUp(true);
    175221        COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl;
    176         this->setCarrier(carrier);
    177         this->setPickedUp(true);
    178         return true;
    179     }
    180    
     222        return true;
     223    }
     224
     225    /**
     226    @brief
     227        Can be called to drop a Pickupable.
     228    @param createSpawner
     229        If true a spawner is to be created for the dropped Pickupable. True is default.
     230    @return
     231        Returns true if the Pickupable has been dropped, false if not.
     232    */
     233    bool Pickupable::drop(bool createSpawner)
     234    {
     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.
     239        if(!this->getCarrier()->removePickup(this)) //TODO Shouldn't this be a little later?
     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
     242        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
     243        this->setUsed(false);
     244        this->setPickedUp(false);
     245
     246        bool created = false;
     247        if(createSpawner)
     248            created = this->createSpawner();
     249
     250        this->setCarrier(NULL);
     251
     252        if(!created && createSpawner) // If a PickupSpawner should have been created but wasn't.
     253            this->destroy();
     254
     255        return true;
     256    }
     257
    181258    /**
    182259    @brief
     
    189266    bool Pickupable::setPickedUp(bool pickedUp)
    190267    {
    191         if(this->pickedUp_ == pickedUp)
    192             return false;
    193        
     268        if(this->pickedUp_ == pickedUp) // If the picked up status has not changed.
     269            return false;
     270
    194271        COUT(4) << "Pickupable (&" << this << ") set to pickedUp " << pickedUp << "." << std::endl;
    195        
     272
    196273        this->pickedUp_ = pickedUp;
     274        if(!pickedUp) // if the Pickupable has been dropped it unregisters itself with its PickupCarrier.
     275            this->getCarrier()->removePickup(this);
    197276        this->changedPickedUp();
    198         return true;
    199     }
    200        
     277        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
     278        return true;
     279    }
     280
    201281    /**
    202282    @brief
     
    204284    @param carrier
    205285        Sets the input PickupCarrier as the carrier of the pickup.
    206     */
    207     inline bool Pickupable::setCarrier(PickupCarrier* carrier)
    208     {
    209         if(this->carrier_ == carrier)
    210             return false;
    211        
     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.
     294            return false;
     295
    212296        COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl;
     297
     298        if(carrier != NULL && tell)
     299        {
     300            if(!carrier->addPickup(this))
     301                return false;
     302        }
    213303       
    214304        this->carrier_ = carrier;
     
    216306        return true;
    217307    }
    218    
    219     /**
    220     @brief
    221         Sets the Pickupable to not picked up or dropped.
    222         This method will be called by the PickupCarrier dropping the Pickupable.
    223     @return
    224         Returns false if the pickup could not be dropped.
    225     */
    226     bool Pickupable::dropped(void)
    227     {
    228         if(!this->isPickedUp()) //!< If the Pickupable is not picked up.
    229             return false;
    230        
    231         COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
    232         this->setUsed(false);
    233         this->setPickedUp(false);
    234        
    235         bool created = this->createSpawner();
    236        
    237         this->setCarrier(NULL);
    238        
    239         if(!created)
    240         {
    241             this->destroy();
    242         }
    243        
    244         return true;
    245     }
    246    
     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();
     316    }
     317
    247318    /**
    248319    @brief
     
    255326        OrxonoxClass* item = NULL;
    256327        this->clone(item);
    257        
     328
    258329        Pickupable* pickup = dynamic_cast<Pickupable*>(item);
    259        
     330
    260331        COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl;
    261332        return pickup;
    262333    }
    263    
     334
    264335    /**
    265336    @brief
     
    273344        SUPER(Pickupable, clone, item);
    274345    }
    275    
     346
     347    /**
     348    @brief
     349        Method to transcribe a Pickupable as a Rewardable to the player.
     350    @param player
     351        A pointer to the PlayerInfo, do whatever you want with it.
     352    @return
     353        Return true if successful.
     354    */
     355    bool Pickupable::reward(PlayerInfo* player)
     356    {
     357        ControllableEntity* entity = player->getControllableEntity();
     358        Pawn* pawn = static_cast<Pawn*>(entity);
     359        PickupCarrier* carrier = static_cast<PickupCarrier*>(pawn);
     360        return this->pickup(carrier);
     361    }
     362
    276363}
Note: See TracChangeset for help on using the changeset viewer.