Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/items/ShieldPickup.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: 7.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 *      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        this->pickupIdentifier_->addParameter("duration", this->getDuration());
87        this->pickupIdentifier_->addParameter("ShieldHealth", this->getShieldHealth());
88        this->pickupIdentifier_->addParameter("ShieldAbsorption", this->getShieldAbsorption());
89    }
90
91    /**
92    @brief
93        Method for creating a ShieldPickup object through XML.
94    */
95    void ShieldPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
96    {
97        SUPER(ShieldPickup, XMLPort, xmlelement, mode);
98
99        XMLPortParam(ShieldPickup, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode);
100        XMLPortParam(ShieldPickup, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode);
101        XMLPortParam(ShieldPickup, "duration", setDuration, getDuration, xmlelement, mode);
102
103        this->initializeIdentifier();
104    }
105
106    /**
107    @brief
108        Is called when the pickup has transited from used to unused or the other way around.
109    */
110    void ShieldPickup::changedUsed(void)
111    {
112        SUPER(ShieldPickup, changedUsed);
113
114        Pawn* pawn = this->carrierToPawnHelper();
115        if(pawn == NULL)
116            this->Pickupable::destroy();
117
118        // If the pickup has transited to used.
119        if(this->isUsed())
120        {
121            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
122            if(this->isContinuous())
123            {
124                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
125                {
126                    this->durationTimer_.unpauseTimer();
127                }
128                else
129                {
130                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&ShieldPickup::pickupTimerCallback, this)));
131                }
132            }
133            pawn->setShieldAbsorption(this->getShieldAbsorption());
134            pawn->setShieldHealth(this->getShieldHealth());
135        }
136        else
137        {
138            pawn->setShieldAbsorption(0.0f);
139            this->setShieldHealth(pawn->getShieldHealth());
140            pawn->setShieldHealth(0.0f);
141
142            // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded.
143            if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
144            {
145                this->Pickupable::destroy();
146            }
147            // We pause the Timer if the pickup is continuous and the duration is not yet exceeded,
148            else if(this->isContinuous() && this->durationTimer_.isActive())
149            {
150                this->durationTimer_.pauseTimer();
151            }
152        }
153    }
154
155    /**
156    @brief
157    Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
158    @return
159    A pointer to the Pawn, or NULL if the conversion failed.
160    */
161    Pawn* ShieldPickup::carrierToPawnHelper(void)
162    {
163        PickupCarrier* carrier = this->getCarrier();
164        Pawn* pawn = orxonox_cast<Pawn*>(carrier);
165
166        if(pawn == NULL)
167        {
168            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in ShieldPickup." << endl;
169        }
170        return pawn;
171    }
172
173    /**
174    @brief
175        Creates a duplicate of the input OrxonoxClass.
176    @param item
177        A pointer to the Orxonox class.
178    */
179    void ShieldPickup::clone(OrxonoxClass*& item)
180    {
181        if(item == NULL)
182            item = new ShieldPickup(this);
183
184        SUPER(ShieldPickup, clone, item);
185
186        ShieldPickup* pickup = orxonox_cast<ShieldPickup*>(item);
187        pickup->setDuration(this->getDuration());
188        pickup->setShieldAbsorption(this->getShieldAbsorption());
189        pickup->setShieldHealth(this->getShieldHealth());
190        pickup->initializeIdentifier();
191    }
192
193    /**
194    @brief
195        Sets the duration.
196    @param duration
197        The duration in seconds.
198    */
199    void ShieldPickup::setDuration(float duration)
200    {
201        if(duration >= 0.0f)
202        {
203            this->duration_ = duration;
204        }
205        else
206        {
207            orxout(internal_error, context::pickups) << "Invalid duration in ShieldPickup." << endl;
208            this->duration_ = 0.0f;
209        }
210    }
211
212    /**
213    @brief
214        Sets the health of the shield.
215    @param shieldHealth
216        The shieldHealth.
217    */
218    void ShieldPickup::setShieldHealth(float shieldHealth)
219    {
220        if (shieldHealth>=0)
221        {
222            this->shieldHealth_=shieldHealth;
223        }
224        else
225        {
226            orxout(internal_error, context::pickups) << "Invalid Shieldhealth in ShieldPickup." << endl;
227            this->shieldHealth_=0;
228        }
229    }
230
231    /**
232    @brief
233        Sets the percentage the shield absorbs of the dealt damage.
234    @param shieldAbsorption
235        The shieldAbsorption. Has to be between 0 and 1.
236    */
237    void ShieldPickup::setShieldAbsorption(float shieldAbsorption)
238    {
239        if (shieldAbsorption>=0 && shieldAbsorption<=1)
240        {
241            this->shieldAbsorption_=shieldAbsorption;
242        }
243        else
244        {
245            orxout(internal_error, context::pickups) << "Invalid Absorption in ShieldPickup." << endl;
246            this->shieldAbsorption_=0;
247        }
248    }
249
250    /**
251    @brief
252        Helper method. Is called by the Timer as soon as it expires.
253    */
254    void ShieldPickup::pickupTimerCallback(void)
255    {
256        this->setUsed(false);
257    }
258}
Note: See TracBrowser for help on using the repository browser.