Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged pickup branch into trunk. Yay. Persisting bugs will be fixed, very soon.

  • Property svn:eol-style set to native
File size: 11.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 "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        //TODO: Remove.
74        //this->getPickups().setOwner(this);
75
76        if (GameMode::isMaster())
77        {
78            this->weaponSystem_ = new WeaponSystem(this);
79            this->weaponSystem_->setPawn(this);
80        }
81        else
82            this->weaponSystem_ = 0;
83
84        this->setRadarObjectColour(ColourValue::Red);
85        this->setRadarObjectShape(RadarViewable::Dot);
86
87        this->registerVariables();
88
89        this->isHumanShip_ = this->hasLocalController();
90    }
91
92    Pawn::~Pawn()
93    {
94        if (this->isInitialized())
95        {
96            if (this->weaponSystem_)
97                this->weaponSystem_->destroy();
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", addWeaponPackXML, 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        registerVariable(this->aimPosition_,   Bidirectionality::ServerMaster, 0, true);
124    }
125
126    void Pawn::tick(float dt)
127    {
128        SUPER(Pawn, tick, dt);
129
130        this->bReload_ = false;
131
132        if (GameMode::isMaster())
133            if (this->health_ <= 0 && bAlive_)
134                this->death();
135    }
136
137    void Pawn::setPlayer(PlayerInfo* player)
138    {
139        ControllableEntity::setPlayer(player);
140
141        if (this->getGametype())
142            this->getGametype()->playerStartsControllingPawn(player, this);
143    }
144
145    void Pawn::removePlayer()
146    {
147        if (this->getGametype())
148            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
149
150        ControllableEntity::removePlayer();
151    }
152
153    void Pawn::setHealth(float health)
154    {
155        this->health_ = std::min(health, this->maxHealth_);
156    }
157
158    void Pawn::damage(float damage, Pawn* originator)
159    {
160        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
161        {
162            this->setHealth(this->health_ - damage);
163            this->lastHitOriginator_ = originator;
164
165            // play damage effect
166        }
167    }
168
169    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
170    {
171        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
172        {
173            this->damage(damage, originator);
174            this->setVelocity(this->getVelocity() + force);
175
176            // play hit effect
177        }
178    }
179
180    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage)
181    {
182        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
183        {
184            this->damage(damage, originator);
185
186            if ( this->getController() )
187                this->getController()->hit(originator, contactpoint, damage);
188
189            // play hit effect
190        }
191    }
192
193    void Pawn::kill()
194    {
195        this->damage(this->health_);
196        this->death();
197    }
198
199    void Pawn::spawneffect()
200    {
201        // play spawn effect
202        if (!this->spawnparticlesource_.empty())
203        {
204            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
205            effect->setPosition(this->getPosition());
206            effect->setOrientation(this->getOrientation());
207            effect->setDestroyAfterLife(true);
208            effect->setSource(this->spawnparticlesource_);
209            effect->setLifetime(this->spawnparticleduration_);
210        }
211    }
212
213    void Pawn::death()
214    {
215        this->setHealth(1);
216        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
217        {
218            // Set bAlive_ to false and wait for PawnManager to do the destruction
219            this->bAlive_ = false;
220
221            this->setDestroyWhenPlayerLeft(false);
222
223            this->dropItems();
224
225            if (this->getGametype())
226                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
227
228            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
229                this->getPlayer()->stopControl();
230
231            if (GameMode::isMaster())
232            {
233//                this->deathEffect();
234                this->goWithStyle();
235            }
236        }
237    }
238    void Pawn::goWithStyle()
239    {
240        this->bAlive_ = false;
241        this->setDestroyWhenPlayerLeft(false);
242
243        BigExplosion* chunk = new BigExplosion(this->getCreator());
244        chunk->setPosition(this->getPosition());
245
246    }
247    void Pawn::deatheffect()
248    {
249        // play death effect
250        {
251            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
252            effect->setPosition(this->getPosition());
253            effect->setOrientation(this->getOrientation());
254            effect->setDestroyAfterLife(true);
255            effect->setSource("Orxonox/explosion2b");
256            effect->setLifetime(4.0f);
257        }
258        {
259            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
260            effect->setPosition(this->getPosition());
261            effect->setOrientation(this->getOrientation());
262            effect->setDestroyAfterLife(true);
263            effect->setSource("Orxonox/smoke6");
264            effect->setLifetime(4.0f);
265        }
266        {
267            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
268            effect->setPosition(this->getPosition());
269            effect->setOrientation(this->getOrientation());
270            effect->setDestroyAfterLife(true);
271            effect->setSource("Orxonox/sparks");
272            effect->setLifetime(4.0f);
273        }
274        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
275        {
276            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
277            chunk->setPosition(this->getPosition());
278        }
279    }
280
281    void Pawn::fired(unsigned int firemode)
282    {
283        if (this->weaponSystem_)
284            this->weaponSystem_->fire(firemode);
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//TODO: Remove.
300//     void Pawn::dropItems()
301//     {
302//         this->getPickups().clear();
303//     }
304
305
306    /* WeaponSystem:
307    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
308    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
309    *       --> e.g. Pickup-Items
310    */
311    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
312    {
313        this->attach(wSlot);
314        if (this->weaponSystem_)
315            this->weaponSystem_->addWeaponSlot(wSlot);
316    }
317
318    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
319    {
320        if (this->weaponSystem_)
321            return this->weaponSystem_->getWeaponSlot(index);
322        else
323            return 0;
324    }
325
326    void Pawn::addWeaponSet(WeaponSet * wSet)
327    {
328        if (this->weaponSystem_)
329            this->weaponSystem_->addWeaponSet(wSet);
330    }
331
332    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
333    {
334        if (this->weaponSystem_)
335            return this->weaponSystem_->getWeaponSet(index);
336        else
337            return 0;
338    }
339
340    void Pawn::addWeaponPack(WeaponPack * wPack)
341    {
342        if (this->weaponSystem_)
343            this->weaponSystem_->addWeaponPack(wPack);
344    }
345
346    void Pawn::addWeaponPackXML(WeaponPack * wPack)
347    {
348        if (this->weaponSystem_)
349            if (!this->weaponSystem_->addWeaponPack(wPack))
350                wPack->destroy();
351    }
352
353    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
354    {
355        if (this->weaponSystem_)
356            return this->weaponSystem_->getWeaponPack(index);
357        else
358            return 0;
359    }
360
361    //Tell the Map (RadarViewable), if this is a playership
362    void Pawn::startLocalHumanControl()
363    {
364//        SUPER(ControllableEntity, changedPlayer());
365        ControllableEntity::startLocalHumanControl();
366        this->isHumanShip_ = true;
367    }
368}
Note: See TracBrowser for help on using the repository browser.