Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup2/src/orxonox/worldentities/pawns/Pawn.cc @ 6405

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

Commit changes in pickup before merge.

  • Property svn:eol-style set to native
File size: 10.7 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 "gametypes/Gametype.h"
41#include "graphics/ParticleSpawner.h"
42#include "worldentities/ExplosionChunk.h"
43#include "worldentities/BigExplosion.h"
44#include "weaponsystem/WeaponSystem.h"
45#include "weaponsystem/WeaponSlot.h"
46#include "weaponsystem/WeaponPack.h"
47#include "weaponsystem/WeaponSet.h"
48
49
50namespace orxonox
51{
52    CreateFactory(Pawn);
53
54    registerMemberNetworkFunction( Pawn, doFire );
55
56    Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator)
57    {
58        RegisterObject(Pawn);
59
60        PawnManager::touch();
61        this->bAlive_ = true;
62        this->bReload_ = false;
63
64        this->health_ = 0;
65        this->maxHealth_ = 0;
66        this->initialHealth_ = 0;
67
68        this->lastHitOriginator_ = 0;
69
70        this->spawnparticleduration_ = 3.0f;
71
72        //TODO: Remove.
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            if (this->weaponSystem_)
96                this->weaponSystem_->destroy();
97        }
98    }
99
100    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
101    {
102        SUPER(Pawn, XMLPort, xmlelement, mode);
103
104        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
105        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
106        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
107        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
108        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
109        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
110
111        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
112        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
113        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPack, getWeaponPack, xmlelement, mode);
114    }
115
116    void Pawn::registerVariables()
117    {
118        registerVariable(this->bAlive_,        VariableDirection::ToClient);
119        registerVariable(this->health_,        VariableDirection::ToClient);
120        registerVariable(this->initialHealth_, VariableDirection::ToClient);
121        registerVariable(this->bReload_,       VariableDirection::ToServer);
122    }
123
124    void Pawn::tick(float dt)
125    {
126        SUPER(Pawn, tick, dt);
127
128        this->bReload_ = false;
129
130        if (GameMode::isMaster())
131            if (this->health_ <= 0 && bAlive_)
132                this->death();
133    }
134
135    void Pawn::setPlayer(PlayerInfo* player)
136    {
137        ControllableEntity::setPlayer(player);
138
139        if (this->getGametype())
140            this->getGametype()->playerStartsControllingPawn(player, this);
141    }
142
143    void Pawn::removePlayer()
144    {
145        if (this->getGametype())
146            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
147
148        ControllableEntity::removePlayer();
149    }
150
151    void Pawn::setHealth(float health)
152    {
153        this->health_ = std::min(health, this->maxHealth_);
154    }
155
156    void Pawn::damage(float damage, Pawn* originator)
157    {
158        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
159        {
160            this->setHealth(this->health_ - damage);
161            this->lastHitOriginator_ = originator;
162
163            // play damage effect
164        }
165    }
166
167    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
168    {
169        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator))
170        {
171            this->damage(damage, originator);
172            this->setVelocity(this->getVelocity() + force);
173
174            // play hit effect
175        }
176    }
177
178    void Pawn::kill()
179    {
180        this->damage(this->health_);
181        this->death();
182    }
183
184    void Pawn::spawneffect()
185    {
186        // play spawn effect
187        if (this->spawnparticlesource_ != "")
188        {
189            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
190            effect->setPosition(this->getPosition());
191            effect->setOrientation(this->getOrientation());
192            effect->setDestroyAfterLife(true);
193            effect->setSource(this->spawnparticlesource_);
194            effect->setLifetime(this->spawnparticleduration_);
195        }
196    }
197
198    void Pawn::death()
199    {
200        this->setHealth(1);
201        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
202        {
203            // Set bAlive_ to false and wait for PawnManager to do the destruction
204            this->bAlive_ = false;
205
206            this->setDestroyWhenPlayerLeft(false);
207
208            this->dropItems();
209
210            if (this->getGametype())
211                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
212
213            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
214                this->getPlayer()->stopControl();
215
216            if (GameMode::isMaster())
217            {
218//                this->deathEffect();
219                this->goWithStyle();
220            }
221        }
222    }
223    void Pawn::goWithStyle()
224    {
225        this->bAlive_ = false;
226        this->setDestroyWhenPlayerLeft(false);
227
228        BigExplosion* chunk = new BigExplosion(this->getCreator());
229        chunk->setPosition(this->getPosition());
230
231    }
232    void Pawn::deatheffect()
233    {
234        // play death effect
235        {
236            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
237            effect->setPosition(this->getPosition());
238            effect->setOrientation(this->getOrientation());
239            effect->setDestroyAfterLife(true);
240            effect->setSource("Orxonox/explosion2b");
241            effect->setLifetime(4.0f);
242        }
243        {
244            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
245            effect->setPosition(this->getPosition());
246            effect->setOrientation(this->getOrientation());
247            effect->setDestroyAfterLife(true);
248            effect->setSource("Orxonox/smoke6");
249            effect->setLifetime(4.0f);
250        }
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/sparks");
257            effect->setLifetime(4.0f);
258        }
259        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
260        {
261            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
262            chunk->setPosition(this->getPosition());
263        }
264    }
265
266    void Pawn::fire(unsigned int firemode)
267    {
268        this->doFire(firemode);
269    }
270
271    void Pawn::doFire(uint8_t firemode)
272    {
273        if(GameMode::isMaster())
274        {
275            if (this->weaponSystem_)
276                this->weaponSystem_->fire(firemode);
277        }
278        else
279        {
280            callMemberNetworkFunction(Pawn, doFire, this->getObjectID(), 0, firemode);
281            if (this->weaponSystem_)
282                this->weaponSystem_->fire(firemode);
283        }
284    }
285
286    void Pawn::reload()
287    {
288        this->bReload_ = true;
289    }
290
291    void Pawn::postSpawn()
292    {
293        this->setHealth(this->initialHealth_);
294        if (GameMode::isMaster())
295            this->spawneffect();
296    }
297
298//TODO: Remove.
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.