Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/items/DamageBoostPickup.cc @ 11904

Last change on this file since 11904 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.0 KB
RevLine 
[9119]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:
[9155]23 *      Kevin Lengauer
[9119]24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
[9155]30    @file DamageBoostPickup.cc
31    @brief Implementation of the DamageBoostPickup class.
[9119]32*/
33
34#include "DamageBoostPickup.h"
35
36#include <sstream>
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
40#include "worldentities/pawns/SpaceShip.h"
41
42namespace orxonox
43{
[9667]44    RegisterClass(DamageBoostPickup);
[9119]45
46    /**
47    @brief
48        Constructor. Registers the object and initializes the member variables.
49    */
[9667]50    DamageBoostPickup::DamageBoostPickup(Context* context) : Pickup(context)
[9119]51    {
52        RegisterObject(DamageBoostPickup);
53
54        this->initialize();
55    }
56
57    /**
58    @brief
59        Destructor.
60    */
61    DamageBoostPickup::~DamageBoostPickup()
62    {
63    }
64
65    /**
66    @brief
67        Initializes the member variables.
68    */
69    void DamageBoostPickup::initialize(void)
70    {
[9272]71        this->duration_ = 0.0f;
72        this->damageMultiplier_ = 1.0f; //The default damage multiplier.
[9119]73        //Defines who is allowed to pick up the pickup.
74        this->addTarget(ClassIdentifier<SpaceShip>::getIdentifier());
75    }
76
77    /**
78    @brief
79        Method for creating a DamageBoostPickup object through XML.
80    */
81    void DamageBoostPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
82    {
83        SUPER(DamageBoostPickup, XMLPort, xmlelement, mode);
84
85        XMLPortParam(DamageBoostPickup, "duration", setDuration, getDuration, xmlelement, mode);
[9155]86        XMLPortParam(DamageBoostPickup, "damageMultiplier", setDamageMultiplier, getDamageMultiplier, xmlelement, mode);
[9119]87    }
88
[9155]89    // Work in Progress setDamage Function
90    void DamageBoostPickup::setDamageMultiplier(float damageMultiplier)
91    {
92        if(damageMultiplier >= 1.0f)
93        {
94            this->damageMultiplier_ = damageMultiplier;
95        }
96    }
97
98
[9119]99    /**
100    @brief
[9184]101        Is called when the pickup has transisted from used to unused or the other way around.
[9119]102    */
103    void DamageBoostPickup::changedUsed(void)
104    {
105        SUPER(DamageBoostPickup, changedUsed);
106
107        SpaceShip* ship = this->carrierToSpaceShipHelper();
[11071]108        if(ship == nullptr) // If the PickupCarrier is no SpaceShip, then this pickup is useless and therefore is destroyed.
[9272]109            this->Pickupable::destroy();
[9119]110
111
112        // If the pickup has transited to used.
113        if(this->isUsed())
114        {
115            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
116            if(this->isContinuous())
117            {
118                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
119                {
120                    this->durationTimer_.unpauseTimer();
121                }
122                else
123                {
124                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&DamageBoostPickup::pickupTimerCallback, this)));
125                }
126            }
127
[9155]128            // Saves the old default Damage that is needed to restore the original damage
[9184]129            this->olddamageMultiplier_ = 1.0f;
130            // Sets the new Damage with the damage multiplier
131            ship->setDamageMultiplier(this->getDamageMultiplier());
[9119]132        }
133        else
134        {
135
136            // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded.
137            if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
138            {
139                this->Pickupable::destroy();
140                ship->setDamageMultiplier ( this->olddamageMultiplier_);
141            }
142            // We pause the Timer if the pickup is continuous and the duration is not yet exceeded,
143            else if(this->isContinuous() && this->durationTimer_.isActive())
144            {
145                this->durationTimer_.pauseTimer();
146            }
147        }
148    }
149
150    /**
151    @brief
152        Helper to transform the PickupCarrier to a SpaceShip, and throw an error message if the conversion fails.
153    @return
[11071]154        A pointer to the SpaceShip, or nullptr if the conversion failed.
[9119]155    */
156    SpaceShip* DamageBoostPickup::carrierToSpaceShipHelper(void)
157    {
158        PickupCarrier* carrier = this->getCarrier();
[9279]159        SpaceShip* ship = orxonox_cast<SpaceShip*>(carrier);
[9119]160
[11071]161        if(ship == nullptr)
[9119]162        {
163            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in DamageBoostPickup." << endl;
164        }
165
166        return ship;
167    }
168
169    /**
170    @brief
171        Sets the duration for which the DamageBoostPickup stays active.
172    @param duration
173        The duration in seconds.
174    */
175    void DamageBoostPickup::setDuration(float duration)
176    {
177        if(duration >= 0.0f)
178        {
179            this->duration_ = duration;
180        }
181        else
182        {
[9155]183            orxout(internal_error, context::pickups) << "Invalid duration in DamagePickup." << endl;
[9119]184            this->duration_ = 0.0f;
185        }
186    }
187
[9155]188    /**
189    @brief
190        Helper method. Is called by the Timer as soon as it expires.
[9119]191    */
192    void DamageBoostPickup::pickupTimerCallback(void)
193    {
194        this->setUsed(false);
195    }
196
197}
198
Note: See TracBrowser for help on using the repository browser.