Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/items/ShieldPickup.cc @ 7547

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

Documenting and cleanup.

  • Property svn:eol-style set to native
File size: 7.9 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        // If the pickup is not picked up nothing must be done.
130        if(!this->isPickedUp())
131            return;
132
133        Pawn* pawn = this->carrierToPawnHelper();
134        if(pawn == NULL)
135            this->Pickupable::destroy();
136
137        // If the pickup has transited to used.
138        if(this->isUsed())
139        {
140            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
141            if(this->isContinuous())
142            {
143                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
144                {
145                    this->durationTimer_.unpauseTimer();
146                }
147                else
148                {
149                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&ShieldPickup::pickupTimerCallback, this)));
150                }
151            }
152            pawn->setShieldAbsorption(this->getShieldAbsorption());
153            pawn->setShieldHealth(this->getShieldHealth());
154        }
155        else
156        {
157            pawn->setShieldAbsorption(0.0f);
158            this->setShieldHealth(pawn->getShieldHealth());
159            pawn->setShieldHealth(0.0f);
160
161            // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded.
162            if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
163            {
164                this->Pickupable::destroy();
165            }
166            // We pause the Timer if the pickup is continuous and the duration is not yet exceeded,
167            else if(this->isContinuous() && this->durationTimer_.isActive())
168            {
169                this->durationTimer_.pauseTimer();
170            }
171        }
172    }
173
174    /**
175    @brief
176    Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
177    @return
178    A pointer to the Pawn, or NULL if the conversion failed.
179    */
180    Pawn* ShieldPickup::carrierToPawnHelper(void)
181    {
182        PickupCarrier* carrier = this->getCarrier();
183        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
184
185        if(pawn == NULL)
186        {
187            COUT(1) << "Invalid PickupCarrier in ShieldPickup." << std::endl;
188        }
189        return pawn;
190    }
191
192    /**
193    @brief
194        Creates a duplicate of the input OrxonoxClass.
195    @param item
196        A pointer to the Orxonox class.
197    */
198    void ShieldPickup::clone(OrxonoxClass*& item)
199    {
200        if(item == NULL)
201            item = new ShieldPickup(this);
202
203        SUPER(ShieldPickup, clone, item);
204
205        ShieldPickup* pickup = dynamic_cast<ShieldPickup*>(item);
206        pickup->setDuration(this->getDuration());
207        pickup->setShieldAbsorption(this->getShieldAbsorption());
208        pickup->setShieldHealth(this->getShieldHealth());
209        pickup->initializeIdentifier();
210    }
211
212    /**
213    @brief
214        Sets the duration.
215    @param duration
216        The duration in seconds.
217    */
218    void ShieldPickup::setDuration(float duration)
219    {
220        if(duration >= 0.0f)
221        {
222            this->duration_ = duration;
223        }
224        else
225        {
226            COUT(1) << "Invalid duration in ShieldPickup." << std::endl;
227            this->duration_ = 0.0f;
228        }
229    }
230
231    /**
232    @brief
233        Sets the health of the shield.
234    @param shieldHealth
235        The shieldHealth.
236    */
237    void ShieldPickup::setShieldHealth(float shieldHealth)
238    {
239        if (shieldHealth>=0)
240        {
241            this->shieldHealth_=shieldHealth;
242        }
243        else
244        {
245            COUT(1) << "Invalid Shieldhealth in ShieldPickup." << std::endl;
246            this->shieldHealth_=0;
247        }
248    }
249
250    /**
251    @brief
252        Sets the percentage the shield absorbs of the dealt damage.
253    @param shieldAbsorption
254        The shieldAbsorption. Has to be between 0 and 1.
255    */
256    void ShieldPickup::setShieldAbsorption(float shieldAbsorption)
257    {
258        if (shieldAbsorption>=0 && shieldAbsorption<=1)
259        {
260            this->shieldAbsorption_=shieldAbsorption;
261        }
262        else
263        {
264            COUT(1) << "Invalid Absorption in ShieldPickup." << std::endl;
265            this->shieldAbsorption_=0;
266        }
267    }
268
269    /**
270    @brief
271        Helper method. Is called by the Timer as soon as it expires.
272    */
273    void ShieldPickup::pickupTimerCallback(void)
274    {
275        this->setUsed(false);
276    }
277}
Note: See TracBrowser for help on using the repository browser.