Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.cc @ 2914

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

More changes in the WeaponSystem:

  • WeaponSets can now contain several WeaponPacks
  • WeaponPacks can now belong to several WeaponSets

(until now this seemingly was a 1:1 relationship… now it's n:m)

  • Started support for multiple weaponmodes
  • Added some code to the destructor of some classes… according to some legends, this helps destroying objects. (WeaponSets and WeaponPacks weren't deleted before…)

Not yet finished, but I have to synchronize desktop computer and notebook.

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