Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added spawn- and destroy-effects to Pawn

  • Property svn:eol-style set to native
File size: 6.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 "OrxonoxStableHeaders.h"
30#include "Pawn.h"
31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "util/Math.h"
35#include "PawnManager.h"
36#include "objects/infos/PlayerInfo.h"
37#include "objects/gametypes/Gametype.h"
38#include "objects/weaponSystem/WeaponSystem.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
52        this->bAlive_ = true;
53
54        this->health_ = 0;
55        this->maxHealth_ = 0;
56        this->initialHealth_ = 0;
57
58        this->lastHitOriginator_ = 0;
59        this->weaponSystem_ = 0;
60
61        this->spawnparticleduration_ = 3.0f;
62
63        /*
64        //WeaponSystem
65        weaponSystem_ = new WeaponSystem();
66        WeaponSet * weaponSet1 = new WeaponSet(1);
67        this->weaponSystem_->attachWeaponSet(weaponSet1);
68        this->weaponSystem_->getWeaponSetPointer(0)->getWeaponSlotPointer(0)->setAmmoType(true);
69        */
70
71        this->setRadarObjectColour(ColourValue::Red);
72        this->setRadarObjectShape(RadarViewable::Dot);
73
74        this->registerVariables();
75    }
76
77    Pawn::~Pawn()
78    {
79        if (this->isInitialized())
80        {
81            for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it)
82                it->destroyedPawn(this);
83        }
84    }
85
86    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
87    {
88        SUPER(Pawn, XMLPort, xmlelement, mode);
89
90        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
91        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
92        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
93        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
94        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
95        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
96    }
97
98    void Pawn::registerVariables()
99    {
100        REGISTERDATA(this->bAlive_, direction::toclient);
101        REGISTERDATA(this->health_, direction::toclient);
102        REGISTERDATA(this->initialHealth_, direction::toclient);
103    }
104
105    void Pawn::tick(float dt)
106    {
107        SUPER(Pawn, tick, dt);
108
109        this->health_ -= 15 * dt * rnd();
110
111        if (this->health_ <= 0)
112            this->death();
113    }
114
115    void Pawn::setHealth(float health)
116    {
117        this->health_ = min(health, this->maxHealth_);
118    }
119
120    void Pawn::damage(float damage, Pawn* originator)
121    {
122        this->setHealth(this->health_ - damage);
123        this->lastHitOriginator_ = originator;
124
125        // play damage effect
126    }
127
128    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
129    {
130        this->damage(damage, originator);
131        this->setVelocity(this->getVelocity() + force);
132
133        // play hit effect
134    }
135
136    void Pawn::kill()
137    {
138        this->damage(this->health_);
139        this->death();
140    }
141
142    void Pawn::spawneffect()
143    {
144        // play spawn effect
145        if (this->spawnparticlesource_ != "")
146        {
147            ParticleSpawner* effect = new ParticleSpawner(this);
148            effect->setPosition(this->getPosition());
149            effect->setOrientation(this->getOrientation());
150            effect->setDestroyAfterLife(true);
151            effect->setSource(this->spawnparticlesource_);
152            effect->setLifetime(this->spawnparticleduration_);
153        }
154    }
155
156    void Pawn::death()
157    {
158        // Set bAlive_ to false and wait for PawnManager to do the destruction
159        this->bAlive_ = false;
160
161        if (this->getGametype())
162            this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
163
164        this->setDestroyWhenPlayerLeft(false);
165
166        if (this->getPlayer())
167            this->getPlayer()->stopControl(this);
168
169        this->deatheffect();
170    }
171
172    void Pawn::deatheffect()
173    {
174        // play death effect
175        {
176            ParticleSpawner* effect = new ParticleSpawner(this);
177            effect->setPosition(this->getPosition());
178            effect->setOrientation(this->getOrientation());
179            effect->setDestroyAfterLife(true);
180            effect->setSource("Orxonox/explosion2");
181            effect->setLifetime(4.0f);
182        }
183        {
184            ParticleSpawner* effect = new ParticleSpawner(this);
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);
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);
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        this->spawneffect();
217    }
218
219    ///////////////////
220    // Pawn Listener //
221    ///////////////////
222    PawnListener::PawnListener()
223    {
224        RegisterRootObject(PawnListener);
225    }
226}
Note: See TracBrowser for help on using the repository browser.