Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged weapons branch back to trunk

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