Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added several new classes

File size: 5.8 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
41#include "GraphicsEngine.h"
42#include "objects/gametypes/Gametype.h"
43#include "objects/worldentities/ControllableEntity.h"
44#include "objects/controllers/HumanController.h"
45
46namespace orxonox
47{
48    CreateUnloadableFactory(PlayerInfo);
49
50    PlayerInfo::PlayerInfo()
51    {
52        RegisterObject(PlayerInfo);
53
54        this->ping_ = -1;
55        this->clientID_ = network::CLIENTID_UNKNOWN;
56        this->bLocalPlayer_ = false;
57        this->bHumanPlayer_ = false;
58        this->bFinishedSetup_ = false;
59
60        this->pawn_ = 0;
61        this->pawnID_ = network::OBJECTID_UNKNOWN;
62        this->controller_ = 0;
63        this->setDefaultController(Class(HumanController));
64
65        this->setConfigValues();
66        this->registerVariables();
67    }
68
69    PlayerInfo::~PlayerInfo()
70    {
71        if (this->isInitialized())
72        {
73            Gametype* gametype = Gametype::getCurrentGametype();
74            if (gametype)
75                gametype->removePlayer(this);
76
77            if (this->controller_)
78                delete this->controller_;
79
80            if (this->pawn_)
81                this->pawn_->removePlayer();
82        }
83    }
84
85    void PlayerInfo::setConfigValues()
86    {
87        SetConfigValue(nick_, "Player").callback(this, &PlayerInfo::checkNick);
88    }
89
90    void PlayerInfo::checkNick()
91    {
92        if (this->bLocalPlayer_)
93        {
94            this->playerName_ = this->nick_;
95
96            if (Core::isMaster())
97                this->setName(this->playerName_);
98        }
99    }
100
101    void PlayerInfo::changedName()
102    {
103        Gametype* gametype = Gametype::getCurrentGametype();
104        if (gametype)
105            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            Gametype* gametype = Gametype::getCurrentGametype();
150            if (gametype)
151                gametype->addPlayer(this);
152        }
153    }
154
155    void PlayerInfo::startControl(ControllableEntity* pawn)
156    {
157        pawn->setPlayer(this);
158        this->pawn_ = pawn;
159        this->pawnID_ = pawn->getObjectID();
160
161        if (this->controller_)
162            this->controller_->setPawn(this->pawn_);
163    }
164
165    void PlayerInfo::stopControl()
166    {
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.