Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ppspickups3/src/orxonox/worldentities/pawns/Pawn.cc @ 6893

Last change on this file since 6893 was 6893, checked in by ebeier, 14 years ago

more cout yeahhhhhh

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