Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6540 was 6540, checked in by dafrick, 14 years ago

Removed some TODO's. Finished up documenting pickup module.

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