Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

More bug fixes.

File size: 12.1 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    @brief
86        Initializes the PickupIdentifier of this pickup.
87    */
88    void HealthPickup::initializeIdentifier(void)
89    {
90        this->pickupIdentifier_->addClass(this->getIdentifier());
91       
92        std::stringstream stream;
93        stream << this->getHealth();
94        std::string type1 = "health";
95        std::string val1 = stream.str();
96        this->pickupIdentifier_->addParameter(type1, val1);
97       
98        //TODO: Does this work, is val valid outside the function scope?
99        std::string val2 = this->getHealthType();
100        std::string type2 = "healthType";
101        this->pickupIdentifier_->addParameter(type2, val2);
102       
103        stream.clear();
104        stream << this->getHealthRate();
105        std::string val3 = stream.str();
106        std::string type3 = "healthRate";
107        this->pickupIdentifier_->addParameter(type3, val3);
108    }
109   
110    /**
111    @brief
112        Method for creating a HealthPickup object through XML.
113    */
114    void HealthPickup::HealthPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
115    {
116        SUPER(HealthPickup, XMLPort, xmlelement, mode);
117       
118        XMLPortParam(HealthPickup, "health", setHealth, getHealth, xmlelement, mode);
119        XMLPortParam(HealthPickup, "healthRate", setHealthRate, getHealthRate, xmlelement, mode);
120        XMLPortParam(HealthPickup, "healthType", setHealthType, getHealthType, xmlelement, mode);
121       
122        if(!this->isContinuous())
123            this->healthRate_ = 0.0;
124       
125        COUT(1) << "HealthPickup " << this->getHealth() << ", " << this->getHealthRate() << ", " << this->getHealthType() << "." << std::endl;
126       
127        this->initializeIdentifier();
128    }
129   
130    /**
131    @brief
132        Is called every tick.
133        Does all the continuous stuff of this HealthPickup.
134    @param dt
135        The duration of the last tick.
136    */
137    void HealthPickup::tick(float dt)
138    {
139        if(this->isContinuous() && this->isUsed())
140        {
141            Pawn* pawn = this->carrierToPawnHelper();
142            if(pawn == NULL) //!< If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
143                this->destroy();
144           
145            //! Calculate the health that is added this tick.
146            float health = dt*this->getHealthRate();
147            if(health > this->getHealth())
148                health = this->getHealth();
149            //! Calculate the health the Pawn will have once the health is added.
150            float fullHealth = pawn->getHealth() + health;
151            this->setHealth(this->getHealth()-health);
152                   
153            switch(this->getHealthTypeDirect())
154            {
155                case pickupHealthType::permanent:
156                    if(pawn->getMaxHealth() > fullHealth)
157                        pawn->setMaxHealth(fullHealth);
158                case pickupHealthType::limited:
159                    pawn->addHealth(health);
160                    break;
161                case pickupHealthType::temporary:
162                    if(pawn->getMaxHealth() > fullHealth)
163                    {
164                        this->maxHealthSave_ = pawn->getMaxHealth();
165                        this->maxHealthOverwrite_ = fullHealth;
166                        pawn->setMaxHealth(fullHealth);
167                    }
168                    pawn->addHealth(health);
169                    break;
170                default:
171                    COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
172            }
173           
174            //! If all health has been transfered.
175            if(this->getHealth() == 0)
176            {
177                this->setUsed(false);
178            }
179        }
180    }
181   
182    /**
183    @brief
184        Is called when the pickup has transited from used to unused or the other way around.
185    */
186    void HealthPickup::changedUsed(void)
187    {
188        SUPER(HealthPickup, changedUsed);
189       
190        //! If the pickup is not picked up nothing must be done.
191        if(!this->isPickedUp())
192            return;
193       
194        //! If the pickup has transited to used.
195        if(this->isUsed())
196        {
197            if(this->isOnce())
198            {
199                Pawn* pawn = this->carrierToPawnHelper();
200                if(pawn == NULL) //!< If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
201                    this->destroy();
202               
203                float health = 0;
204                switch(this->getHealthTypeDirect())
205                {
206                    case pickupHealthType::permanent:
207                        health = pawn->getHealth()+this->getHealth();
208                        if(pawn->getMaxHealth() < health)
209                            pawn->setMaxHealth(health);
210                    case pickupHealthType::limited:
211                        pawn->addHealth(this->getHealth());
212                        break;
213                    case pickupHealthType::temporary:
214                        health = pawn->getHealth()+this->getHealth();
215                        if(pawn->getMaxHealth() < health)
216                        {
217                            this->maxHealthSave_ = pawn->getMaxHealth();
218                            this->maxHealthOverwrite_ = health;
219                            pawn->setMaxHealth(health);
220                        }
221                        pawn->addHealth(this->getHealth());
222                        break;
223                    default:
224                        COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
225                }
226               
227                //! The pickup has been used up.
228                this->setUsed(false);
229            }
230        }
231        else
232        {
233            if(this->getHealthTypeDirect() == pickupHealthType::temporary)
234            {
235                PickupCarrier* carrier = this->getCarrier();
236                Pawn* pawn = dynamic_cast<Pawn*>(carrier);
237               
238                if(pawn == NULL)
239                {
240                    COUT(1) << "Something went horribly wrong in Health Pickup. PickupCarrier is no Pawn." << std::endl;
241                    this->destroy();
242                    return;
243                }
244               
245                if(pawn->getMaxHealth() == this->maxHealthOverwrite_)
246                {
247                    pawn->setMaxHealth(this->maxHealthSave_);
248                    this->maxHealthOverwrite_ = 0;
249                    this->maxHealthSave_ = 0;
250                }
251            }
252           
253            //! If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused.
254            if(this->isOnce() || (this->isContinuous() && this->getHealth() == 0))
255            {
256                this->destroy();
257            }
258        }
259    }
260   
261    /**
262    @brief
263        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
264    @return
265        A pointer to the Pawn, or NULL if the conversion failed.
266    */
267    Pawn* HealthPickup::carrierToPawnHelper(void)
268    {
269        PickupCarrier* carrier = this->getCarrier();
270        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
271       
272        if(pawn == NULL)
273        {
274            COUT(1) << "Invalid PickupCarrier in HealthPickup." << std::endl;
275        }
276       
277        return pawn;
278    }
279   
280    /**
281    @brief
282        Creates a duplicate of the input OrxonoxClass.
283    @param item
284        A pointer to the Orxonox class.
285    */
286    void HealthPickup::clone(OrxonoxClass* item)
287    {
288        if(item == NULL)
289            item = new HealthPickup(this);
290       
291        SUPER(HealthPickup, clone, item);
292       
293        HealthPickup* pickup = dynamic_cast<HealthPickup*>(item);
294        pickup->setHealth(this->getHealth());
295        pickup->setHealthRate(this->getHealthRate());
296        pickup->setHealthTypeDirect(this->getHealthTypeDirect());
297       
298        pickup->initializeIdentifier();
299    }
300   
301    /**
302    @brief
303        Get the health type of this pickup.
304    @return
305        Returns the health type as a string.
306    */
307    const std::string& HealthPickup::getHealthType(void)
308    {
309        switch(this->getHealthTypeDirect())
310        {
311            case pickupHealthType::limited:
312                return HealthPickup::healthTypeLimited_s;
313            case pickupHealthType::temporary:
314                return HealthPickup::healthTypeTemporary_s;
315            case pickupHealthType::permanent:
316                return HealthPickup::healthTypePermanent_s;
317            default:
318                COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
319                return BLANKSTRING;
320        }
321    }
322   
323    /**
324    @brief
325        Sets the health.
326    @param health
327        The health.
328    */
329    void HealthPickup::setHealth(float health)
330    {
331        if(health > 0.0f)
332        {
333            this->health_ = health;
334        }
335        else
336        {
337            COUT(1) << "Invalid health in HealthPickup." << std::endl;
338            this->health_ = 0.0;
339        }
340    }
341   
342    /**
343    @brief
344        Set the rate at which health is transferred if the pickup is continuous.
345    @param rate
346        The rate.
347    */
348    void HealthPickup::setHealthRate(float rate)
349    {
350        if(rate >= 0)
351        {
352            this->healthRate_ = rate;
353        }
354        else
355        {
356            COUT(1) << "Invalid healthSpeed in HealthPickup." << std::endl; 
357        }
358    }
359   
360    /**
361    @brief
362        Set the type of the HealthPickup.
363    @param type
364        The type as a string.
365    */
366    void HealthPickup::setHealthType(std::string type)
367    {
368        if(type == HealthPickup::healthTypeLimited_s)
369        {
370            this->setHealthTypeDirect(pickupHealthType::limited);
371        }
372        else if(type == HealthPickup::healthTypeTemporary_s)
373        {
374            this->setHealthTypeDirect(pickupHealthType::temporary);
375        }
376        else if(type == HealthPickup::healthTypePermanent_s)
377        {
378            this->setHealthTypeDirect(pickupHealthType::permanent);
379        }
380        else
381        {
382            COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
383        }
384    }
385
386}
Note: See TracBrowser for help on using the repository browser.