Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/items/SpeedPickup.cc @ 6710

Last change on this file since 6710 was 6709, checked in by dafrick, 14 years ago

Merged ppspickups1 into trunk.

  • Property svn:eol-style set to native
File size: 6.7 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 "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "util/StringUtils.h"
39
40#include "worldentities/pawns/SpaceShip.h"
41#include "items/Engine.h"
42#include "pickup/PickupIdentifier.h"
43
44#include <sstream>
45
46
47namespace orxonox
48{
49    CreateFactory(SpeedPickup);
50
51    /**
52    @brief
53        Constructor. Registers the object and initializes the member variables.
54    */
55    SpeedPickup::SpeedPickup(BaseObject* creator) : Pickup(creator)
56    {
57        RegisterObject(SpeedPickup);
58
59        this->initialize();
60    }
61
62    /**
63    @brief
64        Destructor.
65    */
66    SpeedPickup::~SpeedPickup()
67    {
68
69    }
70
71    /**
72    @brief
73        Initializes the member variables.
74    */
75    void SpeedPickup::initialize(void)
76    {
77        this->duration_ = 0.0f;
78        this->speedAdd_ = 0.0f;
79        this->speedMultiply_ = 1.0f;
80
81        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
82    }
83
84    /**
85    @brief
86        Initializes the PickupIdentifier of this pickup.
87    */
88    void SpeedPickup::initializeIdentifier(void)
89    {
90        std::stringstream stream;
91        stream << this->getDuration();
92        std::string type1 = "duration";
93        std::string val1 = stream.str();
94        this->pickupIdentifier_->addParameter(type1, val1);
95
96        stream.clear();
97        stream << this->getSpeedAdd();
98        std::string type2 = "speedAdd";
99        std::string val2 = stream.str();
100        this->pickupIdentifier_->addParameter(type2, val2);
101
102        stream.clear();
103        stream << this->getSpeedMultiply();
104        std::string type3 = "speedMultiply";
105        std::string val3 = stream.str();
106        this->pickupIdentifier_->addParameter(type3, val3);
107    }
108
109    /**
110    @brief
111        Method for creating a SpeedPickup object through XML.
112    */
113    void SpeedPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
114    {
115        SUPER(SpeedPickup, XMLPort, xmlelement, mode);
116
117        XMLPortParam(SpeedPickup, "duration", setDuration, getDuration, xmlelement, mode);
118        XMLPortParam(SpeedPickup, "speedAdd", setSpeedAdd, getSpeedAdd, xmlelement, mode);
119        XMLPortParam(SpeedPickup, "speedMultiply", setSpeedMultiply, getSpeedMultiply, xmlelement, mode);
120
121        this->initializeIdentifier();
122    }
123
124    /**
125    @brief
126        Is called when the pickup has transited from used to unused or the other way around.
127    */
128    void SpeedPickup::changedUsed(void)
129    {
130        SUPER(SpeedPickup, changedUsed);
131
132        //! If the pickup is not picked up nothing must be done.
133        if(!this->isPickedUp())
134            return;
135
136        Engine* engine = this->carrierToEngineHelper();
137        if(engine == NULL) //!< If the PickupCarrier is no Engine, then this pickup is useless and therefore is destroyed.
138            this->destroy();
139       
140        //! If the pickup has transited to used.
141        if(this->isUsed())
142        {
143            this->startPickupTimer(this->getDuration());
144            engine->setSpeedAdd(this->getSpeedAdd());
145            engine->setSpeedMultiply(this->getSpeedMultiply());
146        }
147        else
148        {
149            engine->setSpeedAdd(0.0f);
150            engine->setSpeedMultiply(1.0f);
151           
152            if(this->isOnce())
153            {
154                this->destroy();
155            }
156        }
157    }
158
159    /**
160    @brief
161        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
162    @return
163        A pointer to the Pawn, or NULL if the conversion failed.
164    */
165    Engine* SpeedPickup::carrierToEngineHelper(void)
166    {
167        PickupCarrier* carrier = this->getCarrier();
168        SpaceShip* ship = dynamic_cast<SpaceShip*>(carrier);
169
170        if(ship == NULL)
171        {
172            COUT(1) << "Invalid PickupCarrier in SpeedPickup." << std::endl;
173        }
174        else
175        {
176            return ship->getEngine();
177        }
178
179        return 0;
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 SpeedPickup::clone(OrxonoxClass*& item)
189    {
190        if(item == NULL)
191            item = new SpeedPickup(this);
192
193        SUPER(SpeedPickup, clone, item);
194
195        SpeedPickup* pickup = dynamic_cast<SpeedPickup*>(item);
196        pickup->setDuration(this->getDuration());
197        pickup->setSpeedAdd(this->getSpeedAdd());
198        pickup->setSpeedMultiply(this->getSpeedMultiply());
199
200        pickup->initializeIdentifier();
201    }
202
203    /**
204    @brief
205        Sets the duration.
206    @param duration
207        The duration
208    */
209    void SpeedPickup::setDuration(float duration)
210    {
211        if(duration >= 0.0f)
212        {
213            this->duration_ = duration;
214        }
215        else
216        {
217            COUT(1) << "Invalid duration in SpeedPickup." << std::endl;
218            this->duration_ = 0.0f;
219        }
220    }
221
222    /**
223    @brief
224        Sets the SpeedAdd
225    @param speedAdd
226        The added Speed
227    */
228    void SpeedPickup::setSpeedAdd(float speedAdd)
229    {
230        if(speedAdd >= 0.0f)
231        {
232            this->speedAdd_ = speedAdd;
233        }
234        else
235        {
236            COUT(1) << "Invalid speedAdd in SpeedPickup." << std::endl;
237            this->speedAdd_ = 0.0f;
238        }
239    }
240
241    /**
242    @brief
243        Sets the SpeedMultiply
244    @param speedAdd
245        The multiplied Speed
246    */
247    void SpeedPickup::setSpeedMultiply(float speedMultiply)
248    {
249        if(speedMultiply != 0)
250        {
251            this->speedMultiply_ = speedMultiply;
252        }
253        else
254        {
255            COUT(1) << "Invalid speedMultiply in SpeedPickup." << std::endl;
256            this->speedMultiply_ = 1.0f;
257        }
258    }
259
260    void SpeedPickup::pickupTimerCallback(void)
261    {       
262        this->setUsed(false);
263    }
264}
Note: See TracBrowser for help on using the repository browser.