Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6579 was 6540, checked in by dafrick, 15 years ago

Removed some TODO's. Finished up documenting pickup module.

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