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