[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 "OrxonoxStableHeaders.h" |
---|
| 30 | #include "HumanPlayer.h" |
---|
| 31 | |
---|
| 32 | #include "core/Core.h" |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "core/ConfigValueIncludes.h" |
---|
| 35 | #include "network/ClientInformation.h" |
---|
| 36 | #include "network/Host.h" |
---|
| 37 | #include "objects/controllers/HumanController.h" |
---|
| 38 | #include "objects/gametypes/Gametype.h" |
---|
[2662] | 39 | #include "overlays/OverlayGroup.h" |
---|
[2072] | 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
| 43 | CreateUnloadableFactory(HumanPlayer); |
---|
| 44 | |
---|
| 45 | HumanPlayer::HumanPlayer(BaseObject* creator) : PlayerInfo(creator) |
---|
| 46 | { |
---|
| 47 | RegisterObject(HumanPlayer); |
---|
| 48 | |
---|
[2171] | 49 | this->server_initialized_ = Core::isMaster(); |
---|
| 50 | this->client_initialized_ = false; |
---|
[2072] | 51 | |
---|
| 52 | this->bHumanPlayer_ = true; |
---|
| 53 | this->defaultController_ = Class(HumanController); |
---|
| 54 | |
---|
[2789] | 55 | this->humanHud_ = 0; |
---|
| 56 | |
---|
[2072] | 57 | this->setConfigValues(); |
---|
| 58 | this->registerVariables(); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | HumanPlayer::~HumanPlayer() |
---|
| 62 | { |
---|
[2806] | 63 | if (this->BaseObject::isInitialized() && this->humanHud_) |
---|
[2789] | 64 | delete this->humanHud_; |
---|
[2072] | 65 | } |
---|
| 66 | |
---|
| 67 | void HumanPlayer::setConfigValues() |
---|
| 68 | { |
---|
| 69 | SetConfigValue(nick_, "Player").callback(this, &HumanPlayer::configvaluecallback_changednick); |
---|
[2662] | 70 | SetConfigValue(hudtemplate_, "defaultHUD").callback(this, &HumanPlayer::configvaluecallback_changedHUDTemplate); |
---|
[2072] | 71 | } |
---|
| 72 | |
---|
| 73 | void HumanPlayer::registerVariables() |
---|
| 74 | { |
---|
[2662] | 75 | registerVariable(this->synchronize_nick_, variableDirection::toserver, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_changednick)); |
---|
[2072] | 76 | |
---|
[2662] | 77 | registerVariable(this->clientID_, variableDirection::toclient, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_clientIDchanged)); |
---|
| 78 | registerVariable(this->server_initialized_, variableDirection::toclient, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_server_initialized)); |
---|
| 79 | registerVariable(this->client_initialized_, variableDirection::toserver, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_client_initialized)); |
---|
[2072] | 80 | } |
---|
| 81 | |
---|
| 82 | void HumanPlayer::configvaluecallback_changednick() |
---|
| 83 | { |
---|
| 84 | if (this->isLocalPlayer()) |
---|
| 85 | { |
---|
| 86 | this->synchronize_nick_ = this->nick_; |
---|
| 87 | |
---|
| 88 | if (Core::isMaster()) |
---|
| 89 | this->setName(this->nick_); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
[2662] | 93 | void HumanPlayer::configvaluecallback_changedHUDTemplate() |
---|
| 94 | { |
---|
[2789] | 95 | this->setHumanHUDTemplate(this->hudtemplate_); |
---|
[2662] | 96 | } |
---|
| 97 | |
---|
[2072] | 98 | void HumanPlayer::networkcallback_changednick() |
---|
| 99 | { |
---|
| 100 | this->setName(this->synchronize_nick_); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void HumanPlayer::networkcallback_clientIDchanged() |
---|
| 104 | { |
---|
[2171] | 105 | if (this->clientID_ == Host::getPlayerID()) |
---|
[2072] | 106 | { |
---|
| 107 | this->bLocalPlayer_ = true; |
---|
| 108 | this->synchronize_nick_ = this->nick_; |
---|
[2171] | 109 | this->client_initialized_ = true; |
---|
[2072] | 110 | |
---|
| 111 | if (!Core::isMaster()) |
---|
[2662] | 112 | this->setObjectMode(objectDirection::bidirectional); |
---|
[2072] | 113 | else |
---|
| 114 | this->setName(this->nick_); |
---|
| 115 | |
---|
| 116 | this->createController(); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
[2171] | 120 | void HumanPlayer::networkcallback_server_initialized() |
---|
[2072] | 121 | { |
---|
[2171] | 122 | this->client_initialized_ = true; |
---|
[2072] | 123 | } |
---|
| 124 | |
---|
[2171] | 125 | void HumanPlayer::networkcallback_client_initialized() |
---|
[2072] | 126 | { |
---|
| 127 | if (this->getGametype()) |
---|
| 128 | this->getGametype()->playerEntered(this); |
---|
| 129 | } |
---|
| 130 | |
---|
[2171] | 131 | bool HumanPlayer::isInitialized() const |
---|
[2072] | 132 | { |
---|
[2171] | 133 | return (this->server_initialized_ && this->client_initialized_); |
---|
[2072] | 134 | } |
---|
| 135 | |
---|
| 136 | float HumanPlayer::getPing() const |
---|
| 137 | { |
---|
[2171] | 138 | return ClientInformation::findClient(this->getClientID())->getRTT(); |
---|
[2072] | 139 | } |
---|
| 140 | |
---|
| 141 | float HumanPlayer::getPacketLossRatio() const |
---|
| 142 | { |
---|
[2171] | 143 | return ClientInformation::findClient(this->getClientID())->getPacketLoss(); |
---|
[2072] | 144 | } |
---|
| 145 | |
---|
| 146 | void HumanPlayer::setClientID(unsigned int clientID) |
---|
| 147 | { |
---|
| 148 | this->clientID_ = clientID; |
---|
| 149 | this->networkcallback_clientIDchanged(); |
---|
| 150 | } |
---|
[2662] | 151 | |
---|
[2789] | 152 | void HumanPlayer::changedControllableEntity() |
---|
[2662] | 153 | { |
---|
[2789] | 154 | PlayerInfo::changedControllableEntity(); |
---|
| 155 | |
---|
| 156 | if (this->humanHud_) |
---|
| 157 | this->humanHud_->setOwner(this->getControllableEntity()); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | void HumanPlayer::updateHumanHUD() |
---|
| 161 | { |
---|
| 162 | if (this->humanHud_) |
---|
[2662] | 163 | { |
---|
[2789] | 164 | delete this->humanHud_; |
---|
| 165 | this->humanHud_ = 0; |
---|
| 166 | } |
---|
[2662] | 167 | |
---|
[2789] | 168 | if (this->humanHudTemplate_ != "") |
---|
| 169 | { |
---|
| 170 | this->humanHud_ = new OverlayGroup(this); |
---|
| 171 | this->humanHud_->addTemplate(this->humanHudTemplate_); |
---|
| 172 | this->humanHud_->setOwner(this->getControllableEntity()); |
---|
[2662] | 173 | } |
---|
| 174 | } |
---|
[2072] | 175 | } |
---|