Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3089 was 3089, checked in by landauf, 15 years ago

merged map branch back to trunk

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