Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/items/DamageBoostPickup.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.0 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        this->pickupIdentifier_->addParameter("duration", this->getDuration());
85        this->pickupIdentifier_->addParameter("damageMultiplier", this->getDamageMultiplier());
86    }
87
88    /**
89    @brief
90        Method for creating a DamageBoostPickup object through XML.
91    */
92    void DamageBoostPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
93    {
94        SUPER(DamageBoostPickup, XMLPort, xmlelement, mode);
95
96        XMLPortParam(DamageBoostPickup, "duration", setDuration, getDuration, xmlelement, mode);
97        XMLPortParam(DamageBoostPickup, "damageMultiplier", setDamageMultiplier, getDamageMultiplier, xmlelement, mode);
98
99        this->initializeIdentifier();
100    }
101
102    // Work in Progress setDamage Function
103    void DamageBoostPickup::setDamageMultiplier(float damageMultiplier)
104    {
105        if(damageMultiplier >= 1.0f)
106        {
107            this->damageMultiplier_ = damageMultiplier;
108        }
109    }
110
111
112    /**
113    @brief
114        Is called when the pickup has transisted from used to unused or the other way around.
115    */
116    void DamageBoostPickup::changedUsed(void)
117    {
118        SUPER(DamageBoostPickup, changedUsed);
119
120        SpaceShip* ship = this->carrierToSpaceShipHelper();
121        if(ship == NULL) // If the PickupCarrier is no SpaceShip, then this pickup is useless and therefore is destroyed.
122            this->Pickupable::destroy();
123
124
125        // If the pickup has transited to used.
126        if(this->isUsed())
127        {
128            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
129            if(this->isContinuous())
130            {
131                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
132                {
133                    this->durationTimer_.unpauseTimer();
134                }
135                else
136                {
137                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&DamageBoostPickup::pickupTimerCallback, this)));
138                }
139            }
140
141            // Saves the old default Damage that is needed to restore the original damage
142            this->olddamageMultiplier_ = 1.0f;
143            // Sets the new Damage with the damage multiplier
144            ship->setDamageMultiplier(this->getDamageMultiplier());
145        }
146        else
147        {
148
149            // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded.
150            if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
151            {
152                this->Pickupable::destroy();
153                ship->setDamageMultiplier ( this->olddamageMultiplier_);
154            }
155            // We pause the Timer if the pickup is continuous and the duration is not yet exceeded,
156            else if(this->isContinuous() && this->durationTimer_.isActive())
157            {
158                this->durationTimer_.pauseTimer();
159            }
160        }
161    }
162
163    /**
164    @brief
165        Helper to transform the PickupCarrier to a SpaceShip, and throw an error message if the conversion fails.
166    @return
167        A pointer to the SpaceShip, or NULL if the conversion failed.
168    */
169    SpaceShip* DamageBoostPickup::carrierToSpaceShipHelper(void)
170    {
171        PickupCarrier* carrier = this->getCarrier();
172        SpaceShip* ship = orxonox_cast<SpaceShip*>(carrier);
173
174        if(ship == NULL)
175        {
176            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in DamageBoostPickup." << endl;
177        }
178
179        return ship;
180    }
181
182    /**
183    @brief
184        Creates a duplicate of the input OrxonoxClass.
185    @param item
186        A pointer to the Orxonox class.
187    */
188    void DamageBoostPickup::clone(OrxonoxClass*& item)
189    {
190        if(item == NULL)
191            item = new DamageBoostPickup(this);
192
193        SUPER(DamageBoostPickup, clone, item);
194
195        DamageBoostPickup* pickup = orxonox_cast<DamageBoostPickup*>(item);
196        pickup->setDuration(this->getDuration());
197        pickup->setDamageMultiplier(this->getDamageMultiplier());
198        pickup->initializeIdentifier();
199    }
200
201    /**
202    @brief
203        Sets the duration for which the DamageBoostPickup stays active.
204    @param duration
205        The duration in seconds.
206    */
207    void DamageBoostPickup::setDuration(float duration)
208    {
209        if(duration >= 0.0f)
210        {
211            this->duration_ = duration;
212        }
213        else
214        {
215            orxout(internal_error, context::pickups) << "Invalid duration in DamagePickup." << endl;
216            this->duration_ = 0.0f;
217        }
218    }
219
220    /**
221    @brief
222        Helper method. Is called by the Timer as soon as it expires.
223    */
224    void DamageBoostPickup::pickupTimerCallback(void)
225    {
226        this->setUsed(false);
227    }
228
229}
230
Note: See TracBrowser for help on using the repository browser.