Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/items/MetaPickup.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: 7.3 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 MetaPickup.cc
31    @brief Implementation of the MetaPickup class.
32*/
33
34#include "core/CoreIncludes.h"
35#include "core/XMLPort.h"
36
37#include "interfaces/PickupCarrier.h"
38#include "pickup/PickupIdentifier.h"
39#include "worldentities/pawns/Pawn.h"
40
41#include "MetaPickup.h"
42
43namespace orxonox {
44
45    CreateFactory(MetaPickup);
46
47    //! Setting the static variables to their values.
48    /*static*/ const std::string MetaPickup::metaTypeNone_s = "none";
49    /*static*/ const std::string MetaPickup::metaTypeUse_s = "use";
50    /*static*/ const std::string MetaPickup::metaTypeDrop_s = "drop";
51    /*static*/ const std::string MetaPickup::metaTypeDestroy_s = "destroy";
52    /*static*/ const std::string MetaPickup::metaTypeDestroyCarrier_s = "destroyCarrier";
53
54    /**
55    @brief
56        Constructor. Registers and initializes the object.
57    */
58    MetaPickup::MetaPickup(BaseObject* creator) : Pickup(creator)
59    {
60        RegisterObject(MetaPickup);
61
62        this->initialize();
63    }
64
65    /**
66    @brief
67        Destructor.
68    */
69    MetaPickup::~MetaPickup()
70    {
71
72    }
73
74    /**
75    @brief
76        Initializes the object.
77    */
78    void MetaPickup::initialize(void)
79    {
80        this->addTarget(ClassIdentifier<PickupCarrier>::getIdentifier());
81
82        this->setDurationTypeDirect(pickupDurationType::once);
83        this->metaType_ = pickupMetaType::none;
84    }
85
86    /**
87    @brief
88        Helper method to initialize the PickupIdentifier.
89    */
90    void MetaPickup::initializeIdentifier(void)
91    {
92        this->pickupIdentifier_->addParameter("metaType", this->getMetaType());
93    }
94
95    /**
96    @brief
97        Method for creating a MetaPickup object through XML.
98    */
99    void MetaPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
100    {
101        SUPER(MetaPickup, XMLPort, xmlelement, mode);
102
103        XMLPortParam(MetaPickup, "metaType", setMetaType, getMetaType, xmlelement, mode);
104
105        this->initializeIdentifier();
106    }
107
108    /**
109    @brief
110        Is called when the pickup has transited from used to unused or the other way around.
111        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedUsed); to their changdeUsed method.
112    */
113    void MetaPickup::changedUsed(void)
114    {
115        SUPER(MetaPickup, changedUsed);
116
117        // If the MetaPickup transited to used, and the metaType is not none.
118        if(this->isUsed() && this->metaType_ != pickupMetaType::none)
119        {
120            PickupCarrier* carrier = this->getCarrier();
121            if(this->getMetaTypeDirect() != pickupMetaType::none && carrier != NULL)
122            {
123                // If the metaType is destroyCarrier, then the PickupCarrier is destroyed.
124                if(this->getMetaTypeDirect() == pickupMetaType::destroyCarrier)
125                {
126                    Pawn* pawn = orxonox_cast<Pawn*>(carrier);
127                    pawn->kill();
128                    return;
129                }
130                std::set<Pickupable*> pickups = carrier->getPickups();
131                // Iterate over all Pickupables of the PickupCarrier.
132                for(std::set<Pickupable*>::iterator it = pickups.begin(); it != pickups.end(); it++)
133                {
134                    Pickupable* pickup = (*it);
135                    if(pickup == NULL || pickup == this)
136                        continue;
137
138                    // If the metaType is use, then the Pickupable is set to used.
139                    if(this->getMetaTypeDirect() == pickupMetaType::use && !pickup->isUsed())
140                    {
141                        pickup->setUsed(true);
142                    }
143                    // If the metaType is drop, then the Pickupable is dropped.
144                    else if(this->getMetaTypeDirect() == pickupMetaType::drop)
145                    {
146                        pickup->drop();
147                    }
148                    // If the metaType is destroy, then the Pickupable is destroyed.
149                    else if(this->getMetaTypeDirect() == pickupMetaType::destroy)
150                    {
151                        pickup->Pickupable::destroy();
152                    }
153                }
154            }
155            this->Pickupable::destroy();
156        }
157    }
158
159    /**
160    @brief
161        Creates a duplicate of the input OrxonoxClass.
162    @param item
163        A pointer to the Orxonox class.
164    */
165    void MetaPickup::clone(OrxonoxClass*& item)
166    {
167        if(item == NULL)
168            item = new MetaPickup(this);
169
170        SUPER(MetaPickup, clone, item);
171
172        MetaPickup* pickup = orxonox_cast<MetaPickup*>(item);
173        pickup->setMetaTypeDirect(this->getMetaTypeDirect());
174
175        pickup->initializeIdentifier();
176    }
177
178    /**
179    @brief
180        Get the meta type of this MetaPickup.
181    @return
182        Returns a string with the meta type of the MetaPickup.
183    */
184    const std::string& MetaPickup::getMetaType(void) const
185    {
186        switch(this->getMetaTypeDirect())
187        {
188            case pickupMetaType::none:
189                return MetaPickup::metaTypeNone_s;
190            case pickupMetaType::use:
191                return MetaPickup::metaTypeUse_s;
192            case pickupMetaType::drop:
193                return MetaPickup::metaTypeDrop_s;
194            case pickupMetaType::destroy:
195                return MetaPickup::metaTypeDestroy_s;
196            case pickupMetaType::destroyCarrier:
197                return MetaPickup::metaTypeDestroyCarrier_s;
198            default:
199                return BLANKSTRING;
200        }
201    }
202
203    /**
204    @brief
205        Set the meta type of this MetaPickup.
206    @param type
207        A string with the type to be set.
208    */
209    void MetaPickup::setMetaType(const std::string& type)
210    {
211        if(type == MetaPickup::metaTypeNone_s)
212        {
213            this->setMetaTypeDirect(pickupMetaType::none);
214        }
215        else if(type == MetaPickup::metaTypeUse_s)
216        {
217            this->setMetaTypeDirect(pickupMetaType::use);
218        }
219        else if(type == MetaPickup::metaTypeDrop_s)
220        {
221            this->setMetaTypeDirect(pickupMetaType::drop);
222        }
223        else if(type == MetaPickup::metaTypeDestroy_s)
224        {
225            this->setMetaTypeDirect(pickupMetaType::destroy);
226        }
227        else if(type == MetaPickup::metaTypeDestroyCarrier_s)
228        {
229            this->setMetaTypeDirect(pickupMetaType::destroyCarrier);
230        }
231        else
232            orxout(internal_warning, context::pickups) << "Invalid metaType '" << type << "' in MetaPickup." << endl;
233    }
234
235}
Note: See TracBrowser for help on using the repository browser.