[1940] | 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 | |
---|
[1947] | 29 | #include "OrxonoxStableHeaders.h" |
---|
[1940] | 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" |
---|
[1949] | 37 | #include "core/Core.h" |
---|
[1940] | 38 | |
---|
| 39 | #include "network/Host.h" |
---|
| 40 | |
---|
| 41 | #include "GraphicsEngine.h" |
---|
| 42 | #include "objects/gametypes/Gametype.h" |
---|
[1989] | 43 | #include "objects/worldentities/ControllableEntity.h" |
---|
[1993] | 44 | #include "objects/controllers/HumanController.h" |
---|
[1940] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | CreateUnloadableFactory(PlayerInfo); |
---|
| 49 | |
---|
| 50 | PlayerInfo::PlayerInfo() |
---|
| 51 | { |
---|
| 52 | RegisterObject(PlayerInfo); |
---|
| 53 | |
---|
| 54 | this->ping_ = -1; |
---|
[1953] | 55 | this->clientID_ = network::CLIENTID_UNKNOWN; |
---|
[1940] | 56 | this->bLocalPlayer_ = false; |
---|
| 57 | this->bHumanPlayer_ = false; |
---|
| 58 | this->bFinishedSetup_ = false; |
---|
| 59 | |
---|
[1989] | 60 | this->pawn_ = 0; |
---|
| 61 | this->pawnID_ = network::OBJECTID_UNKNOWN; |
---|
[1993] | 62 | this->controller_ = 0; |
---|
| 63 | this->setDefaultController(Class(HumanController)); |
---|
[1989] | 64 | |
---|
[1940] | 65 | this->setConfigValues(); |
---|
| 66 | this->registerVariables(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | PlayerInfo::~PlayerInfo() |
---|
| 70 | { |
---|
[1993] | 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 | } |
---|
[1940] | 83 | } |
---|
| 84 | |
---|
| 85 | void PlayerInfo::setConfigValues() |
---|
| 86 | { |
---|
[1953] | 87 | SetConfigValue(nick_, "Player").callback(this, &PlayerInfo::checkNick); |
---|
[1940] | 88 | } |
---|
| 89 | |
---|
[1953] | 90 | void PlayerInfo::checkNick() |
---|
[1945] | 91 | { |
---|
[1953] | 92 | if (this->bLocalPlayer_) |
---|
| 93 | { |
---|
| 94 | this->playerName_ = this->nick_; |
---|
| 95 | |
---|
| 96 | if (Core::isMaster()) |
---|
| 97 | this->setName(this->playerName_); |
---|
| 98 | } |
---|
[1945] | 99 | } |
---|
| 100 | |
---|
[1946] | 101 | void PlayerInfo::changedName() |
---|
| 102 | { |
---|
[1950] | 103 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
| 104 | if (gametype) |
---|
| 105 | gametype->playerChangedName(this); |
---|
[1946] | 106 | } |
---|
| 107 | |
---|
[1940] | 108 | void PlayerInfo::registerVariables() |
---|
| 109 | { |
---|
[1946] | 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); |
---|
[1989] | 115 | REGISTERDATA(pawnID_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::updatePawn)); |
---|
[1946] | 116 | REGISTERDATA(bFinishedSetup_, network::direction::bidirectional, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::finishedSetup)); |
---|
| 117 | } |
---|
[1940] | 118 | |
---|
[1946] | 119 | void PlayerInfo::clientChangedName() |
---|
| 120 | { |
---|
| 121 | this->setName(this->playerName_); |
---|
[1940] | 122 | } |
---|
| 123 | |
---|
| 124 | void PlayerInfo::checkClientID() |
---|
| 125 | { |
---|
| 126 | this->bHumanPlayer_ = true; |
---|
| 127 | |
---|
| 128 | if (this->clientID_ == network::Host::getPlayerID()) |
---|
| 129 | { |
---|
[1993] | 130 | this->takeLocalControl(); |
---|
[1940] | 131 | |
---|
[1949] | 132 | if (Core::isClient()) |
---|
[1946] | 133 | this->setObjectMode(network::direction::bidirectional); |
---|
| 134 | else |
---|
| 135 | { |
---|
| 136 | this->clientChangedName(); |
---|
[1940] | 137 | this->bFinishedSetup_ = true; |
---|
| 138 | this->finishedSetup(); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void PlayerInfo::finishedSetup() |
---|
| 144 | { |
---|
[1949] | 145 | if (Core::isClient()) |
---|
[1940] | 146 | this->bFinishedSetup_ = true; |
---|
| 147 | else if (this->bFinishedSetup_) |
---|
| 148 | { |
---|
| 149 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
| 150 | if (gametype) |
---|
| 151 | gametype->addPlayer(this); |
---|
| 152 | } |
---|
| 153 | } |
---|
[1989] | 154 | |
---|
| 155 | void PlayerInfo::startControl(ControllableEntity* pawn) |
---|
| 156 | { |
---|
| 157 | pawn->setPlayer(this); |
---|
| 158 | this->pawn_ = pawn; |
---|
| 159 | this->pawnID_ = pawn->getObjectID(); |
---|
[1993] | 160 | |
---|
| 161 | if (this->controller_) |
---|
| 162 | this->controller_->setPawn(this->pawn_); |
---|
[1989] | 163 | } |
---|
| 164 | |
---|
| 165 | void PlayerInfo::stopControl() |
---|
| 166 | { |
---|
| 167 | this->pawn_->removePlayer(); |
---|
| 168 | this->pawn_ = 0; |
---|
| 169 | this->pawnID_ = network::OBJECTID_UNKNOWN; |
---|
| 170 | } |
---|
| 171 | |
---|
[1993] | 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 | |
---|
[1989] | 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 | } |
---|
[1940] | 191 | } |
---|