Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/items/MetaPickup.cc @ 9295

Last change on this file since 9295 was 9279, checked in by landauf, 12 years ago

use orxonox_cast instead of dynamic_cast wherever possible

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