Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/masterserver/src/modules/pickup/Pickup.cc @ 7571

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

Some documenting and cleaning up/re-organization in pickups module.

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