Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/worldentities/pawns/Pawn.cc @ 5895

Last change on this file since 5895 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

  • Property svn:eol-style set to native
File size: 10.8 KB
RevLine 
[2072]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
[3280]31#include <algorithm>
32
[3196]33#include "core/CoreIncludes.h"
[2896]34#include "core/GameMode.h"
[2072]35#include "core/XMLPort.h"
[3196]36#include "network/NetworkFunction.h"
37
38#include "interfaces/PawnListener.h"
[2662]39#include "PawnManager.h"
[5735]40#include "infos/PlayerInfo.h"
41#include "gametypes/Gametype.h"
[5737]42#include "graphics/ParticleSpawner.h"
[5735]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"
[2072]49
[3084]50
[2072]51namespace orxonox
52{
53    CreateFactory(Pawn);
54
[3084]55    registerMemberNetworkFunction( Pawn, doFire );
56
[2072]57    Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator)
58    {
59        RegisterObject(Pawn);
60
[2662]61        PawnManager::touch();
62        this->bAlive_ = true;
[3053]63        this->bReload_ = false;
[2072]64
65        this->health_ = 0;
66        this->maxHealth_ = 0;
67        this->initialHealth_ = 0;
68
69        this->lastHitOriginator_ = 0;
70
[2662]71        this->spawnparticleduration_ = 3.0f;
[2098]72
[3073]73        this->getPickups().setOwner(this);
[2662]74
[2896]75        if (GameMode::isMaster())
[2662]76        {
77            this->weaponSystem_ = new WeaponSystem(this);
[3053]78            this->weaponSystem_->setPawn(this);
[2662]79        }
80        else
81            this->weaponSystem_ = 0;
82
83        this->setRadarObjectColour(ColourValue::Red);
84        this->setRadarObjectShape(RadarViewable::Dot);
85
[2072]86        this->registerVariables();
[3089]87
88        this->isHumanShip_ = this->hasLocalController();
[2072]89    }
90
91    Pawn::~Pawn()
92    {
[2662]93        if (this->isInitialized())
94        {
95            for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it)
96                it->destroyedPawn(this);
97
98            if (this->weaponSystem_)
99                delete this->weaponSystem_;
100        }
[2072]101    }
102
103    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
104    {
105        SUPER(Pawn, XMLPort, xmlelement, mode);
106
[2662]107        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
[2072]108        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
109        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
[2662]110        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
111        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
112        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
113
[3053]114        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
115        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
116        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPack, getWeaponPack, xmlelement, mode);
[2072]117    }
118
119    void Pawn::registerVariables()
120    {
[3280]121        registerVariable(this->bAlive_,        VariableDirection::ToClient);
122        registerVariable(this->health_,        VariableDirection::ToClient);
123        registerVariable(this->initialHealth_, VariableDirection::ToClient);
124        registerVariable(this->bReload_,       VariableDirection::ToServer);
[2072]125    }
126
127    void Pawn::tick(float dt)
128    {
[2809]129        SUPER(Pawn, tick, dt);
[2072]130
[3053]131        this->bReload_ = false;
[2662]132
[3084]133        if (GameMode::isMaster())
[3087]134            if (this->health_ <= 0 && bAlive_)
135                this->death();
[2072]136    }
137
[2826]138    void Pawn::setPlayer(PlayerInfo* player)
139    {
140        ControllableEntity::setPlayer(player);
141
142        if (this->getGametype())
143            this->getGametype()->playerStartsControllingPawn(player, this);
144    }
145
146    void Pawn::removePlayer()
147    {
148        if (this->getGametype())
149            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
150
151        ControllableEntity::removePlayer();
152    }
153
[2072]154    void Pawn::setHealth(float health)
155    {
[3280]156        this->health_ = std::min(health, this->maxHealth_);
[2072]157    }
158
159    void Pawn::damage(float damage, Pawn* originator)
160    {
[2826]161        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
162        {
163            this->setHealth(this->health_ - damage);
164            this->lastHitOriginator_ = originator;
[2072]165
[2826]166            // play damage effect
167        }
[2072]168    }
169
170    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
171    {
[2826]172        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator))
173        {
174            this->damage(damage, originator);
175            this->setVelocity(this->getVelocity() + force);
[2072]176
[2826]177            // play hit effect
178        }
[2072]179    }
180
181    void Pawn::kill()
182    {
183        this->damage(this->health_);
184        this->death();
185    }
186
[2662]187    void Pawn::spawneffect()
[2072]188    {
189        // play spawn effect
[2662]190        if (this->spawnparticlesource_ != "")
191        {
192            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
193            effect->setPosition(this->getPosition());
194            effect->setOrientation(this->getOrientation());
195            effect->setDestroyAfterLife(true);
196            effect->setSource(this->spawnparticlesource_);
197            effect->setLifetime(this->spawnparticleduration_);
198        }
[2072]199    }
200
201    void Pawn::death()
202    {
[3033]203        this->setHealth(1);
[2826]204        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
205        {
206            // Set bAlive_ to false and wait for PawnManager to do the destruction
207            this->bAlive_ = false;
[2662]208
[2826]209            this->setDestroyWhenPlayerLeft(false);
[2662]210
[3073]211            this->dropItems();
212
[2826]213            if (this->getGametype())
214                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
[2662]215
[3038]216            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
217                this->getPlayer()->stopControl();
[2072]218
[2896]219            if (GameMode::isMaster())
[3087]220            {
221//                this->deathEffect();
222                this->goWithStyle();
223            }
[2826]224        }
[2662]225    }
[3087]226    void Pawn::goWithStyle()
227    {
228        this->bAlive_ = false;
229        this->setDestroyWhenPlayerLeft(false);
[2072]230
[3087]231        BigExplosion* chunk = new BigExplosion(this->getCreator());
232        chunk->setPosition(this->getPosition());
233
234    }
[2662]235    void Pawn::deatheffect()
236    {
[2072]237        // play death effect
[2662]238        {
239            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
240            effect->setPosition(this->getPosition());
241            effect->setOrientation(this->getOrientation());
242            effect->setDestroyAfterLife(true);
243            effect->setSource("Orxonox/explosion2b");
244            effect->setLifetime(4.0f);
245        }
246        {
247            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
248            effect->setPosition(this->getPosition());
249            effect->setOrientation(this->getOrientation());
250            effect->setDestroyAfterLife(true);
251            effect->setSource("Orxonox/smoke6");
252            effect->setLifetime(4.0f);
253        }
254        {
255            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
256            effect->setPosition(this->getPosition());
257            effect->setOrientation(this->getOrientation());
258            effect->setDestroyAfterLife(true);
259            effect->setSource("Orxonox/sparks");
260            effect->setLifetime(4.0f);
261        }
262        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
263        {
264            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
265            chunk->setPosition(this->getPosition());
266        }
[2072]267    }
268
[3053]269    void Pawn::fire(unsigned int firemode)
[2098]270    {
[3084]271        this->doFire(firemode);
[2098]272    }
273
[3084]274    void Pawn::doFire(uint8_t firemode)
275    {
276        if(GameMode::isMaster())
277        {
278            if (this->weaponSystem_)
279                this->weaponSystem_->fire(firemode);
280        }
281        else
282        {
[3300]283            callMemberNetworkFunction(Pawn, doFire, this->getObjectID(), 0, firemode);
[3084]284            if (this->weaponSystem_)
285                this->weaponSystem_->fire(firemode);
286        }
287    }
288
[3053]289    void Pawn::reload()
290    {
291        this->bReload_ = true;
292    }
293
[2072]294    void Pawn::postSpawn()
295    {
296        this->setHealth(this->initialHealth_);
[2896]297        if (GameMode::isMaster())
[2662]298            this->spawneffect();
[2072]299    }
[2662]300
301    void Pawn::dropItems()
302    {
[3073]303        this->getPickups().clear();
[2662]304    }
305
[2893]306
307    /* WeaponSystem:
308    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
309    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
310    *       --> e.g. Pickup-Items
311    */
[3053]312    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
[2662]313    {
314        this->attach(wSlot);
315        if (this->weaponSystem_)
[3053]316            this->weaponSystem_->addWeaponSlot(wSlot);
[2662]317    }
318
319    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
320    {
321        if (this->weaponSystem_)
[3053]322            return this->weaponSystem_->getWeaponSlot(index);
[2662]323        else
324            return 0;
325    }
326
[3053]327    void Pawn::addWeaponSet(WeaponSet * wSet)
[2662]328    {
329        if (this->weaponSystem_)
[3053]330            this->weaponSystem_->addWeaponSet(wSet);
[2662]331    }
332
[3053]333    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
[2662]334    {
335        if (this->weaponSystem_)
[3053]336            return this->weaponSystem_->getWeaponSet(index);
[2662]337        else
338            return 0;
339    }
340
[3053]341    void Pawn::addWeaponPack(WeaponPack * wPack)
[2662]342    {
343        if (this->weaponSystem_)
[3053]344            this->weaponSystem_->addWeaponPack(wPack);
[2662]345    }
346
[3053]347    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
[2662]348    {
349        if (this->weaponSystem_)
[3053]350            return this->weaponSystem_->getWeaponPack(index);
[2662]351        else
352            return 0;
353    }
354
[3089]355    //Tell the Map (RadarViewable), if this is a playership
356    void Pawn::startLocalHumanControl()
357    {
358//        SUPER(ControllableEntity, changedPlayer());
359        ControllableEntity::startLocalHumanControl();
360        this->isHumanShip_ = true;
361    }
[2072]362}
Note: See TracBrowser for help on using the repository browser.