Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/map/src/orxonox/objects/worldentities/pawns/Pawn.cc @ 3050

Last change on this file since 3050 was 2956, checked in by Naaduun, 16 years ago

added rotate and zoom to map. using static mapscenemanager now

  • Property svn:eol-style set to native
File size: 9.4 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 "OrxonoxStableHeaders.h"
30#include "Pawn.h"
31
32#include "core/Core.h"
33#include "core/CoreIncludes.h"
34#include "core/XMLPort.h"
35#include "util/Math.h"
36#include "PawnManager.h"
37#include "objects/infos/PlayerInfo.h"
38#include "objects/gametypes/Gametype.h"
39#include "objects/worldentities/ParticleSpawner.h"
40#include "objects/worldentities/ExplosionChunk.h"
41
42namespace orxonox
43{
44    CreateFactory(Pawn);
45
46    Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator)
47    {
48        RegisterObject(Pawn);
49
50        PawnManager::touch();
51        this->bAlive_ = true;
52        this->fire_ = 0x0;
53        this->firehack_ = 0x0;
54
55        this->health_ = 0;
56        this->maxHealth_ = 0;
57        this->initialHealth_ = 0;
58
59        this->lastHitOriginator_ = 0;
60
61        this->spawnparticleduration_ = 3.0f;
62
63        this->getPickUp().setPlayer(this);
64
65        if (Core::isMaster())
66        {
67            this->weaponSystem_ = new WeaponSystem(this);
68            this->weaponSystem_->setParentPawn(this);
69        }
70        else
71            this->weaponSystem_ = 0;
72
73        this->setRadarObjectColour(ColourValue::Red);
74        this->setRadarObjectShape(RadarViewable::Dot);
75
76        this->registerVariables();
77
78        this->isHumanShip_ = this->hasLocalController();
79    }
80
81    Pawn::~Pawn()
82    {
83        if (this->isInitialized())
84        {
85            for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it)
86                it->destroyedPawn(this);
87
88            if (this->weaponSystem_)
89                delete this->weaponSystem_;
90        }
91    }
92
93    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
94    {
95        SUPER(Pawn, XMLPort, xmlelement, mode);
96
97        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
98        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
99        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
100        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
101        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
102        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
103
104        XMLPortObject(Pawn, WeaponSlot, "weaponslots", setWeaponSlot, getWeaponSlot, xmlelement, mode);
105        XMLPortObject(Pawn, WeaponSet, "weaponsets", setWeaponSet, getWeaponSet, xmlelement, mode);
106        XMLPortObject(Pawn, WeaponPack, "weapons", setWeaponPack, getWeaponPack, xmlelement, mode);
107    }
108
109    void Pawn::registerVariables()
110    {
111        registerVariable(this->bAlive_,        variableDirection::toclient);
112        registerVariable(this->health_,        variableDirection::toclient);
113        registerVariable(this->initialHealth_, variableDirection::toclient);
114        registerVariable(this->fire_,          variableDirection::toserver);
115    }
116
117    void Pawn::tick(float dt)
118    {
119        ControllableEntity::tick(dt);
120
121        if (this->weaponSystem_)
122        {
123            if (this->fire_ & WeaponMode::fire)
124                this->weaponSystem_->fire(WeaponMode::fire);
125            if (this->fire_ & WeaponMode::altFire)
126                this->weaponSystem_->fire(WeaponMode::altFire);
127            if (this->fire_ & WeaponMode::altFire2)
128                this->weaponSystem_->fire(WeaponMode::altFire2);
129        }
130        this->fire_ = this->firehack_;
131        this->firehack_ = 0x0;
132
133        if (this->health_ <= 0)
134            this->death();
135    }
136
137    void Pawn::setHealth(float health)
138    {
139        this->health_ = min(health, this->maxHealth_);
140    }
141
142    void Pawn::damage(float damage, Pawn* originator)
143    {
144        this->setHealth(this->health_ - damage);
145        this->lastHitOriginator_ = originator;
146
147        // play damage effect
148    }
149
150    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
151    {
152        this->damage(damage, originator);
153        this->setVelocity(this->getVelocity() + force);
154
155        // play hit effect
156    }
157
158    void Pawn::kill()
159    {
160        this->damage(this->health_);
161        this->death();
162    }
163
164    void Pawn::spawneffect()
165    {
166        // play spawn effect
167        if (this->spawnparticlesource_ != "")
168        {
169            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
170            effect->setPosition(this->getPosition());
171            effect->setOrientation(this->getOrientation());
172            effect->setDestroyAfterLife(true);
173            effect->setSource(this->spawnparticlesource_);
174            effect->setLifetime(this->spawnparticleduration_);
175        }
176    }
177
178    void Pawn::death()
179    {
180        // Set bAlive_ to false and wait for PawnManager to do the destruction
181        this->bAlive_ = false;
182
183        this->setDestroyWhenPlayerLeft(false);
184
185        if (this->getGametype())
186            this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
187
188        if (this->getPlayer())
189            this->getPlayer()->stopControl(this);
190
191        if (Core::isMaster())
192            this->deatheffect();
193    }
194
195    void Pawn::deatheffect()
196    {
197        // play death effect
198        {
199            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
200            effect->setPosition(this->getPosition());
201            effect->setOrientation(this->getOrientation());
202            effect->setDestroyAfterLife(true);
203            effect->setSource("Orxonox/explosion2b");
204            effect->setLifetime(4.0f);
205        }
206        {
207            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
208            effect->setPosition(this->getPosition());
209            effect->setOrientation(this->getOrientation());
210            effect->setDestroyAfterLife(true);
211            effect->setSource("Orxonox/smoke6");
212            effect->setLifetime(4.0f);
213        }
214        {
215            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
216            effect->setPosition(this->getPosition());
217            effect->setOrientation(this->getOrientation());
218            effect->setDestroyAfterLife(true);
219            effect->setSource("Orxonox/sparks");
220            effect->setLifetime(4.0f);
221        }
222        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
223        {
224            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
225            chunk->setPosition(this->getPosition());
226
227        }
228    }
229
230    void Pawn::fire(WeaponMode::Enum fireMode)
231    {
232        this->firehack_ |= fireMode;
233    }
234
235    void Pawn::postSpawn()
236    {
237        this->setHealth(this->initialHealth_);
238        if (Core::isMaster())
239            this->spawneffect();
240    }
241
242    void Pawn::dropItems()
243    {
244        pickUp.eraseAll();
245    }
246
247    void Pawn::setWeaponSlot(WeaponSlot * wSlot)
248    {
249        this->attach(wSlot);
250        if (this->weaponSystem_)
251            this->weaponSystem_->attachWeaponSlot(wSlot);
252    }
253
254    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
255    {
256        if (this->weaponSystem_)
257            return this->weaponSystem_->getWeaponSlotPointer(index);
258        else
259            return 0;
260    }
261
262    void Pawn::setWeaponPack(WeaponPack * wPack)
263    {
264        if (this->weaponSystem_)
265        {
266            wPack->setParentWeaponSystem(this->weaponSystem_);
267            wPack->setParentWeaponSystemToAllWeapons(this->weaponSystem_);
268            this->weaponSystem_->attachWeaponPack( wPack,wPack->getFireMode() );
269            wPack->attachNeededMunitionToAllWeapons();
270        }
271    }
272
273    WeaponPack * Pawn::getWeaponPack(unsigned int firemode) const
274    {
275        if (this->weaponSystem_)
276            return this->weaponSystem_->getWeaponPackPointer(firemode);
277        else
278            return 0;
279    }
280
281    void Pawn::setWeaponSet(WeaponSet * wSet)
282    {
283        if (this->weaponSystem_)
284            this->weaponSystem_->attachWeaponSet(wSet);
285    }
286
287    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
288    {
289        if (this->weaponSystem_)
290            return this->weaponSystem_->getWeaponSetPointer(index);
291        else
292            return 0;
293    }
294
295    //Tell the Map (RadarViewable), if this is a playership
296    void Pawn::startLocalHumanControl()
297    {
298//        SUPER(ControllableEntity, changedPlayer());
299        ControllableEntity::startLocalHumanControl();
300        this->isHumanShip_ = true;
301    }
302
303
304    ///////////////////
305    // Pawn Listener //
306    ///////////////////
307    PawnListener::PawnListener()
308    {
309        RegisterRootObject(PawnListener);
310    }
311}
Note: See TracBrowser for help on using the repository browser.