Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib2/src/orxonox/worldentities/pawns/Pawn.cc @ 8474

Last change on this file since 8474 was 8307, checked in by rgrieder, 14 years ago

Manage PawnManager by Scopes. Then the singleton should get deleted in any case.

  • Property svn:eol-style set to native
File size: 12.5 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 "Pawn.h"
30
31#include <algorithm>
32
33#include "core/CoreIncludes.h"
34#include "core/GameMode.h"
35#include "core/XMLPort.h"
36#include "network/NetworkFunction.h"
37
38#include "infos/PlayerInfo.h"
39#include "controllers/Controller.h"
40#include "gametypes/Gametype.h"
41#include "graphics/ParticleSpawner.h"
42#include "worldentities/ExplosionChunk.h"
43#include "worldentities/BigExplosion.h"
44#include "weaponsystem/WeaponSystem.h"
45#include "weaponsystem/WeaponSlot.h"
46#include "weaponsystem/WeaponPack.h"
47#include "weaponsystem/WeaponSet.h"
48
49
50namespace orxonox
51{
52    CreateFactory(Pawn);
53
54    Pawn::Pawn(BaseObject* creator)
55        : ControllableEntity(creator)
56        , RadarViewable(creator, static_cast<WorldEntity*>(this))
57    {
58        RegisterObject(Pawn);
59
60        this->bAlive_ = true;
61        this->bReload_ = false;
62
63        this->health_ = 0;
64        this->maxHealth_ = 0;
65        this->initialHealth_ = 0;
66        this->shieldHealth_ = 0;
67        this->shieldAbsorption_ = 0.5;
68
69        this->lastHitOriginator_ = 0;
70
71        this->spawnparticleduration_ = 3.0f;
72
73        this->aimPosition_ = Vector3::ZERO;
74
75        if (GameMode::isMaster())
76        {
77            this->weaponSystem_ = new WeaponSystem(this);
78            this->weaponSystem_->setPawn(this);
79        }
80        else
81            this->weaponSystem_ = 0;
82
83        this->setRadarObjectColour(ColourValue::Red);
84        this->setRadarObjectShape(RadarViewable::Dot);
85
86        this->registerVariables();
87
88        this->isHumanShip_ = this->hasLocalController();
89    }
90
91    Pawn::~Pawn()
92    {
93        if (this->isInitialized())
94        {
95            if (this->weaponSystem_)
96                this->weaponSystem_->destroy();
97        }
98    }
99
100    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
101    {
102        SUPER(Pawn, XMLPort, xmlelement, mode);
103
104        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
105        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
106        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
107
108        XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
109        XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
110
111        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
112        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
113        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
114
115        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
116        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
117        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode);
118    }
119
120    void Pawn::registerVariables()
121    {
122        registerVariable(this->bAlive_,           VariableDirection::ToClient);
123        registerVariable(this->health_,           VariableDirection::ToClient);
124        registerVariable(this->initialHealth_,    VariableDirection::ToClient);
125        registerVariable(this->shieldHealth_,     VariableDirection::ToClient);
126        registerVariable(this->shieldAbsorption_, VariableDirection::ToClient);
127        registerVariable(this->bReload_,          VariableDirection::ToServer);
128        registerVariable(this->aimPosition_,      VariableDirection::ToServer);  // For the moment this variable gets only transfered to the server
129    }
130
131    void Pawn::tick(float dt)
132    {
133        SUPER(Pawn, tick, dt);
134
135        this->bReload_ = false;
136
137        if (GameMode::isMaster())
138            if (this->health_ <= 0 && bAlive_)
139            {
140                this->fireEvent(); // Event to notify anyone who want's to know about the death.
141                this->death();
142            }
143    }
144
145    void Pawn::preDestroy()
146    {
147        // yay, multiple inheritance!
148        this->ControllableEntity::preDestroy();
149        this->PickupCarrier::preDestroy();
150    }
151
152    void Pawn::setPlayer(PlayerInfo* player)
153    {
154        ControllableEntity::setPlayer(player);
155
156        if (this->getGametype())
157            this->getGametype()->playerStartsControllingPawn(player, this);
158    }
159
160    void Pawn::removePlayer()
161    {
162        if (this->getGametype())
163            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
164
165        ControllableEntity::removePlayer();
166    }
167
168    void Pawn::setHealth(float health)
169    {
170        this->health_ = std::min(health, this->maxHealth_);
171    }
172
173    void Pawn::damage(float damage, Pawn* originator)
174    {
175        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
176        {
177            //share the dealt damage to the shield and the Pawn.
178            float shielddamage = damage*this->shieldAbsorption_;
179            float healthdamage = damage*(1-this->shieldAbsorption_);
180
181            // In case the shield can not take all the shield damage.
182            if (shielddamage > this->getShieldHealth())
183            {
184                healthdamage += shielddamage-this->getShieldHealth();
185                this->setShieldHealth(0);
186            }
187
188            this->setHealth(this->health_ - healthdamage);
189
190            if (this->getShieldHealth() > 0)
191            {
192                this->setShieldHealth(this->shieldHealth_ - shielddamage);
193            }
194
195            this->lastHitOriginator_ = originator;
196
197            // play damage effect
198        }
199    }
200
201    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
202    {
203        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
204        {
205            this->damage(damage, originator);
206            this->setVelocity(this->getVelocity() + force);
207
208            // play hit effect
209        }
210    }
211
212    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage)
213    {
214        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
215        {
216            this->damage(damage, originator);
217
218            if ( this->getController() )
219                this->getController()->hit(originator, contactpoint, damage);
220
221            // play hit effect
222        }
223    }
224
225    void Pawn::kill()
226    {
227        this->damage(this->health_);
228        this->death();
229    }
230
231    void Pawn::spawneffect()
232    {
233        // play spawn effect
234        if (!this->spawnparticlesource_.empty())
235        {
236            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
237            effect->setPosition(this->getPosition());
238            effect->setOrientation(this->getOrientation());
239            effect->setDestroyAfterLife(true);
240            effect->setSource(this->spawnparticlesource_);
241            effect->setLifetime(this->spawnparticleduration_);
242        }
243    }
244
245    void Pawn::death()
246    {
247        this->setHealth(1);
248        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
249        {
250            // Set bAlive_ to false and wait for PawnManager to do the destruction
251            this->bAlive_ = false;
252
253            this->setDestroyWhenPlayerLeft(false);
254
255            if (this->getGametype())
256                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
257
258            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
259                this->getPlayer()->stopControl();
260
261            if (GameMode::isMaster())
262            {
263//                this->deathEffect();
264                this->goWithStyle();
265            }
266        }
267    }
268    void Pawn::goWithStyle()
269    {
270        this->bAlive_ = false;
271        this->setDestroyWhenPlayerLeft(false);
272
273        BigExplosion* chunk = new BigExplosion(this->getCreator());
274        chunk->setPosition(this->getPosition());
275
276    }
277    void Pawn::deatheffect()
278    {
279        // play death effect
280        {
281            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
282            effect->setPosition(this->getPosition());
283            effect->setOrientation(this->getOrientation());
284            effect->setDestroyAfterLife(true);
285            effect->setSource("Orxonox/explosion2b");
286            effect->setLifetime(4.0f);
287        }
288        {
289            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
290            effect->setPosition(this->getPosition());
291            effect->setOrientation(this->getOrientation());
292            effect->setDestroyAfterLife(true);
293            effect->setSource("Orxonox/smoke6");
294            effect->setLifetime(4.0f);
295        }
296        {
297            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
298            effect->setPosition(this->getPosition());
299            effect->setOrientation(this->getOrientation());
300            effect->setDestroyAfterLife(true);
301            effect->setSource("Orxonox/sparks");
302            effect->setLifetime(4.0f);
303        }
304        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
305        {
306            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
307            chunk->setPosition(this->getPosition());
308        }
309    }
310
311    void Pawn::fired(unsigned int firemode)
312    {
313        if (this->weaponSystem_)
314            this->weaponSystem_->fire(firemode);
315    }
316
317    void Pawn::reload()
318    {
319        this->bReload_ = true;
320    }
321
322    void Pawn::postSpawn()
323    {
324        this->setHealth(this->initialHealth_);
325        if (GameMode::isMaster())
326            this->spawneffect();
327    }
328
329    /* WeaponSystem:
330    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
331    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
332    *       --> e.g. Pickup-Items
333    */
334    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
335    {
336        this->attach(wSlot);
337        if (this->weaponSystem_)
338            this->weaponSystem_->addWeaponSlot(wSlot);
339    }
340
341    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
342    {
343        if (this->weaponSystem_)
344            return this->weaponSystem_->getWeaponSlot(index);
345        else
346            return 0;
347    }
348
349    void Pawn::addWeaponSet(WeaponSet * wSet)
350    {
351        if (this->weaponSystem_)
352            this->weaponSystem_->addWeaponSet(wSet);
353    }
354
355    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
356    {
357        if (this->weaponSystem_)
358            return this->weaponSystem_->getWeaponSet(index);
359        else
360            return 0;
361    }
362
363    void Pawn::addWeaponPack(WeaponPack * wPack)
364    {
365        if (this->weaponSystem_)
366        {
367            this->weaponSystem_->addWeaponPack(wPack);
368            this->addedWeaponPack(wPack);
369        }
370    }
371
372    void Pawn::addWeaponPackXML(WeaponPack * wPack)
373    {
374        if (this->weaponSystem_)
375        {
376            if (!this->weaponSystem_->addWeaponPack(wPack))
377                wPack->destroy();
378            else
379                this->addedWeaponPack(wPack);
380        }
381    }
382
383    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
384    {
385        if (this->weaponSystem_)
386            return this->weaponSystem_->getWeaponPack(index);
387        else
388            return 0;
389    }
390
391    //Tell the Map (RadarViewable), if this is a playership
392    void Pawn::startLocalHumanControl()
393    {
394//        SUPER(ControllableEntity, changedPlayer());
395        ControllableEntity::startLocalHumanControl();
396        this->isHumanShip_ = true;
397    }
398}
Note: See TracBrowser for help on using the repository browser.