Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/worldentities/pawns/Pawn.cc @ 3186

Last change on this file since 3186 was 3186, checked in by rgrieder, 15 years ago

Last part of the cleanup: world entities.

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