Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/items/SpeedPickup.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.5 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 SpeedPickup.cc
31    @brief Implementation of the SpeedPickup class.
32*/
33
34#include "SpeedPickup.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(SpeedPickup);
46
47    /**
48    @brief
49        Constructor. Registers the object and initializes the member variables.
50    */
51    SpeedPickup::SpeedPickup(BaseObject* creator) : Pickup(creator)
52    {
53        RegisterObject(SpeedPickup);
54
55        this->initialize();
56    }
57
58    /**
59    @brief
60        Destructor.
61    */
62    SpeedPickup::~SpeedPickup()
63    {
64
65    }
66
67    /**
68    @brief
69        Initializes the member variables.
70    */
71    void SpeedPickup::initialize(void)
72    {
73        this->duration_ = 0.0f;
74        this->speedAdd_ = 0.0f;
75        this->speedMultiply_ = 1.0f;
76
77        this->addTarget(ClassIdentifier<SpaceShip>::getIdentifier());
78    }
79
80    /**
81    @brief
82        Initializes the PickupIdentifier of this pickup.
83    */
84    void SpeedPickup::initializeIdentifier(void)
85    {
86        this->pickupIdentifier_->addParameter("duration", this->getDuration());
87        this->pickupIdentifier_->addParameter("speedAdd", this->getSpeedAdd());
88        this->pickupIdentifier_->addParameter("speedMultiply", this->getSpeedMultiply());
89    }
90
91    /**
92    @brief
93        Method for creating a SpeedPickup object through XML.
94    */
95    void SpeedPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
96    {
97        SUPER(SpeedPickup, XMLPort, xmlelement, mode);
98
99        XMLPortParam(SpeedPickup, "duration", setDuration, getDuration, xmlelement, mode);
100        XMLPortParam(SpeedPickup, "speedAdd", setSpeedAdd, getSpeedAdd, xmlelement, mode);
101        XMLPortParam(SpeedPickup, "speedMultiply", setSpeedMultiply, getSpeedMultiply, xmlelement, mode);
102
103        this->initializeIdentifier();
104    }
105
106    /**
107    @brief
108        Is called when the pickup has transited from used to unused or the other way around.
109    */
110    void SpeedPickup::changedUsed(void)
111    {
112        SUPER(SpeedPickup, changedUsed);
113
114        SpaceShip* ship = this->carrierToSpaceShipHelper();
115        if(ship == NULL) // If the PickupCarrier is no SpaceShip, then this pickup is useless and therefore is destroyed.
116            this->Pickupable::destroy();
117
118        // If the pickup has transited to used.
119        if(this->isUsed())
120        {
121            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
122            if(this->isContinuous())
123            {
124                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
125                {
126                    this->durationTimer_.unpauseTimer();
127                }
128                else
129                {
130                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&SpeedPickup::pickupTimerCallback, this)));
131                }
132            }
133
134            ship->addSpeed(this->getSpeedAdd());
135            ship->addSpeedFactor(this->getSpeedMultiply());
136        }
137        else
138        {
139            ship->addSpeed(-this->getSpeedAdd());
140            ship->addSpeedFactor(1.0f/this->getSpeedMultiply());
141
142            // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded.
143            if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
144            {
145                this->Pickupable::destroy();
146            }
147            // We pause the Timer if the pickup is continuous and the duration is not yet exceeded,
148            else if(this->isContinuous() && this->durationTimer_.isActive())
149            {
150                this->durationTimer_.pauseTimer();
151            }
152        }
153    }
154
155    /**
156    @brief
157        Helper to transform the PickupCarrier to a SpaceShip, and throw an error message if the conversion fails.
158    @return
159        A pointer to the SpaceShip, or NULL if the conversion failed.
160    */
161    SpaceShip* SpeedPickup::carrierToSpaceShipHelper(void)
162    {
163        PickupCarrier* carrier = this->getCarrier();
164        SpaceShip* ship = orxonox_cast<SpaceShip*>(carrier);
165
166        if(ship == NULL)
167        {
168            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in SpeedPickup." << endl;
169        }
170
171        return ship;
172    }
173
174    /**
175    @brief
176        Creates a duplicate of the input OrxonoxClass.
177    @param item
178        A pointer to the Orxonox class.
179    */
180    void SpeedPickup::clone(OrxonoxClass*& item)
181    {
182        if(item == NULL)
183            item = new SpeedPickup(this);
184
185        SUPER(SpeedPickup, clone, item);
186
187        SpeedPickup* pickup = orxonox_cast<SpeedPickup*>(item);
188        pickup->setDuration(this->getDuration());
189        pickup->setSpeedAdd(this->getSpeedAdd());
190        pickup->setSpeedMultiply(this->getSpeedMultiply());
191
192        pickup->initializeIdentifier();
193    }
194
195    /**
196    @brief
197        Sets the duration for which the SpeedPickup stays active.
198    @param duration
199        The duration in seconds.
200    */
201    void SpeedPickup::setDuration(float duration)
202    {
203        if(duration >= 0.0f)
204        {
205            this->duration_ = duration;
206        }
207        else
208        {
209            orxout(internal_error, context::pickups) << "Invalid duration in SpeedPickup." << endl;
210            this->duration_ = 0.0f;
211        }
212    }
213
214    /**
215    @brief
216        Sets the speedAdd, the value that is added to the speed of the Pawn.
217    @param speedAdd
218        The added speed.
219    */
220    void SpeedPickup::setSpeedAdd(float speedAdd)
221    {
222        if(speedAdd >= 0.0f)
223        {
224            this->speedAdd_ = speedAdd;
225        }
226        else
227        {
228            orxout(internal_error, context::pickups) << "Invalid speedAdd in SpeedPickup." << endl;
229            this->speedAdd_ = 0.0f;
230        }
231    }
232
233    /**
234    @brief
235        Sets the speedMultiply, the factor by which the speed of the Pawn is multiplied.
236    @param speedMultiply
237        The factor by which the speed is mutiplied.
238    */
239    void SpeedPickup::setSpeedMultiply(float speedMultiply)
240    {
241        if(speedMultiply != 0)
242        {
243            this->speedMultiply_ = speedMultiply;
244        }
245        else
246        {
247            orxout(internal_error, context::pickups) << "Invalid speedMultiply in SpeedPickup." << endl;
248            this->speedMultiply_ = 1.0f;
249        }
250    }
251
252    void SpeedPickup::pickupTimerCallback(void)
253    {
254        this->setUsed(false);
255    }
256}
Note: See TracBrowser for help on using the repository browser.