Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/dockingsystem2/src/modules/pickup/items/ShieldPickup.cc @ 8953

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

Resolving some TODO's.

  • Property svn:eol-style set to native
File size: 7.8 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 *      Eric Beier
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file ShieldPickup.cc
31    @brief Implementation of the ShieldPickup class.
32*/
33
34#include "ShieldPickup.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    CreateFactory(ShieldPickup);
46
47    /**
48    @brief
49        Constructor. Registers the object and initializes the member variables.
50    */
51    ShieldPickup::ShieldPickup(BaseObject* creator) : Pickup(creator)
52    {
53        RegisterObject(ShieldPickup);
54
55        this->initialize();
56    }
57
58    /**
59    @brief
60        Destructor.
61    */
62    ShieldPickup::~ShieldPickup()
63    {
64
65    }
66
67    /**
68    @brief
69        Initializes the member variables.
70    */
71    void ShieldPickup::initialize(void)
72    {
73        this->duration_ = 0.0f;
74        this->shieldAbsorption_ = 0.0f;
75        this->shieldHealth_ = 0.0f;
76
77        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
78    }
79
80    /**
81    @brief
82        Initializes the PickupIdentifier of this pickup.
83    */
84    void ShieldPickup::initializeIdentifier(void)
85    {
86        std::stringstream stream;
87        stream << this->getDuration();
88        std::string type1 = "duration";
89        std::string val1 = stream.str();
90        this->pickupIdentifier_->addParameter(type1, val1);
91
92        stream.clear();
93        stream << this->getShieldHealth();
94        std::string type2 = "ShieldHealth";
95        std::string val2 = stream.str();
96        this->pickupIdentifier_->addParameter(type2, val2);
97
98        stream.clear();
99        stream << this->getShieldAbsorption();
100        std::string type3 = "ShieldAbsorption";
101        std::string val3 = stream.str();
102        this->pickupIdentifier_->addParameter(type3, val3);
103
104    }
105
106    /**
107    @brief
108        Method for creating a ShieldPickup object through XML.
109    */
110    void ShieldPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
111    {
112        SUPER(ShieldPickup, XMLPort, xmlelement, mode);
113
114        XMLPortParam(ShieldPickup, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode);
115        XMLPortParam(ShieldPickup, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode);
116        XMLPortParam(ShieldPickup, "duration", setDuration, getDuration, xmlelement, mode);
117
118        this->initializeIdentifier();
119    }
120
121    /**
122    @brief
123        Is called when the pickup has transited from used to unused or the other way around.
124    */
125    void ShieldPickup::changedUsed(void)
126    {
127        SUPER(ShieldPickup, changedUsed);
128
129        Pawn* pawn = this->carrierToPawnHelper();
130        if(pawn == NULL)
131            this->Pickupable::destroy();
132
133        // If the pickup has transited to used.
134        if(this->isUsed())
135        {
136            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
137            if(this->isContinuous())
138            {
139                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
140                {
141                    this->durationTimer_.unpauseTimer();
142                }
143                else
144                {
145                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&ShieldPickup::pickupTimerCallback, this)));
146                }
147            }
148            pawn->setShieldAbsorption(this->getShieldAbsorption());
149            pawn->setShieldHealth(this->getShieldHealth());
150        }
151        else
152        {
153            pawn->setShieldAbsorption(0.0f);
154            this->setShieldHealth(pawn->getShieldHealth());
155            pawn->setShieldHealth(0.0f);
156
157            // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded.
158            if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
159            {
160                this->Pickupable::destroy();
161            }
162            // We pause the Timer if the pickup is continuous and the duration is not yet exceeded,
163            else if(this->isContinuous() && this->durationTimer_.isActive())
164            {
165                this->durationTimer_.pauseTimer();
166            }
167        }
168    }
169
170    /**
171    @brief
172    Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
173    @return
174    A pointer to the Pawn, or NULL if the conversion failed.
175    */
176    Pawn* ShieldPickup::carrierToPawnHelper(void)
177    {
178        PickupCarrier* carrier = this->getCarrier();
179        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
180
181        if(pawn == NULL)
182        {
183            COUT(1) << "Invalid PickupCarrier in ShieldPickup." << std::endl;
184        }
185        return pawn;
186    }
187
188    /**
189    @brief
190        Creates a duplicate of the input OrxonoxClass.
191    @param item
192        A pointer to the Orxonox class.
193    */
194    void ShieldPickup::clone(OrxonoxClass*& item)
195    {
196        if(item == NULL)
197            item = new ShieldPickup(this);
198
199        SUPER(ShieldPickup, clone, item);
200
201        ShieldPickup* pickup = dynamic_cast<ShieldPickup*>(item);
202        pickup->setDuration(this->getDuration());
203        pickup->setShieldAbsorption(this->getShieldAbsorption());
204        pickup->setShieldHealth(this->getShieldHealth());
205        pickup->initializeIdentifier();
206    }
207
208    /**
209    @brief
210        Sets the duration.
211    @param duration
212        The duration in seconds.
213    */
214    void ShieldPickup::setDuration(float duration)
215    {
216        if(duration >= 0.0f)
217        {
218            this->duration_ = duration;
219        }
220        else
221        {
222            COUT(1) << "Invalid duration in ShieldPickup." << std::endl;
223            this->duration_ = 0.0f;
224        }
225    }
226
227    /**
228    @brief
229        Sets the health of the shield.
230    @param shieldHealth
231        The shieldHealth.
232    */
233    void ShieldPickup::setShieldHealth(float shieldHealth)
234    {
235        if (shieldHealth>=0)
236        {
237            this->shieldHealth_=shieldHealth;
238        }
239        else
240        {
241            COUT(1) << "Invalid Shieldhealth in ShieldPickup." << std::endl;
242            this->shieldHealth_=0;
243        }
244    }
245
246    /**
247    @brief
248        Sets the percentage the shield absorbs of the dealt damage.
249    @param shieldAbsorption
250        The shieldAbsorption. Has to be between 0 and 1.
251    */
252    void ShieldPickup::setShieldAbsorption(float shieldAbsorption)
253    {
254        if (shieldAbsorption>=0 && shieldAbsorption<=1)
255        {
256            this->shieldAbsorption_=shieldAbsorption;
257        }
258        else
259        {
260            COUT(1) << "Invalid Absorption in ShieldPickup." << std::endl;
261            this->shieldAbsorption_=0;
262        }
263    }
264
265    /**
266    @brief
267        Helper method. Is called by the Timer as soon as it expires.
268    */
269    void ShieldPickup::pickupTimerCallback(void)
270    {
271        this->setUsed(false);
272    }
273}
Note: See TracBrowser for help on using the repository browser.