Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/modules/pickup/items/ShieldPickup.cc @ 7687

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

Removed Timer from Pickup, as this is no his core functionality.

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