Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup3/src/modules/pickup/Pickup.cc @ 6475

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

Additional documentation, code niceifying and potential bug fixing. Also: Renamed DroppedItem to DroppedPickup.

File size: 7.0 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#include "Pickup.h"
30
31#include "core/CoreIncludes.h"
32#include "util/StringUtils.h"
33#include "pickup/PickupIdentifier.h"
34#include "DroppedPickup.h"
35
36namespace orxonox
37{
38   
39    /*static*/ const std::string Pickup::activationTypeImmediate_s = "immediate";
40    /*static*/ const std::string Pickup::activationTypeOnUse_s = "onUse";
41    /*static*/ const std::string Pickup::durationTypeOnce_s = "once";
42    /*static*/ const std::string Pickup::durationTypeContinuous_s = "continuous";
43   
44    //TODO: Should this be here? Does it work without?
45    CreateFactory(Pickup);
46   
47    Pickup::Pickup(BaseObject* creator) : BaseObject(creator)
48    {
49        RegisterObject(Pickup);
50       
51        this->initialize();
52    }
53   
54    Pickup::~Pickup()
55    {
56       
57    }
58   
59    /**
60    @brief
61        Initializes the member variables.
62    */
63    void Pickup::initialize(void)
64    {
65        this->activationType_ = pickupActivationType::immediate;
66        this->durationType_ = pickupDurationType::once;
67    }
68   
69    /**
70    @brief
71        Initializes the PickupIdentififer of this Pickup.
72    */
73    void Pickup::initializeIdentifier(void)
74    {
75        //TODO: Check whether this could not be done in the Constructor if Pickupable. Would be much more convenient.
76        this->pickupIdentifier_->addClass(this->getIdentifier());
77       
78        //TODO: Works?
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, changedCarrier); to their changedCarrier method.
188    */
189    void Pickup::changedCarrier(void)
190    {
191        SUPER(Pickup, changedCarrier);
192       
193        //! Sets the Pickup to used if the Pickup has activation type 'immediate' and gets picked up.
194        if(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(const Vector3& position)
231    {
232        DroppedPickup::DroppedPickup(this, this, position);
233        return true;
234    }
235   
236}
Note: See TracBrowser for help on using the repository browser.