Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ppspickups1/src/modules/pickup/items/SpeedPickup.cc @ 6575

Last change on this file since 6575 was 6575, checked in by ebeier, 15 years ago

SpeedPickup Class is now "working" (able to spawn/be picked up), but speed-manipulation of pawn still missing.

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