Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/worldentities/pawns/Pawn.cc @ 2369

Last change on this file since 2369 was 2369, checked in by landauf, 15 years ago
  • Added a health bar
  • Some changes in CameraManager to handle the case when no camera exists after removing the last one, but this is still somehow buggy (freezes and later crashes reproducible but inexplicable after a few respawns)
  • Added PawnManager to handle destruction of Pawns without using delete within tick()
  • Property svn:eol-style set to native
File size: 4.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 "OrxonoxStableHeaders.h"
30#include "Pawn.h"
31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "util/Math.h"
35#include "PawnManager.h"
36#include "objects/infos/PlayerInfo.h"
37#include "objects/gametypes/Gametype.h"
38#include "objects/weaponSystem/WeaponSystem.h"
39
40namespace orxonox
41{
42    CreateFactory(Pawn);
43
44    Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator)
45    {
46        RegisterObject(Pawn);
47
48        PawnManager::touch();
49
50        this->bAlive_ = true;
51
52        this->health_ = 0;
53        this->maxHealth_ = 0;
54        this->initialHealth_ = 0;
55
56        this->lastHitOriginator_ = 0;
57        this->weaponSystem_ = 0;
58
59        /*
60        //WeaponSystem
61        weaponSystem_ = new WeaponSystem();
62        WeaponSet * weaponSet1 = new WeaponSet(1);
63        this->weaponSystem_->attachWeaponSet(weaponSet1);
64        this->weaponSystem_->getWeaponSetPointer(0)->getWeaponSlotPointer(0)->setAmmoType(true);
65        */
66
67        this->setRadarObjectColour(ColourValue::Red);
68        this->setRadarObjectShape(RadarViewable::Dot);
69
70        this->registerVariables();
71    }
72
73    Pawn::~Pawn()
74    {
75        if (this->isInitialized())
76        {
77            for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it)
78                it->destroyedPawn(this);
79        }
80    }
81
82    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
83    {
84        SUPER(Pawn, XMLPort, xmlelement, mode);
85
86        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
87        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
88        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
89    }
90
91    void Pawn::registerVariables()
92    {
93        REGISTERDATA(this->bAlive_, direction::toclient);
94        REGISTERDATA(this->health_, direction::toclient);
95        REGISTERDATA(this->initialHealth_, direction::toclient);
96    }
97
98    void Pawn::tick(float dt)
99    {
100        SUPER(Pawn, tick, dt);
101
102        this->health_ -= 15 * dt * rnd();
103
104        if (this->health_ <= 0)
105            this->death();
106    }
107
108    void Pawn::setHealth(float health)
109    {
110        this->health_ = min(health, this->maxHealth_);
111    }
112
113    void Pawn::damage(float damage, Pawn* originator)
114    {
115        this->setHealth(this->health_ - damage);
116        this->lastHitOriginator_ = originator;
117
118        // play damage effect
119    }
120
121    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
122    {
123        this->damage(damage, originator);
124        this->setVelocity(this->getVelocity() + force);
125
126        // play hit effect
127    }
128
129    void Pawn::kill()
130    {
131        this->damage(this->health_);
132        this->death();
133    }
134
135    void Pawn::spawn()
136    {
137        // play spawn effect
138    }
139
140    void Pawn::death()
141    {
142        // Set bAlive_ to false and wait for PawnManager to do the destruction
143        this->bAlive_ = false;
144
145        if (this->getGametype())
146            this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
147
148        this->setDestroyWhenPlayerLeft(false);
149
150        if (this->getPlayer())
151            this->getPlayer()->stopControl(this);
152
153        // play death effect
154    }
155
156    void Pawn::fire()
157    {
158        if (this->weaponSystem_)
159            this->weaponSystem_->fire();
160    }
161
162    void Pawn::postSpawn()
163    {
164        this->setHealth(this->initialHealth_);
165        this->spawn();
166    }
167
168    ///////////////////
169    // Pawn Listener //
170    ///////////////////
171    PawnListener::PawnListener()
172    {
173        RegisterRootObject(PawnListener);
174    }
175}
Note: See TracBrowser for help on using the repository browser.