Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/particles/src/orxonox/objects/worldentities/pawns/Pawn.cc @ 3054

Last change on this file since 3054 was 3054, checked in by decapitb, 15 years ago

everything

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