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 | |
---|
44 | namespace orxonox |
---|
45 | { |
---|
46 | CreateUnloadableFactory(PlayerInfo); |
---|
47 | |
---|
48 | PlayerInfo::PlayerInfo() |
---|
49 | { |
---|
50 | RegisterObject(PlayerInfo); |
---|
51 | |
---|
52 | this->ping_ = -1; |
---|
53 | this->clientID_ = (unsigned int)-1; |
---|
54 | this->bLocalPlayer_ = Core::isStandalone(); |
---|
55 | this->bLocalPlayer_ = false; |
---|
56 | this->bHumanPlayer_ = false; |
---|
57 | this->bFinishedSetup_ = false; |
---|
58 | |
---|
59 | this->setConfigValues(); |
---|
60 | this->registerVariables(); |
---|
61 | |
---|
62 | COUT(0) << "created PlayerInfo (" << this->getObjectID() << ")" << std::endl; |
---|
63 | } |
---|
64 | |
---|
65 | PlayerInfo::~PlayerInfo() |
---|
66 | { |
---|
67 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
68 | if (gametype) |
---|
69 | gametype->removePlayer(this); |
---|
70 | COUT(0) << "destroyed PlayerInfo (" << this->getObjectID() << ")" << std::endl; |
---|
71 | } |
---|
72 | |
---|
73 | void PlayerInfo::setConfigValues() |
---|
74 | { |
---|
75 | SetConfigValue(playerName_, "Player").callback(this, &PlayerInfo::checkName); |
---|
76 | } |
---|
77 | |
---|
78 | void PlayerInfo::checkName() |
---|
79 | { |
---|
80 | std::cout << "# PI(" << this->getObjectID() << "): checkName: " << this->bLocalPlayer_ << std::endl; |
---|
81 | if (this->bLocalPlayer_ && Core::isMaster()) |
---|
82 | this->setName(this->playerName_); |
---|
83 | } |
---|
84 | |
---|
85 | void PlayerInfo::changedName() |
---|
86 | { |
---|
87 | std::cout << "# PI(" << this->getObjectID() << "): changedName to " << this->getName() << std::endl; |
---|
88 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
89 | if (gametype) |
---|
90 | gametype->playerChangedName(this); |
---|
91 | } |
---|
92 | |
---|
93 | void PlayerInfo::registerVariables() |
---|
94 | { |
---|
95 | REGISTERSTRING(name_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); |
---|
96 | REGISTERSTRING(playerName_, network::direction::toserver, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::clientChangedName)); |
---|
97 | REGISTERDATA(clientID_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::checkClientID)); |
---|
98 | REGISTERDATA(ping_, network::direction::toclient); |
---|
99 | REGISTERDATA(bHumanPlayer_, network::direction::toclient); |
---|
100 | REGISTERDATA(bFinishedSetup_, network::direction::bidirectional, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::finishedSetup)); |
---|
101 | } |
---|
102 | |
---|
103 | void PlayerInfo::clientChangedName() |
---|
104 | { |
---|
105 | std::cout << "# PI(" << this->getObjectID() << "): clientChangedName() to " << this->playerName_ << std::endl; |
---|
106 | this->setName(this->playerName_); |
---|
107 | } |
---|
108 | |
---|
109 | void PlayerInfo::checkClientID() |
---|
110 | { |
---|
111 | std::cout << "# PI(" << this->getObjectID() << "): checkClientID()" << std::endl; |
---|
112 | this->bHumanPlayer_ = true; |
---|
113 | |
---|
114 | if (this->clientID_ == network::Host::getPlayerID()) |
---|
115 | { |
---|
116 | std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): it's the client's ID" << std::endl; |
---|
117 | this->bLocalPlayer_ = true; |
---|
118 | //std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): name: " << this->getName() << std::endl; |
---|
119 | |
---|
120 | if (Core::isClient()) |
---|
121 | { |
---|
122 | std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): we're on a client: set object mode to bidirectional" << std::endl; |
---|
123 | this->setObjectMode(network::direction::bidirectional); |
---|
124 | this->playerName_ += "blub"; |
---|
125 | std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): proposed name: " << this->playerName_ << std::endl; |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): we're not on a client: finish setup" << std::endl; |
---|
130 | this->clientChangedName(); |
---|
131 | this->bFinishedSetup_ = true; |
---|
132 | this->finishedSetup(); |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | void PlayerInfo::finishedSetup() |
---|
138 | { |
---|
139 | std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): " << this->bFinishedSetup_ << std::endl; |
---|
140 | if (Core::isClient()) |
---|
141 | { |
---|
142 | std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): we're a client: finish setup" << std::endl; |
---|
143 | this->bFinishedSetup_ = true; |
---|
144 | } |
---|
145 | else if (this->bFinishedSetup_) |
---|
146 | { |
---|
147 | std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): we're a server: add player" << std::endl; |
---|
148 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
149 | if (gametype) |
---|
150 | gametype->addPlayer(this); |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): we're a server: client not yet finished" << std::endl; |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|