Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Simplification in creation of PickupIdentifier.

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