Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/Pickup.cc @ 9305

Last change on this file since 9305 was 9305, checked in by landauf, 12 years ago

simplified code a little by using MultiType instead of explicit conversion

  • 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 *      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
39#include "pickup/PickupIdentifier.h"
40
41#include "DroppedPickup.h"
42
43namespace orxonox
44{
45
46    //! Initializing the static strings.
47    /*static*/ const std::string Pickup::activationTypeImmediate_s = "immediate";
48    /*static*/ const std::string Pickup::activationTypeOnUse_s = "onUse";
49    /*static*/ const std::string Pickup::durationTypeOnce_s = "once";
50    /*static*/ const std::string Pickup::durationTypeContinuous_s = "continuous";
51
52    CreateUnloadableFactory(Pickup);
53
54    /**
55    @brief
56        Constructor. Registers and initializes the object.
57    @param creator
58        The objects creator.
59    */
60    Pickup::Pickup(BaseObject* creator) : BaseObject(creator)
61    {
62        RegisterObject(Pickup);
63
64        this->initialize();
65    }
66
67    /**
68    @brief
69        Destructor.
70    */
71    Pickup::~Pickup()
72    {
73
74    }
75
76    /**
77    @brief
78        Initializes the member variables.
79    */
80    void Pickup::initialize(void)
81    {
82        this->activationType_ = pickupActivationType::immediate;
83        this->durationType_ = pickupDurationType::once;
84    }
85
86    /**
87    @brief
88        Initializes the PickupIdentififer of this Pickup.
89    */
90    void Pickup::initializeIdentifier(void)
91    {
92        this->pickupIdentifier_->addParameter("activationType", this->getActivationType());
93        this->pickupIdentifier_->addParameter("durationType", this->getDurationType());
94    }
95
96    /**
97    @brief
98        Method for creating a Pickup object through XML.
99    */
100    void Pickup::XMLPort(Element& xmlelement, XMLPort::Mode mode)
101    {
102        SUPER(Pickup, XMLPort, xmlelement, mode);
103
104        XMLPortParam(Pickup, "activationType", setActivationType, getActivationType, xmlelement, mode);
105        XMLPortParam(Pickup, "durationType", setDurationType, getDurationType, xmlelement, mode);
106
107        this->initializeIdentifier();
108    }
109
110    /**
111    @brief
112        Get the activation type of the pickup.
113    @return
114        Returns a string containing the activation type.
115    */
116    const std::string& Pickup::getActivationType(void) const
117    {
118        switch(this->activationType_)
119        {
120            case pickupActivationType::immediate:
121                return activationTypeImmediate_s;
122            case pickupActivationType::onUse:
123                return activationTypeOnUse_s;
124            default:
125                return BLANKSTRING;
126        }
127    }
128
129    /**
130    @brief
131        Get the duration type of the pickup.
132    @return
133        Returns a string containing the duration type.
134    */
135    const std::string& Pickup::getDurationType(void) const
136    {
137        switch(this->durationType_)
138        {
139            case pickupDurationType::once:
140                return durationTypeOnce_s;
141            case pickupDurationType::continuous:
142                return durationTypeContinuous_s;
143            default:
144                return BLANKSTRING;
145        }
146    }
147
148    /**
149    @brief
150        Set the activation type of the Pickup.
151    @param type
152        The activation type of the Pickup as a string.
153    */
154    void Pickup::setActivationType(const std::string& type)
155    {
156        if(type == Pickup::activationTypeImmediate_s)
157            this->activationType_ = pickupActivationType::immediate;
158        else if(type == Pickup::activationTypeOnUse_s)
159            this->activationType_ = pickupActivationType::onUse;
160        else
161            orxout(internal_error, context::pickups) << "Invalid activationType '" << type << "' in " << this->getIdentifier()->getName() << "." << endl;
162    }
163
164    /**
165    @brief
166        Set the duration type of the Pickup.
167    @param type
168        The duration type of the Pickup as a string.
169    */
170    void Pickup::setDurationType(const std::string& type)
171    {
172        if(type == Pickup::durationTypeOnce_s)
173            this->durationType_ = pickupDurationType::once;
174        else if(type == Pickup::durationTypeContinuous_s)
175            this->durationType_ = pickupDurationType::continuous;
176        else
177            orxout(internal_error, context::pickups) << "Invalid durationType '" << type << "' in " << this->getIdentifier()->getName() << "." << endl;
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->isPickedUp() && this->isImmediate())
191            this->setUsed(true);
192    }
193
194    /**
195    @brief
196        Creates a duplicate of the OrxonoxClass.
197    @param item
198        A reference to the pointer of the item that we're duplicating.
199    */
200    void Pickup::clone(OrxonoxClass*& item)
201    {
202        if(item == NULL)
203            item = new Pickup(this);
204
205        SUPER(Pickup, clone, item);
206
207        Pickup* pickup = orxonox_cast<Pickup*>(item);
208        pickup->setActivationTypeDirect(this->getActivationTypeDirect());
209        pickup->setDurationTypeDirect(this->getDurationTypeDirect());
210
211        pickup->initializeIdentifier();
212    }
213
214    /**
215    @brief
216        Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
217        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.:
218        DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position);
219    @return
220        Returns true if a spawner was created, false if not.
221    */
222    bool Pickup::createSpawner(void)
223    {
224        new DroppedPickup(this, this, this->getCarrier());
225        return true;
226    }
227
228}
Note: See TracBrowser for help on using the repository browser.