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, 13 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
RevLine 
[6552]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:
[6574]23 *      Eric Beier
[6552]24 *   Co-authors:
25 *      ...
26 *
27 */
[6681]28
29/**
30    @file SpeedPickup.cc
31    @brief Implementation of the SpeedPickup class.
32*/
33
34#include "SpeedPickup.h"
35
[7547]36#include <sstream>
[6681]37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
40#include "pickup/PickupIdentifier.h"
[7547]41#include "worldentities/pawns/SpaceShip.h"
[6681]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    {
[6706]73        this->duration_ = 0.0f;
74        this->speedAdd_ = 0.0f;
75        this->speedMultiply_ = 1.0f;
[6681]76
[8727]77        this->addTarget(ClassIdentifier<SpaceShip>::getIdentifier());
[6681]78    }
79
80    /**
81    @brief
82        Initializes the PickupIdentifier of this pickup.
83    */
84    void SpeedPickup::initializeIdentifier(void)
85    {
[9305]86        this->pickupIdentifier_->addParameter("duration", this->getDuration());
87        this->pickupIdentifier_->addParameter("speedAdd", this->getSpeedAdd());
88        this->pickupIdentifier_->addParameter("speedMultiply", this->getSpeedMultiply());
[6681]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
[8727]114        SpaceShip* ship = this->carrierToSpaceShipHelper();
115        if(ship == NULL) // If the PickupCarrier is no SpaceShip, then this pickup is useless and therefore is destroyed.
[7163]116            this->Pickupable::destroy();
117
[7547]118        // If the pickup has transited to used.
[6681]119        if(this->isUsed())
120        {
[7547]121            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
122            if(this->isContinuous())
[6728]123            {
[7547]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                }
[6728]132            }
[7547]133
[8727]134            ship->addSpeed(this->getSpeedAdd());
135            ship->addSpeedFactor(this->getSpeedMultiply());
[6681]136        }
[6706]137        else
138        {
[8727]139            ship->addSpeed(-this->getSpeedAdd());
140            ship->addSpeedFactor(1.0f/this->getSpeedMultiply());
[7163]141
[7547]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()))
[6706]144            {
[7547]145                this->Pickupable::destroy();
[6706]146            }
[7547]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            }
[6706]152        }
[6681]153    }
154
155    /**
156    @brief
[8727]157        Helper to transform the PickupCarrier to a SpaceShip, and throw an error message if the conversion fails.
[6681]158    @return
[8727]159        A pointer to the SpaceShip, or NULL if the conversion failed.
[6681]160    */
[8727]161    SpaceShip* SpeedPickup::carrierToSpaceShipHelper(void)
[6681]162    {
163        PickupCarrier* carrier = this->getCarrier();
[9279]164        SpaceShip* ship = orxonox_cast<SpaceShip*>(carrier);
[6681]165
[8727]166        if(ship == NULL)
[6681]167        {
[8858]168            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in SpeedPickup." << endl;
[6681]169        }
[7163]170
[8727]171        return ship;
[6681]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
[9279]187        SpeedPickup* pickup = orxonox_cast<SpeedPickup*>(item);
[6681]188        pickup->setDuration(this->getDuration());
189        pickup->setSpeedAdd(this->getSpeedAdd());
190        pickup->setSpeedMultiply(this->getSpeedMultiply());
191
192        pickup->initializeIdentifier();
193    }
194
195    /**
196    @brief
[7547]197        Sets the duration for which the SpeedPickup stays active.
[6681]198    @param duration
[7547]199        The duration in seconds.
[6681]200    */
201    void SpeedPickup::setDuration(float duration)
202    {
203        if(duration >= 0.0f)
204        {
205            this->duration_ = duration;
206        }
207        else
208        {
[8858]209            orxout(internal_error, context::pickups) << "Invalid duration in SpeedPickup." << endl;
[6694]210            this->duration_ = 0.0f;
[6681]211        }
212    }
213
214    /**
215    @brief
[7547]216        Sets the speedAdd, the value that is added to the speed of the Pawn.
[6681]217    @param speedAdd
[7547]218        The added speed.
[6681]219    */
220    void SpeedPickup::setSpeedAdd(float speedAdd)
221    {
222        if(speedAdd >= 0.0f)
223        {
224            this->speedAdd_ = speedAdd;
225        }
226        else
227        {
[8858]228            orxout(internal_error, context::pickups) << "Invalid speedAdd in SpeedPickup." << endl;
[6694]229            this->speedAdd_ = 0.0f;
[6681]230        }
231    }
232
233    /**
234    @brief
[7547]235        Sets the speedMultiply, the factor by which the speed of the Pawn is multiplied.
[7401]236    @param speedMultiply
[7547]237        The factor by which the speed is mutiplied.
[6681]238    */
239    void SpeedPickup::setSpeedMultiply(float speedMultiply)
240    {
241        if(speedMultiply != 0)
242        {
243            this->speedMultiply_ = speedMultiply;
244        }
245        else
246        {
[8858]247            orxout(internal_error, context::pickups) << "Invalid speedMultiply in SpeedPickup." << endl;
[6694]248            this->speedMultiply_ = 1.0f;
[6681]249        }
250    }
251
[6706]252    void SpeedPickup::pickupTimerCallback(void)
[7163]253    {
[6706]254        this->setUsed(false);
[6681]255    }
256}
Note: See TracBrowser for help on using the repository browser.