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
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->getPickUp().setPlayer(this);
52        this->bAlive_ = true;
53        this->fire_ = 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        if (Core::isMaster())
64        {
65            this->weaponSystem_ = new WeaponSystem(this);
66            this->weaponSystem_->setParentPawn(this);
67        }
68        else
69            this->weaponSystem_ = 0;
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            if (this->weaponSystem_)
85                delete this->weaponSystem_;
86        }
87    }
88
89    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
90    {
91        SUPER(Pawn, XMLPort, xmlelement, mode);
92
93        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
94        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
95        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
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);
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);
103    }
104
105    void Pawn::registerVariables()
106    {
107        registerVariable(this->bAlive_,        variableDirection::toclient);
108        registerVariable(this->health_,        variableDirection::toclient);
109        registerVariable(this->initialHealth_, variableDirection::toclient);
110        registerVariable(this->fire_,          variableDirection::toclient);
111    }
112
113    void Pawn::tick(float dt)
114    {
115        SUPER(Pawn, tick, dt);
116
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
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
159    void Pawn::spawneffect()
160    {
161        // play spawn effect
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        }
171    }
172
173    void Pawn::death()
174    {
175        // Set bAlive_ to false and wait for PawnManager to do the destruction
176        this->bAlive_ = false;
177
178        this->setDestroyWhenPlayerLeft(false);
179
180        if (this->getGametype())
181            this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
182
183        if (this->getPlayer())
184            this->getPlayer()->stopControl(this);
185
186        if (Core::isMaster())
187            this->deatheffect();
188    }
189
190    void Pawn::deatheffect()
191    {
192        // play death effect
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        }
223    }
224
225    void Pawn::fire(WeaponMode::Enum fireMode)
226    {
227        this->fire_ |= fireMode;
228    }
229
230    void Pawn::postSpawn()
231    {
232        this->setHealth(this->initialHealth_);
233        if (Core::isMaster())
234            this->spawneffect();
235    }
236
237    void Pawn::dropItems()
238    {
239        pickUp.eraseAll();
240    }
241
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
291    ///////////////////
292    // Pawn Listener //
293    ///////////////////
294    PawnListener::PawnListener()
295    {
296        RegisterRootObject(PawnListener);
297    }
298}
Note: See TracBrowser for help on using the repository browser.