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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Pawn.h" |
---|
30 | |
---|
31 | #include <algorithm> |
---|
32 | |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/GameMode.h" |
---|
35 | #include "core/XMLPort.h" |
---|
36 | #include "network/NetworkFunction.h" |
---|
37 | |
---|
38 | #include "PawnManager.h" |
---|
39 | #include "infos/PlayerInfo.h" |
---|
40 | #include "controllers/Controller.h" |
---|
41 | #include "gametypes/Gametype.h" |
---|
42 | #include "graphics/ParticleSpawner.h" |
---|
43 | #include "worldentities/ExplosionChunk.h" |
---|
44 | #include "worldentities/BigExplosion.h" |
---|
45 | #include "weaponsystem/WeaponSystem.h" |
---|
46 | #include "weaponsystem/WeaponSlot.h" |
---|
47 | #include "weaponsystem/WeaponPack.h" |
---|
48 | #include "weaponsystem/WeaponSet.h" |
---|
49 | |
---|
50 | |
---|
51 | namespace orxonox |
---|
52 | { |
---|
53 | CreateFactory(Pawn); |
---|
54 | |
---|
55 | Pawn::Pawn(BaseObject* creator) |
---|
56 | : ControllableEntity(creator) |
---|
57 | , RadarViewable(creator, static_cast<WorldEntity*>(this)) |
---|
58 | { |
---|
59 | RegisterObject(Pawn); |
---|
60 | |
---|
61 | PawnManager::touch(); |
---|
62 | this->bAlive_ = true; |
---|
63 | this->bReload_ = false; |
---|
64 | |
---|
65 | this->health_ = 0; |
---|
66 | this->maxHealth_ = 0; |
---|
67 | this->initialHealth_ = 0; |
---|
68 | this->shieldHealth_ = 0; |
---|
69 | this->shieldAbsorption_ = 0.5; |
---|
70 | |
---|
71 | this->lastHitOriginator_ = 0; |
---|
72 | |
---|
73 | this->spawnparticleduration_ = 3.0f; |
---|
74 | |
---|
75 | this->aimPosition_ = Vector3::ZERO; |
---|
76 | |
---|
77 | if (GameMode::isMaster()) |
---|
78 | { |
---|
79 | this->weaponSystem_ = new WeaponSystem(this); |
---|
80 | this->weaponSystem_->setPawn(this); |
---|
81 | } |
---|
82 | else |
---|
83 | this->weaponSystem_ = 0; |
---|
84 | |
---|
85 | this->setRadarObjectColour(ColourValue::Red); |
---|
86 | this->setRadarObjectShape(RadarViewable::Dot); |
---|
87 | |
---|
88 | this->registerVariables(); |
---|
89 | |
---|
90 | this->isHumanShip_ = this->hasLocalController(); |
---|
91 | } |
---|
92 | |
---|
93 | Pawn::~Pawn() |
---|
94 | { |
---|
95 | if (this->isInitialized()) |
---|
96 | { |
---|
97 | if (this->weaponSystem_) |
---|
98 | this->weaponSystem_->destroy(); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
103 | { |
---|
104 | SUPER(Pawn, XMLPort, xmlelement, mode); |
---|
105 | |
---|
106 | XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
107 | XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
108 | XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
109 | |
---|
110 | XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0); |
---|
111 | XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0); |
---|
112 | |
---|
113 | XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode); |
---|
114 | XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f); |
---|
115 | XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7); |
---|
116 | |
---|
117 | XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode); |
---|
118 | XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode); |
---|
119 | XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode); |
---|
120 | |
---|
121 | /////// me |
---|
122 | XMLPortParam(Pawn, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0); |
---|
123 | |
---|
124 | /////// end me |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | void Pawn::registerVariables() |
---|
129 | { |
---|
130 | registerVariable(this->bAlive_, VariableDirection::ToClient); |
---|
131 | registerVariable(this->health_, VariableDirection::ToClient); |
---|
132 | registerVariable(this->initialHealth_, VariableDirection::ToClient); |
---|
133 | registerVariable(this->shieldHealth_, VariableDirection::ToClient); |
---|
134 | registerVariable(this->shieldAbsorption_, VariableDirection::ToClient); |
---|
135 | registerVariable(this->bReload_, VariableDirection::ToServer); |
---|
136 | registerVariable(this->aimPosition_, VariableDirection::ToServer); // For the moment this variable gets only transfered to the server |
---|
137 | } |
---|
138 | |
---|
139 | void Pawn::tick(float dt) |
---|
140 | { |
---|
141 | SUPER(Pawn, tick, dt); |
---|
142 | |
---|
143 | this->bReload_ = false; |
---|
144 | |
---|
145 | ////////me |
---|
146 | this->addShieldHealth(this->getReloadRate() * dt); |
---|
147 | // TODO max. shield hinzufuegen |
---|
148 | ////////end me |
---|
149 | if (GameMode::isMaster()) |
---|
150 | if (this->health_ <= 0 && bAlive_) |
---|
151 | { |
---|
152 | this->fireEvent(); // Event to notify anyone who wants to know about the death. |
---|
153 | this->death(); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | void Pawn::preDestroy() |
---|
158 | { |
---|
159 | // yay, multiple inheritance! |
---|
160 | this->ControllableEntity::preDestroy(); |
---|
161 | this->PickupCarrier::preDestroy(); |
---|
162 | } |
---|
163 | |
---|
164 | void Pawn::setPlayer(PlayerInfo* player) |
---|
165 | { |
---|
166 | ControllableEntity::setPlayer(player); |
---|
167 | |
---|
168 | if (this->getGametype()) |
---|
169 | this->getGametype()->playerStartsControllingPawn(player, this); |
---|
170 | } |
---|
171 | |
---|
172 | void Pawn::removePlayer() |
---|
173 | { |
---|
174 | if (this->getGametype()) |
---|
175 | this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this); |
---|
176 | |
---|
177 | ControllableEntity::removePlayer(); |
---|
178 | } |
---|
179 | |
---|
180 | //////////////////me |
---|
181 | void Pawn::setReloadRate(float reloadrate) |
---|
182 | { |
---|
183 | this->reloadRate_ = reloadrate; |
---|
184 | COUT(2) << "RELOAD RATE SET TO " << this->reloadRate_ << endl; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | ///////////////end me |
---|
189 | |
---|
190 | void Pawn::setHealth(float health) |
---|
191 | { |
---|
192 | this->health_ = std::min(health, this->maxHealth_); |
---|
193 | } |
---|
194 | |
---|
195 | void Pawn::damage(float damage, Pawn* originator) |
---|
196 | { |
---|
197 | if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator)) |
---|
198 | { |
---|
199 | //share the dealt damage to the shield and the Pawn. |
---|
200 | float shielddamage = damage*this->shieldAbsorption_; |
---|
201 | float healthdamage = damage*(1-this->shieldAbsorption_); |
---|
202 | |
---|
203 | // In case the shield can not take all the shield damage. |
---|
204 | if (shielddamage > this->getShieldHealth()) |
---|
205 | { |
---|
206 | healthdamage += shielddamage-this->getShieldHealth(); |
---|
207 | this->setShieldHealth(0); |
---|
208 | COUT(3) << "### SHIELD KAPUTT ###" << endl; |
---|
209 | } |
---|
210 | |
---|
211 | else { COUT(3) << "## SHIELD : " << this->getShieldHealth() << endl; } |
---|
212 | |
---|
213 | this->setHealth(this->health_ - healthdamage); |
---|
214 | |
---|
215 | if (this->getShieldHealth() > 0) |
---|
216 | { |
---|
217 | this->setShieldHealth(this->shieldHealth_ - shielddamage); |
---|
218 | } |
---|
219 | |
---|
220 | this->lastHitOriginator_ = originator; |
---|
221 | |
---|
222 | // play damage effect |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | void Pawn::hit(Pawn* originator, const Vector3& force, float damage) |
---|
227 | { |
---|
228 | if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) ) |
---|
229 | { |
---|
230 | this->damage(damage, originator); |
---|
231 | this->setVelocity(this->getVelocity() + force); |
---|
232 | |
---|
233 | // play hit effect |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage) |
---|
238 | { |
---|
239 | if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) ) |
---|
240 | { |
---|
241 | this->damage(damage, originator); |
---|
242 | |
---|
243 | if ( this->getController() ) |
---|
244 | this->getController()->hit(originator, contactpoint, damage); |
---|
245 | |
---|
246 | // play hit effect |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | void Pawn::kill() |
---|
251 | { |
---|
252 | this->damage(this->health_); |
---|
253 | this->death(); |
---|
254 | } |
---|
255 | |
---|
256 | void Pawn::spawneffect() |
---|
257 | { |
---|
258 | // play spawn effect |
---|
259 | if (!this->spawnparticlesource_.empty()) |
---|
260 | { |
---|
261 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
262 | effect->setPosition(this->getPosition()); |
---|
263 | effect->setOrientation(this->getOrientation()); |
---|
264 | effect->setDestroyAfterLife(true); |
---|
265 | effect->setSource(this->spawnparticlesource_); |
---|
266 | effect->setLifetime(this->spawnparticleduration_); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | void Pawn::death() |
---|
271 | { |
---|
272 | this->setHealth(1); |
---|
273 | if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_)) |
---|
274 | { |
---|
275 | // Set bAlive_ to false and wait for PawnManager to do the destruction |
---|
276 | this->bAlive_ = false; |
---|
277 | |
---|
278 | this->setDestroyWhenPlayerLeft(false); |
---|
279 | |
---|
280 | if (this->getGametype()) |
---|
281 | this->getGametype()->pawnKilled(this, this->lastHitOriginator_); |
---|
282 | |
---|
283 | if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this) |
---|
284 | this->getPlayer()->stopControl(); |
---|
285 | |
---|
286 | if (GameMode::isMaster()) |
---|
287 | { |
---|
288 | // this->deathEffect(); |
---|
289 | this->goWithStyle(); |
---|
290 | } |
---|
291 | } |
---|
292 | } |
---|
293 | void Pawn::goWithStyle() |
---|
294 | { |
---|
295 | this->bAlive_ = false; |
---|
296 | this->setDestroyWhenPlayerLeft(false); |
---|
297 | |
---|
298 | BigExplosion* chunk = new BigExplosion(this->getCreator()); |
---|
299 | chunk->setPosition(this->getPosition()); |
---|
300 | |
---|
301 | } |
---|
302 | void Pawn::deatheffect() |
---|
303 | { |
---|
304 | // play death effect |
---|
305 | { |
---|
306 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
307 | effect->setPosition(this->getPosition()); |
---|
308 | effect->setOrientation(this->getOrientation()); |
---|
309 | effect->setDestroyAfterLife(true); |
---|
310 | effect->setSource("Orxonox/explosion2b"); |
---|
311 | effect->setLifetime(4.0f); |
---|
312 | } |
---|
313 | { |
---|
314 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
315 | effect->setPosition(this->getPosition()); |
---|
316 | effect->setOrientation(this->getOrientation()); |
---|
317 | effect->setDestroyAfterLife(true); |
---|
318 | effect->setSource("Orxonox/smoke6"); |
---|
319 | effect->setLifetime(4.0f); |
---|
320 | } |
---|
321 | { |
---|
322 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
323 | effect->setPosition(this->getPosition()); |
---|
324 | effect->setOrientation(this->getOrientation()); |
---|
325 | effect->setDestroyAfterLife(true); |
---|
326 | effect->setSource("Orxonox/sparks"); |
---|
327 | effect->setLifetime(4.0f); |
---|
328 | } |
---|
329 | for (unsigned int i = 0; i < this->numexplosionchunks_; ++i) |
---|
330 | { |
---|
331 | ExplosionChunk* chunk = new ExplosionChunk(this->getCreator()); |
---|
332 | chunk->setPosition(this->getPosition()); |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | void Pawn::fired(unsigned int firemode) |
---|
337 | { |
---|
338 | if (this->weaponSystem_) |
---|
339 | this->weaponSystem_->fire(firemode); |
---|
340 | } |
---|
341 | |
---|
342 | void Pawn::reload() |
---|
343 | { |
---|
344 | this->bReload_ = true; |
---|
345 | } |
---|
346 | |
---|
347 | void Pawn::postSpawn() |
---|
348 | { |
---|
349 | this->setHealth(this->initialHealth_); |
---|
350 | if (GameMode::isMaster()) |
---|
351 | this->spawneffect(); |
---|
352 | } |
---|
353 | |
---|
354 | /* WeaponSystem: |
---|
355 | * functions load Slot, Set, Pack from XML and make sure all parent-pointers are set. |
---|
356 | * with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it. |
---|
357 | * --> e.g. Pickup-Items |
---|
358 | */ |
---|
359 | void Pawn::addWeaponSlot(WeaponSlot * wSlot) |
---|
360 | { |
---|
361 | this->attach(wSlot); |
---|
362 | if (this->weaponSystem_) |
---|
363 | this->weaponSystem_->addWeaponSlot(wSlot); |
---|
364 | } |
---|
365 | |
---|
366 | WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const |
---|
367 | { |
---|
368 | if (this->weaponSystem_) |
---|
369 | return this->weaponSystem_->getWeaponSlot(index); |
---|
370 | else |
---|
371 | return 0; |
---|
372 | } |
---|
373 | |
---|
374 | void Pawn::addWeaponSet(WeaponSet * wSet) |
---|
375 | { |
---|
376 | if (this->weaponSystem_) |
---|
377 | this->weaponSystem_->addWeaponSet(wSet); |
---|
378 | } |
---|
379 | |
---|
380 | WeaponSet * Pawn::getWeaponSet(unsigned int index) const |
---|
381 | { |
---|
382 | if (this->weaponSystem_) |
---|
383 | return this->weaponSystem_->getWeaponSet(index); |
---|
384 | else |
---|
385 | return 0; |
---|
386 | } |
---|
387 | |
---|
388 | void Pawn::addWeaponPack(WeaponPack * wPack) |
---|
389 | { |
---|
390 | if (this->weaponSystem_) |
---|
391 | { |
---|
392 | this->weaponSystem_->addWeaponPack(wPack); |
---|
393 | this->addedWeaponPack(wPack); |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | void Pawn::addWeaponPackXML(WeaponPack * wPack) |
---|
398 | { |
---|
399 | if (this->weaponSystem_) |
---|
400 | { |
---|
401 | if (!this->weaponSystem_->addWeaponPack(wPack)) |
---|
402 | wPack->destroy(); |
---|
403 | else |
---|
404 | this->addedWeaponPack(wPack); |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | WeaponPack * Pawn::getWeaponPack(unsigned int index) const |
---|
409 | { |
---|
410 | if (this->weaponSystem_) |
---|
411 | return this->weaponSystem_->getWeaponPack(index); |
---|
412 | else |
---|
413 | return 0; |
---|
414 | } |
---|
415 | |
---|
416 | //Tell the Map (RadarViewable), if this is a playership |
---|
417 | void Pawn::startLocalHumanControl() |
---|
418 | { |
---|
419 | // SUPER(ControllableEntity, changedPlayer()); |
---|
420 | ControllableEntity::startLocalHumanControl(); |
---|
421 | this->isHumanShip_ = true; |
---|
422 | } |
---|
423 | } |
---|