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 | * Daniel 'Huty' Haggenmueller |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief Implementation of ModifierPickup (temporary(?) pickup for testing). |
---|
32 | */ |
---|
33 | |
---|
34 | #include "ModifierPickup.h" |
---|
35 | |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/XMLPort.h" |
---|
38 | #include "worldentities/pawns/Pawn.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | CreateFactory(ModifierPickup); |
---|
43 | |
---|
44 | /** |
---|
45 | @brief |
---|
46 | Constructor. Registers the ModifierPickup. |
---|
47 | @param creator |
---|
48 | Pointer to the object which created this item. |
---|
49 | */ |
---|
50 | ModifierPickup::ModifierPickup(BaseObject* creator) : PassiveItem(creator) |
---|
51 | { |
---|
52 | RegisterObject(ModifierPickup); |
---|
53 | |
---|
54 | this->duration_ = 0.0f; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | @brief |
---|
59 | Destructor. |
---|
60 | */ |
---|
61 | ModifierPickup::~ModifierPickup() |
---|
62 | { |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | @brief |
---|
68 | Method for loading information from a level file. |
---|
69 | @param element |
---|
70 | XMLElement from which to read the data. |
---|
71 | @param mode |
---|
72 | XMLPort mode to use. |
---|
73 | */ |
---|
74 | //TODO: Comments: params can probably be ommitted. |
---|
75 | void ModifierPickup::XMLPort(Element& element, XMLPort::Mode mode) |
---|
76 | { |
---|
77 | SUPER(ModifierPickup, XMLPort, element, mode); |
---|
78 | |
---|
79 | XMLPortParam(ModifierPickup, "duration", setDuration, getDuration, element, mode); |
---|
80 | |
---|
81 | XMLPortParamTemplate(ModifierPickup, "damageAdd", setAdditiveDamage, getAdditiveDamage, element, mode, float); |
---|
82 | XMLPortParamTemplate(ModifierPickup, "damageMulti", setMultiplicativeDamage, getMultiplicativeDamage, element, mode, float); |
---|
83 | |
---|
84 | XMLPortParamTemplate(ModifierPickup, "accelerationAdd", setAdditiveAcceleration, getAdditiveAcceleration, element, mode, float); |
---|
85 | XMLPortParamTemplate(ModifierPickup, "accelerationMulti", setMultiplicativeAcceleration, getMultiplicativeAcceleration, element, mode, float); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | @brief |
---|
90 | Invoked when a pawn picks up the pickup. |
---|
91 | |
---|
92 | Adds the modifiers to the pawn and sets a timer (if effect is limited) |
---|
93 | if the pickup could be added to the pawn's PickupCollection. |
---|
94 | |
---|
95 | @param pawn |
---|
96 | Pawn which picked up the pickup. |
---|
97 | @return |
---|
98 | Returns whether the pickup was able to be added to the pawn. |
---|
99 | */ |
---|
100 | bool ModifierPickup::pickedUp(Pawn* pawn) |
---|
101 | { |
---|
102 | if (this->addTo(pawn)) |
---|
103 | { |
---|
104 | std::map<ModifierType::Value, float>::iterator it; |
---|
105 | |
---|
106 | for (it = this->additiveModifiers_.begin(); it != this->additiveModifiers_.end(); it++) |
---|
107 | { |
---|
108 | pawn->getPickups().addAdditiveModifier(it->first, it->second); |
---|
109 | } |
---|
110 | |
---|
111 | for (it = this->multiplicativeModifiers_.begin(); it != this->multiplicativeModifiers_.end(); it++) |
---|
112 | { |
---|
113 | pawn->getPickups().addMultiplicativeModifier(it->first, it->second); |
---|
114 | } |
---|
115 | |
---|
116 | if (this->duration_ > 0.0f) |
---|
117 | { |
---|
118 | Executor* executor = createExecutor(createFunctor(&ModifierPickup::timerCallback, this)); |
---|
119 | executor->setDefaultValues(pawn); |
---|
120 | this->timer_.setTimer(this->duration_, false, executor); |
---|
121 | } |
---|
122 | |
---|
123 | return true; |
---|
124 | } |
---|
125 | return false; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | @brief |
---|
130 | Invoked when a pawn drops the pickup. |
---|
131 | |
---|
132 | Removes the modifiers from the pawn if the pickup |
---|
133 | was successfully removed from it's PickupCollection. |
---|
134 | |
---|
135 | @param pawn |
---|
136 | Pawn which dropped the pickup. |
---|
137 | @return |
---|
138 | Returns whether the pickup could be removed. |
---|
139 | */ |
---|
140 | bool ModifierPickup::dropped(Pawn* pawn) |
---|
141 | { |
---|
142 | if (this->removeFrom(pawn)) |
---|
143 | { |
---|
144 | std::map<ModifierType::Value, float>::iterator it; |
---|
145 | |
---|
146 | for (it = this->additiveModifiers_.begin(); it != this->additiveModifiers_.end(); it++) |
---|
147 | { |
---|
148 | pawn->getPickups().removeAdditiveModifier(it->first, it->second); |
---|
149 | } |
---|
150 | |
---|
151 | for (it = this->multiplicativeModifiers_.begin(); it != this->multiplicativeModifiers_.end(); it++) |
---|
152 | { |
---|
153 | pawn->getPickups().removeMultiplicativeModifier(it->first, it->second); |
---|
154 | } |
---|
155 | |
---|
156 | if (this->timer_.getRemainingTime() > 0.0f) |
---|
157 | this->timer_.stopTimer(); |
---|
158 | |
---|
159 | this->destroy(); |
---|
160 | |
---|
161 | return true; |
---|
162 | } |
---|
163 | return false; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | @brief Invoked when the timer finished, calls dropped(). |
---|
168 | */ |
---|
169 | //TODO: Other name for function? |
---|
170 | void ModifierPickup::timerCallback(Pawn* pawn) |
---|
171 | { |
---|
172 | if (!this->dropped(pawn)) |
---|
173 | COUT(2) << "Failed to remove modifier pickup after the timer ran out!" << std::endl; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | @brief |
---|
178 | Gets the additive modifier of a given type. |
---|
179 | @param type |
---|
180 | ModifierType for which to return the modifier. |
---|
181 | @return |
---|
182 | Returns the additive modifier for type (or 0 if not exists). |
---|
183 | */ |
---|
184 | float ModifierPickup::getAdditiveModifier(ModifierType::Value type) const |
---|
185 | { |
---|
186 | std::map<ModifierType::Value, float>::const_iterator it = this->additiveModifiers_.find(type); |
---|
187 | if (it != this->additiveModifiers_.end()) |
---|
188 | return it->second; |
---|
189 | else |
---|
190 | return 0.0f; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | @brief |
---|
195 | Gets the multiplicative modifier of a given type. |
---|
196 | @param type |
---|
197 | ModifierType for which to return the modifier. |
---|
198 | @return |
---|
199 | Returns the multiplicative modifier for type (or 1 if not exists). |
---|
200 | */ |
---|
201 | float ModifierPickup::getMultiplicativeModifier(ModifierType::Value type) const |
---|
202 | { |
---|
203 | std::map<ModifierType::Value, float>::const_iterator it = this->multiplicativeModifiers_.find(type); |
---|
204 | if (it != this->multiplicativeModifiers_.end()) |
---|
205 | return it->second; |
---|
206 | else |
---|
207 | return 1.0f; |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | @brief |
---|
212 | Gets the additive modifier of a given type. |
---|
213 | @param type |
---|
214 | ModifierType for which to return the modifier. |
---|
215 | @param value |
---|
216 | The new additive modifier for type. |
---|
217 | */ |
---|
218 | void ModifierPickup::setAdditiveModifier(ModifierType::Value type, float value) |
---|
219 | { |
---|
220 | if (this->additiveModifiers_.find(type) == this->additiveModifiers_.end()) |
---|
221 | this->additiveModifiers_.insert( std::pair<ModifierType::Value, float>(type, value) ); |
---|
222 | else |
---|
223 | this->additiveModifiers_[type] = value; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | @brief |
---|
228 | Gets the multiplicative modifier of a given type. |
---|
229 | @param type |
---|
230 | ModifierType for which to return the modifier. |
---|
231 | @param value |
---|
232 | The new multiplicative modifier for type. |
---|
233 | */ |
---|
234 | void ModifierPickup::setMultiplicativeModifier(ModifierType::Value type, float value) |
---|
235 | { |
---|
236 | if (this->multiplicativeModifiers_.find(type) == this->multiplicativeModifiers_.end()) |
---|
237 | this->multiplicativeModifiers_.insert( std::pair<ModifierType::Value, float>(type, value) ); |
---|
238 | else |
---|
239 | this->multiplicativeModifiers_[type] = value; |
---|
240 | } |
---|
241 | |
---|
242 | } |
---|