Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2422 was 2422, checked in by landauf, 15 years ago
  • deatheffect (explosion) of Pawns works on client and server (creator of the effects can't be the Pawn itself because it will be destroyed on the client before synchronizing the effects)
  • fixed a small initialization error in Shader
  • fixed a bug in the resynchronization of already deleted MovableEntities
  • ExplosionChunk only recalculates it's velocity on the Master
  • Property svn:eol-style set to native
File size: 6.9 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        this->health_ -= 15 * dt * rnd();
111
112        if (this->health_ <= 0)
113            this->death();
114    }
115
116    void Pawn::setHealth(float health)
117    {
118        this->health_ = min(health, this->maxHealth_);
119    }
120
121    void Pawn::damage(float damage, Pawn* originator)
122    {
123        this->setHealth(this->health_ - damage);
124        this->lastHitOriginator_ = originator;
125
126        // play damage effect
127    }
128
129    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
130    {
131        this->damage(damage, originator);
132        this->setVelocity(this->getVelocity() + force);
133
134        // play hit effect
135    }
136
137    void Pawn::kill()
138    {
139        this->damage(this->health_);
140        this->death();
141    }
142
143    void Pawn::spawneffect()
144    {
145        // play spawn effect
146        if (this->spawnparticlesource_ != "")
147        {
148            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
149            effect->setPosition(this->getPosition());
150            effect->setOrientation(this->getOrientation());
151            effect->setDestroyAfterLife(true);
152            effect->setSource(this->spawnparticlesource_);
153            effect->setLifetime(this->spawnparticleduration_);
154        }
155    }
156
157    void Pawn::death()
158    {
159        // Set bAlive_ to false and wait for PawnManager to do the destruction
160        this->bAlive_ = false;
161
162        if (this->getGametype())
163            this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
164
165        this->setDestroyWhenPlayerLeft(false);
166
167        if (this->getPlayer())
168            this->getPlayer()->stopControl(this);
169
170        if (Core::isMaster())
171            this->deatheffect();
172    }
173
174    void Pawn::deatheffect()
175    {
176        // play death effect
177        {
178            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
179            effect->setPosition(this->getPosition());
180            effect->setOrientation(this->getOrientation());
181            effect->setDestroyAfterLife(true);
182            effect->setSource("Orxonox/explosion2");
183            effect->setLifetime(4.0f);
184        }
185        {
186            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
187            effect->setPosition(this->getPosition());
188            effect->setOrientation(this->getOrientation());
189            effect->setDestroyAfterLife(true);
190            effect->setSource("Orxonox/smoke6");
191            effect->setLifetime(4.0f);
192        }
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/sparks");
199            effect->setLifetime(4.0f);
200        }
201        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
202        {
203            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
204            chunk->setPosition(this->getPosition());
205
206        }
207    }
208
209    void Pawn::fire()
210    {
211        if (this->weaponSystem_)
212            this->weaponSystem_->fire();
213    }
214
215    void Pawn::postSpawn()
216    {
217        this->setHealth(this->initialHealth_);
218        if (Core::isMaster())
219            this->spawneffect();
220    }
221
222    ///////////////////
223    // Pawn Listener //
224    ///////////////////
225    PawnListener::PawnListener()
226    {
227        RegisterRootObject(PawnListener);
228    }
229}
Note: See TracBrowser for help on using the repository browser.