Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

First try of self-reloading shield

  • Property svn:eol-style set to native
File size: 13.1 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
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////////end me
148        if (GameMode::isMaster())
149            if (this->health_ <= 0 && bAlive_)
150            {
151                this->fireEvent(); // Event to notify anyone who wants to know about the death.
152                this->death();
153            }
154    }
155
156    void Pawn::preDestroy()
157    {
158        // yay, multiple inheritance!
159        this->ControllableEntity::preDestroy();
160        this->PickupCarrier::preDestroy();
161    }
162
163    void Pawn::setPlayer(PlayerInfo* player)
164    {
165        ControllableEntity::setPlayer(player);
166
167        if (this->getGametype())
168            this->getGametype()->playerStartsControllingPawn(player, this);
169    }
170
171    void Pawn::removePlayer()
172    {
173        if (this->getGametype())
174            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
175
176        ControllableEntity::removePlayer();
177    }
178
179//////////////////me
180    void Pawn::setReloadRate(float reloadrate)
181    {
182        this->reloadRate_ = reloadrate;
183        COUT(2) << "RELOAD RATE SET TO " << this->reloadRate_ << endl;
184    }
185
186
187///////////////end me
188
189    void Pawn::setHealth(float health)
190    {
191        this->health_ = std::min(health, this->maxHealth_);
192    }
193
194    void Pawn::damage(float damage, Pawn* originator)
195    {
196        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
197        {
198            //share the dealt damage to the shield and the Pawn.
199            float shielddamage = damage*this->shieldAbsorption_;
200            float healthdamage = damage*(1-this->shieldAbsorption_);
201
202            // In case the shield can not take all the shield damage.
203            if (shielddamage > this->getShieldHealth())
204            {
205                healthdamage += shielddamage-this->getShieldHealth();
206                this->setShieldHealth(0);
207                COUT(3) << "### SHIELD KAPUTT ###" << endl;
208            }
209
210            else { COUT(3) << "## SHIELD : " << this->getShieldHealth() << endl; }
211
212            this->setHealth(this->health_ - healthdamage);
213
214            if (this->getShieldHealth() > 0)
215            {
216                this->setShieldHealth(this->shieldHealth_ - shielddamage);
217            }
218
219            this->lastHitOriginator_ = originator;
220
221            // play damage effect
222        }
223    }
224
225    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
226    {
227        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
228        {
229            this->damage(damage, originator);
230            this->setVelocity(this->getVelocity() + force);
231
232            // play hit effect
233        }
234    }
235
236    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage)
237    {
238        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
239        {
240            this->damage(damage, originator);
241
242            if ( this->getController() )
243                this->getController()->hit(originator, contactpoint, damage);
244
245            // play hit effect
246        }
247    }
248
249    void Pawn::kill()
250    {
251        this->damage(this->health_);
252        this->death();
253    }
254
255    void Pawn::spawneffect()
256    {
257        // play spawn effect
258        if (!this->spawnparticlesource_.empty())
259        {
260            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
261            effect->setPosition(this->getPosition());
262            effect->setOrientation(this->getOrientation());
263            effect->setDestroyAfterLife(true);
264            effect->setSource(this->spawnparticlesource_);
265            effect->setLifetime(this->spawnparticleduration_);
266        }
267    }
268
269    void Pawn::death()
270    {
271        this->setHealth(1);
272        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
273        {
274            // Set bAlive_ to false and wait for PawnManager to do the destruction
275            this->bAlive_ = false;
276
277            this->setDestroyWhenPlayerLeft(false);
278
279            if (this->getGametype())
280                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
281
282            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
283                this->getPlayer()->stopControl();
284
285            if (GameMode::isMaster())
286            {
287//                this->deathEffect();
288                this->goWithStyle();
289            }
290        }
291    }
292    void Pawn::goWithStyle()
293    {
294        this->bAlive_ = false;
295        this->setDestroyWhenPlayerLeft(false);
296
297        BigExplosion* chunk = new BigExplosion(this->getCreator());
298        chunk->setPosition(this->getPosition());
299
300    }
301    void Pawn::deatheffect()
302    {
303        // play death effect
304        {
305            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
306            effect->setPosition(this->getPosition());
307            effect->setOrientation(this->getOrientation());
308            effect->setDestroyAfterLife(true);
309            effect->setSource("Orxonox/explosion2b");
310            effect->setLifetime(4.0f);
311        }
312        {
313            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
314            effect->setPosition(this->getPosition());
315            effect->setOrientation(this->getOrientation());
316            effect->setDestroyAfterLife(true);
317            effect->setSource("Orxonox/smoke6");
318            effect->setLifetime(4.0f);
319        }
320        {
321            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
322            effect->setPosition(this->getPosition());
323            effect->setOrientation(this->getOrientation());
324            effect->setDestroyAfterLife(true);
325            effect->setSource("Orxonox/sparks");
326            effect->setLifetime(4.0f);
327        }
328        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
329        {
330            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
331            chunk->setPosition(this->getPosition());
332        }
333    }
334
335    void Pawn::fired(unsigned int firemode)
336    {
337        if (this->weaponSystem_)
338            this->weaponSystem_->fire(firemode);
339    }
340
341    void Pawn::reload()
342    {
343        this->bReload_ = true;
344    }
345
346    void Pawn::postSpawn()
347    {
348        this->setHealth(this->initialHealth_);
349        if (GameMode::isMaster())
350            this->spawneffect();
351    }
352
353    /* WeaponSystem:
354    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
355    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
356    *       --> e.g. Pickup-Items
357    */
358    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
359    {
360        this->attach(wSlot);
361        if (this->weaponSystem_)
362            this->weaponSystem_->addWeaponSlot(wSlot);
363    }
364
365    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
366    {
367        if (this->weaponSystem_)
368            return this->weaponSystem_->getWeaponSlot(index);
369        else
370            return 0;
371    }
372
373    void Pawn::addWeaponSet(WeaponSet * wSet)
374    {
375        if (this->weaponSystem_)
376            this->weaponSystem_->addWeaponSet(wSet);
377    }
378
379    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
380    {
381        if (this->weaponSystem_)
382            return this->weaponSystem_->getWeaponSet(index);
383        else
384            return 0;
385    }
386
387    void Pawn::addWeaponPack(WeaponPack * wPack)
388    {
389        if (this->weaponSystem_)
390        {
391            this->weaponSystem_->addWeaponPack(wPack);
392            this->addedWeaponPack(wPack);
393        }
394    }
395
396    void Pawn::addWeaponPackXML(WeaponPack * wPack)
397    {
398        if (this->weaponSystem_)
399        {
400            if (!this->weaponSystem_->addWeaponPack(wPack))
401                wPack->destroy();
402            else
403                this->addedWeaponPack(wPack);
404        }
405    }
406
407    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
408    {
409        if (this->weaponSystem_)
410            return this->weaponSystem_->getWeaponPack(index);
411        else
412            return 0;
413    }
414
415    //Tell the Map (RadarViewable), if this is a playership
416    void Pawn::startLocalHumanControl()
417    {
418//        SUPER(ControllableEntity, changedPlayer());
419        ControllableEntity::startLocalHumanControl();
420        this->isHumanShip_ = true;
421    }
422}
Note: See TracBrowser for help on using the repository browser.