Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup3/src/modules/pickup/items/HealthPickup.cc @ 6477

Last change on this file since 6477 was 6477, checked in by dafrick, 14 years ago

Some more documenting. Completed HealthPickup.

File size: 12.0 KB
Line 
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
29#include "HealthPickup.h"
30
31#include "core/CoreIncludes.h"
32#include "core/XMLPort.h"
33#include "util/StringUtils.h"
34
35#include "worldentities/pawns/Pawn.h"
36#include "pickup/PickupIdentifier.h"
37
38#include <sstream>
39
40namespace orxonox
41{
42   
43    /*static*/ const std::string HealthPickup::healthTypeLimited_s = "limited";
44    /*static*/ const std::string HealthPickup::healthTypeTemporary_s = "temporary";
45    /*static*/ const std::string HealthPickup::healthTypePermanent_s = "permanent";
46   
47    CreateFactory(HealthPickup);
48   
49    /**
50    @brief
51        Constructor. Registers the object and initializes the member variables.
52    */
53    HealthPickup::HealthPickup(BaseObject* creator) : Pickup(creator)
54    {
55        RegisterObject(HealthPickup);
56       
57        this->initialize();
58    }
59   
60    /**
61    @brief
62        Destructor.
63    */
64    HealthPickup::~HealthPickup()
65    {
66       
67    }
68   
69    /**
70    @brief
71        Initializes the member variables.
72    */
73    void HealthPickup::initialize(void)
74    {
75        RegisterObject(HealthPickup);
76       
77        this->health_ = 0;
78        this->healthRate_ = 0;
79        this->healthType_ = pickupHealthType::limited;
80        this->maxHealthSave_ = 0;
81        this->maxHealthOverwrite_ = 0;
82       
83    }
84   
85    /**
86    @brief
87        Initializes the PickupIdentifier of this pickup.
88    */
89    void HealthPickup::initializeIdentifier(void)
90    {
91        this->pickupIdentifier_->addClass(this->getIdentifier());
92       
93        std::stringstream stream;
94        stream << this->getHealth();
95        std::string type1 = "health";
96        std::string val1 = stream.str();
97        this->pickupIdentifier_->addParameter(type1, val1);
98       
99        //TODO: Does this work, is val valid outside the function scope?
100        std::string val2 = this->getHealthType();
101        std::string type2 = "healthType";
102        this->pickupIdentifier_->addParameter(type2, val2);
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    */
115    void HealthPickup::HealthPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
116    {
117        SUPER(HealthPickup, XMLPort, xmlelement, mode);
118       
119        XMLPortParam(HealthPickup, "health", setHealth, getHealth, xmlelement, mode);
120        XMLPortParam(HealthPickup, "healthRate", setHealthRate, getHealthRate, xmlelement, mode);
121        XMLPortParam(HealthPickup, "healthType", setHealthType, getHealthType, xmlelement, mode);
122       
123        if(!this->isContinuous())
124            this->healthRate_ = 0.0;
125       
126        this->initializeIdentifier();
127    }
128   
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    */
136    void HealthPickup::tick(float dt)
137    {
138        if(this->isContinuous() && this->isUsed())
139        {
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();
143           
144            //! Calculate the health that is added this tick.
145            float health = dt*this->getHealthRate();
146            if(health > this->getHealth())
147                health = this->getHealth();
148            //! Calculate the health the Pawn will have once the health is added.
149            float fullHealth = pawn->getHealth() + health;
150            this->setHealth(this->getHealth()-health);
151                   
152            switch(this->getHealthTypeDirect())
153            {
154                case pickupHealthType::permanent:
155                    if(pawn->getMaxHealth() > fullHealth)
156                        pawn->setMaxHealth(fullHealth);
157                case pickupHealthType::limited:
158                    pawn->addHealth(health);
159                    break;
160                case pickupHealthType::temporary:
161                    if(pawn->getMaxHealth() > fullHealth)
162                    {
163                        this->maxHealthSave_ = pawn->getMaxHealth();
164                        this->maxHealthOverwrite_ = fullHealth;
165                        pawn->setMaxHealth(fullHealth);
166                    }
167                    pawn->addHealth(health);
168                    break;
169                default:
170                    COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
171            }
172           
173            //! If all health has been transfered.
174            if(this->getHealth() == 0)
175            {
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    */
185    void HealthPickup::changedUsed(void)
186    {
187        SUPER(HealthPickup, changedUsed);
188       
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.
194        if(this->isUsed())
195        {
196            if(this->isOnce())
197            {
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               
202                float health = 0;
203                switch(this->getHealthTypeDirect())
204                {
205                    case pickupHealthType::permanent:
206                        health = pawn->getHealth()+this->getHealth();
207                        if(pawn->getMaxHealth() < health)
208                            pawn->setMaxHealth(health);
209                    case pickupHealthType::limited:
210                        pawn->addHealth(this->getHealth());
211                        break;
212                    case pickupHealthType::temporary:
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());
221                        break;
222                    default:
223                        COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
224                }
225               
226                //! The pickup has been used up.
227                this->setUsed(false);
228            }
229        }
230        else
231        {
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;
382        }
383    }
384
385}
Note: See TracBrowser for help on using the repository browser.