Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3214 was 3214, checked in by scheusso, 15 years ago

merged netp5 back to trunk

  • Property svn:eol-style set to native
File size: 10.9 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->bReload_ = false;
62
63        this->health_ = 0;
64        this->maxHealth_ = 0;
65        this->initialHealth_ = 0;
66
67        this->lastHitOriginator_ = 0;
68
69        this->spawnparticleduration_ = 3.0f;
70
71        this->getPickups().setOwner(this);
72
73        if (GameMode::isMaster())
74        {
75            this->weaponSystem_ = new WeaponSystem(this);
76            this->weaponSystem_->setPawn(this);
77        }
78        else
79            this->weaponSystem_ = 0;
80
81        this->setRadarObjectColour(ColourValue::Red);
82        this->setRadarObjectShape(RadarViewable::Dot);
83
84        this->registerVariables();
85
86        this->isHumanShip_ = this->hasLocalController();
87    }
88
89    Pawn::~Pawn()
90    {
91        if (this->isInitialized())
92        {
93            for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it)
94                it->destroyedPawn(this);
95
96            if (this->weaponSystem_)
97                delete this->weaponSystem_;
98        }
99    }
100
101    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
102    {
103        SUPER(Pawn, XMLPort, xmlelement, mode);
104
105        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
106        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
107        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
108        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
109        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
110        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
111
112        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
113        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
114        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPack, getWeaponPack, xmlelement, mode);
115    }
116
117    void Pawn::registerVariables()
118    {
119        registerVariable(this->bAlive_,        variableDirection::toclient);
120        registerVariable(this->health_,        variableDirection::toclient);
121        registerVariable(this->initialHealth_, variableDirection::toclient);
122        registerVariable(this->bReload_,       variableDirection::toserver);
123    }
124
125    void Pawn::tick(float dt)
126    {
127        SUPER(Pawn, tick, dt);
128
129        this->bReload_ = false;
130
131        if (GameMode::isMaster())
132            if (this->health_ <= 0 && bAlive_)
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        this->setHealth(1);
202        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
203        {
204            // Set bAlive_ to false and wait for PawnManager to do the destruction
205            this->bAlive_ = false;
206
207            this->setDestroyWhenPlayerLeft(false);
208
209            this->dropItems();
210
211            if (this->getGametype())
212                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
213
214            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
215                this->getPlayer()->stopControl();
216
217            if (GameMode::isMaster())
218            {
219//                this->deathEffect();
220                this->goWithStyle();
221            }
222        }
223    }
224    void Pawn::goWithStyle()
225    {
226        this->bAlive_ = false;
227        this->setDestroyWhenPlayerLeft(false);
228
229        BigExplosion* chunk = new BigExplosion(this->getCreator());
230        chunk->setPosition(this->getPosition());
231
232    }
233    void Pawn::deatheffect()
234    {
235        // play death effect
236        {
237            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
238            effect->setPosition(this->getPosition());
239            effect->setOrientation(this->getOrientation());
240            effect->setDestroyAfterLife(true);
241            effect->setSource("Orxonox/explosion2b");
242            effect->setLifetime(4.0f);
243        }
244        {
245            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
246            effect->setPosition(this->getPosition());
247            effect->setOrientation(this->getOrientation());
248            effect->setDestroyAfterLife(true);
249            effect->setSource("Orxonox/smoke6");
250            effect->setLifetime(4.0f);
251        }
252        {
253            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
254            effect->setPosition(this->getPosition());
255            effect->setOrientation(this->getOrientation());
256            effect->setDestroyAfterLife(true);
257            effect->setSource("Orxonox/sparks");
258            effect->setLifetime(4.0f);
259        }
260        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
261        {
262            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
263            chunk->setPosition(this->getPosition());
264        }
265    }
266
267    void Pawn::fire(unsigned int firemode)
268    {
269        this->doFire(firemode);
270    }
271
272    void Pawn::doFire(uint8_t firemode)
273    {
274        if(GameMode::isMaster())
275        {
276            if (this->weaponSystem_)
277                this->weaponSystem_->fire(firemode);
278        }
279        else
280        {
281            callMemberNetworkFunction(Pawn, doFire, this->getObjectID(), 0, ((uint8_t)firemode));
282            if (this->weaponSystem_)
283                this->weaponSystem_->fire(firemode);
284        }
285    }
286
287    void Pawn::reload()
288    {
289        this->bReload_ = true;
290    }
291
292    void Pawn::postSpawn()
293    {
294        this->setHealth(this->initialHealth_);
295        if (GameMode::isMaster())
296            this->spawneffect();
297    }
298
299    void Pawn::dropItems()
300    {
301        this->getPickups().clear();
302    }
303
304
305    /* WeaponSystem:
306    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
307    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
308    *       --> e.g. Pickup-Items
309    */
310    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
311    {
312        this->attach(wSlot);
313        if (this->weaponSystem_)
314            this->weaponSystem_->addWeaponSlot(wSlot);
315    }
316
317    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
318    {
319        if (this->weaponSystem_)
320            return this->weaponSystem_->getWeaponSlot(index);
321        else
322            return 0;
323    }
324
325    void Pawn::addWeaponSet(WeaponSet * wSet)
326    {
327        if (this->weaponSystem_)
328            this->weaponSystem_->addWeaponSet(wSet);
329    }
330
331    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
332    {
333        if (this->weaponSystem_)
334            return this->weaponSystem_->getWeaponSet(index);
335        else
336            return 0;
337    }
338
339    void Pawn::addWeaponPack(WeaponPack * wPack)
340    {
341        if (this->weaponSystem_)
342            this->weaponSystem_->addWeaponPack(wPack);
343    }
344
345    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
346    {
347        if (this->weaponSystem_)
348            return this->weaponSystem_->getWeaponPack(index);
349        else
350            return 0;
351    }
352
353    //Tell the Map (RadarViewable), if this is a playership
354    void Pawn::startLocalHumanControl()
355    {
356//        SUPER(ControllableEntity, changedPlayer());
357        ControllableEntity::startLocalHumanControl();
358        this->isHumanShip_ = true;
359    }
360}
Note: See TracBrowser for help on using the repository browser.