Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged pch branch back to trunk.

  • Property svn:eol-style set to native
File size: 11.5 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
[3196]31#include "core/CoreIncludes.h"
[2896]32#include "core/GameMode.h"
[2072]33#include "core/XMLPort.h"
[3196]34#include "network/NetworkFunction.h"
35
36#include "interfaces/PawnListener.h"
[2662]37#include "PawnManager.h"
[2072]38#include "objects/infos/PlayerInfo.h"
39#include "objects/gametypes/Gametype.h"
[2662]40#include "objects/worldentities/ParticleSpawner.h"
41#include "objects/worldentities/ExplosionChunk.h"
[3087]42#include "objects/worldentities/BigExplosion.h"
[3053]43#include "objects/weaponsystem/WeaponSystem.h"
44#include "objects/weaponsystem/WeaponSlot.h"
45#include "objects/weaponsystem/WeaponPack.h"
46#include "objects/weaponsystem/WeaponSet.h"
[2072]47
[3084]48
[2072]49namespace orxonox
50{
51    CreateFactory(Pawn);
52
[3084]53    registerMemberNetworkFunction( Pawn, doFire );
54
[2072]55    Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator)
56    {
57        RegisterObject(Pawn);
58
[2662]59        PawnManager::touch();
60        this->bAlive_ = true;
61        this->fire_ = 0x0;
62        this->firehack_ = 0x0;
[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    {
[2662]121        registerVariable(this->bAlive_,        variableDirection::toclient);
122        registerVariable(this->health_,        variableDirection::toclient);
123        registerVariable(this->initialHealth_, variableDirection::toclient);
124        registerVariable(this->fire_,          variableDirection::toserver);
[3053]125        registerVariable(this->bReload_,       variableDirection::toserver);
[2072]126    }
127
128    void Pawn::tick(float dt)
129    {
[2809]130        SUPER(Pawn, tick, dt);
[2072]131
[3084]132//        if (this->weaponSystem_ && GameMode::isMaster())
133//        {
134//            for (unsigned int firemode = 0; firemode < WeaponSystem::MAX_FIRE_MODES; firemode++)
135//                if (this->fire_ & WeaponSystem::getFiremodeMask(firemode))
136//                    this->weaponSystem_->fire(firemode);
137//
138//            if (this->bReload_)
139//                this->weaponSystem_->reload();
140//        }
141//
142//        this->fire_ = this->firehack_;
143//        this->firehack_ = 0x0;
[3053]144        this->bReload_ = false;
[2662]145
[3084]146        if (GameMode::isMaster())
[3087]147            if (this->health_ <= 0 && bAlive_)
148                this->death();
[2072]149    }
150
[2826]151    void Pawn::setPlayer(PlayerInfo* player)
152    {
153        ControllableEntity::setPlayer(player);
154
155        if (this->getGametype())
156            this->getGametype()->playerStartsControllingPawn(player, this);
157    }
158
159    void Pawn::removePlayer()
160    {
161        if (this->getGametype())
162            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
163
164        ControllableEntity::removePlayer();
165    }
166
[2072]167    void Pawn::setHealth(float health)
168    {
169        this->health_ = min(health, this->maxHealth_);
170    }
171
172    void Pawn::damage(float damage, Pawn* originator)
173    {
[2826]174        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
175        {
176            this->setHealth(this->health_ - damage);
177            this->lastHitOriginator_ = originator;
[2072]178
[2826]179            // play damage effect
180        }
[2072]181    }
182
183    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
184    {
[2826]185        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator))
186        {
187            this->damage(damage, originator);
188            this->setVelocity(this->getVelocity() + force);
[2072]189
[2826]190            // play hit effect
191        }
[2072]192    }
193
194    void Pawn::kill()
195    {
196        this->damage(this->health_);
197        this->death();
198    }
199
[2662]200    void Pawn::spawneffect()
[2072]201    {
202        // play spawn effect
[2662]203        if (this->spawnparticlesource_ != "")
204        {
205            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
206            effect->setPosition(this->getPosition());
207            effect->setOrientation(this->getOrientation());
208            effect->setDestroyAfterLife(true);
209            effect->setSource(this->spawnparticlesource_);
210            effect->setLifetime(this->spawnparticleduration_);
211        }
[2072]212    }
213
214    void Pawn::death()
215    {
[3033]216        this->setHealth(1);
[2826]217        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
218        {
219            // Set bAlive_ to false and wait for PawnManager to do the destruction
220            this->bAlive_ = false;
[2662]221
[2826]222            this->setDestroyWhenPlayerLeft(false);
[2662]223
[3073]224            this->dropItems();
225
[2826]226            if (this->getGametype())
227                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
[2662]228
[3038]229            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
230                this->getPlayer()->stopControl();
[2072]231
[2896]232            if (GameMode::isMaster())
[3087]233            {
234//                this->deathEffect();
235                this->goWithStyle();
236            }
[2826]237        }
[2662]238    }
[3087]239    void Pawn::goWithStyle()
240    {
241        this->bAlive_ = false;
242        this->setDestroyWhenPlayerLeft(false);
[2072]243
[3087]244        BigExplosion* chunk = new BigExplosion(this->getCreator());
245        chunk->setPosition(this->getPosition());
246
247    }
[2662]248    void Pawn::deatheffect()
249    {
[2072]250        // play death effect
[2662]251        {
252            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
253            effect->setPosition(this->getPosition());
254            effect->setOrientation(this->getOrientation());
255            effect->setDestroyAfterLife(true);
256            effect->setSource("Orxonox/explosion2b");
257            effect->setLifetime(4.0f);
258        }
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("Orxonox/smoke6");
265            effect->setLifetime(4.0f);
266        }
267        {
268            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
269            effect->setPosition(this->getPosition());
270            effect->setOrientation(this->getOrientation());
271            effect->setDestroyAfterLife(true);
272            effect->setSource("Orxonox/sparks");
273            effect->setLifetime(4.0f);
274        }
275        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
276        {
277            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
278            chunk->setPosition(this->getPosition());
279        }
[2072]280    }
281
[3053]282    void Pawn::fire(unsigned int firemode)
[2098]283    {
[3084]284        this->doFire(firemode);
[2098]285    }
286
[3084]287    void Pawn::doFire(uint8_t firemode)
288    {
289        if(GameMode::isMaster())
290        {
291            if (this->weaponSystem_)
292                this->weaponSystem_->fire(firemode);
293        }
294        else
295        {
296            callMemberNetworkFunction(Pawn, doFire, this->getObjectID(), 0, ((uint8_t)firemode));
297            if (this->weaponSystem_)
298                this->weaponSystem_->fire(firemode);
299        }
300    }
301
[3053]302    void Pawn::reload()
303    {
304        this->bReload_ = true;
305    }
306
[2072]307    void Pawn::postSpawn()
308    {
309        this->setHealth(this->initialHealth_);
[2896]310        if (GameMode::isMaster())
[2662]311            this->spawneffect();
[2072]312    }
[2662]313
314    void Pawn::dropItems()
315    {
[3073]316        this->getPickups().clear();
[2662]317    }
318
[2893]319
320    /* WeaponSystem:
321    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
322    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
323    *       --> e.g. Pickup-Items
324    */
[3053]325    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
[2662]326    {
327        this->attach(wSlot);
328        if (this->weaponSystem_)
[3053]329            this->weaponSystem_->addWeaponSlot(wSlot);
[2662]330    }
331
332    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
333    {
334        if (this->weaponSystem_)
[3053]335            return this->weaponSystem_->getWeaponSlot(index);
[2662]336        else
337            return 0;
338    }
339
[3053]340    void Pawn::addWeaponSet(WeaponSet * wSet)
[2662]341    {
342        if (this->weaponSystem_)
[3053]343            this->weaponSystem_->addWeaponSet(wSet);
[2662]344    }
345
[3053]346    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
[2662]347    {
348        if (this->weaponSystem_)
[3053]349            return this->weaponSystem_->getWeaponSet(index);
[2662]350        else
351            return 0;
352    }
353
[3053]354    void Pawn::addWeaponPack(WeaponPack * wPack)
[2662]355    {
356        if (this->weaponSystem_)
[3053]357            this->weaponSystem_->addWeaponPack(wPack);
[2662]358    }
359
[3053]360    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
[2662]361    {
362        if (this->weaponSystem_)
[3053]363            return this->weaponSystem_->getWeaponPack(index);
[2662]364        else
365            return 0;
366    }
367
[3089]368    //Tell the Map (RadarViewable), if this is a playership
369    void Pawn::startLocalHumanControl()
370    {
371//        SUPER(ControllableEntity, changedPlayer());
372        ControllableEntity::startLocalHumanControl();
373        this->isHumanShip_ = true;
374    }
[2072]375}
Note: See TracBrowser for help on using the repository browser.