Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/orxonox/worldentities/pawns/Pawn.cc @ 6942

Last change on this file since 6942 was 6942, checked in by scheusso, 14 years ago

merging hudelements into presentation3 and reducing (or increasing) output level of lod debug output

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