Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/worldentities/pawns/Pawn.cc @ 2677

Last change on this file since 2677 was 2479, checked in by landauf, 16 years ago

Cured poor pawns from losing health

  • Property svn:eol-style set to native
File size: 6.8 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/weaponSystem/WeaponSystem.h"
40#include "objects/worldentities/ParticleSpawner.h"
41#include "objects/worldentities/ExplosionChunk.h"
42
43namespace orxonox
44{
45    CreateFactory(Pawn);
46
47    Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator)
48    {
49        RegisterObject(Pawn);
50
51        PawnManager::touch();
52
53        this->bAlive_ = true;
54
55        this->health_ = 0;
56        this->maxHealth_ = 0;
57        this->initialHealth_ = 0;
58
59        this->lastHitOriginator_ = 0;
60        this->weaponSystem_ = 0;
61
62        this->spawnparticleduration_ = 3.0f;
63
64        /*
65        //WeaponSystem
66        weaponSystem_ = new WeaponSystem();
67        WeaponSet * weaponSet1 = new WeaponSet(1);
68        this->weaponSystem_->attachWeaponSet(weaponSet1);
69        this->weaponSystem_->getWeaponSetPointer(0)->getWeaponSlotPointer(0)->setAmmoType(true);
70        */
71
72        this->setRadarObjectColour(ColourValue::Red);
73        this->setRadarObjectShape(RadarViewable::Dot);
74
75        this->registerVariables();
76    }
77
78    Pawn::~Pawn()
79    {
80        if (this->isInitialized())
81        {
82            for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it)
83                it->destroyedPawn(this);
84        }
85    }
86
87    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
88    {
89        SUPER(Pawn, XMLPort, xmlelement, mode);
90
91        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
92        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
93        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
94        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
95        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
96        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
97    }
98
99    void Pawn::registerVariables()
100    {
101        REGISTERDATA(this->bAlive_, direction::toclient);
102        REGISTERDATA(this->health_, direction::toclient);
103        REGISTERDATA(this->initialHealth_, direction::toclient);
104    }
105
106    void Pawn::tick(float dt)
107    {
108        SUPER(Pawn, tick, dt);
109
110        if (this->health_ <= 0)
111            this->death();
112    }
113
114    void Pawn::setHealth(float health)
115    {
116        this->health_ = min(health, this->maxHealth_);
117    }
118
119    void Pawn::damage(float damage, Pawn* originator)
120    {
121        this->setHealth(this->health_ - damage);
122        this->lastHitOriginator_ = originator;
123
124        // play damage effect
125    }
126
127    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
128    {
129        this->damage(damage, originator);
130        this->setVelocity(this->getVelocity() + force);
131
132        // play hit effect
133    }
134
135    void Pawn::kill()
136    {
137        this->damage(this->health_);
138        this->death();
139    }
140
141    void Pawn::spawneffect()
142    {
143        // play spawn effect
144        if (this->spawnparticlesource_ != "")
145        {
146            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
147            effect->setPosition(this->getPosition());
148            effect->setOrientation(this->getOrientation());
149            effect->setDestroyAfterLife(true);
150            effect->setSource(this->spawnparticlesource_);
151            effect->setLifetime(this->spawnparticleduration_);
152        }
153    }
154
155    void Pawn::death()
156    {
157        // Set bAlive_ to false and wait for PawnManager to do the destruction
158        this->bAlive_ = false;
159
160        this->setDestroyWhenPlayerLeft(false);
161
162        if (this->getGametype())
163            this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
164
165        if (this->getPlayer())
166            this->getPlayer()->stopControl(this);
167
168        if (Core::isMaster())
169            this->deatheffect();
170    }
171
172    void Pawn::deatheffect()
173    {
174        // play death effect
175        {
176            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
177            effect->setPosition(this->getPosition());
178            effect->setOrientation(this->getOrientation());
179            effect->setDestroyAfterLife(true);
180            effect->setSource("Orxonox/explosion2b");
181            effect->setLifetime(4.0f);
182        }
183        {
184            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
185            effect->setPosition(this->getPosition());
186            effect->setOrientation(this->getOrientation());
187            effect->setDestroyAfterLife(true);
188            effect->setSource("Orxonox/smoke6");
189            effect->setLifetime(4.0f);
190        }
191        {
192            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
193            effect->setPosition(this->getPosition());
194            effect->setOrientation(this->getOrientation());
195            effect->setDestroyAfterLife(true);
196            effect->setSource("Orxonox/sparks");
197            effect->setLifetime(4.0f);
198        }
199        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
200        {
201            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
202            chunk->setPosition(this->getPosition());
203
204        }
205    }
206
207    void Pawn::fire()
208    {
209        if (this->weaponSystem_)
210            this->weaponSystem_->fire();
211    }
212
213    void Pawn::postSpawn()
214    {
215        this->setHealth(this->initialHealth_);
216        if (Core::isMaster())
217            this->spawneffect();
218    }
219
220    ///////////////////
221    // Pawn Listener //
222    ///////////////////
223    PawnListener::PawnListener()
224    {
225        RegisterRootObject(PawnListener);
226    }
227}
Note: See TracBrowser for help on using the repository browser.