[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" |
---|
| 36 | #include "objects/controllers/HumanController.h" |
---|
| 37 | #include "objects/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_) |
---|
| 66 | delete this->humanHud_; |
---|
| 67 | |
---|
| 68 | if (this->gametypeHud_) |
---|
| 69 | delete this->gametypeHud_; |
---|
| 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()) |
---|
[3280] | 118 | this->setObjectMode(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()) |
---|
| 164 | if (this->getGametype()->getHUDTemplate() != "") |
---|
| 165 | this->setGametypeHUDTemplate(this->getGametype()->getHUDTemplate()); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | void HumanPlayer::updateHumanHUD() |
---|
| 169 | { |
---|
| 170 | if (this->humanHud_) |
---|
[2662] | 171 | { |
---|
[2826] | 172 | delete this->humanHud_; |
---|
| 173 | this->humanHud_ = 0; |
---|
| 174 | } |
---|
[2662] | 175 | |
---|
[2826] | 176 | if (this->isLocalPlayer() && this->humanHudTemplate_ != "") |
---|
| 177 | { |
---|
| 178 | this->humanHud_ = new OverlayGroup(this); |
---|
| 179 | this->humanHud_->addTemplate(this->humanHudTemplate_); |
---|
[2973] | 180 | this->humanHud_->setOwner(this); |
---|
[2662] | 181 | } |
---|
| 182 | } |
---|
[2826] | 183 | |
---|
| 184 | void HumanPlayer::updateGametypeHUD() |
---|
| 185 | { |
---|
| 186 | if (this->gametypeHud_) |
---|
| 187 | { |
---|
| 188 | delete this->gametypeHud_; |
---|
| 189 | this->gametypeHud_ = 0; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | if (this->isLocalPlayer() && this->gametypeHudTemplate_ != "") |
---|
| 193 | { |
---|
| 194 | this->gametypeHud_ = new OverlayGroup(this); |
---|
| 195 | this->gametypeHud_->addTemplate(this->gametypeHudTemplate_); |
---|
[2973] | 196 | this->gametypeHud_->setOwner(this); |
---|
[2826] | 197 | } |
---|
| 198 | } |
---|
[2072] | 199 | } |
---|