Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/items/HealthPickup.cc @ 9305

Last change on this file since 9305 was 9305, checked in by landauf, 12 years ago

simplified code a little by using MultiType instead of explicit conversion

  • Property svn:eol-style set to native
File size: 11.5 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/**
30    @file HealthPickup.cc
31    @brief Implementation of the HealthPickup class.
32*/
33
34#include "HealthPickup.h"
35
36#include <sstream>
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
40#include "pickup/PickupIdentifier.h"
41#include "worldentities/pawns/Pawn.h"
42
43namespace orxonox
44{
45
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";
49
50    CreateFactory(HealthPickup);
51
52    /**
53    @brief
54        Constructor. Registers the object and initializes the member variables.
55    */
56    HealthPickup::HealthPickup(BaseObject* creator) : Pickup(creator)
57    {
58        RegisterObject(HealthPickup);
59
60        this->initialize();
61    }
62
63    /**
64    @brief
65        Destructor.
66    */
67    HealthPickup::~HealthPickup()
68    {
69
70    }
71
72    /**
73    @brief
74        Initializes the member variables.
75    */
76    void HealthPickup::initialize(void)
77    {
78        this->health_ = 0.0f;
79        this->healthRate_ = 0.0f;
80        this->healthType_ = pickupHealthType::limited;
81        this->maxHealthSave_ = 0.0f;
82        this->maxHealthOverwrite_ = 0.0f;
83
84        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
85    }
86
87    /**
88    @brief
89        Initializes the PickupIdentifier of this pickup.
90    */
91    void HealthPickup::initializeIdentifier(void)
92    {
93        this->pickupIdentifier_->addParameter("health", this->getHealth());
94        this->pickupIdentifier_->addParameter("healthType", this->getHealthType());
95        this->pickupIdentifier_->addParameter("healthRate", this->getHealthRate());
96    }
97
98    /**
99    @brief
100        Method for creating a HealthPickup object through XML.
101    */
102    void HealthPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
103    {
104        SUPER(HealthPickup, XMLPort, xmlelement, mode);
105
106        XMLPortParam(HealthPickup, "health", setHealth, getHealth, xmlelement, mode);
107        XMLPortParam(HealthPickup, "healthRate", setHealthRate, getHealthRate, xmlelement, mode);
108        XMLPortParam(HealthPickup, "healthType", setHealthType, getHealthType, xmlelement, mode);
109
110        if(!this->isContinuous())
111            this->healthRate_ = 0.0f;
112
113        this->initializeIdentifier();
114    }
115
116    /**
117    @brief
118        Is called every tick.
119        Does all the continuous stuff of this HealthPickup.
120    @param dt
121        The duration of the last tick.
122    */
123    void HealthPickup::tick(float dt)
124    {
125        SUPER(HealthPickup, tick, dt);
126
127        if(this->isContinuous() && this->isUsed())
128        {
129            Pawn* pawn = this->carrierToPawnHelper();
130            if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
131                this->Pickupable::destroy();
132
133            // Calculate the health that is added this tick.
134            float health = dt*this->getHealthRate();
135            if(health > this->getHealth())
136                health = this->getHealth();
137            // Calculate the health the Pawn will have once the health is added.
138            float fullHealth = pawn->getHealth() + health;
139            this->setHealth(this->getHealth()-health);
140
141            switch(this->getHealthTypeDirect())
142            {
143                case pickupHealthType::permanent:
144                    if(pawn->getMaxHealth() < fullHealth)
145                        pawn->setMaxHealth(fullHealth);
146                case pickupHealthType::limited:
147                    pawn->addHealth(health);
148                    break;
149                case pickupHealthType::temporary:
150                    if(pawn->getMaxHealth() > fullHealth)
151                    {
152                        this->maxHealthSave_ = pawn->getMaxHealth();
153                        this->maxHealthOverwrite_ = fullHealth;
154                        pawn->setMaxHealth(fullHealth);
155                    }
156                    pawn->addHealth(health);
157                    break;
158                default:
159                    orxout(internal_error, context::pickups) << "Invalid healthType in HealthPickup." << endl;
160            }
161
162            // If all health has been transferred.
163            if(this->getHealth() == 0.0f)
164            {
165                this->setUsed(false);
166            }
167        }
168    }
169
170    /**
171    @brief
172        Is called when the pickup has transited from used to unused or the other way around.
173    */
174    void HealthPickup::changedUsed(void)
175    {
176        SUPER(HealthPickup, changedUsed);
177
178        // If the pickup has transited to used.
179        if(this->isUsed())
180        {
181            if(this->isOnce())
182            {
183                Pawn* pawn = this->carrierToPawnHelper();
184                if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
185                    this->Pickupable::destroy();
186
187                float health = 0.0f;
188                switch(this->getHealthTypeDirect())
189                {
190                    case pickupHealthType::permanent:
191                        health = pawn->getHealth()+this->getHealth();
192                        if(pawn->getMaxHealth() < health)
193                            pawn->setMaxHealth(health);
194                    case pickupHealthType::limited:
195                        pawn->addHealth(this->getHealth());
196                        break;
197                    case pickupHealthType::temporary:
198                        health = pawn->getHealth()+this->getHealth();
199                        if(pawn->getMaxHealth() < health)
200                        {
201                            this->maxHealthSave_ = pawn->getMaxHealth();
202                            this->maxHealthOverwrite_ = health;
203                            pawn->setMaxHealth(health);
204                        }
205                        pawn->addHealth(this->getHealth());
206                        break;
207                    default:
208                        orxout(internal_error, context::pickups) << "Invalid healthType in HealthPickup." << endl;
209                }
210
211                // The pickup has been used up.
212                this->setUsed(false);
213            }
214        }
215        else
216        {
217            if(this->getHealthTypeDirect() == pickupHealthType::temporary)
218            {
219                PickupCarrier* carrier = this->getCarrier();
220                Pawn* pawn = orxonox_cast<Pawn*>(carrier);
221
222                if(pawn == NULL)
223                {
224                    orxout(internal_error, context::pickups) << "Something went horribly wrong in Health Pickup. PickupCarrier is '" << carrier->getIdentifier()->getName() << "' instead of Pawn." << endl;
225                    this->Pickupable::destroy();
226                    return;
227                }
228
229                if(pawn->getMaxHealth() == this->maxHealthOverwrite_)
230                {
231                    pawn->setMaxHealth(this->maxHealthSave_);
232                    this->maxHealthOverwrite_ = 0;
233                    this->maxHealthSave_ = 0;
234                }
235            }
236
237            // If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused.
238            if(this->isOnce() || (this->isContinuous() && this->getHealth() == 0.0f))
239            {
240                this->Pickupable::destroy();
241            }
242        }
243    }
244
245    /**
246    @brief
247        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
248    @return
249        A pointer to the Pawn, or NULL if the conversion failed.
250    */
251    Pawn* HealthPickup::carrierToPawnHelper(void)
252    {
253        PickupCarrier* carrier = this->getCarrier();
254        Pawn* pawn = orxonox_cast<Pawn*>(carrier);
255
256        if(pawn == NULL)
257            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in HealthPickup." << endl;
258
259        return pawn;
260    }
261
262    /**
263    @brief
264        Creates a duplicate of the input OrxonoxClass.
265    @param item
266        A pointer to the Orxonox class.
267    */
268    void HealthPickup::clone(OrxonoxClass*& item)
269    {
270        if(item == NULL)
271            item = new HealthPickup(this);
272
273        SUPER(HealthPickup, clone, item);
274
275        HealthPickup* pickup = orxonox_cast<HealthPickup*>(item);
276        pickup->setHealth(this->getHealth());
277        pickup->setHealthRate(this->getHealthRate());
278        pickup->setHealthTypeDirect(this->getHealthTypeDirect());
279
280        pickup->initializeIdentifier();
281    }
282
283    /**
284    @brief
285        Get the health type of this pickup.
286    @return
287        Returns the health type as a string.
288    */
289    const std::string& HealthPickup::getHealthType(void) const
290    {
291        switch(this->getHealthTypeDirect())
292        {
293            case pickupHealthType::limited:
294                return HealthPickup::healthTypeLimited_s;
295            case pickupHealthType::temporary:
296                return HealthPickup::healthTypeTemporary_s;
297            case pickupHealthType::permanent:
298                return HealthPickup::healthTypePermanent_s;
299            default:
300                orxout(internal_error, context::pickups) << "Invalid healthType in HealthPickup." << endl;
301                return BLANKSTRING;
302        }
303    }
304
305    /**
306    @brief
307        Sets the health.
308    @param health
309        The health.
310    */
311    void HealthPickup::setHealth(float health)
312    {
313        if(health >= 0.0f)
314            this->health_ = health;
315        else
316        {
317            orxout(internal_error, context::pickups) << "Invalid health '" << health << "' in HealthPickup. The health must be non.negative." << endl;
318            this->health_ = 0.0f;
319        }
320    }
321
322    /**
323    @brief
324        Set the rate at which health is transferred if the pickup is continuous.
325    @param rate
326        The rate.
327    */
328    void HealthPickup::setHealthRate(float rate)
329    {
330        if(rate >= 0.0f)
331            this->healthRate_ = rate;
332        else
333            orxout(internal_error, context::pickups) << "Invalid healthRate '" << rate << "' in HealthPickup. The healthRate must be non-negative." << endl;
334    }
335
336    /**
337    @brief
338        Set the type of the HealthPickup.
339    @param type
340        The type as a string.
341    */
342    void HealthPickup::setHealthType(std::string type)
343    {
344        if(type == HealthPickup::healthTypeLimited_s)
345            this->setHealthTypeDirect(pickupHealthType::limited);
346        else if(type == HealthPickup::healthTypeTemporary_s)
347            this->setHealthTypeDirect(pickupHealthType::temporary);
348        else if(type == HealthPickup::healthTypePermanent_s)
349            this->setHealthTypeDirect(pickupHealthType::permanent);
350        else
351            orxout(internal_error, context::pickups) << "Invalid healthType '" << type << "' in HealthPickup." << endl;
352    }
353
354}
Note: See TracBrowser for help on using the repository browser.