Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/worldentities/pawns/Pawn.cc @ 2500

Last change on this file since 2500 was 2500, checked in by landauf, 15 years ago

merged pickups2 to presentation

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