Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gameimmersion/src/orxonox/worldentities/pawns/Pawn.cc @ 8184

Last change on this file since 8184 was 8184, checked in by simonmie, 13 years ago

Added maxShieldHealth

  • Property svn:eol-style set to native
File size: 14.4 KB
Line 
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
51namespace 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////////////////////////me
71        this->reloadRate_ = 0;
72        this->reloadWaitTime_ = 1.0f;
73        this->reloadWaitCountdown_ = 0;
74
75        this->maxShieldHealth_ = 0;
76////////////////////////end me
77
78        this->lastHitOriginator_ = 0;
79
80        this->spawnparticleduration_ = 3.0f;
81
82        this->aimPosition_ = Vector3::ZERO;
83
84        if (GameMode::isMaster())
85        {
86            this->weaponSystem_ = new WeaponSystem(this);
87            this->weaponSystem_->setPawn(this);
88        }
89        else
90            this->weaponSystem_ = 0;
91
92        this->setRadarObjectColour(ColourValue::Red);
93        this->setRadarObjectShape(RadarViewable::Dot);
94
95        this->registerVariables();
96
97        this->isHumanShip_ = this->hasLocalController();
98    }
99
100    Pawn::~Pawn()
101    {
102        if (this->isInitialized())
103        {
104            if (this->weaponSystem_)
105                this->weaponSystem_->destroy();
106        }
107    }
108
109    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
110    {
111        SUPER(Pawn, XMLPort, xmlelement, mode);
112
113        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
114        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
115        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
116
117        XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
118        XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
119
120        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
121        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
122        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
123
124        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
125        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
126        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode);
127
128/////// me
129        XMLPortParam(Pawn, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0);
130        XMLPortParam(Pawn, "reloadwaittime", setReloadWaitTime, getReloadWaitTime, xmlelement, mode).defaultValues(1.0f);
131        XMLPortParam(Pawn, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100);
132
133/////// end me
134        //TODO: DEFINES fuer defaultwerte (hier und weiter oben dieselben)
135    }
136
137    void Pawn::registerVariables()
138    {
139        registerVariable(this->bAlive_,           VariableDirection::ToClient);
140        registerVariable(this->health_,           VariableDirection::ToClient);
141        registerVariable(this->initialHealth_,    VariableDirection::ToClient);
142        registerVariable(this->shieldHealth_,     VariableDirection::ToClient);
143        registerVariable(this->shieldAbsorption_, VariableDirection::ToClient);
144        registerVariable(this->bReload_,          VariableDirection::ToServer);
145        registerVariable(this->aimPosition_,      VariableDirection::ToServer);  // For the moment this variable gets only transfered to the server
146    }
147
148    void Pawn::tick(float dt)
149    {
150        SUPER(Pawn, tick, dt);
151
152        this->bReload_ = false;
153
154////////me
155        if(this->reloadWaitCountdown_ > 0)
156        {
157            this->decreaseReloadCountdownTime(dt);
158        }
159        else
160        {
161            this->addShieldHealth(this->getReloadRate() * dt);
162            this->resetReloadCountdown();
163        }
164
165////////end me
166        if (GameMode::isMaster())
167            if (this->health_ <= 0 && bAlive_)
168            {
169                this->fireEvent(); // Event to notify anyone who wants to know about the death.
170                this->death();
171            }
172    }
173
174    void Pawn::preDestroy()
175    {
176        // yay, multiple inheritance!
177        this->ControllableEntity::preDestroy();
178        this->PickupCarrier::preDestroy();
179    }
180
181    void Pawn::setPlayer(PlayerInfo* player)
182    {
183        ControllableEntity::setPlayer(player);
184
185        if (this->getGametype())
186            this->getGametype()->playerStartsControllingPawn(player, this);
187    }
188
189    void Pawn::removePlayer()
190    {
191        if (this->getGametype())
192            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
193
194        ControllableEntity::removePlayer();
195    }
196
197//////////////////me
198    void Pawn::setReloadRate(float reloadrate)
199    {
200        this->reloadRate_ = reloadrate;
201        COUT(2) << "RELOAD RATE SET TO " << this->reloadRate_ << endl;
202    }
203
204    void Pawn::setReloadWaitTime(float reloadwaittime)
205    {
206        this->reloadWaitTime_ = reloadwaittime;
207        COUT(2) << "RELOAD WAIT TIME SET TO " << this->reloadWaitTime_ << endl;
208    }
209
210    void Pawn::decreaseReloadCountdownTime(float dt)
211    {
212        this->reloadWaitCountdown_ -= dt;
213    }
214
215    void Pawn::setMaxShieldHealth(float maxshieldhealth)
216    {
217        this->maxShieldHealth_ = maxshieldhealth;
218    }
219
220    void Pawn::setShieldHealth(float shieldHealth)
221    {
222        this->shieldHealth_ = std::min(shieldHealth, this->maxShieldHealth_);
223    }
224
225///////////////end me
226
227    void Pawn::setHealth(float health)
228    {
229        this->health_ = std::min(health, this->maxHealth_);
230    }
231
232//////////////////me edit
233    void Pawn::damage(float damage, Pawn* originator)
234    {
235        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
236        {
237            //share the dealt damage to the shield and the Pawn.
238            float shielddamage = damage*this->shieldAbsorption_;
239            float healthdamage = damage*(1-this->shieldAbsorption_);
240
241            // In case the shield can not take all the shield damage.
242            if (shielddamage > this->getShieldHealth())
243            {
244                healthdamage += shielddamage-this->getShieldHealth();
245                this->setShieldHealth(0);
246                COUT(3) << "### SHIELD KAPUTT ###" << endl;
247            }
248
249            else { COUT(3) << "## SHIELD : " << this->getShieldHealth() << endl; }
250
251            this->setHealth(this->health_ - healthdamage);
252
253            if (this->getShieldHealth() > 0)
254            {
255                this->setShieldHealth(this->shieldHealth_ - shielddamage);
256            }
257
258            this->lastHitOriginator_ = originator;
259
260            // play damage effect
261        }
262    }
263////////////////////end edit
264
265    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
266    {
267        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
268        {
269            this->damage(damage, originator);
270            this->setVelocity(this->getVelocity() + force);
271
272            // play hit effect
273        }
274    }
275
276    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage)
277    {
278        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
279        {
280            this->damage(damage, originator);
281
282            if ( this->getController() )
283                this->getController()->hit(originator, contactpoint, damage);
284
285            // play hit effect
286        }
287    }
288
289    void Pawn::kill()
290    {
291        this->damage(this->health_);
292        this->death();
293    }
294
295    void Pawn::spawneffect()
296    {
297        // play spawn effect
298        if (!this->spawnparticlesource_.empty())
299        {
300            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
301            effect->setPosition(this->getPosition());
302            effect->setOrientation(this->getOrientation());
303            effect->setDestroyAfterLife(true);
304            effect->setSource(this->spawnparticlesource_);
305            effect->setLifetime(this->spawnparticleduration_);
306        }
307    }
308
309    void Pawn::death()
310    {
311        this->setHealth(1);
312        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
313        {
314            // Set bAlive_ to false and wait for PawnManager to do the destruction
315            this->bAlive_ = false;
316
317            this->setDestroyWhenPlayerLeft(false);
318
319            if (this->getGametype())
320                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
321
322            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
323                this->getPlayer()->stopControl();
324
325            if (GameMode::isMaster())
326            {
327//                this->deathEffect();
328                this->goWithStyle();
329            }
330        }
331    }
332    void Pawn::goWithStyle()
333    {
334        this->bAlive_ = false;
335        this->setDestroyWhenPlayerLeft(false);
336
337        BigExplosion* chunk = new BigExplosion(this->getCreator());
338        chunk->setPosition(this->getPosition());
339
340    }
341    void Pawn::deatheffect()
342    {
343        // play death effect
344        {
345            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
346            effect->setPosition(this->getPosition());
347            effect->setOrientation(this->getOrientation());
348            effect->setDestroyAfterLife(true);
349            effect->setSource("Orxonox/explosion2b");
350            effect->setLifetime(4.0f);
351        }
352        {
353            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
354            effect->setPosition(this->getPosition());
355            effect->setOrientation(this->getOrientation());
356            effect->setDestroyAfterLife(true);
357            effect->setSource("Orxonox/smoke6");
358            effect->setLifetime(4.0f);
359        }
360        {
361            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
362            effect->setPosition(this->getPosition());
363            effect->setOrientation(this->getOrientation());
364            effect->setDestroyAfterLife(true);
365            effect->setSource("Orxonox/sparks");
366            effect->setLifetime(4.0f);
367        }
368        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
369        {
370            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
371            chunk->setPosition(this->getPosition());
372        }
373    }
374
375    void Pawn::fired(unsigned int firemode)
376    {
377        if (this->weaponSystem_)
378            this->weaponSystem_->fire(firemode);
379    }
380
381    void Pawn::reload()
382    {
383        this->bReload_ = true;
384    }
385
386    void Pawn::postSpawn()
387    {
388        this->setHealth(this->initialHealth_);
389        if (GameMode::isMaster())
390            this->spawneffect();
391    }
392
393    /* WeaponSystem:
394    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
395    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
396    *       --> e.g. Pickup-Items
397    */
398    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
399    {
400        this->attach(wSlot);
401        if (this->weaponSystem_)
402            this->weaponSystem_->addWeaponSlot(wSlot);
403    }
404
405    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
406    {
407        if (this->weaponSystem_)
408            return this->weaponSystem_->getWeaponSlot(index);
409        else
410            return 0;
411    }
412
413    void Pawn::addWeaponSet(WeaponSet * wSet)
414    {
415        if (this->weaponSystem_)
416            this->weaponSystem_->addWeaponSet(wSet);
417    }
418
419    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
420    {
421        if (this->weaponSystem_)
422            return this->weaponSystem_->getWeaponSet(index);
423        else
424            return 0;
425    }
426
427    void Pawn::addWeaponPack(WeaponPack * wPack)
428    {
429        if (this->weaponSystem_)
430        {
431            this->weaponSystem_->addWeaponPack(wPack);
432            this->addedWeaponPack(wPack);
433        }
434    }
435
436    void Pawn::addWeaponPackXML(WeaponPack * wPack)
437    {
438        if (this->weaponSystem_)
439        {
440            if (!this->weaponSystem_->addWeaponPack(wPack))
441                wPack->destroy();
442            else
443                this->addedWeaponPack(wPack);
444        }
445    }
446
447    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
448    {
449        if (this->weaponSystem_)
450            return this->weaponSystem_->getWeaponPack(index);
451        else
452            return 0;
453    }
454
455    //Tell the Map (RadarViewable), if this is a playership
456    void Pawn::startLocalHumanControl()
457    {
458//        SUPER(ControllableEntity, changedPlayer());
459        ControllableEntity::startLocalHumanControl();
460        this->isHumanShip_ = true;
461    }
462}
Note: See TracBrowser for help on using the repository browser.