Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SpaceshipNavigation2/src/modules/pickup/items/MetaPickup.cc @ 10560

Last change on this file since 10560 was 9348, checked in by landauf, 12 years ago

merged branch presentation2012merge back to trunk

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