| [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 <cassert> |
|---|
| 30 | |
|---|
| 31 | #include "PlayerInfo.h" |
|---|
| 32 | |
|---|
| 33 | #include "core/CoreIncludes.h" |
|---|
| 34 | #include "network/ClientInformation.h" |
|---|
| 35 | #include "objects/gametypes/Gametype.h" |
|---|
| 36 | |
|---|
| 37 | namespace orxonox |
|---|
| 38 | { |
|---|
| 39 | PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator) |
|---|
| 40 | { |
|---|
| 41 | RegisterObject(PlayerInfo); |
|---|
| 42 | |
|---|
| [2171] | 43 | this->clientID_ = CLIENTID_UNKNOWN; |
|---|
| [2072] | 44 | this->bHumanPlayer_ = false; |
|---|
| 45 | this->bLocalPlayer_ = false; |
|---|
| 46 | this->bReadyToSpawn_ = false; |
|---|
| [2662] | 47 | this->bSetUnreadyAfterSpawn_ = true; |
|---|
| [2072] | 48 | this->controller_ = 0; |
|---|
| 49 | this->controllableEntity_ = 0; |
|---|
| [2171] | 50 | this->controllableEntityID_ = CLIENTID_UNKNOWN; |
|---|
| [2072] | 51 | |
|---|
| [2973] | 52 | this->gtinfo_ = 0; |
|---|
| 53 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
|---|
| 54 | this->updateGametypeInfo(); |
|---|
| 55 | |
|---|
| [2072] | 56 | this->registerVariables(); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | PlayerInfo::~PlayerInfo() |
|---|
| 60 | { |
|---|
| [2171] | 61 | if (this->BaseObject::isInitialized()) |
|---|
| [2072] | 62 | { |
|---|
| [3038] | 63 | this->stopControl(); |
|---|
| [2072] | 64 | |
|---|
| 65 | if (this->controller_) |
|---|
| 66 | { |
|---|
| 67 | delete this->controller_; |
|---|
| 68 | this->controller_ = 0; |
|---|
| 69 | } |
|---|
| [2662] | 70 | |
|---|
| 71 | if (this->getGametype()) |
|---|
| 72 | this->getGametype()->playerLeft(this); |
|---|
| [2072] | 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void PlayerInfo::registerVariables() |
|---|
| 77 | { |
|---|
| [2662] | 78 | registerVariable(this->name_, variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); |
|---|
| 79 | registerVariable(this->controllableEntityID_, variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID)); |
|---|
| 80 | registerVariable(this->bReadyToSpawn_, variableDirection::toserver); |
|---|
| [2973] | 81 | registerVariable(this->gtinfoID_, variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedgtinfoID)); |
|---|
| [2072] | 82 | } |
|---|
| 83 | |
|---|
| 84 | void PlayerInfo::changedName() |
|---|
| 85 | { |
|---|
| [2662] | 86 | SUPER(PlayerInfo, changedName); |
|---|
| 87 | |
|---|
| [2171] | 88 | if (this->isInitialized() && this->getGametype()) |
|---|
| [2072] | 89 | this->getGametype()->playerChangedName(this); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void PlayerInfo::changedGametype() |
|---|
| 93 | { |
|---|
| [2973] | 94 | this->updateGametypeInfo(); |
|---|
| 95 | |
|---|
| [2171] | 96 | if (this->isInitialized()) |
|---|
| [2072] | 97 | { |
|---|
| 98 | if (this->getOldGametype()) |
|---|
| 99 | { |
|---|
| 100 | if (this->getGametype()) |
|---|
| 101 | this->getOldGametype()->playerSwitched(this, this->getGametype()); |
|---|
| 102 | else |
|---|
| 103 | this->getOldGametype()->playerLeft(this); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | if (this->getGametype()) |
|---|
| 107 | { |
|---|
| 108 | if (this->getOldGametype()) |
|---|
| 109 | this->getGametype()->playerSwitchedBack(this, this->getOldGametype()); |
|---|
| 110 | else |
|---|
| 111 | this->getGametype()->playerEntered(this); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| [2973] | 116 | void PlayerInfo::updateGametypeInfo() |
|---|
| 117 | { |
|---|
| 118 | this->gtinfo_ = 0; |
|---|
| 119 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
|---|
| 120 | |
|---|
| 121 | if (this->getGametype() && this->getGametype()->getGametypeInfo()) |
|---|
| 122 | { |
|---|
| 123 | this->gtinfo_ = this->getGametype()->getGametypeInfo(); |
|---|
| 124 | this->gtinfoID_ = this->gtinfo_->getObjectID(); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| [2072] | 128 | void PlayerInfo::createController() |
|---|
| 129 | { |
|---|
| [2839] | 130 | if (this->controller_) |
|---|
| 131 | { |
|---|
| 132 | delete this->controller_; |
|---|
| 133 | this->controller_ = 0; |
|---|
| 134 | } |
|---|
| [2072] | 135 | this->controller_ = this->defaultController_.fabricate(this); |
|---|
| 136 | assert(this->controller_); |
|---|
| 137 | this->controller_->setPlayer(this); |
|---|
| 138 | if (this->controllableEntity_) |
|---|
| 139 | this->controller_->setControllableEntity(this->controllableEntity_); |
|---|
| [2662] | 140 | this->changedController(); |
|---|
| [2072] | 141 | } |
|---|
| 142 | |
|---|
| [3038] | 143 | void PlayerInfo::startControl(ControllableEntity* entity) |
|---|
| [2072] | 144 | { |
|---|
| [3038] | 145 | if (!entity || entity == this->controllableEntity_) |
|---|
| [2662] | 146 | return; |
|---|
| 147 | |
|---|
| [2072] | 148 | if (this->controllableEntity_) |
|---|
| [3038] | 149 | this->stopControl(); |
|---|
| [2072] | 150 | |
|---|
| 151 | this->controllableEntity_ = entity; |
|---|
| [3038] | 152 | this->controllableEntityID_ = entity->getObjectID(); |
|---|
| [2072] | 153 | |
|---|
| [3038] | 154 | entity->setPlayer(this); |
|---|
| [2072] | 155 | |
|---|
| [3038] | 156 | this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); |
|---|
| 157 | |
|---|
| [2072] | 158 | if (this->controller_) |
|---|
| 159 | this->controller_->setControllableEntity(entity); |
|---|
| [2826] | 160 | |
|---|
| 161 | this->changedControllableEntity(); |
|---|
| [2072] | 162 | } |
|---|
| 163 | |
|---|
| [3038] | 164 | void PlayerInfo::stopControl() |
|---|
| [2072] | 165 | { |
|---|
| [3038] | 166 | ControllableEntity* entity = this->controllableEntity_; |
|---|
| [2072] | 167 | |
|---|
| [3038] | 168 | if (!entity) |
|---|
| 169 | return; |
|---|
| [2072] | 170 | |
|---|
| [3038] | 171 | this->controllableEntity_ = 0; |
|---|
| 172 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
|---|
| [2826] | 173 | |
|---|
| [3038] | 174 | if (this->controller_) |
|---|
| 175 | this->controller_->setControllableEntity(0); |
|---|
| 176 | |
|---|
| 177 | entity->removePlayer(); |
|---|
| 178 | |
|---|
| 179 | this->changedControllableEntity(); |
|---|
| [2072] | 180 | } |
|---|
| 181 | |
|---|
| 182 | void PlayerInfo::networkcallback_changedcontrollableentityID() |
|---|
| 183 | { |
|---|
| [2171] | 184 | if (this->controllableEntityID_ != OBJECTID_UNKNOWN) |
|---|
| [2072] | 185 | { |
|---|
| 186 | Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); |
|---|
| 187 | ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp); |
|---|
| 188 | this->startControl(entity); |
|---|
| 189 | } |
|---|
| 190 | else |
|---|
| 191 | { |
|---|
| [3038] | 192 | this->stopControl(); |
|---|
| [2072] | 193 | } |
|---|
| 194 | } |
|---|
| [2973] | 195 | |
|---|
| 196 | void PlayerInfo::networkcallback_changedgtinfoID() |
|---|
| 197 | { |
|---|
| 198 | if (this->gtinfoID_ != OBJECTID_UNKNOWN) |
|---|
| 199 | { |
|---|
| 200 | this->gtinfo_ = dynamic_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_)); |
|---|
| 201 | |
|---|
| 202 | if (!this->gtinfo_) |
|---|
| 203 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| [2072] | 206 | } |
|---|