Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/worldentities/pawns/Pawn.cc @ 6111

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

made some changes in NewHumanController, Pawn, CE, Controller and WeaponMode preparing the synchronisation of clients target and aimPosition

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