Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/worldentities/pawns/Pawn.cc @ 7144

Last change on this file since 7144 was 6864, checked in by dafrick, 15 years ago

Created a new MutliTrigger: EventMultiTrigger, which essentially does the same as EventTrigger but for every object that is a target as specified by teh EventMultiTrigger an Event is fired with information as being triggered by the target object.
This allows, e.g. to use the EventMultiTrigger to trigger QuestEffectBeacons.
I've also added a fireEvent() to Pawn such that an Event is fired when the Pawn is destroyed (forcebly). With EventMultiTrigger (but also with EventTrigger) one can now find out whether a Pawn died.
Here a little piece of example XML code to illustrate the usage:
<EventMutliTrigger name="trigger1">

<events>

<trigger>

<Spaceship />

</trigger>

</events>

</EventMultiTrigger>
To be able to implement this another feature was added to MultiTrigger, which is called broadcast. If enabled the MultiTrigger sends all Events triggered by NULL as to have come by all possible triggerers.

Additionally I've fixed a bug:
In DistanceMultiTrigger, an infinite loop occured under some circumstances. Not any more.

And did some niceifying (aka. documenting, renaming and code restructuring).

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