Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp3/src/orxonox/objects/worldentities/pawns/Pawn.cc @ 2991

Last change on this file since 2991 was 2991, checked in by scheusso, 15 years ago

forgot some things

  • Property svn:eol-style set to native
File size: 10.8 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 "OrxonoxStableHeaders.h"
30#include "Pawn.h"
31
[2896]32#include "core/GameMode.h"
[2072]33#include "core/CoreIncludes.h"
34#include "core/XMLPort.h"
35#include "util/Math.h"
[2662]36#include "PawnManager.h"
[2072]37#include "objects/infos/PlayerInfo.h"
38#include "objects/gametypes/Gametype.h"
[2662]39#include "objects/worldentities/ParticleSpawner.h"
40#include "objects/worldentities/ExplosionChunk.h"
[2990]41#include "network/NetworkFunction.h"
[2072]42
43namespace orxonox
44{
45    CreateFactory(Pawn);
[2990]46   
47    registerMemberNetworkFunction( Pawn, doFire );
[2072]48
49    Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator)
50    {
51        RegisterObject(Pawn);
52
[2662]53        PawnManager::touch();
54        this->bAlive_ = true;
55        this->fire_ = 0x0;
56        this->firehack_ = 0x0;
[2072]57
58        this->health_ = 0;
59        this->maxHealth_ = 0;
60        this->initialHealth_ = 0;
61
62        this->lastHitOriginator_ = 0;
63
[2662]64        this->spawnparticleduration_ = 3.0f;
[2098]65
[2662]66        this->getPickUp().setPlayer(this);
67
[2896]68        if (GameMode::isMaster())
[2662]69        {
70            this->weaponSystem_ = new WeaponSystem(this);
71            this->weaponSystem_->setParentPawn(this);
72        }
73        else
74            this->weaponSystem_ = 0;
75
76        this->setRadarObjectColour(ColourValue::Red);
77        this->setRadarObjectShape(RadarViewable::Dot);
78
[2072]79        this->registerVariables();
80    }
81
82    Pawn::~Pawn()
83    {
[2662]84        if (this->isInitialized())
85        {
86            for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it)
87                it->destroyedPawn(this);
88
89            if (this->weaponSystem_)
90                delete this->weaponSystem_;
91        }
[2072]92    }
93
94    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
95    {
96        SUPER(Pawn, XMLPort, xmlelement, mode);
97
[2662]98        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
[2072]99        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
100        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
[2662]101        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
102        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
103        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
104
105        XMLPortObject(Pawn, WeaponSlot, "weaponslots", setWeaponSlot, getWeaponSlot, xmlelement, mode);
106        XMLPortObject(Pawn, WeaponSet, "weaponsets", setWeaponSet, getWeaponSet, xmlelement, mode);
107        XMLPortObject(Pawn, WeaponPack, "weapons", setWeaponPack, getWeaponPack, xmlelement, mode);
[2072]108    }
109
110    void Pawn::registerVariables()
111    {
[2662]112        registerVariable(this->bAlive_,        variableDirection::toclient);
113        registerVariable(this->health_,        variableDirection::toclient);
114        registerVariable(this->initialHealth_, variableDirection::toclient);
115        registerVariable(this->fire_,          variableDirection::toserver);
[2072]116    }
117
118    void Pawn::tick(float dt)
119    {
[2809]120        SUPER(Pawn, tick, dt);
[2072]121
[2990]122//         if (this->weaponSystem_)
123//         {
124//             if (this->fire_ & WeaponMode::fire)
125//                 this->weaponSystem_->fire(WeaponMode::fire);
126//             if (this->fire_ & WeaponMode::altFire)
127//                 this->weaponSystem_->fire(WeaponMode::altFire);
128//             if (this->fire_ & WeaponMode::altFire2)
129//                 this->weaponSystem_->fire(WeaponMode::altFire2);
130//         }
131//         this->fire_ = this->firehack_;
132//         this->firehack_ = 0x0;
[2662]133
[2991]134        if (GameMode::isMaster())
[2990]135          if (this->health_ <= 0)
[2072]136            this->death();
137    }
138
[2826]139    void Pawn::setPlayer(PlayerInfo* player)
140    {
141        ControllableEntity::setPlayer(player);
142
143        if (this->getGametype())
144            this->getGametype()->playerStartsControllingPawn(player, this);
145    }
146
147    void Pawn::removePlayer()
148    {
149        if (this->getGametype())
150            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
151
152        ControllableEntity::removePlayer();
153    }
154
[2072]155    void Pawn::setHealth(float health)
156    {
157        this->health_ = min(health, this->maxHealth_);
158    }
159
160    void Pawn::damage(float damage, Pawn* originator)
161    {
[2826]162        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
163        {
164            this->setHealth(this->health_ - damage);
165            this->lastHitOriginator_ = originator;
[2072]166
[2826]167            // play damage effect
168        }
[2072]169    }
170
171    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
172    {
[2826]173        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator))
174        {
175            this->damage(damage, originator);
176            this->setVelocity(this->getVelocity() + force);
[2072]177
[2826]178            // play hit effect
179        }
[2072]180    }
181
182    void Pawn::kill()
183    {
184        this->damage(this->health_);
185        this->death();
186    }
187
[2662]188    void Pawn::spawneffect()
[2072]189    {
190        // play spawn effect
[2662]191        if (this->spawnparticlesource_ != "")
192        {
193            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
194            effect->setPosition(this->getPosition());
195            effect->setOrientation(this->getOrientation());
196            effect->setDestroyAfterLife(true);
197            effect->setSource(this->spawnparticlesource_);
198            effect->setLifetime(this->spawnparticleduration_);
199        }
[2072]200    }
201
202    void Pawn::death()
203    {
[2826]204        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
205        {
206            // Set bAlive_ to false and wait for PawnManager to do the destruction
207            this->bAlive_ = false;
[2662]208
[2826]209            this->setDestroyWhenPlayerLeft(false);
[2662]210
[2826]211            if (this->getGametype())
212                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
[2662]213
[2826]214            if (this->getPlayer())
215                this->getPlayer()->stopControl(this);
[2072]216
[2896]217            if (GameMode::isMaster())
[2826]218                this->deatheffect();
219        }
[2904]220        else
221            this->setHealth(1);
[2662]222    }
[2072]223
[2662]224    void Pawn::deatheffect()
225    {
[2072]226        // play death effect
[2662]227        {
228            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
229            effect->setPosition(this->getPosition());
230            effect->setOrientation(this->getOrientation());
231            effect->setDestroyAfterLife(true);
232            effect->setSource("Orxonox/explosion2b");
233            effect->setLifetime(4.0f);
234        }
235        {
236            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
237            effect->setPosition(this->getPosition());
238            effect->setOrientation(this->getOrientation());
239            effect->setDestroyAfterLife(true);
240            effect->setSource("Orxonox/smoke6");
241            effect->setLifetime(4.0f);
242        }
243        {
244            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
245            effect->setPosition(this->getPosition());
246            effect->setOrientation(this->getOrientation());
247            effect->setDestroyAfterLife(true);
248            effect->setSource("Orxonox/sparks");
249            effect->setLifetime(4.0f);
250        }
251        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
252        {
253            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
254            chunk->setPosition(this->getPosition());
255        }
[2072]256    }
257
[2662]258    void Pawn::fire(WeaponMode::Enum fireMode)
[2098]259    {
[2990]260        doFire(fireMode);
[2098]261    }
[2990]262   
263    void Pawn::doFire(uint8_t fireMode)
264    {
[2991]265        if(GameMode::isMaster())
[2990]266        {
267            if (this->weaponSystem_)
268                this->weaponSystem_->fire((WeaponMode::Enum)fireMode);
269        }
270        else
271        {
272            callMemberNetworkFunction( Pawn, doFire, this->getObjectID(), 0, ((uint8_t)fireMode));
273            if (this->weaponSystem_)
274                this->weaponSystem_->fire((WeaponMode::Enum)fireMode);
275        }
276    }
[2098]277
[2072]278    void Pawn::postSpawn()
279    {
280        this->setHealth(this->initialHealth_);
[2896]281        if (GameMode::isMaster())
[2662]282            this->spawneffect();
[2072]283    }
[2662]284
285    void Pawn::dropItems()
286    {
287        pickUp.eraseAll();
288    }
289
[2893]290
291    /* WeaponSystem:
292    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
293    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
294    *       --> e.g. Pickup-Items
295    */
[2662]296    void Pawn::setWeaponSlot(WeaponSlot * wSlot)
297    {
298        this->attach(wSlot);
299        if (this->weaponSystem_)
300            this->weaponSystem_->attachWeaponSlot(wSlot);
301    }
302
303    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
304    {
305        if (this->weaponSystem_)
306            return this->weaponSystem_->getWeaponSlotPointer(index);
307        else
308            return 0;
309    }
310
311    void Pawn::setWeaponPack(WeaponPack * wPack)
312    {
313        if (this->weaponSystem_)
314        {
315            wPack->setParentWeaponSystem(this->weaponSystem_);
316            wPack->setParentWeaponSystemToAllWeapons(this->weaponSystem_);
317            this->weaponSystem_->attachWeaponPack( wPack,wPack->getFireMode() );
318            wPack->attachNeededMunitionToAllWeapons();
319        }
320    }
321
322    WeaponPack * Pawn::getWeaponPack(unsigned int firemode) const
323    {
324        if (this->weaponSystem_)
325            return this->weaponSystem_->getWeaponPackPointer(firemode);
326        else
327            return 0;
328    }
329
330    void Pawn::setWeaponSet(WeaponSet * wSet)
331    {
332        if (this->weaponSystem_)
333            this->weaponSystem_->attachWeaponSet(wSet);
334    }
335
336    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
337    {
338        if (this->weaponSystem_)
339            return this->weaponSystem_->getWeaponSetPointer(index);
340        else
341            return 0;
342    }
343
344
345    ///////////////////
346    // Pawn Listener //
347    ///////////////////
348    PawnListener::PawnListener()
349    {
350        RegisterRootObject(PawnListener);
351    }
[2072]352}
Note: See TracBrowser for help on using the repository browser.