Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/items/HealthPickup.cc @ 9218

Last change on this file since 9218 was 8864, checked in by dafrick, 13 years ago

Improving output.

  • Property svn:eol-style set to native
File size: 11.8 KB
RevLine 
[6474]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[6540]29/**
30    @file HealthPickup.cc
31    @brief Implementation of the HealthPickup class.
32*/
33
[6474]34#include "HealthPickup.h"
35
[7547]36#include <sstream>
[6474]37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
[7547]40#include "pickup/PickupIdentifier.h"
[6474]41#include "worldentities/pawns/Pawn.h"
42
43namespace orxonox
44{
[7163]45
[6474]46    /*static*/ const std::string HealthPickup::healthTypeLimited_s = "limited";
47    /*static*/ const std::string HealthPickup::healthTypeTemporary_s = "temporary";
48    /*static*/ const std::string HealthPickup::healthTypePermanent_s = "permanent";
[7163]49
[6474]50    CreateFactory(HealthPickup);
[7163]51
[6477]52    /**
53    @brief
54        Constructor. Registers the object and initializes the member variables.
55    */
[6474]56    HealthPickup::HealthPickup(BaseObject* creator) : Pickup(creator)
57    {
58        RegisterObject(HealthPickup);
[7163]59
[6474]60        this->initialize();
61    }
[7163]62
[6477]63    /**
64    @brief
65        Destructor.
66    */
[6474]67    HealthPickup::~HealthPickup()
68    {
[7163]69
[6474]70    }
[7163]71
[6477]72    /**
[7163]73    @brief
[6477]74        Initializes the member variables.
75    */
[6474]76    void HealthPickup::initialize(void)
[7163]77    {
[8864]78        this->health_ = 0.0f;
79        this->healthRate_ = 0.0f;
[6474]80        this->healthType_ = pickupHealthType::limited;
[8864]81        this->maxHealthSave_ = 0.0f;
82        this->maxHealthOverwrite_ = 0.0f;
[7163]83
[6490]84        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
[6474]85    }
[7163]86
[6477]87    /**
88    @brief
89        Initializes the PickupIdentifier of this pickup.
90    */
[6474]91    void HealthPickup::initializeIdentifier(void)
92    {
93        std::stringstream stream;
94        stream << this->getHealth();
95        std::string type1 = "health";
96        std::string val1 = stream.str();
[6475]97        this->pickupIdentifier_->addParameter(type1, val1);
[7163]98
[6474]99        std::string val2 = this->getHealthType();
100        std::string type2 = "healthType";
[6475]101        this->pickupIdentifier_->addParameter(type2, val2);
[7163]102
[6477]103        stream.clear();
104        stream << this->getHealthRate();
105        std::string val3 = stream.str();
106        std::string type3 = "healthRate";
107        this->pickupIdentifier_->addParameter(type3, val3);
[6474]108    }
[7163]109
[6477]110    /**
111    @brief
112        Method for creating a HealthPickup object through XML.
113    */
[6496]114    void HealthPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
[6474]115    {
116        SUPER(HealthPickup, XMLPort, xmlelement, mode);
[7163]117
[6474]118        XMLPortParam(HealthPickup, "health", setHealth, getHealth, xmlelement, mode);
[6477]119        XMLPortParam(HealthPickup, "healthRate", setHealthRate, getHealthRate, xmlelement, mode);
[6474]120        XMLPortParam(HealthPickup, "healthType", setHealthType, getHealthType, xmlelement, mode);
[7163]121
[6474]122        if(!this->isContinuous())
[8864]123            this->healthRate_ = 0.0f;
[7163]124
[6474]125        this->initializeIdentifier();
126    }
[7163]127
[6477]128    /**
129    @brief
130        Is called every tick.
131        Does all the continuous stuff of this HealthPickup.
132    @param dt
133        The duration of the last tick.
134    */
[6474]135    void HealthPickup::tick(float dt)
136    {
[6540]137        SUPER(HealthPickup, tick, dt);
[7163]138
[6474]139        if(this->isContinuous() && this->isUsed())
140        {
[6477]141            Pawn* pawn = this->carrierToPawnHelper();
[7541]142            if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[7163]143                this->Pickupable::destroy();
144
[7541]145            // Calculate the health that is added this tick.
[6477]146            float health = dt*this->getHealthRate();
[6474]147            if(health > this->getHealth())
148                health = this->getHealth();
[7541]149            // Calculate the health the Pawn will have once the health is added.
[6474]150            float fullHealth = pawn->getHealth() + health;
151            this->setHealth(this->getHealth()-health);
[7163]152
[6474]153            switch(this->getHealthTypeDirect())
154            {
155                case pickupHealthType::permanent:
[6520]156                    if(pawn->getMaxHealth() < fullHealth)
[6474]157                        pawn->setMaxHealth(fullHealth);
158                case pickupHealthType::limited:
159                    pawn->addHealth(health);
160                    break;
161                case pickupHealthType::temporary:
[6477]162                    if(pawn->getMaxHealth() > fullHealth)
163                    {
164                        this->maxHealthSave_ = pawn->getMaxHealth();
165                        this->maxHealthOverwrite_ = fullHealth;
166                        pawn->setMaxHealth(fullHealth);
167                    }
168                    pawn->addHealth(health);
[6474]169                    break;
170                default:
[8858]171                    orxout(internal_error, context::pickups) << "Invalid healthType in HealthPickup." << endl;
[6474]172            }
[7163]173
[7541]174            // If all health has been transferred.
[8864]175            if(this->getHealth() == 0.0f)
[6474]176            {
[6477]177                this->setUsed(false);
[6474]178            }
179        }
180    }
[7163]181
[6477]182    /**
183    @brief
184        Is called when the pickup has transited from used to unused or the other way around.
185    */
[6474]186    void HealthPickup::changedUsed(void)
187    {
188        SUPER(HealthPickup, changedUsed);
[7163]189
[7541]190        // If the pickup has transited to used.
[6474]191        if(this->isUsed())
192        {
193            if(this->isOnce())
194            {
[6477]195                Pawn* pawn = this->carrierToPawnHelper();
[7541]196                if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[7163]197                    this->Pickupable::destroy();
198
[8864]199                float health = 0.0f;
[6474]200                switch(this->getHealthTypeDirect())
201                {
202                    case pickupHealthType::permanent:
203                        health = pawn->getHealth()+this->getHealth();
204                        if(pawn->getMaxHealth() < health)
205                            pawn->setMaxHealth(health);
206                    case pickupHealthType::limited:
207                        pawn->addHealth(this->getHealth());
208                        break;
209                    case pickupHealthType::temporary:
[6477]210                        health = pawn->getHealth()+this->getHealth();
211                        if(pawn->getMaxHealth() < health)
212                        {
213                            this->maxHealthSave_ = pawn->getMaxHealth();
214                            this->maxHealthOverwrite_ = health;
215                            pawn->setMaxHealth(health);
216                        }
217                        pawn->addHealth(this->getHealth());
[6474]218                        break;
219                    default:
[8858]220                        orxout(internal_error, context::pickups) << "Invalid healthType in HealthPickup." << endl;
[6474]221                }
[7163]222
[7541]223                // The pickup has been used up.
[6477]224                this->setUsed(false);
[6474]225            }
226        }
227        else
228        {
[6477]229            if(this->getHealthTypeDirect() == pickupHealthType::temporary)
230            {
231                PickupCarrier* carrier = this->getCarrier();
232                Pawn* pawn = dynamic_cast<Pawn*>(carrier);
[7163]233
[6477]234                if(pawn == NULL)
235                {
[8864]236                    orxout(internal_error, context::pickups) << "Something went horribly wrong in Health Pickup. PickupCarrier is '" << carrier->getIdentifier()->getName() << "' instead of Pawn." << endl;
[7163]237                    this->Pickupable::destroy();
[6477]238                    return;
239                }
[7163]240
[6477]241                if(pawn->getMaxHealth() == this->maxHealthOverwrite_)
242                {
243                    pawn->setMaxHealth(this->maxHealthSave_);
244                    this->maxHealthOverwrite_ = 0;
245                    this->maxHealthSave_ = 0;
246                }
247            }
[7163]248
[7541]249            // If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused.
[8864]250            if(this->isOnce() || (this->isContinuous() && this->getHealth() == 0.0f))
[6477]251            {
[7163]252                this->Pickupable::destroy();
[6477]253            }
[6474]254        }
255    }
[7163]256
[6477]257    /**
258    @brief
259        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
260    @return
261        A pointer to the Pawn, or NULL if the conversion failed.
262    */
263    Pawn* HealthPickup::carrierToPawnHelper(void)
264    {
265        PickupCarrier* carrier = this->getCarrier();
266        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
[7163]267
[6477]268        if(pawn == NULL)
[8858]269            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in HealthPickup." << endl;
[7163]270
[6477]271        return pawn;
272    }
[7163]273
[6477]274    /**
275    @brief
276        Creates a duplicate of the input OrxonoxClass.
277    @param item
278        A pointer to the Orxonox class.
279    */
[6497]280    void HealthPickup::clone(OrxonoxClass*& item)
[6477]281    {
282        if(item == NULL)
283            item = new HealthPickup(this);
[7163]284
[6477]285        SUPER(HealthPickup, clone, item);
[7163]286
[6477]287        HealthPickup* pickup = dynamic_cast<HealthPickup*>(item);
288        pickup->setHealth(this->getHealth());
289        pickup->setHealthRate(this->getHealthRate());
290        pickup->setHealthTypeDirect(this->getHealthTypeDirect());
[7163]291
[6477]292        pickup->initializeIdentifier();
293    }
[7163]294
[6477]295    /**
296    @brief
297        Get the health type of this pickup.
298    @return
299        Returns the health type as a string.
300    */
[7547]301    const std::string& HealthPickup::getHealthType(void) const
[6477]302    {
303        switch(this->getHealthTypeDirect())
304        {
305            case pickupHealthType::limited:
306                return HealthPickup::healthTypeLimited_s;
307            case pickupHealthType::temporary:
308                return HealthPickup::healthTypeTemporary_s;
309            case pickupHealthType::permanent:
310                return HealthPickup::healthTypePermanent_s;
311            default:
[8858]312                orxout(internal_error, context::pickups) << "Invalid healthType in HealthPickup." << endl;
[6477]313                return BLANKSTRING;
314        }
315    }
[7163]316
[6477]317    /**
318    @brief
319        Sets the health.
320    @param health
321        The health.
322    */
323    void HealthPickup::setHealth(float health)
324    {
[6499]325        if(health >= 0.0f)
[6477]326            this->health_ = health;
327        else
328        {
[8864]329            orxout(internal_error, context::pickups) << "Invalid health '" << health << "' in HealthPickup. The health must be non.negative." << endl;
330            this->health_ = 0.0f;
[6477]331        }
332    }
[7163]333
[6477]334    /**
335    @brief
336        Set the rate at which health is transferred if the pickup is continuous.
337    @param rate
338        The rate.
339    */
340    void HealthPickup::setHealthRate(float rate)
341    {
[8864]342        if(rate >= 0.0f)
[6477]343            this->healthRate_ = rate;
344        else
[8864]345            orxout(internal_error, context::pickups) << "Invalid healthRate '" << rate << "' in HealthPickup. The healthRate must be non-negative." << endl;
[6477]346    }
[7163]347
[6477]348    /**
349    @brief
350        Set the type of the HealthPickup.
351    @param type
352        The type as a string.
353    */
354    void HealthPickup::setHealthType(std::string type)
355    {
356        if(type == HealthPickup::healthTypeLimited_s)
357            this->setHealthTypeDirect(pickupHealthType::limited);
358        else if(type == HealthPickup::healthTypeTemporary_s)
359            this->setHealthTypeDirect(pickupHealthType::temporary);
360        else if(type == HealthPickup::healthTypePermanent_s)
361            this->setHealthTypeDirect(pickupHealthType::permanent);
362        else
[8864]363            orxout(internal_error, context::pickups) << "Invalid healthType '" << type << "' in HealthPickup." << endl;
[6477]364    }
[6474]365
366}
Note: See TracBrowser for help on using the repository browser.