Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/ggz2/src/orxonox/worldentities/pawns/Pawn.cc @ 11744

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

merged libraries2 back to trunk

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