Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6528 was 6524, checked in by dafrick, 15 years ago

Merged pickup branch into trunk. Yay. Persisting bugs will be fixed, very soon.

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