Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/infos/PlayerInfo.cc @ 2006

Last change on this file since 2006 was 2006, checked in by landauf, 16 years ago

many changes, can't remember everything, but these are the most important:

  • attaching entities to other entities works
  • displaying models, lights and shadows works
  • controlling a spectator works
  • removed an update hack in PositionableEntity because I've found a much better solution

and with "works" I mean: it works on client, server and standalone

File size: 5.7 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 "PlayerInfo.h"
31
32#include <OgreSceneManager.h>
33
34#include "core/CoreIncludes.h"
35#include "core/ConfigValueIncludes.h"
36#include "core/XMLPort.h"
37#include "core/Core.h"
38
39#include "network/Host.h"
40#include "network/ClientInformation.h"
41
42#include "GraphicsEngine.h"
43#include "objects/gametypes/Gametype.h"
44#include "objects/worldentities/ControllableEntity.h"
45#include "objects/controllers/HumanController.h"
46
47namespace orxonox
48{
49    CreateUnloadableFactory(PlayerInfo);
50
51    PlayerInfo::PlayerInfo()
52    {
53        RegisterObject(PlayerInfo);
54
55        this->ping_ = -1;
56        this->clientID_ = network::CLIENTID_UNKNOWN;
57        this->bLocalPlayer_ = false;
58        this->bHumanPlayer_ = false;
59        this->bFinishedSetup_ = false;
60        this->gametype_ = 0;
61
62        this->pawn_ = 0;
63        this->pawnID_ = network::OBJECTID_UNKNOWN;
64        this->controller_ = 0;
65        this->setDefaultController(Class(HumanController));
66
67        this->setConfigValues();
68        this->registerVariables();
69    }
70
71    PlayerInfo::~PlayerInfo()
72    {
73        if (this->isInitialized())
74        {
75            if (this->gametype_)
76                this->gametype_->removePlayer(this);
77
78            if (this->controller_)
79                delete this->controller_;
80
81            if (this->pawn_)
82                this->pawn_->removePlayer();
83        }
84    }
85
86    void PlayerInfo::setConfigValues()
87    {
88        SetConfigValue(nick_, "Player").callback(this, &PlayerInfo::checkNick);
89    }
90
91    void PlayerInfo::checkNick()
92    {
93        if (this->bLocalPlayer_)
94        {
95            this->playerName_ = this->nick_;
96
97            if (Core::isMaster())
98                this->setName(this->playerName_);
99        }
100    }
101
102    void PlayerInfo::changedName()
103    {
104        if (this->gametype_)
105            this->gametype_->playerChangedName(this);
106    }
107
108    void PlayerInfo::registerVariables()
109    {
110        REGISTERSTRING(name_,         network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
111        REGISTERSTRING(playerName_,   network::direction::toserver, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::clientChangedName));
112        REGISTERDATA(clientID_,       network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::checkClientID));
113        REGISTERDATA(ping_,           network::direction::toclient);
114        REGISTERDATA(bHumanPlayer_,   network::direction::toclient);
115        REGISTERDATA(pawnID_,         network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::updatePawn));
116        REGISTERDATA(bFinishedSetup_, network::direction::bidirectional, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::finishedSetup));
117    }
118
119    void PlayerInfo::clientChangedName()
120    {
121        this->setName(this->playerName_);
122    }
123
124    void PlayerInfo::checkClientID()
125    {
126        this->bHumanPlayer_ = true;
127
128        if (this->clientID_ == network::Host::getPlayerID())
129        {
130            this->takeLocalControl();
131
132            if (Core::isClient())
133                this->setObjectMode(network::direction::bidirectional);
134            else
135            {
136                this->clientChangedName();
137                this->bFinishedSetup_ = true;
138                this->finishedSetup();
139            }
140        }
141    }
142
143    void PlayerInfo::finishedSetup()
144    {
145        if (Core::isClient())
146            this->bFinishedSetup_ = true;
147        else if (this->bFinishedSetup_)
148        {
149            if (this->gametype_)
150                this->gametype_->addPlayer(this);
151        }
152    }
153
154    void PlayerInfo::startControl(ControllableEntity* pawn)
155    {
156        pawn->setPlayer(this);
157        this->pawn_ = pawn;
158        this->pawnID_ = pawn->getObjectID();
159
160        if (this->controller_)
161            this->controller_->setPawn(this->pawn_);
162    }
163
164    void PlayerInfo::stopControl()
165    {
166        if (this->pawn_)
167            this->pawn_->removePlayer();
168        this->pawn_ = 0;
169        this->pawnID_ = network::OBJECTID_UNKNOWN;
170    }
171
172    void PlayerInfo::takeLocalControl()
173    {
174        this->bLocalPlayer_ = true;
175        this->playerName_ = this->nick_;
176        this->createController();
177    }
178
179    void PlayerInfo::createController()
180    {
181        this->controller_ = this->defaultController_.fabricate();
182        this->controller_->setPawn(this->pawn_);
183    }
184
185    void PlayerInfo::updatePawn()
186    {
187        this->pawn_ = dynamic_cast<ControllableEntity*>(network::Synchronisable::getSynchronisable(this->pawnID_));
188        if (this->pawn_ && (this->pawn_->getPlayer() != this))
189            this->pawn_->setPlayer(this);
190    }
191}
Note: See TracBrowser for help on using the repository browser.