Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added countdown time before shield reload starts

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