Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6829 was 6394, checked in by rgrieder, 15 years ago

std::string tweaks:

  • Declared BLANKSTRING in UtilPrereqs.h as well (removed obsolete StringUtils.h includes to avoid dependencies)
  • Using BLANKSTRING if const std::string& return type is possible
  • Replaced a few (const) std::string arguments with const std::string&
  • if (str == "") —> if (str.empty())
  • std::string msg = name + "adsf"; —> const std::string& msg = name + "asdf";
  • std::string asdf = object→getFooBar(); —> const std::string& asdf = object→getFooBar();
  • std::string asdf = "asdf"; —> std::string asdf("asdf");
  • ostream << "."; and name + "." —> ostream << '.'; and name + '.'
  • str = ""; —> str.clear()
  • std::string asdf = ""; —> std::string asdf;
  • asdf_ = ""; (in c'tor) —> delete line
  • Property svn:eol-style set to native
File size: 11.1 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        this->getPickups().setOwner(this);
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->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    void Pawn::dropItems()
299    {
300        this->getPickups().clear();
301    }
302
303
304    /* WeaponSystem:
305    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
306    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
307    *       --> e.g. Pickup-Items
308    */
309    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
310    {
311        this->attach(wSlot);
312        if (this->weaponSystem_)
313            this->weaponSystem_->addWeaponSlot(wSlot);
314    }
315
316    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
317    {
318        if (this->weaponSystem_)
319            return this->weaponSystem_->getWeaponSlot(index);
320        else
321            return 0;
322    }
323
324    void Pawn::addWeaponSet(WeaponSet * wSet)
325    {
326        if (this->weaponSystem_)
327            this->weaponSystem_->addWeaponSet(wSet);
328    }
329
330    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
331    {
332        if (this->weaponSystem_)
333            return this->weaponSystem_->getWeaponSet(index);
334        else
335            return 0;
336    }
337
338    void Pawn::addWeaponPack(WeaponPack * wPack)
339    {
340        if (this->weaponSystem_)
341            this->weaponSystem_->addWeaponPack(wPack);
342    }
343
344    void Pawn::addWeaponPackXML(WeaponPack * wPack)
345    {
346        if (this->weaponSystem_)
347            if (!this->weaponSystem_->addWeaponPack(wPack))
348                wPack->destroy();
349    }
350
351    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
352    {
353        if (this->weaponSystem_)
354            return this->weaponSystem_->getWeaponPack(index);
355        else
356            return 0;
357    }
358
359    //Tell the Map (RadarViewable), if this is a playership
360    void Pawn::startLocalHumanControl()
361    {
362//        SUPER(ControllableEntity, changedPlayer());
363        ControllableEntity::startLocalHumanControl();
364        this->isHumanShip_ = true;
365    }
366}
Note: See TracBrowser for help on using the repository browser.