Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6151 was 6112, checked in by scheusso, 15 years ago

first attempts to have synchronised fire direction and target
however client cannot select targets yet (probably because of classtreemask hack)

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