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 | * Eric Beier |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file SpeedPickup.cc |
---|
31 | @brief Implementation of the SpeedPickup class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "DamageBoostPickup.h" |
---|
35 | |
---|
36 | #include <sstream> |
---|
37 | #include "core/CoreIncludes.h" |
---|
38 | #include "core/XMLPort.h" |
---|
39 | |
---|
40 | #include "pickup/PickupIdentifier.h" |
---|
41 | #include "worldentities/pawns/SpaceShip.h" |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | CreateFactory(DamageBoostPickup); |
---|
46 | |
---|
47 | /** |
---|
48 | @brief |
---|
49 | Constructor. Registers the object and initializes the member variables. |
---|
50 | */ |
---|
51 | DamageBoostPickup::DamageBoostPickup(BaseObject* creator) : Pickup(creator) |
---|
52 | { |
---|
53 | RegisterObject(DamageBoostPickup); |
---|
54 | |
---|
55 | this->initialize(); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | @brief |
---|
60 | Destructor. |
---|
61 | */ |
---|
62 | DamageBoostPickup::~DamageBoostPickup() |
---|
63 | { |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | @brief |
---|
68 | Initializes the member variables. |
---|
69 | */ |
---|
70 | void DamageBoostPickup::initialize(void) |
---|
71 | { |
---|
72 | this->duration_ = 0.0f; |
---|
73 | this->damageMultiplier_ = 20.0f; |
---|
74 | //Defines who is allowed to pick up the pickup. |
---|
75 | this->addTarget(ClassIdentifier<SpaceShip>::getIdentifier()); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | @brief |
---|
80 | Initializes the PickupIdentifier of this pickup. |
---|
81 | */ |
---|
82 | void DamageBoostPickup::initializeIdentifier(void) |
---|
83 | { |
---|
84 | std::stringstream stream; |
---|
85 | stream << this->getDuration(); |
---|
86 | std::string type1 = "duration"; |
---|
87 | std::string val1 = stream.str(); |
---|
88 | this->pickupIdentifier_->addParameter(type1, val1); |
---|
89 | |
---|
90 | stream.clear(); |
---|
91 | stream << this->damageMultiplier_; |
---|
92 | std::string type2 = "damageMultiplier"; |
---|
93 | std::string val2 = stream.str(); |
---|
94 | this->pickupIdentifier_->addParameter(type2, val2); |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | @brief |
---|
100 | Method for creating a DamageBoostPickup object through XML. |
---|
101 | */ |
---|
102 | void DamageBoostPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
103 | { |
---|
104 | SUPER(DamageBoostPickup, XMLPort, xmlelement, mode); |
---|
105 | |
---|
106 | XMLPortParam(DamageBoostPickup, "duration", setDuration, getDuration, xmlelement, mode); |
---|
107 | XMLPortParam(DamageBoostPickup, "damageMultiplier", setDefaultDamageMultiplier, getDefaultDamageMultiplier, xmlelement, mode); |
---|
108 | |
---|
109 | this->initializeIdentifier(); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | @brief |
---|
114 | Is called when the pickup has transited from used to unused or the other way around. |
---|
115 | */ |
---|
116 | void DamageBoostPickup::changedUsed(void) |
---|
117 | { |
---|
118 | SUPER(DamageBoostPickup, changedUsed); |
---|
119 | |
---|
120 | SpaceShip* ship = this->carrierToSpaceShipHelper(); |
---|
121 | if(ship == NULL) // If the PickupCarrier is no SpaceShip, then this pickup is useless and therefore is destroyed. |
---|
122 | this->Pickupable::destroy(); |
---|
123 | |
---|
124 | |
---|
125 | // If the pickup has transited to used. |
---|
126 | if(this->isUsed()) |
---|
127 | { |
---|
128 | // If its durationType is continuous, we set a Timer to be reminded, when the time has run out. |
---|
129 | if(this->isContinuous()) |
---|
130 | { |
---|
131 | if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f) |
---|
132 | { |
---|
133 | this->durationTimer_.unpauseTimer(); |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&DamageBoostPickup::pickupTimerCallback, this))); |
---|
138 | } |
---|
139 | } |
---|
140 | // NOTE: commented this since its use was not apparent |
---|
141 | // ship->getCreator(); |
---|
142 | |
---|
143 | this->olddamageMultiplier_ = ship->getDamageMultiplier(); |
---|
144 | ship->setDamageMultiplier( this->damageMultiplier_ ); |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | |
---|
149 | // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded. |
---|
150 | if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration())) |
---|
151 | { |
---|
152 | this->Pickupable::destroy(); |
---|
153 | ship->setDamageMultiplier ( this->olddamageMultiplier_); |
---|
154 | } |
---|
155 | // We pause the Timer if the pickup is continuous and the duration is not yet exceeded, |
---|
156 | else if(this->isContinuous() && this->durationTimer_.isActive()) |
---|
157 | { |
---|
158 | this->durationTimer_.pauseTimer(); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | @brief |
---|
165 | Helper to transform the PickupCarrier to a SpaceShip, and throw an error message if the conversion fails. |
---|
166 | @return |
---|
167 | A pointer to the SpaceShip, or NULL if the conversion failed. |
---|
168 | */ |
---|
169 | SpaceShip* DamageBoostPickup::carrierToSpaceShipHelper(void) |
---|
170 | { |
---|
171 | PickupCarrier* carrier = this->getCarrier(); |
---|
172 | SpaceShip* ship = dynamic_cast<SpaceShip*>(carrier); |
---|
173 | |
---|
174 | if(ship == NULL) |
---|
175 | { |
---|
176 | orxout(internal_error, context::pickups) << "Invalid PickupCarrier in DamageBoostPickup." << endl; |
---|
177 | } |
---|
178 | |
---|
179 | return ship; |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | @brief |
---|
184 | Creates a duplicate of the input OrxonoxClass. |
---|
185 | @param item |
---|
186 | A pointer to the Orxonox class. |
---|
187 | */ |
---|
188 | void DamageBoostPickup::clone(OrxonoxClass*& item) |
---|
189 | { |
---|
190 | if(item == NULL) |
---|
191 | item = new DamageBoostPickup(this); |
---|
192 | |
---|
193 | SUPER(DamageBoostPickup, clone, item); |
---|
194 | |
---|
195 | DamageBoostPickup* pickup = dynamic_cast<DamageBoostPickup*>(item); |
---|
196 | pickup->setDuration(this->getDuration()); |
---|
197 | |
---|
198 | |
---|
199 | pickup->initializeIdentifier(); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | @brief |
---|
204 | Sets the duration for which the DamageBoostPickup stays active. |
---|
205 | @param duration |
---|
206 | The duration in seconds. |
---|
207 | */ |
---|
208 | void DamageBoostPickup::setDuration(float duration) |
---|
209 | { |
---|
210 | if(duration >= 0.0f) |
---|
211 | { |
---|
212 | this->duration_ = duration; |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | orxout(internal_error, context::pickups) << "Invalid duration in SpeedPickup." << endl; |
---|
217 | this->duration_ = 0.0f; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /* void DamageBoostPickup::setDamageBoost(float damageBoost) |
---|
222 | { |
---|
223 | |
---|
224 | } |
---|
225 | void DamageBoostPickup::setDamageSave(float damageSave) |
---|
226 | { |
---|
227 | |
---|
228 | } |
---|
229 | */ |
---|
230 | |
---|
231 | void DamageBoostPickup::pickupTimerCallback(void) |
---|
232 | { |
---|
233 | this->setUsed(false); |
---|
234 | } |
---|
235 | |
---|
236 | } |
---|
237 | |
---|