Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/miniprojects/src/orxonox/objects/worldentities/pawns/Pawn.cc @ 2768

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

added TeamGametype

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