Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Resolved segmentation fault, when destroying a PickupCompilation.

File size: 6.8 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        std::string val1 = this->getActivationType();
76        std::string type1 = "activationType";
77        this->pickupIdentifier_->addParameter(type1, val1);
78       
79        std::string val2 = this->getDurationType();
80        std::string type2 = "durationType";
81        this->pickupIdentifier_->addParameter(type2, val2);
82    }
83   
84    /**
85    @brief
86        Method for creating a Pickup object through XML.
87    */
88    void Pickup::XMLPort(Element& xmlelement, XMLPort::Mode mode)
89    {
90        SUPER(Pickup, XMLPort, xmlelement, mode);
91
92        XMLPortParam(Pickup, "activationType", setActivationType, getActivationType, xmlelement, mode);
93        XMLPortParam(Pickup, "durationType", setDurationType, getDurationType, xmlelement, mode);
94       
95        this->initializeIdentifier();
96    }
97   
98    /**
99    @brief
100        Get the activation type of the pickup.
101    @return
102        Returns a string containing the activation type.
103    */
104    const std::string& Pickup::getActivationType(void)
105    {
106        switch(this->activationType_)
107        {
108            case pickupActivationType::immediate:
109                return activationTypeImmediate_s;
110            case pickupActivationType::onUse:
111                return activationTypeOnUse_s;
112            default:
113                return BLANKSTRING;
114        }
115    }
116       
117    /**
118    @brief
119        Get the duration type of the pickup.
120    @return
121        Returns a string containing the duration type.
122    */
123    const std::string& Pickup::getDurationType(void)
124    {
125        switch(this->durationType_)
126        {
127            case pickupDurationType::once:
128                return durationTypeOnce_s;
129            case pickupDurationType::continuous:
130                return durationTypeContinuous_s;
131            default:
132                return BLANKSTRING;
133        }
134    }
135   
136    /**
137    @brief
138        Set the activation type of the Pickup.
139    @param type
140        The activation type of the Pickup as a string.
141    */
142    void Pickup::setActivationType(const std::string& type)
143    {
144        if(type == activationTypeImmediate_s)
145        {
146            this->activationType_ = pickupActivationType::immediate;
147        }
148        else if(type == activationTypeOnUse_s)
149        {
150            this->activationType_ = pickupActivationType::onUse;
151        }
152        else
153        {
154            COUT(1) << "Invalid activationType in pickup." << std::endl;
155        }
156    }
157       
158    /**
159    @brief
160        Set the duration type of the Pickup.
161    @param type
162        The duration type of the Pickup as a string.
163    */
164    void Pickup::setDurationType(const std::string& type)
165    {
166        if(type == durationTypeOnce_s)
167        {
168            this->durationType_ = pickupDurationType::once;
169        }
170        else if(type == durationTypeContinuous_s)
171        {
172            this->durationType_ = pickupDurationType::continuous;
173        }
174        else
175        {
176            COUT(1) << "Invalid durationType in pickup." << std::endl;
177        }
178    }
179   
180    /**
181    @brief
182        Should be called when the pickup has transited from picked up to dropped or the other way around.
183        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedPickedUp); to their changedPickedUp method.
184    */
185    void Pickup::changedPickedUp(void)
186    {
187        SUPER(Pickup, changedPickedUp);
188       
189        //! Sets the Pickup to used if the Pickup has activation type 'immediate' and gets picked up.
190        if(this->getCarrier() != NULL && this->isPickedUp() && this->isImmediate())
191        {
192            this->setUsed(true);
193        }
194    }
195   
196    /**
197    @brief
198        Creates a duplicate of the Pickup.
199    @return
200        Returns the clone of this pickup as a pointer to a Pickupable.
201    */
202    void Pickup::clone(OrxonoxClass*& item)
203    {
204        if(item == NULL)
205            item = new Pickup(this);
206       
207        SUPER(Pickup, clone, item);
208       
209        Pickup* pickup = dynamic_cast<Pickup*>(item);
210        pickup->setActivationTypeDirect(this->getActivationTypeDirect());
211        pickup->setDurationTypeDirect(this->getDurationTypeDirect());
212       
213        pickup->initializeIdentifier();
214    }
215       
216    /**
217    @brief
218        Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
219        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.:
220        DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position);
221    @param position
222        The position at which the PickupSpawner should be placed.
223    @return
224        Returns true if a spawner was created, false if not.
225    */
226    bool Pickup::createSpawner(const Vector3& position)
227    {
228        new DroppedPickup(this, this, position);
229        return true;
230    }
231   
232}
Note: See TracBrowser for help on using the repository browser.