Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/infos/PlayerInfo.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: 5.0 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 <cassert>
30
31#include "OrxonoxStableHeaders.h"
32#include "PlayerInfo.h"
33
34#include "core/CoreIncludes.h"
35#include "network/ClientInformation.h"
36#include "objects/gametypes/Gametype.h"
37
38namespace orxonox
39{
40    PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator)
41    {
42        RegisterObject(PlayerInfo);
43
44        this->clientID_ = CLIENTID_UNKNOWN;
45        this->bHumanPlayer_ = false;
46        this->bLocalPlayer_ = false;
47        this->bReadyToSpawn_ = false;
48        this->bSetUnreadyAfterSpawn_ = true;
49        this->controller_ = 0;
50        this->controllableEntity_ = 0;
51        this->controllableEntityID_ = CLIENTID_UNKNOWN;
52
53        this->registerVariables();
54    }
55
56    PlayerInfo::~PlayerInfo()
57    {
58        if (this->BaseObject::isInitialized())
59        {
60            this->stopControl(this->controllableEntity_);
61
62            if (this->controller_)
63            {
64                delete this->controller_;
65                this->controller_ = 0;
66            }
67        }
68    }
69
70    void PlayerInfo::registerVariables()
71    {
72        REGISTERSTRING(this->name_,                 direction::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
73        REGISTERDATA  (this->controllableEntityID_, direction::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID));
74        REGISTERDATA  (this->bReadyToSpawn_,        direction::toserver);
75    }
76
77    void PlayerInfo::changedName()
78    {
79        SUPER(PlayerInfo, changedName);
80
81        if (this->isInitialized() && this->getGametype())
82            this->getGametype()->playerChangedName(this);
83    }
84
85    void PlayerInfo::changedGametype()
86    {
87        if (this->isInitialized())
88        {
89            if (this->getOldGametype())
90            {
91                if (this->getGametype())
92                    this->getOldGametype()->playerSwitched(this, this->getGametype());
93                else
94                    this->getOldGametype()->playerLeft(this);
95            }
96
97            if (this->getGametype())
98            {
99                if (this->getOldGametype())
100                    this->getGametype()->playerSwitchedBack(this, this->getOldGametype());
101                else
102                    this->getGametype()->playerEntered(this);
103            }
104        }
105    }
106
107    void PlayerInfo::createController()
108    {
109        this->controller_ = this->defaultController_.fabricate(this);
110        assert(this->controller_);
111        this->controller_->setPlayer(this);
112        if (this->controllableEntity_)
113            this->controller_->setControllableEntity(this->controllableEntity_);
114    }
115
116    void PlayerInfo::startControl(ControllableEntity* entity)
117    {
118        if (this->controllableEntity_)
119            this->stopControl(this->controllableEntity_);
120
121        this->controllableEntity_ = entity;
122
123        if (entity)
124        {
125            this->controllableEntityID_ = entity->getObjectID();
126            entity->setPlayer(this);
127            this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
128        }
129        else
130        {
131            this->controllableEntityID_ = OBJECTID_UNKNOWN;
132        }
133
134        if (this->controller_)
135            this->controller_->setControllableEntity(entity);
136    }
137
138    void PlayerInfo::stopControl(ControllableEntity* entity, bool callback)
139    {
140        if (entity && this->controllableEntity_ == entity)
141        {
142            this->controllableEntity_ = 0;
143            this->controllableEntityID_ = OBJECTID_UNKNOWN;
144
145            if (this->controller_)
146                this->controller_->setControllableEntity(0);
147
148            if (callback)
149                entity->removePlayer();
150        }
151    }
152
153    void PlayerInfo::networkcallback_changedcontrollableentityID()
154    {
155        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
156        {
157            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
158            ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp);
159
160            this->startControl(entity);
161        }
162        else
163        {
164            this->stopControl(this->controllableEntity_);
165        }
166    }
167}
Note: See TracBrowser for help on using the repository browser.