Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates3/src/orxonox/worldentities/pawns/Pawn.cc @ 7324

Last change on this file since 7324 was 6711, checked in by dafrick, 15 years ago

Merged pickup4 branch back to trunk.

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