Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/Pickup.cc @ 6937

Last change on this file since 6937 was 6709, checked in by dafrick, 15 years ago

Merged ppspickups1 into trunk.

File size: 7.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 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27*/
28
29/**
30    @file Pickup.cc
31    @brief Implementation of the Pickup class.
32*/
33
34#include "Pickup.h"
35
36#include "core/CoreIncludes.h"
37#include "util/StringUtils.h"
38#include "pickup/PickupIdentifier.h"
39#include "DroppedPickup.h"
40
41#include "tools/Timer.h"
42
43namespace orxonox
44{
45
46    /*static*/ const std::string Pickup::activationTypeImmediate_s = "immediate";
47    /*static*/ const std::string Pickup::activationTypeOnUse_s = "onUse";
48    /*static*/ const std::string Pickup::durationTypeOnce_s = "once";
49    /*static*/ const std::string Pickup::durationTypeContinuous_s = "continuous";
50
51    Pickup::Pickup(BaseObject* creator) : BaseObject(creator)
52    {
53        RegisterObject(Pickup);
54
55        this->initialize();
56    }
57
58    Pickup::~Pickup()
59    {
60
61    }
62
63    /**
64    @brief
65        Initializes the member variables.
66    */
67    void Pickup::initialize(void)
68    {
69        this->activationType_ = pickupActivationType::immediate;
70        this->durationType_ = pickupDurationType::once;
71    }
72
73    /**
74    @brief
75        Initializes the PickupIdentififer of this Pickup.
76    */
77    void Pickup::initializeIdentifier(void)
78    {
79        std::string val1 = this->getActivationType();
80        std::string type1 = "activationType";
81        this->pickupIdentifier_->addParameter(type1, val1);
82
83        std::string val2 = this->getDurationType();
84        std::string type2 = "durationType";
85        this->pickupIdentifier_->addParameter(type2, val2);
86    }
87
88    /**
89    @brief
90        Method for creating a Pickup object through XML.
91    */
92    void Pickup::XMLPort(Element& xmlelement, XMLPort::Mode mode)
93    {
94        SUPER(Pickup, XMLPort, xmlelement, mode);
95
96        XMLPortParam(Pickup, "activationType", setActivationType, getActivationType, xmlelement, mode);
97        XMLPortParam(Pickup, "durationType", setDurationType, getDurationType, xmlelement, mode);
98
99        this->initializeIdentifier();
100    }
101
102    /**
103    @brief
104        Get the activation type of the pickup.
105    @return
106        Returns a string containing the activation type.
107    */
108    const std::string& Pickup::getActivationType(void)
109    {
110        switch(this->activationType_)
111        {
112            case pickupActivationType::immediate:
113                return activationTypeImmediate_s;
114            case pickupActivationType::onUse:
115                return activationTypeOnUse_s;
116            default:
117                return BLANKSTRING;
118        }
119    }
120
121    /**
122    @brief
123        Get the duration type of the pickup.
124    @return
125        Returns a string containing the duration type.
126    */
127    const std::string& Pickup::getDurationType(void)
128    {
129        switch(this->durationType_)
130        {
131            case pickupDurationType::once:
132                return durationTypeOnce_s;
133            case pickupDurationType::continuous:
134                return durationTypeContinuous_s;
135            default:
136                return BLANKSTRING;
137        }
138    }
139
140    /**
141    @brief
142        Set the activation type of the Pickup.
143    @param type
144        The activation type of the Pickup as a string.
145    */
146    void Pickup::setActivationType(const std::string& type)
147    {
148        if(type == activationTypeImmediate_s)
149        {
150            this->activationType_ = pickupActivationType::immediate;
151        }
152        else if(type == activationTypeOnUse_s)
153        {
154            this->activationType_ = pickupActivationType::onUse;
155        }
156        else
157        {
158            COUT(1) << "Invalid activationType in pickup." << std::endl;
159        }
160    }
161
162    /**
163    @brief
164        Set the duration type of the Pickup.
165    @param type
166        The duration type of the Pickup as a string.
167    */
168    void Pickup::setDurationType(const std::string& type)
169    {
170        if(type == durationTypeOnce_s)
171        {
172            this->durationType_ = pickupDurationType::once;
173        }
174        else if(type == durationTypeContinuous_s)
175        {
176            this->durationType_ = pickupDurationType::continuous;
177        }
178        else
179        {
180            COUT(1) << "Invalid durationType in pickup." << std::endl;
181        }
182    }
183
184    /**
185    @brief
186        Should be called when the pickup has transited from picked up to dropped or the other way around.
187        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedPickedUp); to their changedPickedUp method.
188    */
189    void Pickup::changedPickedUp(void)
190    {
191        SUPER(Pickup, changedPickedUp);
192
193        //! Sets the Pickup to used if the Pickup has activation type 'immediate' and gets picked up.
194        if(this->getCarrier() != NULL && this->isPickedUp() && this->isImmediate())
195        {
196            this->setUsed(true);
197        }
198    }
199
200    /**
201    @brief
202        Creates a duplicate of the Pickup.
203    @return
204        Returns the clone of this pickup as a pointer to a Pickupable.
205    */
206    void Pickup::clone(OrxonoxClass*& item)
207    {
208        if(item == NULL)
209            item = new Pickup(this);
210
211        SUPER(Pickup, clone, item);
212
213        Pickup* pickup = dynamic_cast<Pickup*>(item);
214        pickup->setActivationTypeDirect(this->getActivationTypeDirect());
215        pickup->setDurationTypeDirect(this->getDurationTypeDirect());
216
217        pickup->initializeIdentifier();
218    }
219
220    /**
221    @brief
222        Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
223        This method must be implemented by any class directly inheriting from Pickupable. It is most easily done by just creating a new DroppedPickup, e.g.:
224        DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position);
225    @param position
226        The position at which the PickupSpawner should be placed.
227    @return
228        Returns true if a spawner was created, false if not.
229    */
230    bool Pickup::createSpawner(void)
231    {
232        new DroppedPickup(this, this, this->getCarrier());
233        return true;
234    }
235
236    /**
237    @brief
238        Starts the pickup duration timer.
239        After the specified durationTime has expired the function pickupTimerCallback is called.
240        pickupTimerCallback can be overloaded and thus the desired functionality can be implemented.
241    @param durationTime
242        The duration after which the expires and the callback function is called.
243    @return
244        Returns true if the pickup duration timer was started successfully, false if not.
245    */
246    bool Pickup::startPickupTimer(float durationTime)
247    {
248        if (durationTime<=0)
249        {
250            COUT(1) << "Invalid durationTime in pickup." << std::endl;
251            return false;
252        }
253        if (this->durationTimer_.isActive()) //!< Check if Timer is already running
254        {
255            COUT(1) << "Pickup durationTimer already in use." << std::endl;
256            return false;
257        }
258        this->durationTimer_.setTimer(durationTime, false, createExecutor(createFunctor(&Pickup::pickupTimerCallback, this)));
259        return true;
260    }
261}
Note: See TracBrowser for help on using the repository browser.