Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 6, 2010, 7:44:04 PM (15 years ago)
Author:
dafrick
Message:

Some more documenting. Completed HealthPickup.

Location:
code/branches/pickup3/src/modules/pickup
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pickup3/src/modules/pickup/Pickup.cc

    r6475 r6477  
    192192       
    193193        //! Sets the Pickup to used if the Pickup has activation type 'immediate' and gets picked up.
    194         if(this->isPickedUp() && this->isImmediate())
     194        if(this->getCarrier() != NULL && this->isPickedUp() && this->isImmediate())
    195195        {
    196196            this->setUsed(true);
  • code/branches/pickup3/src/modules/pickup/items/HealthPickup.cc

    r6475 r6477  
    3131#include "core/CoreIncludes.h"
    3232#include "core/XMLPort.h"
     33#include "util/StringUtils.h"
    3334
    3435#include "worldentities/pawns/Pawn.h"
     
    4344    /*static*/ const std::string HealthPickup::healthTypeTemporary_s = "temporary";
    4445    /*static*/ const std::string HealthPickup::healthTypePermanent_s = "permanent";
    45     /*static*/ const std::string HealthPickup::blankString_s = "";
    4646   
    4747    CreateFactory(HealthPickup);
    4848   
     49    /**
     50    @brief
     51        Constructor. Registers the object and initializes the member variables.
     52    */
    4953    HealthPickup::HealthPickup(BaseObject* creator) : Pickup(creator)
    5054    {
     
    5458    }
    5559   
     60    /**
     61    @brief
     62        Destructor.
     63    */
    5664    HealthPickup::~HealthPickup()
    5765    {
     
    5967    }
    6068   
     69    /**
     70    @brief
     71        Initializes the member variables.
     72    */
    6173    void HealthPickup::initialize(void)
    6274    {
     
    6476       
    6577        this->health_ = 0;
    66         this->healthSpeed_ = 0;
     78        this->healthRate_ = 0;
    6779        this->healthType_ = pickupHealthType::limited;
    68        
    69     }
    70    
     80        this->maxHealthSave_ = 0;
     81        this->maxHealthOverwrite_ = 0;
     82       
     83    }
     84   
     85    /**
     86    @brief
     87        Initializes the PickupIdentifier of this pickup.
     88    */
    7189    void HealthPickup::initializeIdentifier(void)
    7290    {
     
    83101        std::string type2 = "healthType";
    84102        this->pickupIdentifier_->addParameter(type2, val2);
    85     }
    86    
     103       
     104        stream.clear();
     105        stream << this->getHealthRate();
     106        std::string val3 = stream.str();
     107        std::string type3 = "healthRate";
     108        this->pickupIdentifier_->addParameter(type3, val3);
     109    }
     110   
     111    /**
     112    @brief
     113        Method for creating a HealthPickup object through XML.
     114    */
    87115    void HealthPickup::HealthPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
    88116    {
     
    90118       
    91119        XMLPortParam(HealthPickup, "health", setHealth, getHealth, xmlelement, mode);
    92         XMLPortParam(HealthPickup, "healthSpeed", setHealthSpeed, getHealthSpeed, xmlelement, mode);
     120        XMLPortParam(HealthPickup, "healthRate", setHealthRate, getHealthRate, xmlelement, mode);
    93121        XMLPortParam(HealthPickup, "healthType", setHealthType, getHealthType, xmlelement, mode);
    94122       
    95123        if(!this->isContinuous())
    96             this->healthSpeed_ = 0.0;
     124            this->healthRate_ = 0.0;
    97125       
    98126        this->initializeIdentifier();
    99127    }
    100128   
    101     void HealthPickup::setHealth(float health)
    102     {
    103         if(health > 0.0f)
    104         {
    105             this->health_ = health;
    106         }
    107         else
    108         {
    109             COUT(1) << "Invalid health in HealthPickup." << std::endl;
    110         }
    111     }
    112    
    113     void HealthPickup::setHealthSpeed(float speed)
    114     {
    115         if(speed >= 0)
    116         {
    117             this->healthSpeed_ = speed;
    118         }
    119         else
    120         {
    121             COUT(1) << "Invalid healthSpeed in HealthPickup." << std::endl;
    122         }
    123     }
    124    
    125     void HealthPickup::setHealthType(std::string type)
    126     {
    127         if(type == HealthPickup::healthTypeLimited_s)
    128         {
    129             this->setHealthTypeDirect(pickupHealthType::limited);
    130         }
    131         else if(type == HealthPickup::healthTypeTemporary_s)
    132         {
    133             this->setHealthTypeDirect(pickupHealthType::temporary);
    134         }
    135         else if(type == HealthPickup::healthTypePermanent_s)
    136         {
    137             this->setHealthTypeDirect(pickupHealthType::permanent);
    138         }
    139         else
    140         {
    141             COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
    142         }
    143     }
    144    
     129    /**
     130    @brief
     131        Is called every tick.
     132        Does all the continuous stuff of this HealthPickup.
     133    @param dt
     134        The duration of the last tick.
     135    */
    145136    void HealthPickup::tick(float dt)
    146137    {
    147138        if(this->isContinuous() && this->isUsed())
    148139        {
    149             PickupCarrier* carrier = this->getCarrier();
    150             Pawn* pawn = dynamic_cast<Pawn*>(carrier);
     140            Pawn* pawn = this->carrierToPawnHelper();
     141            if(pawn == NULL) //!< If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
     142                this->destroy();
    151143           
    152             if(pawn == NULL)
    153             {
    154                 COUT(1) << "Invalid PickupCarrier in HealthPickup." << std::endl;
    155                 return;
    156             }
    157            
    158             float health = dt*this->getHealthSpeed();
     144            //! Calculate the health that is added this tick.
     145            float health = dt*this->getHealthRate();
    159146            if(health > this->getHealth())
    160147                health = this->getHealth();
     148            //! Calculate the health the Pawn will have once the health is added.
    161149            float fullHealth = pawn->getHealth() + health;
    162150            this->setHealth(this->getHealth()-health);
     
    171159                    break;
    172160                case pickupHealthType::temporary:
    173                     //TODO: How?
     161                    if(pawn->getMaxHealth() > fullHealth)
     162                    {
     163                        this->maxHealthSave_ = pawn->getMaxHealth();
     164                        this->maxHealthOverwrite_ = fullHealth;
     165                        pawn->setMaxHealth(fullHealth);
     166                    }
     167                    pawn->addHealth(health);
    174168                    break;
    175169                default:
     
    177171            }
    178172           
     173            //! If all health has been transfered.
    179174            if(this->getHealth() == 0)
    180175            {
    181                 //TODO: Destroy
    182             }
    183         }
    184     }
    185    
    186     const std::string& HealthPickup::getHealthType(void)
    187     {
    188         switch(this->getHealthTypeDirect())
    189         {
    190             case pickupHealthType::limited:
    191                 return HealthPickup::healthTypeLimited_s;
    192             case pickupHealthType::temporary:
    193                 return HealthPickup::healthTypeTemporary_s;
    194             case pickupHealthType::permanent:
    195                 return HealthPickup::healthTypePermanent_s;
    196             default:
    197                 COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
    198                 return HealthPickup::blankString_s;
    199         }
    200     }
    201    
    202     void HealthPickup::clone(OrxonoxClass* item)
    203     {
    204         if(item == NULL)
    205             item = new HealthPickup(this);
    206        
    207         SUPER(HealthPickup, clone, item);
    208        
    209         //TODO: No temp needed?
    210         HealthPickup* pickup = dynamic_cast<HealthPickup*>(item);
    211         pickup->setHealth(this->getHealth());
    212         pickup->setHealthSpeed(this->getHealthSpeed());
    213         pickup->setHealthTypeDirect(this->getHealthTypeDirect());
    214        
    215         pickup->initializeIdentifier();
    216     }
    217    
    218     //TODO: Does this work even if Pickup doesn't implement it?
     176                this->setUsed(false);
     177            }
     178        }
     179    }
     180   
     181    /**
     182    @brief
     183        Is called when the pickup has transited from used to unused or the other way around.
     184    */
    219185    void HealthPickup::changedUsed(void)
    220186    {
    221187        SUPER(HealthPickup, changedUsed);
    222188       
     189        //! If the pickup is not picked up nothing must be done.
     190        if(!this->isPickedUp())
     191            return;
     192       
     193        //! If the pickup has transited to used.
    223194        if(this->isUsed())
    224195        {
    225             PickupCarrier* carrier = this->getCarrier();
    226             Pawn* pawn = dynamic_cast<Pawn*>(carrier);
    227            
    228             if(pawn == NULL)
    229             {
    230                 COUT(1) << "Invalid PickupCarrier in HealthPickup." << std::endl;
    231                 return;
    232             }
    233            
    234196            if(this->isOnce())
    235197            {
     198                Pawn* pawn = this->carrierToPawnHelper();
     199                if(pawn == NULL) //!< If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
     200                    this->destroy();
     201               
    236202                float health = 0;
    237203                switch(this->getHealthTypeDirect())
     
    245211                        break;
    246212                    case pickupHealthType::temporary:
    247                         //TODO: How?
     213                        health = pawn->getHealth()+this->getHealth();
     214                        if(pawn->getMaxHealth() < health)
     215                        {
     216                            this->maxHealthSave_ = pawn->getMaxHealth();
     217                            this->maxHealthOverwrite_ = health;
     218                            pawn->setMaxHealth(health);
     219                        }
     220                        pawn->addHealth(this->getHealth());
    248221                        break;
    249222                    default:
     
    251224                }
    252225               
    253                 //TODO: Destroy.
     226                //! The pickup has been used up.
     227                this->setUsed(false);
    254228            }
    255229        }
    256230        else
    257231        {
    258             //TODO: Destroy, but be careful to not destroy before being used.
     232            if(this->getHealthTypeDirect() == pickupHealthType::temporary)
     233            {
     234                PickupCarrier* carrier = this->getCarrier();
     235                Pawn* pawn = dynamic_cast<Pawn*>(carrier);
     236               
     237                if(pawn == NULL)
     238                {
     239                    COUT(1) << "Something went horribly wrong in Health Pickup. PickupCarrier is no Pawn." << std::endl;
     240                    this->destroy();
     241                    return;
     242                }
     243               
     244                if(pawn->getMaxHealth() == this->maxHealthOverwrite_)
     245                {
     246                    pawn->setMaxHealth(this->maxHealthSave_);
     247                    this->maxHealthOverwrite_ = 0;
     248                    this->maxHealthSave_ = 0;
     249                }
     250            }
     251           
     252            //! If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused.
     253            if(this->isOnce() || (this->isContinuous() && this->getHealth() == 0))
     254            {
     255                this->destroy();
     256            }
     257        }
     258    }
     259   
     260    /**
     261    @brief
     262        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
     263    @return
     264        A pointer to the Pawn, or NULL if the conversion failed.
     265    */
     266    Pawn* HealthPickup::carrierToPawnHelper(void)
     267    {
     268        PickupCarrier* carrier = this->getCarrier();
     269        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
     270       
     271        if(pawn == NULL)
     272        {
     273            COUT(1) << "Invalid PickupCarrier in HealthPickup." << std::endl;
     274        }
     275       
     276        return pawn;
     277    }
     278   
     279    /**
     280    @brief
     281        Creates a duplicate of the input OrxonoxClass.
     282    @param item
     283        A pointer to the Orxonox class.
     284    */
     285    void HealthPickup::clone(OrxonoxClass* item)
     286    {
     287        if(item == NULL)
     288            item = new HealthPickup(this);
     289       
     290        SUPER(HealthPickup, clone, item);
     291       
     292        HealthPickup* pickup = dynamic_cast<HealthPickup*>(item);
     293        pickup->setHealth(this->getHealth());
     294        pickup->setHealthRate(this->getHealthRate());
     295        pickup->setHealthTypeDirect(this->getHealthTypeDirect());
     296       
     297        pickup->initializeIdentifier();
     298    }
     299   
     300    /**
     301    @brief
     302        Get the health type of this pickup.
     303    @return
     304        Returns the health type as a string.
     305    */
     306    const std::string& HealthPickup::getHealthType(void)
     307    {
     308        switch(this->getHealthTypeDirect())
     309        {
     310            case pickupHealthType::limited:
     311                return HealthPickup::healthTypeLimited_s;
     312            case pickupHealthType::temporary:
     313                return HealthPickup::healthTypeTemporary_s;
     314            case pickupHealthType::permanent:
     315                return HealthPickup::healthTypePermanent_s;
     316            default:
     317                COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
     318                return BLANKSTRING;
     319        }
     320    }
     321   
     322    /**
     323    @brief
     324        Sets the health.
     325    @param health
     326        The health.
     327    */
     328    void HealthPickup::setHealth(float health)
     329    {
     330        if(health > 0.0f)
     331        {
     332            this->health_ = health;
     333        }
     334        else
     335        {
     336            COUT(1) << "Invalid health in HealthPickup." << std::endl;
     337            this->health_ = 0.0;
     338        }
     339    }
     340   
     341    /**
     342    @brief
     343        Set the rate at which health is transferred if the pickup is continuous.
     344    @param rate
     345        The rate.
     346    */
     347    void HealthPickup::setHealthRate(float rate)
     348    {
     349        if(rate >= 0)
     350        {
     351            this->healthRate_ = rate;
     352        }
     353        else
     354        {
     355            COUT(1) << "Invalid healthSpeed in HealthPickup." << std::endl;
     356        }
     357    }
     358   
     359    /**
     360    @brief
     361        Set the type of the HealthPickup.
     362    @param type
     363        The type as a string.
     364    */
     365    void HealthPickup::setHealthType(std::string type)
     366    {
     367        if(type == HealthPickup::healthTypeLimited_s)
     368        {
     369            this->setHealthTypeDirect(pickupHealthType::limited);
     370        }
     371        else if(type == HealthPickup::healthTypeTemporary_s)
     372        {
     373            this->setHealthTypeDirect(pickupHealthType::temporary);
     374        }
     375        else if(type == HealthPickup::healthTypePermanent_s)
     376        {
     377            this->setHealthTypeDirect(pickupHealthType::permanent);
     378        }
     379        else
     380        {
     381            COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
    259382        }
    260383    }
  • code/branches/pickup3/src/modules/pickup/items/HealthPickup.h

    r6474 r6477  
    3232#include "pickup/PickupPrereqs.h"
    3333
     34#include <string>
     35#include <worldentities/pawns/Pawn.h>
     36#include "worldentities/StaticEntity.h"
     37
    3438#include "pickup/Pickup.h"
    3539#include "tools/interfaces/Tickable.h"
    36 #include "worldentities/StaticEntity.h"
    37 
    38 #include <string>
    3940
    4041namespace orxonox {
     
    5152    }
    5253   
     54    /**
     55    @brief
     56        A pickup that can do (dependent upon the parameters) lots of different things to the health of a Pawn.
     57        There are 4 parameters that can be choosen:
     58        1) The health. The amount of health that (in a way dependent on the other parameters) is transfered to the Pawn.
     59        2) The activation type: It can be chosen to be either 'immediate' or 'onUse'. The activation type essentially (as indicated by the name) defines when the health is transfered, either immediately after being picked up or only after the player uses it.
     60        3) The duration type: It can be chosen to be either 'once' or 'continuous'. For 'once' the specified health is transfered once to the Pawn, for 'continuous' the set health is transfered over a span of time at a rate defined by the health rate parameter.
     61        4) The health type: The health type can be choosen to be 'limited', 'temporary' or 'permanent'. 'limited' means that the health is increased only to the maximum health of the Pawn. 'temporary' means that the maximum health is temporarily elevated but will be set back as soon as the pickup is no longer in use. 'permanent' means that the maximum health of the Pawn is increased such that the health provided by the pickup will fit in and the maximum health stays that way.
     62    @author
     63        Damian 'Mozork' Frick
     64    */
    5365    class _PickupExport HealthPickup : public Pickup, public Tickable
    5466    {
    5567        public:
    5668       
    57             HealthPickup(BaseObject* creator);
    58             virtual ~HealthPickup();
     69            HealthPickup(BaseObject* creator); //!< Constructor.
     70            virtual ~HealthPickup(); //!< Destructor.
    5971           
    60             virtual void XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode);
    61             virtual void tick(float dt);
     72            virtual void XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode); //!< Method for creating a HealthPickup object through XML.
     73            virtual void tick(float dt); //!< Is called every tick.
    6274           
    63             virtual void clone(OrxonoxClass* item);
    64                        
    65             virtual void changedUsed(void);
     75            virtual void changedUsed(void); //!< Is called when the pickup has transited from used to unused or the other way around.
     76            virtual void clone(OrxonoxClass* item); //!< Creates a duplicate of the input OrxonoxClass.
     77           
     78            /**
     79            @brief Get the health that is transfered to the Pawn upon usage of this pickup.
     80            @return Returns the health.
     81            */
     82            inline float getHealth(void)
     83                { return this->health_; }
     84            /**
     85            @brief Get the rate at which the health is transferred to the Pawn, if this pickup has duration type 'continuous'.
     86            @return Returns the rate.
     87            */
     88            inline float getHealthRate(void)
     89                { return this->healthRate_; }
     90               
     91            /**
     92            @brief Get the type of HealthPickup, this pickup is.
     93            @return Returns the health type as an enum.
     94            */
     95            inline pickupHealthType::Value getHealthTypeDirect(void)
     96                { return this->healthType_; }
     97            const std::string& getHealthType(void); //!< Get the health type of this pickup.
    6698           
    6799        protected:
    68             void initializeIdentifier(void);
     100            void initializeIdentifier(void); //!< Initializes the PickupIdentifier of this pickup.
    69101
    70             void setHealth(float health);
    71             void setHealthSpeed(float speed);
    72             void setHealthType(std::string type);
     102            void setHealth(float health); //!< Sets the health.
     103            void setHealthRate(float speed); //!< Set the rate at which health is transferred if the pickup is continuous.
     104           
     105            /**
     106            @brief Set the health type of this pickup.
     107            @param type The type of this pickup as an enum.
     108            */
    73109            inline void setHealthTypeDirect(pickupHealthType::Value type)
    74110                { this->healthType_ = type; }
    75            
    76             inline float getHealth(void)
    77                 { return this->health_; }
    78             inline float getHealthSpeed(void)
    79                 { return this->healthSpeed_; }
    80             const std::string& getHealthType(void);
    81             inline pickupHealthType::Value getHealthTypeDirect(void)
    82                 { return this->healthType_; }
     111            void setHealthType(std::string type); //!< Set the type of the HealthPickup.
    83112       
    84113        private:
    85             void initialize(void);
     114            void initialize(void); //!< Initializes the member variables.
     115            Pawn* carrierToPawnHelper(void); //!< Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
    86116           
    87             float health_;
    88             float healthSpeed_;
    89             pickupHealthType::Value healthType_;
     117            float health_; //!< The health that is transferred to the Pawn.
     118            float healthRate_; //!< The rate at which the health is transferred.
     119            float maxHealthSave_; //!< Helper to remember what the actual maxHealth of the Pawn was before we changed it.
     120            float maxHealthOverwrite_; //!< Helper to remember with which value we overwrote the maxHealh, to detect if someone else changed it as well.
     121            pickupHealthType::Value healthType_; //!< The type of the HealthPickup.
    90122           
     123            //! Strings for the health types.
    91124            static const std::string healthTypeLimited_s;
    92125            static const std::string healthTypeTemporary_s;
    93126            static const std::string healthTypePermanent_s;
    94             static const std::string blankString_s; //TODO: Maybe already implemented somewhere?
    95127       
    96128    };
Note: See TracChangeset for help on using the changeset viewer.