Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Some documenting done. Added files, that I had forgotten to add. Cleaned the old pickups out.

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