[2072] | 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 "HumanPlayer.h" |
---|
| 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
| 32 | #include "core/ConfigValueIncludes.h" |
---|
[3196] | 33 | #include "core/GameMode.h" |
---|
[2072] | 34 | #include "network/ClientInformation.h" |
---|
| 35 | #include "network/Host.h" |
---|
[5735] | 36 | #include "controllers/HumanController.h" |
---|
| 37 | #include "gametypes/Gametype.h" |
---|
[2662] | 38 | #include "overlays/OverlayGroup.h" |
---|
[2072] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | CreateUnloadableFactory(HumanPlayer); |
---|
| 43 | |
---|
| 44 | HumanPlayer::HumanPlayer(BaseObject* creator) : PlayerInfo(creator) |
---|
| 45 | { |
---|
| 46 | RegisterObject(HumanPlayer); |
---|
| 47 | |
---|
[2896] | 48 | this->server_initialized_ = GameMode::isMaster(); |
---|
[2171] | 49 | this->client_initialized_ = false; |
---|
[2072] | 50 | |
---|
| 51 | this->bHumanPlayer_ = true; |
---|
| 52 | this->defaultController_ = Class(HumanController); |
---|
| 53 | |
---|
[2826] | 54 | this->humanHud_ = 0; |
---|
| 55 | this->gametypeHud_ = 0; |
---|
| 56 | |
---|
[2072] | 57 | this->setConfigValues(); |
---|
| 58 | this->registerVariables(); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | HumanPlayer::~HumanPlayer() |
---|
| 62 | { |
---|
[2826] | 63 | if (this->BaseObject::isInitialized()) |
---|
| 64 | { |
---|
| 65 | if (this->humanHud_) |
---|
[5929] | 66 | this->humanHud_->destroy(); |
---|
[2826] | 67 | |
---|
| 68 | if (this->gametypeHud_) |
---|
[5929] | 69 | this->gametypeHud_->destroy(); |
---|
[2826] | 70 | } |
---|
[2072] | 71 | } |
---|
| 72 | |
---|
| 73 | void HumanPlayer::setConfigValues() |
---|
| 74 | { |
---|
| 75 | SetConfigValue(nick_, "Player").callback(this, &HumanPlayer::configvaluecallback_changednick); |
---|
[2662] | 76 | SetConfigValue(hudtemplate_, "defaultHUD").callback(this, &HumanPlayer::configvaluecallback_changedHUDTemplate); |
---|
[2072] | 77 | } |
---|
| 78 | |
---|
| 79 | void HumanPlayer::registerVariables() |
---|
| 80 | { |
---|
[3280] | 81 | registerVariable(this->synchronize_nick_, VariableDirection::ToServer, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_changednick)); |
---|
[2072] | 82 | |
---|
[3280] | 83 | registerVariable(this->clientID_, VariableDirection::ToClient, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_clientIDchanged)); |
---|
| 84 | registerVariable(this->server_initialized_, VariableDirection::ToClient, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_server_initialized)); |
---|
| 85 | registerVariable(this->client_initialized_, VariableDirection::ToServer, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_client_initialized)); |
---|
[2072] | 86 | } |
---|
| 87 | |
---|
| 88 | void HumanPlayer::configvaluecallback_changednick() |
---|
| 89 | { |
---|
| 90 | if (this->isLocalPlayer()) |
---|
| 91 | { |
---|
| 92 | this->synchronize_nick_ = this->nick_; |
---|
| 93 | |
---|
[2896] | 94 | if (GameMode::isMaster()) |
---|
[2072] | 95 | this->setName(this->nick_); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
[2662] | 99 | void HumanPlayer::configvaluecallback_changedHUDTemplate() |
---|
| 100 | { |
---|
[2826] | 101 | this->setHumanHUDTemplate(this->hudtemplate_); |
---|
[2662] | 102 | } |
---|
| 103 | |
---|
[2072] | 104 | void HumanPlayer::networkcallback_changednick() |
---|
| 105 | { |
---|
| 106 | this->setName(this->synchronize_nick_); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | void HumanPlayer::networkcallback_clientIDchanged() |
---|
| 110 | { |
---|
[2171] | 111 | if (this->clientID_ == Host::getPlayerID()) |
---|
[2072] | 112 | { |
---|
| 113 | this->bLocalPlayer_ = true; |
---|
| 114 | this->synchronize_nick_ = this->nick_; |
---|
[2171] | 115 | this->client_initialized_ = true; |
---|
[2072] | 116 | |
---|
[2896] | 117 | if (!GameMode::isMaster()) |
---|
[5929] | 118 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
[2072] | 119 | else |
---|
| 120 | this->setName(this->nick_); |
---|
| 121 | |
---|
| 122 | this->createController(); |
---|
[2826] | 123 | this->updateHumanHUD(); |
---|
[2072] | 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
[2171] | 127 | void HumanPlayer::networkcallback_server_initialized() |
---|
[2072] | 128 | { |
---|
[2171] | 129 | this->client_initialized_ = true; |
---|
[2072] | 130 | } |
---|
| 131 | |
---|
[2171] | 132 | void HumanPlayer::networkcallback_client_initialized() |
---|
[2072] | 133 | { |
---|
| 134 | if (this->getGametype()) |
---|
| 135 | this->getGametype()->playerEntered(this); |
---|
| 136 | } |
---|
| 137 | |
---|
[2171] | 138 | bool HumanPlayer::isInitialized() const |
---|
[2072] | 139 | { |
---|
[2171] | 140 | return (this->server_initialized_ && this->client_initialized_); |
---|
[2072] | 141 | } |
---|
| 142 | |
---|
| 143 | float HumanPlayer::getPing() const |
---|
| 144 | { |
---|
[3196] | 145 | return static_cast<float>(ClientInformation::findClient(this->getClientID())->getRTT()); |
---|
[2072] | 146 | } |
---|
| 147 | |
---|
| 148 | float HumanPlayer::getPacketLossRatio() const |
---|
| 149 | { |
---|
[3196] | 150 | return static_cast<float>(ClientInformation::findClient(this->getClientID())->getPacketLoss()); |
---|
[2072] | 151 | } |
---|
| 152 | |
---|
| 153 | void HumanPlayer::setClientID(unsigned int clientID) |
---|
| 154 | { |
---|
| 155 | this->clientID_ = clientID; |
---|
| 156 | this->networkcallback_clientIDchanged(); |
---|
| 157 | } |
---|
[2662] | 158 | |
---|
[2826] | 159 | void HumanPlayer::changedGametype() |
---|
[2662] | 160 | { |
---|
[2826] | 161 | PlayerInfo::changedGametype(); |
---|
| 162 | |
---|
| 163 | if (this->isInitialized() && this->isLocalPlayer()) |
---|
[5929] | 164 | { |
---|
| 165 | if (this->getGametype() && this->getGametype()->getHUDTemplate() != "") |
---|
[2826] | 166 | this->setGametypeHUDTemplate(this->getGametype()->getHUDTemplate()); |
---|
[5929] | 167 | else |
---|
| 168 | this->setGametypeHUDTemplate(""); |
---|
| 169 | } |
---|
[2826] | 170 | } |
---|
| 171 | |
---|
| 172 | void HumanPlayer::updateHumanHUD() |
---|
| 173 | { |
---|
| 174 | if (this->humanHud_) |
---|
[2662] | 175 | { |
---|
[5929] | 176 | this->humanHud_->destroy(); |
---|
[2826] | 177 | this->humanHud_ = 0; |
---|
| 178 | } |
---|
[2662] | 179 | |
---|
[5929] | 180 | if (this->isLocalPlayer() && this->humanHudTemplate_ != "" && GameMode::showsGraphics()) |
---|
[2826] | 181 | { |
---|
| 182 | this->humanHud_ = new OverlayGroup(this); |
---|
| 183 | this->humanHud_->addTemplate(this->humanHudTemplate_); |
---|
[2973] | 184 | this->humanHud_->setOwner(this); |
---|
[2662] | 185 | } |
---|
| 186 | } |
---|
[2826] | 187 | |
---|
| 188 | void HumanPlayer::updateGametypeHUD() |
---|
| 189 | { |
---|
| 190 | if (this->gametypeHud_) |
---|
| 191 | { |
---|
[5929] | 192 | this->gametypeHud_->destroy(); |
---|
[2826] | 193 | this->gametypeHud_ = 0; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | if (this->isLocalPlayer() && this->gametypeHudTemplate_ != "") |
---|
| 197 | { |
---|
| 198 | this->gametypeHud_ = new OverlayGroup(this); |
---|
| 199 | this->gametypeHud_->addTemplate(this->gametypeHudTemplate_); |
---|
[2973] | 200 | this->gametypeHud_->setOwner(this); |
---|
[2826] | 201 | } |
---|
| 202 | } |
---|
[2072] | 203 | } |
---|