Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/dockingsystem2/src/modules/pickup/Pickup.cc @ 8474

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

Documenting and cleanup.

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