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 | |
---|
43 | namespace 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 | Pawn* pawn = orxonox_cast<Pawn*>(carrier); |
---|
129 | pawn->kill(); |
---|
130 | return; |
---|
131 | } |
---|
132 | std::set<Pickupable*> pickups = carrier->getPickups(); |
---|
133 | // Iterate over all Pickupables of the PickupCarrier. |
---|
134 | for(std::set<Pickupable*>::iterator it = pickups.begin(); it != pickups.end(); it++) |
---|
135 | { |
---|
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()) |
---|
142 | { |
---|
143 | pickup->setUsed(true); |
---|
144 | } |
---|
145 | // If the metaType is drop, then the Pickupable is dropped. |
---|
146 | else if(this->getMetaTypeDirect() == pickupMetaType::drop) |
---|
147 | { |
---|
148 | pickup->drop(); |
---|
149 | } |
---|
150 | // If the metaType is destroy, then the Pickupable is destroyed. |
---|
151 | else if(this->getMetaTypeDirect() == pickupMetaType::destroy) |
---|
152 | { |
---|
153 | pickup->Pickupable::destroy(); |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | this->Pickupable::destroy(); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
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); |
---|
171 | |
---|
172 | SUPER(MetaPickup, clone, item); |
---|
173 | |
---|
174 | MetaPickup* pickup = dynamic_cast<MetaPickup*>(item); |
---|
175 | pickup->setMetaTypeDirect(this->getMetaTypeDirect()); |
---|
176 | |
---|
177 | pickup->initializeIdentifier(); |
---|
178 | } |
---|
179 | |
---|
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 | */ |
---|
186 | const std::string& MetaPickup::getMetaType(void) const |
---|
187 | { |
---|
188 | switch(this->getMetaTypeDirect()) |
---|
189 | { |
---|
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; |
---|
196 | case pickupMetaType::destroy: |
---|
197 | return MetaPickup::metaTypeDestroy_s; |
---|
198 | case pickupMetaType::destroyCarrier: |
---|
199 | return MetaPickup::metaTypeDestroyCarrier_s; |
---|
200 | default: |
---|
201 | return BLANKSTRING; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | @brief |
---|
207 | Set the meta type of this MetaPickup. |
---|
208 | @param type |
---|
209 | A string with the type to be set. |
---|
210 | */ |
---|
211 | void MetaPickup::setMetaType(const std::string& type) |
---|
212 | { |
---|
213 | if(type == MetaPickup::metaTypeNone_s) |
---|
214 | { |
---|
215 | this->setMetaTypeDirect(pickupMetaType::none); |
---|
216 | } |
---|
217 | else if(type == MetaPickup::metaTypeUse_s) |
---|
218 | { |
---|
219 | this->setMetaTypeDirect(pickupMetaType::use); |
---|
220 | } |
---|
221 | else if(type == MetaPickup::metaTypeDrop_s) |
---|
222 | { |
---|
223 | this->setMetaTypeDirect(pickupMetaType::drop); |
---|
224 | } |
---|
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 |
---|
234 | COUT(2) << "Invalid metaType '" << type << "' in MetaPickup." << std::endl; |
---|
235 | } |
---|
236 | |
---|
237 | } |
---|