Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/particles2/src/orxonox/worldentities/pawns/Pawn.cc @ 6101

Last change on this file since 6101 was 6101, checked in by scheusso, 14 years ago

rocket now steerable also on client
AIController shoots again now ;)
fire network function is now in CE instead of Pawn
some changes in PlayerInfo that allow controlling of temporary objects (such as Rocket)

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