Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/items/DamageBoostPickup.cc @ 9279

Last change on this file since 9279 was 9279, checked in by landauf, 12 years ago

use orxonox_cast instead of dynamic_cast wherever possible

  • Property svn:eol-style set to native
File size: 7.2 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 *      Kevin Lengauer
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file DamageBoostPickup.cc
31    @brief Implementation of the DamageBoostPickup class.
32*/
33
34#include "DamageBoostPickup.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/SpaceShip.h"
42
43namespace orxonox
44{
45    CreateFactory(DamageBoostPickup);
46
47    /**
48    @brief
49        Constructor. Registers the object and initializes the member variables.
50    */
51    DamageBoostPickup::DamageBoostPickup(BaseObject* creator) : Pickup(creator)
52    {
53        RegisterObject(DamageBoostPickup);
54
55        this->initialize();
56    }
57
58    /**
59    @brief
60        Destructor.
61    */
62    DamageBoostPickup::~DamageBoostPickup()
63    {
64    }
65
66    /**
67    @brief
68        Initializes the member variables.
69    */
70    void DamageBoostPickup::initialize(void)
71    {
72        this->duration_ = 0.0f;
73        this->damageMultiplier_ = 1.0f; //The default damage multiplier.
74        //Defines who is allowed to pick up the pickup.
75        this->addTarget(ClassIdentifier<SpaceShip>::getIdentifier());
76    }
77
78    /**
79    @brief
80        Initializes the PickupIdentifier of this pickup.
81    */
82    void DamageBoostPickup::initializeIdentifier(void)
83    {
84        std::stringstream stream;
85        stream << this->getDuration();
86        std::string type1 = "duration";
87        std::string val1 = stream.str();
88        this->pickupIdentifier_->addParameter(type1, val1);
89
90        stream.clear();
91        stream << this->getDamageMultiplier();
92        std::string type2 = "damageMultiplier";
93        std::string val2 = stream.str();
94        this->pickupIdentifier_->addParameter(type2, val2);
95
96    }
97
98    /**
99    @brief
100        Method for creating a DamageBoostPickup object through XML.
101    */
102    void DamageBoostPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
103    {
104        SUPER(DamageBoostPickup, XMLPort, xmlelement, mode);
105
106        XMLPortParam(DamageBoostPickup, "duration", setDuration, getDuration, xmlelement, mode);
107        XMLPortParam(DamageBoostPickup, "damageMultiplier", setDamageMultiplier, getDamageMultiplier, xmlelement, mode);
108
109        this->initializeIdentifier();
110    }
111
112    // Work in Progress setDamage Function
113    void DamageBoostPickup::setDamageMultiplier(float damageMultiplier)
114    {
115        if(damageMultiplier >= 1.0f)
116        {
117            this->damageMultiplier_ = damageMultiplier;
118        }
119    }
120
121
122    /**
123    @brief
124        Is called when the pickup has transisted from used to unused or the other way around.
125    */
126    void DamageBoostPickup::changedUsed(void)
127    {
128        SUPER(DamageBoostPickup, changedUsed);
129
130        SpaceShip* ship = this->carrierToSpaceShipHelper();
131        if(ship == NULL) // If the PickupCarrier is no SpaceShip, then this pickup is useless and therefore is destroyed.
132            this->Pickupable::destroy();
133
134
135        // If the pickup has transited to used.
136        if(this->isUsed())
137        {
138            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
139            if(this->isContinuous())
140            {
141                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
142                {
143                    this->durationTimer_.unpauseTimer();
144                }
145                else
146                {
147                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&DamageBoostPickup::pickupTimerCallback, this)));
148                }
149            }
150
151            // Saves the old default Damage that is needed to restore the original damage
152            this->olddamageMultiplier_ = 1.0f;
153            // Sets the new Damage with the damage multiplier
154            ship->setDamageMultiplier(this->getDamageMultiplier());
155        }
156        else
157        {
158
159            // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded.
160            if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
161            {
162                this->Pickupable::destroy();
163                ship->setDamageMultiplier ( this->olddamageMultiplier_);
164            }
165            // We pause the Timer if the pickup is continuous and the duration is not yet exceeded,
166            else if(this->isContinuous() && this->durationTimer_.isActive())
167            {
168                this->durationTimer_.pauseTimer();
169            }
170        }
171    }
172
173    /**
174    @brief
175        Helper to transform the PickupCarrier to a SpaceShip, and throw an error message if the conversion fails.
176    @return
177        A pointer to the SpaceShip, or NULL if the conversion failed.
178    */
179    SpaceShip* DamageBoostPickup::carrierToSpaceShipHelper(void)
180    {
181        PickupCarrier* carrier = this->getCarrier();
182        SpaceShip* ship = orxonox_cast<SpaceShip*>(carrier);
183
184        if(ship == NULL)
185        {
186            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in DamageBoostPickup." << endl;
187        }
188
189        return ship;
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 DamageBoostPickup::clone(OrxonoxClass*& item)
199    {
200        if(item == NULL)
201            item = new DamageBoostPickup(this);
202
203        SUPER(DamageBoostPickup, clone, item);
204
205        DamageBoostPickup* pickup = orxonox_cast<DamageBoostPickup*>(item);
206        pickup->setDuration(this->getDuration());
207        pickup->setDamageMultiplier(this->getDamageMultiplier());
208        pickup->initializeIdentifier();
209    }
210
211    /**
212    @brief
213        Sets the duration for which the DamageBoostPickup stays active.
214    @param duration
215        The duration in seconds.
216    */
217    void DamageBoostPickup::setDuration(float duration)
218    {
219        if(duration >= 0.0f)
220        {
221            this->duration_ = duration;
222        }
223        else
224        {
225            orxout(internal_error, context::pickups) << "Invalid duration in DamagePickup." << endl;
226            this->duration_ = 0.0f;
227        }
228    }
229
230    /**
231    @brief
232        Helper method. Is called by the Timer as soon as it expires.
233    */
234    void DamageBoostPickup::pickupTimerCallback(void)
235    {
236        this->setUsed(false);
237    }
238
239}
240
Note: See TracBrowser for help on using the repository browser.