Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6735 was 6711, checked in by dafrick, 15 years ago

Merged pickup4 branch back to trunk.

  • 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;
[6711]80       
81        this->setCarrierName("Pawn");
[2662]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
[2893]298    /* WeaponSystem:
299    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
300    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
301    *       --> e.g. Pickup-Items
302    */
[3053]303    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
[2662]304    {
305        this->attach(wSlot);
306        if (this->weaponSystem_)
[3053]307            this->weaponSystem_->addWeaponSlot(wSlot);
[2662]308    }
309
310    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
311    {
312        if (this->weaponSystem_)
[3053]313            return this->weaponSystem_->getWeaponSlot(index);
[2662]314        else
315            return 0;
316    }
317
[3053]318    void Pawn::addWeaponSet(WeaponSet * wSet)
[2662]319    {
320        if (this->weaponSystem_)
[3053]321            this->weaponSystem_->addWeaponSet(wSet);
[2662]322    }
323
[3053]324    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
[2662]325    {
326        if (this->weaponSystem_)
[3053]327            return this->weaponSystem_->getWeaponSet(index);
[2662]328        else
329            return 0;
330    }
331
[3053]332    void Pawn::addWeaponPack(WeaponPack * wPack)
[2662]333    {
334        if (this->weaponSystem_)
[3053]335            this->weaponSystem_->addWeaponPack(wPack);
[2662]336    }
337
[6417]338    void Pawn::addWeaponPackXML(WeaponPack * wPack)
339    {
340        if (this->weaponSystem_)
341            if (!this->weaponSystem_->addWeaponPack(wPack))
342                wPack->destroy();
343    }
344
[3053]345    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
[2662]346    {
347        if (this->weaponSystem_)
[3053]348            return this->weaponSystem_->getWeaponPack(index);
[2662]349        else
350            return 0;
351    }
352
[3089]353    //Tell the Map (RadarViewable), if this is a playership
354    void Pawn::startLocalHumanControl()
355    {
356//        SUPER(ControllableEntity, changedPlayer());
357        ControllableEntity::startLocalHumanControl();
358        this->isHumanShip_ = true;
359    }
[2072]360}
Note: See TracBrowser for help on using the repository browser.