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 | /** |
---|
78 | @brief |
---|
79 | Invoked when a pawn picks up the pickup. |
---|
80 | |
---|
81 | Adds the modifiers to the pawn and sets a timer (if effect is limited) |
---|
82 | if the pickup could be added to the pawn's PickupCollection. |
---|
83 | |
---|
84 | @param pawn Pawn which picked up the pickup. |
---|
85 | @return Returns whether the pickup was able to be added to the pawn. |
---|
86 | */ |
---|
87 | bool ModifierPickup::pickedUp(Pawn* pawn) |
---|
88 | { |
---|
89 | if (this->addTo(pawn)) |
---|
90 | { |
---|
91 | std::map<ModifierType::Enum, float>::iterator it; |
---|
92 | |
---|
93 | for (it = this->additiveModifiers_.begin(); it != this->additiveModifiers_.end(); it++) |
---|
94 | { |
---|
95 | pawn->getPickups().addAdditiveModifier((*it).first, (*it).second); |
---|
96 | } |
---|
97 | |
---|
98 | for (it = this->multiplicativeModifiers_.begin(); it != this->multiplicativeModifiers_.end(); it++) |
---|
99 | { |
---|
100 | pawn->getPickups().addMultiplicativeModifier((*it).first, (*it).second); |
---|
101 | } |
---|
102 | |
---|
103 | if (this->duration_ > 0.0f) |
---|
104 | { |
---|
105 | ExecutorMember<ModifierPickup>* executor = createExecutor(createFunctor(&ModifierPickup::timerCallback)); |
---|
106 | executor->setDefaultValues(pawn); |
---|
107 | this->timer_.setTimer(this->duration_, false, this, executor); |
---|
108 | } |
---|
109 | |
---|
110 | return true; |
---|
111 | } |
---|
112 | return false; |
---|
113 | } |
---|
114 | /** |
---|
115 | @brief |
---|
116 | Invoked when a pawn drops the pickup. |
---|
117 | |
---|
118 | Removes the modifiers from the pawn if the pickup |
---|
119 | was successfully removed from it's PickupCollection. |
---|
120 | |
---|
121 | @param pawn Pawn which dropped the pickup. |
---|
122 | @return Returns whether the pickup could be removed. |
---|
123 | */ |
---|
124 | bool ModifierPickup::dropped(Pawn* pawn) |
---|
125 | { |
---|
126 | if (this->removeFrom(pawn)) |
---|
127 | { |
---|
128 | std::map<ModifierType::Enum, float>::iterator it; |
---|
129 | |
---|
130 | for (it = this->additiveModifiers_.begin(); it != this->additiveModifiers_.end(); it++) |
---|
131 | { |
---|
132 | pawn->getPickups().removeAdditiveModifier((*it).first, (*it).second); |
---|
133 | } |
---|
134 | |
---|
135 | for (it = this->multiplicativeModifiers_.begin(); it != this->multiplicativeModifiers_.end(); it++) |
---|
136 | { |
---|
137 | pawn->getPickups().removeMultiplicativeModifier((*it).first, (*it).second); |
---|
138 | } |
---|
139 | |
---|
140 | if (this->timer_.getRemainingTime() > 0.0f) |
---|
141 | this->timer_.stopTimer(); |
---|
142 | |
---|
143 | return true; |
---|
144 | } |
---|
145 | return false; |
---|
146 | } |
---|
147 | /** |
---|
148 | @brief Invoked when the timer finished, calls dropped(). |
---|
149 | */ |
---|
150 | void ModifierPickup::timerCallback(Pawn* pawn) |
---|
151 | { |
---|
152 | if (!this->dropped(pawn)) |
---|
153 | COUT(2) << "Failed to remove modifier pickup after the timer ran out!" << std::endl; |
---|
154 | } |
---|
155 | /** |
---|
156 | @brief Gets the additive modifier of a given type. |
---|
157 | @param type ModifierType for which to return the modifier. |
---|
158 | @return Returns the additive modifier for type (or 0 if not exists). |
---|
159 | */ |
---|
160 | float ModifierPickup::getAdditiveModifier(ModifierType::Enum type) const |
---|
161 | { |
---|
162 | std::map<ModifierType::Enum, float>::const_iterator it = this->additiveModifiers_.find(type); |
---|
163 | if (it != this->additiveModifiers_.end()) |
---|
164 | return (*it).second; |
---|
165 | else |
---|
166 | return 0.0f; |
---|
167 | } |
---|
168 | /** |
---|
169 | @brief Gets the multiplicative modifier of a given type. |
---|
170 | @param type ModifierType for which to return the modifier. |
---|
171 | @return Returns the multiplicative modifier for type (or 1 if not exists). |
---|
172 | */ |
---|
173 | float ModifierPickup::getMultiplicativeModifier(ModifierType::Enum type) const |
---|
174 | { |
---|
175 | std::map<ModifierType::Enum, float>::const_iterator it = this->multiplicativeModifiers_.find(type); |
---|
176 | if (it != this->multiplicativeModifiers_.end()) |
---|
177 | return (*it).second; |
---|
178 | else |
---|
179 | return 1.0f; |
---|
180 | } |
---|
181 | /** |
---|
182 | @brief Gets the additive modifier of a given type. |
---|
183 | @param type ModifierType for which to return the modifier. |
---|
184 | @param value The new additive modifier for type. |
---|
185 | */ |
---|
186 | void ModifierPickup::setAdditiveModifier(ModifierType::Enum type, float value) |
---|
187 | { |
---|
188 | if (this->additiveModifiers_.find(type) == this->additiveModifiers_.end()) |
---|
189 | this->additiveModifiers_.insert( std::pair<ModifierType::Enum, float>(type, value) ); |
---|
190 | else |
---|
191 | this->additiveModifiers_[type] = value; |
---|
192 | } |
---|
193 | /** |
---|
194 | @brief Gets the multiplicative modifier of a given type. |
---|
195 | @param type ModifierType for which to return the modifier. |
---|
196 | @param value The new multiplicative modifier for type. |
---|
197 | */ |
---|
198 | void ModifierPickup::setMultiplicativeModifier(ModifierType::Enum type, float value) |
---|
199 | { |
---|
200 | if (this->multiplicativeModifiers_.find(type) == this->multiplicativeModifiers_.end()) |
---|
201 | this->multiplicativeModifiers_.insert( std::pair<ModifierType::Enum, float>(type, value) ); |
---|
202 | else |
---|
203 | this->multiplicativeModifiers_[type] = value; |
---|
204 | } |
---|
205 | } |
---|