Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/infos/PlayerInfo.cc @ 2428

Last change on this file since 2428 was 2428, checked in by landauf, 15 years ago
  • Moved some variables from Gametype to GametypeInfo (Gametype exists only on the Server while GametypeInfo is synchronized)
  • Created a new HUD-element, GametypeStatus, based on GametypeInfo (the same effect was formerly achieved by using a hack in Spectator and a TextOverlay)
  • HumanController creates a HUD (additionally to the SpaceShips HUD) which includes GametypeStatus and ChatOverlay and even more in the future
  • Property svn:eol-style set to native
File size: 5.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 <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        this->changedController();
115    }
116
117    void PlayerInfo::startControl(ControllableEntity* entity)
118    {
119        if (entity == this->controllableEntity_)
120            return;
121
122        if (this->controllableEntity_)
123            this->stopControl(this->controllableEntity_);
124
125        this->controllableEntity_ = entity;
126
127        if (entity)
128        {
129            this->controllableEntityID_ = entity->getObjectID();
130            entity->setPlayer(this);
131            this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
132        }
133        else
134        {
135            this->controllableEntityID_ = OBJECTID_UNKNOWN;
136        }
137
138        if (this->controller_)
139            this->controller_->setControllableEntity(entity);
140    }
141
142    void PlayerInfo::stopControl(ControllableEntity* entity, bool callback)
143    {
144        if (entity && this->controllableEntity_ == entity)
145        {
146            this->controllableEntity_ = 0;
147            this->controllableEntityID_ = OBJECTID_UNKNOWN;
148
149            if (this->controller_)
150                this->controller_->setControllableEntity(0);
151
152            if (callback)
153                entity->removePlayer();
154        }
155    }
156
157    void PlayerInfo::networkcallback_changedcontrollableentityID()
158    {
159        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
160        {
161            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
162            ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp);
163
164            this->startControl(entity);
165        }
166        else
167        {
168            this->stopControl(this->controllableEntity_);
169        }
170    }
171}
Note: See TracBrowser for help on using the repository browser.