Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged pickups2 branch back to trunk. not yet tested.

  • Property svn:eol-style set to native
File size: 10.5 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->getPickups().setOwner(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            this->dropItems();
217
218            if (this->getGametype())
219                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
220
221            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
222                this->getPlayer()->stopControl();
223
224            if (GameMode::isMaster())
225                this->deatheffect();
226        }
227    }
228
229    void Pawn::deatheffect()
230    {
231        // play death effect
232        {
233            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
234            effect->setPosition(this->getPosition());
235            effect->setOrientation(this->getOrientation());
236            effect->setDestroyAfterLife(true);
237            effect->setSource("Orxonox/explosion2b");
238            effect->setLifetime(4.0f);
239        }
240        {
241            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
242            effect->setPosition(this->getPosition());
243            effect->setOrientation(this->getOrientation());
244            effect->setDestroyAfterLife(true);
245            effect->setSource("Orxonox/smoke6");
246            effect->setLifetime(4.0f);
247        }
248        {
249            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
250            effect->setPosition(this->getPosition());
251            effect->setOrientation(this->getOrientation());
252            effect->setDestroyAfterLife(true);
253            effect->setSource("Orxonox/sparks");
254            effect->setLifetime(4.0f);
255        }
256        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
257        {
258            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
259            chunk->setPosition(this->getPosition());
260        }
261    }
262
263    void Pawn::fire(unsigned int firemode)
264    {
265        this->firehack_ |= WeaponSystem::getFiremodeMask(firemode);
266    }
267
268    void Pawn::reload()
269    {
270        this->bReload_ = true;
271    }
272
273    void Pawn::postSpawn()
274    {
275        this->setHealth(this->initialHealth_);
276        if (GameMode::isMaster())
277            this->spawneffect();
278    }
279
280    void Pawn::dropItems()
281    {
282        this->getPickups().clear();
283    }
284
285
286    /* WeaponSystem:
287    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
288    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
289    *       --> e.g. Pickup-Items
290    */
291    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
292    {
293        this->attach(wSlot);
294        if (this->weaponSystem_)
295            this->weaponSystem_->addWeaponSlot(wSlot);
296    }
297
298    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
299    {
300        if (this->weaponSystem_)
301            return this->weaponSystem_->getWeaponSlot(index);
302        else
303            return 0;
304    }
305
306    void Pawn::addWeaponSet(WeaponSet * wSet)
307    {
308        if (this->weaponSystem_)
309            this->weaponSystem_->addWeaponSet(wSet);
310    }
311
312    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
313    {
314        if (this->weaponSystem_)
315            return this->weaponSystem_->getWeaponSet(index);
316        else
317            return 0;
318    }
319
320    void Pawn::addWeaponPack(WeaponPack * wPack)
321    {
322        if (this->weaponSystem_)
323            this->weaponSystem_->addWeaponPack(wPack);
324    }
325
326    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
327    {
328        if (this->weaponSystem_)
329            return this->weaponSystem_->getWeaponPack(index);
330        else
331            return 0;
332    }
333
334
335    ///////////////////
336    // Pawn Listener //
337    ///////////////////
338    PawnListener::PawnListener()
339    {
340        RegisterRootObject(PawnListener);
341    }
342}
Note: See TracBrowser for help on using the repository browser.