Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6475 was 6475, checked in by dafrick, 15 years ago

Additional documentation, code niceifying and potential bug fixing. Also: Renamed DroppedItem to DroppedPickup.

File size: 8.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
34#include "worldentities/pawns/Pawn.h"
35#include "pickup/PickupIdentifier.h"
36
37#include <sstream>
38
39namespace orxonox
40{
41   
42    /*static*/ const std::string HealthPickup::healthTypeLimited_s = "limited";
43    /*static*/ const std::string HealthPickup::healthTypeTemporary_s = "temporary";
44    /*static*/ const std::string HealthPickup::healthTypePermanent_s = "permanent";
45    /*static*/ const std::string HealthPickup::blankString_s = "";
46   
47    CreateFactory(HealthPickup);
48   
49    HealthPickup::HealthPickup(BaseObject* creator) : Pickup(creator)
50    {
51        RegisterObject(HealthPickup);
52       
53        this->initialize();
54    }
55   
56    HealthPickup::~HealthPickup()
57    {
58       
59    }
60   
61    void HealthPickup::initialize(void)
62    {
63        RegisterObject(HealthPickup);
64       
65        this->health_ = 0;
66        this->healthSpeed_ = 0;
67        this->healthType_ = pickupHealthType::limited;
68       
69    }
70   
71    void HealthPickup::initializeIdentifier(void)
72    {
73        this->pickupIdentifier_->addClass(this->getIdentifier());
74       
75        std::stringstream stream;
76        stream << this->getHealth();
77        std::string type1 = "health";
78        std::string val1 = stream.str();
79        this->pickupIdentifier_->addParameter(type1, val1);
80       
81        //TODO: Does this work, is val valid outside the function scope?
82        std::string val2 = this->getHealthType();
83        std::string type2 = "healthType";
84        this->pickupIdentifier_->addParameter(type2, val2);
85    }
86   
87    void HealthPickup::HealthPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
88    {
89        SUPER(HealthPickup, XMLPort, xmlelement, mode);
90       
91        XMLPortParam(HealthPickup, "health", setHealth, getHealth, xmlelement, mode);
92        XMLPortParam(HealthPickup, "healthSpeed", setHealthSpeed, getHealthSpeed, xmlelement, mode);
93        XMLPortParam(HealthPickup, "healthType", setHealthType, getHealthType, xmlelement, mode);
94       
95        if(!this->isContinuous())
96            this->healthSpeed_ = 0.0;
97       
98        this->initializeIdentifier();
99    }
100   
101    void HealthPickup::setHealth(float health)
102    {
103        if(health > 0.0f)
104        {
105            this->health_ = health;
106        }
107        else
108        {
109            COUT(1) << "Invalid health in HealthPickup." << std::endl;
110        }
111    }
112   
113    void HealthPickup::setHealthSpeed(float speed)
114    {
115        if(speed >= 0)
116        {
117            this->healthSpeed_ = speed;
118        }
119        else
120        {
121            COUT(1) << "Invalid healthSpeed in HealthPickup." << std::endl; 
122        }
123    }
124   
125    void HealthPickup::setHealthType(std::string type)
126    {
127        if(type == HealthPickup::healthTypeLimited_s)
128        {
129            this->setHealthTypeDirect(pickupHealthType::limited);
130        }
131        else if(type == HealthPickup::healthTypeTemporary_s)
132        {
133            this->setHealthTypeDirect(pickupHealthType::temporary);
134        }
135        else if(type == HealthPickup::healthTypePermanent_s)
136        {
137            this->setHealthTypeDirect(pickupHealthType::permanent);
138        }
139        else
140        {
141            COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
142        }
143    }
144   
145    void HealthPickup::tick(float dt)
146    {
147        if(this->isContinuous() && this->isUsed())
148        {
149            PickupCarrier* carrier = this->getCarrier();
150            Pawn* pawn = dynamic_cast<Pawn*>(carrier);
151           
152            if(pawn == NULL)
153            {
154                COUT(1) << "Invalid PickupCarrier in HealthPickup." << std::endl;
155                return;
156            }
157           
158            float health = dt*this->getHealthSpeed();
159            if(health > this->getHealth())
160                health = this->getHealth();
161            float fullHealth = pawn->getHealth() + health;
162            this->setHealth(this->getHealth()-health);
163                   
164            switch(this->getHealthTypeDirect())
165            {
166                case pickupHealthType::permanent:
167                    if(pawn->getMaxHealth() > fullHealth)
168                        pawn->setMaxHealth(fullHealth);
169                case pickupHealthType::limited:
170                    pawn->addHealth(health);
171                    break;
172                case pickupHealthType::temporary:
173                    //TODO: How?
174                    break;
175                default:
176                    COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
177            }
178           
179            if(this->getHealth() == 0)
180            {
181                //TODO: Destroy
182            }
183        }
184    }
185   
186    const std::string& HealthPickup::getHealthType(void)
187    {
188        switch(this->getHealthTypeDirect())
189        {
190            case pickupHealthType::limited:
191                return HealthPickup::healthTypeLimited_s;
192            case pickupHealthType::temporary:
193                return HealthPickup::healthTypeTemporary_s;
194            case pickupHealthType::permanent:
195                return HealthPickup::healthTypePermanent_s;
196            default:
197                COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
198                return HealthPickup::blankString_s;
199        }
200    }
201   
202    void HealthPickup::clone(OrxonoxClass* item)
203    {
204        if(item == NULL)
205            item = new HealthPickup(this);
206       
207        SUPER(HealthPickup, clone, item);
208       
209        //TODO: No temp needed?
210        HealthPickup* pickup = dynamic_cast<HealthPickup*>(item);
211        pickup->setHealth(this->getHealth());
212        pickup->setHealthSpeed(this->getHealthSpeed());
213        pickup->setHealthTypeDirect(this->getHealthTypeDirect());
214       
215        pickup->initializeIdentifier();
216    }
217   
218    //TODO: Does this work even if Pickup doesn't implement it?
219    void HealthPickup::changedUsed(void)
220    {
221        SUPER(HealthPickup, changedUsed);
222       
223        if(this->isUsed())
224        {
225            PickupCarrier* carrier = this->getCarrier();
226            Pawn* pawn = dynamic_cast<Pawn*>(carrier);
227           
228            if(pawn == NULL)
229            {
230                COUT(1) << "Invalid PickupCarrier in HealthPickup." << std::endl;
231                return;
232            }
233           
234            if(this->isOnce())
235            {
236                float health = 0;
237                switch(this->getHealthTypeDirect())
238                {
239                    case pickupHealthType::permanent:
240                        health = pawn->getHealth()+this->getHealth();
241                        if(pawn->getMaxHealth() < health)
242                            pawn->setMaxHealth(health);
243                    case pickupHealthType::limited:
244                        pawn->addHealth(this->getHealth());
245                        break;
246                    case pickupHealthType::temporary:
247                        //TODO: How?
248                        break;
249                    default:
250                        COUT(1) << "Invalid healthType in HealthPickup." << std::endl;
251                }
252               
253                //TODO: Destroy.
254            }
255        }
256        else
257        {
258            //TODO: Destroy, but be careful to not destroy before being used.
259        }
260    }
261
262}
Note: See TracBrowser for help on using the repository browser.