Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6417 was 6417, checked in by rgrieder, 14 years ago

Merged presentation2 branch back to trunk.
Major new features:

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