Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12398 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 6.5 KB
RevLine 
[6869]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
[7547]36#include <sstream>
[6869]37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
[7547]40#include "worldentities/pawns/Pawn.h"
[6869]41
42namespace orxonox
43{
[9667]44    RegisterClass(ShieldPickup);
[6869]45
46    /**
47    @brief
48        Constructor. Registers the object and initializes the member variables.
49    */
[9667]50    ShieldPickup::ShieldPickup(Context* context) : Pickup(context)
[6869]51    {
52        RegisterObject(ShieldPickup);
53
54        this->initialize();
55    }
56
57    /**
58    @brief
59        Destructor.
60    */
61    ShieldPickup::~ShieldPickup()
62    {
63
64    }
65
66    /**
67    @brief
68        Initializes the member variables.
69    */
70    void ShieldPickup::initialize(void)
71    {
72        this->duration_ = 0.0f;
[6884]73        this->shieldAbsorption_ = 0.0f;
74        this->shieldHealth_ = 0.0f;
[6869]75
[6887]76        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
[6869]77    }
78
79    /**
80    @brief
81        Method for creating a ShieldPickup object through XML.
82    */
83    void ShieldPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
84    {
85        SUPER(ShieldPickup, XMLPort, xmlelement, mode);
86
[6884]87        XMLPortParam(ShieldPickup, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode);
88        XMLPortParam(ShieldPickup, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode);
[7547]89        XMLPortParam(ShieldPickup, "duration", setDuration, getDuration, xmlelement, mode);
[6869]90    }
91
92    /**
93    @brief
94        Is called when the pickup has transited from used to unused or the other way around.
95    */
96    void ShieldPickup::changedUsed(void)
97    {
98        SUPER(ShieldPickup, changedUsed);
99
[6884]100        Pawn* pawn = this->carrierToPawnHelper();
[11071]101        if(pawn == nullptr)
[7162]102            this->Pickupable::destroy();
[6884]103
[7547]104        // If the pickup has transited to used.
[6884]105        if(this->isUsed())
106        {
[7547]107            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
108            if(this->isContinuous())
[6884]109            {
[7547]110                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
111                {
112                    this->durationTimer_.unpauseTimer();
113                }
114                else
115                {
116                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&ShieldPickup::pickupTimerCallback, this)));
117                }
[6884]118            }
119            pawn->setShieldAbsorption(this->getShieldAbsorption());
120            pawn->setShieldHealth(this->getShieldHealth());
121        }
122        else
123        {
124            pawn->setShieldAbsorption(0.0f);
125            this->setShieldHealth(pawn->getShieldHealth());
126            pawn->setShieldHealth(0.0f);
127
[7547]128            // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded.
129            if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
[6884]130            {
[7547]131                this->Pickupable::destroy();
[6884]132            }
[7547]133            // We pause the Timer if the pickup is continuous and the duration is not yet exceeded,
134            else if(this->isContinuous() && this->durationTimer_.isActive())
135            {
136                this->durationTimer_.pauseTimer();
137            }
[6884]138        }
[6869]139    }
140
141    /**
142    @brief
[7547]143    Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
144    @return
[11071]145    A pointer to the Pawn, or nullptr if the conversion failed.
[7547]146    */
147    Pawn* ShieldPickup::carrierToPawnHelper(void)
148    {
149        PickupCarrier* carrier = this->getCarrier();
[9348]150        Pawn* pawn = orxonox_cast<Pawn*>(carrier);
[7547]151
[11071]152        if(pawn == nullptr)
[7547]153        {
[8858]154            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in ShieldPickup." << endl;
[7547]155        }
156        return pawn;
157    }
158
159    /**
160    @brief
161        Sets the duration.
162    @param duration
163        The duration in seconds.
[6884]164    */
[7547]165    void ShieldPickup::setDuration(float duration)
[6884]166    {
[7547]167        if(duration >= 0.0f)
[6884]168        {
[7547]169            this->duration_ = duration;
[6884]170        }
171        else
172        {
[8858]173            orxout(internal_error, context::pickups) << "Invalid duration in ShieldPickup." << endl;
[7547]174            this->duration_ = 0.0f;
[6884]175        }
176    }
177
178    /**
179    @brief
[7547]180        Sets the health of the shield.
[6884]181    @param shieldHealth
[7547]182        The shieldHealth.
[6884]183    */
184    void ShieldPickup::setShieldHealth(float shieldHealth)
185    {
186        if (shieldHealth>=0)
187        {
188            this->shieldHealth_=shieldHealth;
189        }
190        else
191        {
[8858]192            orxout(internal_error, context::pickups) << "Invalid Shieldhealth in ShieldPickup." << endl;
[6884]193            this->shieldHealth_=0;
194        }
195    }
196
197    /**
198    @brief
[7547]199        Sets the percentage the shield absorbs of the dealt damage.
200    @param shieldAbsorption
201        The shieldAbsorption. Has to be between 0 and 1.
[6869]202    */
[7547]203    void ShieldPickup::setShieldAbsorption(float shieldAbsorption)
[6869]204    {
[7547]205        if (shieldAbsorption>=0 && shieldAbsorption<=1)
[6869]206        {
[7547]207            this->shieldAbsorption_=shieldAbsorption;
[6869]208        }
209        else
210        {
[8858]211            orxout(internal_error, context::pickups) << "Invalid Absorption in ShieldPickup." << endl;
[7547]212            this->shieldAbsorption_=0;
[6869]213        }
214    }
215
[7547]216    /**
217    @brief
218        Helper method. Is called by the Timer as soon as it expires.
219    */
[6869]220    void ShieldPickup::pickupTimerCallback(void)
[7127]221    {
[6869]222        this->setUsed(false);
223    }
224}
Note: See TracBrowser for help on using the repository browser.