Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/items/MetaPickup.cc @ 7547

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

Documenting and cleanup.

  • Property svn:eol-style set to native
File size: 7.4 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        std::string val = this->getMetaType();
93        std::string type = "metaType";
94        this->pickupIdentifier_->addParameter(type, val);
95    }
96
97    /**
98    @brief
99        Method for creating a MetaPickup object through XML.
100    */
101    void MetaPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
102    {
103        SUPER(MetaPickup, XMLPort, xmlelement, mode);
104
105        XMLPortParam(MetaPickup, "metaType", setMetaType, getMetaType, xmlelement, mode);
106
107        this->initializeIdentifier();
108    }
109
110    /**
111    @brief
112        Is called when the pickup has transited from used to unused or the other way around.
113        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedUsed); to their changdeUsed method.
114    */
115    void MetaPickup::changedUsed(void)
116    {
117        SUPER(MetaPickup, changedUsed);
118
119        // If the MetaPickup transited to used, and the metaType is not none.
120        if(this->isUsed() && this->metaType_ != pickupMetaType::none)
121        {
122            PickupCarrier* carrier = this->getCarrier();
123            if(this->getMetaTypeDirect() != pickupMetaType::none && carrier != NULL)
124            {
125                // If the metaType is destroyCarrier, then the PickupCarrier is destroyed.
126                if(this->getMetaTypeDirect() == pickupMetaType::destroyCarrier)
127                {
128                    this->Pickupable::destroy(); //TODO: Needed?
129                    Pawn* pawn = orxonox_cast<Pawn*>(carrier);
130                    pawn->kill();
131                    return;
132                }
133                std::set<Pickupable*> pickups = carrier->getPickups();
134                // Iterate over all Pickupables of the PickupCarrier.
135                for(std::set<Pickupable*>::iterator it = pickups.begin(); it != pickups.end(); it++)
136                {
137                    Pickupable* pickup = (*it);
138                    if(pickup == NULL || pickup == this)
139                        continue;
140
141                    // If the metaType is use, then the Pickupable is set to used.
142                    if(this->getMetaTypeDirect() == pickupMetaType::use && !pickup->isUsed())
143                    {
144                        pickup->setUsed(true);
145                    }
146                    // If the metaType is drop, then the Pickupable is dropped.
147                    else if(this->getMetaTypeDirect() == pickupMetaType::drop)
148                    {
149                        pickup->drop();
150                    }
151                    // If the metaType is destroy, then the Pickupable is destroyed.
152                    else if(this->getMetaTypeDirect() == pickupMetaType::destroy)
153                    {
154                        pickup->Pickupable::destroy();
155                    }
156                }
157            }
158            this->Pickupable::destroy();
159        }
160    }
161
162    /**
163    @brief
164        Creates a duplicate of the input OrxonoxClass.
165    @param item
166        A pointer to the Orxonox class.
167    */
168    void MetaPickup::clone(OrxonoxClass*& item)
169    {
170        if(item == NULL)
171            item = new MetaPickup(this);
172
173        SUPER(MetaPickup, clone, item);
174
175        MetaPickup* pickup = dynamic_cast<MetaPickup*>(item);
176        pickup->setMetaTypeDirect(this->getMetaTypeDirect());
177
178        pickup->initializeIdentifier();
179    }
180
181    /**
182    @brief
183        Get the meta type of this MetaPickup.
184    @return
185        Returns a string with the meta type of the MetaPickup.
186    */
187    const std::string& MetaPickup::getMetaType(void) const
188    {
189        switch(this->getMetaTypeDirect())
190        {
191            case pickupMetaType::none:
192                return MetaPickup::metaTypeNone_s;
193            case pickupMetaType::use:
194                return MetaPickup::metaTypeUse_s;
195            case pickupMetaType::drop:
196                return MetaPickup::metaTypeDrop_s;
197            case pickupMetaType::destroy:
198                return MetaPickup::metaTypeDestroy_s;
199            case pickupMetaType::destroyCarrier:
200                return MetaPickup::metaTypeDestroyCarrier_s;
201            default:
202                return BLANKSTRING;
203        }
204    }
205
206    /**
207    @brief
208        Set the meta type of this MetaPickup.
209    @param type
210        A string with the type to be set.
211    */
212    void MetaPickup::setMetaType(const std::string& type)
213    {
214        if(type == MetaPickup::metaTypeNone_s)
215        {
216            this->setMetaTypeDirect(pickupMetaType::none);
217        }
218        else if(type == MetaPickup::metaTypeUse_s)
219        {
220            this->setMetaTypeDirect(pickupMetaType::use);
221        }
222        else if(type == MetaPickup::metaTypeDrop_s)
223        {
224            this->setMetaTypeDirect(pickupMetaType::drop);
225        }
226        else if(type == MetaPickup::metaTypeDestroy_s)
227        {
228            this->setMetaTypeDirect(pickupMetaType::destroy);
229        }
230        else if(type == MetaPickup::metaTypeDestroyCarrier_s)
231        {
232            this->setMetaTypeDirect(pickupMetaType::destroyCarrier);
233        }
234        else
235            COUT(2) << "Invalid metaType '" << type << "' in MetaPickup." << std::endl;
236    }
237
238}
Note: See TracBrowser for help on using the repository browser.