Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial2/src/modules/pickup/Pickup.cc @ 11479

Last change on this file since 11479 was 8864, checked in by dafrick, 13 years ago

Improving output.

  • 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(type == Pickup::activationTypeImmediate_s)
162            this->activationType_ = pickupActivationType::immediate;
163        else if(type == Pickup::activationTypeOnUse_s)
164            this->activationType_ = pickupActivationType::onUse;
165        else
166            orxout(internal_error, context::pickups) << "Invalid activationType '" << type << "' in " << this->getIdentifier()->getName() << "." << endl;
167    }
168
169    /**
170    @brief
171        Set the duration type of the Pickup.
172    @param type
173        The duration type of the Pickup as a string.
174    */
175    void Pickup::setDurationType(const std::string& type)
176    {
177        if(type == Pickup::durationTypeOnce_s)
178            this->durationType_ = pickupDurationType::once;
179        else if(type == Pickup::durationTypeContinuous_s)
180            this->durationType_ = pickupDurationType::continuous;
181        else
182            orxout(internal_error, context::pickups) << "Invalid durationType '" << type << "' in " << this->getIdentifier()->getName() << "." << endl;
183    }
184
185    /**
186    @brief
187        Should be called when the pickup has transited from picked up to dropped or the other way around.
188        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedPickedUp); to their changedPickedUp method.
189    */
190    void Pickup::changedPickedUp(void)
191    {
192        SUPER(Pickup, changedPickedUp);
193
194        // Sets the Pickup to used if the Pickup has activation type 'immediate' and gets picked up.
195        if(this->isPickedUp() && this->isImmediate())
196            this->setUsed(true);
197    }
198
199    /**
200    @brief
201        Creates a duplicate of the OrxonoxClass.
202    @param item
203        A reference to the pointer of the item that we're duplicating.
204    */
205    void Pickup::clone(OrxonoxClass*& item)
206    {
207        if(item == NULL)
208            item = new Pickup(this);
209
210        SUPER(Pickup, clone, item);
211
212        Pickup* pickup = dynamic_cast<Pickup*>(item);
213        pickup->setActivationTypeDirect(this->getActivationTypeDirect());
214        pickup->setDurationTypeDirect(this->getDurationTypeDirect());
215
216        pickup->initializeIdentifier();
217    }
218
219    /**
220    @brief
221        Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
222        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.:
223        DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position);
224    @return
225        Returns true if a spawner was created, false if not.
226    */
227    bool Pickup::createSpawner(void)
228    {
229        new DroppedPickup(this, this, this->getCarrier());
230        return true;
231    }
232
233}
Note: See TracBrowser for help on using the repository browser.