Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged pickup branch into trunk. Yay. Persisting bugs will be fixed, very soon.

File size: 4.8 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
31    @brief Implementation of the MetaPickup class.
32*/
33
34#include "core/CoreIncludes.h"
35#include "core/XMLPort.h"
36#include "interfaces/PickupCarrier.h"
37#include "pickup/PickupIdentifier.h"
38
39#include "MetaPickup.h"
40
41namespace orxonox {
42 
43    CreateFactory(MetaPickup);
44   
45    /*static*/ const std::string MetaPickup::metaTypeNone_s = "none";
46    /*static*/ const std::string MetaPickup::metaTypeUse_s = "use";
47    /*static*/ const std::string MetaPickup::metaTypeDrop_s = "drop";
48   
49    /**
50    @brief
51        Constructor.
52    */
53    MetaPickup::MetaPickup(BaseObject* creator) : Pickup(creator)
54    {
55        RegisterObject(MetaPickup);
56       
57        this->addTarget(ClassIdentifier<PickupCarrier>::getIdentifier());
58        this->setActivationTypeDirect(pickupActivationType::immediate);
59        this->setDurationTypeDirect(pickupDurationType::once);
60        this->metaType_ = pickupMetaType::none;
61    }
62   
63    MetaPickup::~MetaPickup()
64    {
65       
66    }
67   
68    void MetaPickup::initializeIdentifier(void)
69    {
70        std::string val = this->getMetaType();
71        std::string type = "metaType";
72        this->pickupIdentifier_->addParameter(type, val);
73    }
74   
75    void MetaPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
76    {
77        SUPER(MetaPickup, XMLPort, xmlelement, mode);
78       
79        XMLPortParam(MetaPickup, "metaType", setMetaType, getMetaType, xmlelement, mode);
80       
81        this->initializeIdentifier();
82    }
83   
84    void MetaPickup::changedUsed(void)
85    {
86        SUPER(MetaPickup, changedUsed);
87       
88        if(this->isUsed())
89        {
90            PickupCarrier* carrier = this->getCarrier();
91            if(this->getMetaTypeDirect() != pickupMetaType::none && carrier != NULL)
92            {
93                std::set<Pickupable*> pickups = carrier->getPickups();
94                for(std::set<Pickupable*>::iterator it = pickups.begin(); it != pickups.end(); it++)
95                {
96                    Pickup* pickup = dynamic_cast<Pickup*>(*it);
97                    if(this->getMetaTypeDirect() == pickupMetaType::use)
98                    {
99                        if(pickup != NULL && pickup != this && pickup->isOnUse() && !pickup->isUsed())
100                        {
101                            pickup->setUsed(true);
102                        }
103                    }
104                    if(this->getMetaTypeDirect() == pickupMetaType::drop)
105                    {
106                        if(pickup != NULL && pickup != this)
107                        {
108                            carrier->drop(pickup);
109                        }
110                    }
111                }
112            }
113            this->destroy();
114        }
115    }
116   
117    const std::string& MetaPickup::getMetaType(void)
118    {
119        switch(this->getMetaTypeDirect())
120        {
121            case pickupMetaType::none:
122                return MetaPickup::metaTypeNone_s;
123            case pickupMetaType::use:
124                return MetaPickup::metaTypeUse_s;
125            case pickupMetaType::drop:
126                return MetaPickup::metaTypeDrop_s;
127            default:
128                return BLANKSTRING;
129        }
130    }
131   
132    void MetaPickup::setMetaType(const std::string& type)
133    {
134        if(type == MetaPickup::metaTypeNone_s)
135        {
136            this->setMetaTypeDirect(pickupMetaType::none);
137        }
138        else if(type == MetaPickup::metaTypeUse_s)
139        {
140            this->setMetaTypeDirect(pickupMetaType::use);
141        }
142        else if(type == MetaPickup::metaTypeDrop_s)
143        {
144            this->setMetaTypeDirect(pickupMetaType::drop);
145        }
146    }
147   
148    void MetaPickup::clone(OrxonoxClass*& item)
149    {
150        if(item == NULL)
151            item = new MetaPickup(this);
152       
153        SUPER(MetaPickup, clone, item);
154       
155        MetaPickup* pickup = dynamic_cast<MetaPickup*>(item);
156        pickup->setMetaTypeDirect(this->getMetaTypeDirect());
157       
158        pickup->initializeIdentifier();
159    }
160   
161}
Note: See TracBrowser for help on using the repository browser.