[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" |
---|
[5735] | 35 | #include "gametypes/Gametype.h" |
---|
| 36 | #include "worldentities/ControllableEntity.h" |
---|
[5929] | 37 | #include "controllers/Controller.h" |
---|
[2072] | 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator) |
---|
| 42 | { |
---|
| 43 | RegisterObject(PlayerInfo); |
---|
| 44 | |
---|
[2171] | 45 | this->clientID_ = CLIENTID_UNKNOWN; |
---|
[2072] | 46 | this->bHumanPlayer_ = false; |
---|
| 47 | this->bLocalPlayer_ = false; |
---|
| 48 | this->bReadyToSpawn_ = false; |
---|
[2662] | 49 | this->bSetUnreadyAfterSpawn_ = true; |
---|
[2072] | 50 | this->controller_ = 0; |
---|
| 51 | this->controllableEntity_ = 0; |
---|
[6087] | 52 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[6101] | 53 | this->oldControllableEntity_ = 0; |
---|
[2072] | 54 | |
---|
[2973] | 55 | this->gtinfo_ = 0; |
---|
| 56 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 57 | this->updateGametypeInfo(); |
---|
| 58 | |
---|
[2072] | 59 | this->registerVariables(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | PlayerInfo::~PlayerInfo() |
---|
| 63 | { |
---|
[2171] | 64 | if (this->BaseObject::isInitialized()) |
---|
[2072] | 65 | { |
---|
[3038] | 66 | this->stopControl(); |
---|
[2072] | 67 | |
---|
| 68 | if (this->controller_) |
---|
| 69 | { |
---|
[5929] | 70 | this->controller_->destroy(); |
---|
[2072] | 71 | this->controller_ = 0; |
---|
| 72 | } |
---|
[2662] | 73 | |
---|
| 74 | if (this->getGametype()) |
---|
| 75 | this->getGametype()->playerLeft(this); |
---|
[2072] | 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void PlayerInfo::registerVariables() |
---|
| 80 | { |
---|
[3280] | 81 | registerVariable(this->name_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); |
---|
| 82 | registerVariable(this->controllableEntityID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID)); |
---|
| 83 | registerVariable(this->gtinfoID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedgtinfoID)); |
---|
[2072] | 84 | } |
---|
| 85 | |
---|
| 86 | void PlayerInfo::changedName() |
---|
| 87 | { |
---|
[2662] | 88 | SUPER(PlayerInfo, changedName); |
---|
| 89 | |
---|
[2171] | 90 | if (this->isInitialized() && this->getGametype()) |
---|
[2072] | 91 | this->getGametype()->playerChangedName(this); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void PlayerInfo::changedGametype() |
---|
| 95 | { |
---|
[2973] | 96 | this->updateGametypeInfo(); |
---|
| 97 | |
---|
[2171] | 98 | if (this->isInitialized()) |
---|
[2072] | 99 | { |
---|
| 100 | if (this->getOldGametype()) |
---|
| 101 | { |
---|
| 102 | if (this->getGametype()) |
---|
| 103 | this->getOldGametype()->playerSwitched(this, this->getGametype()); |
---|
| 104 | else |
---|
| 105 | this->getOldGametype()->playerLeft(this); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | if (this->getGametype()) |
---|
| 109 | { |
---|
| 110 | if (this->getOldGametype()) |
---|
| 111 | this->getGametype()->playerSwitchedBack(this, this->getOldGametype()); |
---|
| 112 | else |
---|
| 113 | this->getGametype()->playerEntered(this); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[2973] | 118 | void PlayerInfo::updateGametypeInfo() |
---|
| 119 | { |
---|
| 120 | this->gtinfo_ = 0; |
---|
| 121 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 122 | |
---|
| 123 | if (this->getGametype() && this->getGametype()->getGametypeInfo()) |
---|
| 124 | { |
---|
| 125 | this->gtinfo_ = this->getGametype()->getGametypeInfo(); |
---|
| 126 | this->gtinfoID_ = this->gtinfo_->getObjectID(); |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
[2072] | 130 | void PlayerInfo::createController() |
---|
| 131 | { |
---|
[2839] | 132 | if (this->controller_) |
---|
| 133 | { |
---|
[5929] | 134 | this->controller_->destroy(); |
---|
[2839] | 135 | this->controller_ = 0; |
---|
| 136 | } |
---|
[2072] | 137 | this->controller_ = this->defaultController_.fabricate(this); |
---|
| 138 | assert(this->controller_); |
---|
| 139 | this->controller_->setPlayer(this); |
---|
| 140 | if (this->controllableEntity_) |
---|
| 141 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
[2662] | 142 | this->changedController(); |
---|
[2072] | 143 | } |
---|
| 144 | |
---|
[3038] | 145 | void PlayerInfo::startControl(ControllableEntity* entity) |
---|
[2072] | 146 | { |
---|
[3038] | 147 | if (!entity || entity == this->controllableEntity_) |
---|
[2662] | 148 | return; |
---|
| 149 | |
---|
[6101] | 150 | if (this->oldControllableEntity_) |
---|
| 151 | this->stopTemporaryControl(); |
---|
[2072] | 152 | if (this->controllableEntity_) |
---|
[3038] | 153 | this->stopControl(); |
---|
[2072] | 154 | |
---|
| 155 | this->controllableEntity_ = entity; |
---|
[3038] | 156 | this->controllableEntityID_ = entity->getObjectID(); |
---|
[2072] | 157 | |
---|
[3038] | 158 | entity->setPlayer(this); |
---|
[2072] | 159 | |
---|
[3038] | 160 | this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); |
---|
| 161 | |
---|
[2072] | 162 | if (this->controller_) |
---|
| 163 | this->controller_->setControllableEntity(entity); |
---|
[2826] | 164 | |
---|
| 165 | this->changedControllableEntity(); |
---|
[2072] | 166 | } |
---|
[6082] | 167 | |
---|
| 168 | void PlayerInfo::startTemporaryControl(ControllableEntity* entity) |
---|
| 169 | { |
---|
[6087] | 170 | if (!entity) |
---|
[6082] | 171 | return; |
---|
[6087] | 172 | |
---|
[6101] | 173 | // assert( this->temporaryControllableEntity_==0 ); |
---|
[2072] | 174 | |
---|
[6101] | 175 | this->oldControllableEntity_ = this->controllableEntity_; |
---|
| 176 | this->controllableEntity_ = entity; |
---|
| 177 | this->controllableEntityID_ = entity->getObjectID(); |
---|
[6082] | 178 | |
---|
| 179 | entity->setPlayer(this); |
---|
| 180 | |
---|
| 181 | if (this->controller_) |
---|
| 182 | this->controller_->setControllableEntity(entity); |
---|
| 183 | |
---|
| 184 | this->changedControllableEntity(); |
---|
| 185 | } |
---|
| 186 | |
---|
[3038] | 187 | void PlayerInfo::stopControl() |
---|
[2072] | 188 | { |
---|
[6101] | 189 | if ( this->oldControllableEntity_ ) |
---|
| 190 | this->stopTemporaryControl(); |
---|
| 191 | |
---|
[3038] | 192 | ControllableEntity* entity = this->controllableEntity_; |
---|
[2072] | 193 | |
---|
[3038] | 194 | if (!entity) |
---|
| 195 | return; |
---|
[2072] | 196 | |
---|
[3038] | 197 | this->controllableEntity_ = 0; |
---|
| 198 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[2826] | 199 | |
---|
[3038] | 200 | if (this->controller_) |
---|
| 201 | this->controller_->setControllableEntity(0); |
---|
| 202 | |
---|
[6101] | 203 | if ( GameMode::isMaster() ) |
---|
| 204 | entity->removePlayer(); |
---|
[3038] | 205 | |
---|
| 206 | this->changedControllableEntity(); |
---|
[2072] | 207 | } |
---|
[6082] | 208 | |
---|
| 209 | void PlayerInfo::stopTemporaryControl() |
---|
| 210 | { |
---|
[6101] | 211 | ControllableEntity* entity = this->controllableEntity_; |
---|
[2072] | 212 | |
---|
[6101] | 213 | if (!entity || !this->oldControllableEntity_) |
---|
[6082] | 214 | return; |
---|
| 215 | |
---|
[6101] | 216 | this->controllableEntity_ = this->oldControllableEntity_; |
---|
| 217 | this->controllableEntityID_ = this->controllableEntity_->getObjectID(); |
---|
| 218 | this->oldControllableEntity_ = 0; |
---|
[6082] | 219 | |
---|
[6087] | 220 | if ( this->controllableEntity_ && this->controller_) |
---|
[6082] | 221 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
| 222 | |
---|
[6101] | 223 | if ( GameMode::isMaster() ) |
---|
| 224 | entity->removePlayer(); |
---|
[6082] | 225 | |
---|
| 226 | this->changedControllableEntity(); |
---|
| 227 | } |
---|
[6087] | 228 | |
---|
[2072] | 229 | void PlayerInfo::networkcallback_changedcontrollableentityID() |
---|
| 230 | { |
---|
[2171] | 231 | if (this->controllableEntityID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 232 | { |
---|
| 233 | Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); |
---|
[3325] | 234 | ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp); |
---|
[2072] | 235 | this->startControl(entity); |
---|
| 236 | } |
---|
| 237 | else |
---|
| 238 | { |
---|
[3038] | 239 | this->stopControl(); |
---|
[2072] | 240 | } |
---|
| 241 | } |
---|
[2973] | 242 | |
---|
[6087] | 243 | |
---|
[2973] | 244 | void PlayerInfo::networkcallback_changedgtinfoID() |
---|
| 245 | { |
---|
| 246 | if (this->gtinfoID_ != OBJECTID_UNKNOWN) |
---|
| 247 | { |
---|
[3325] | 248 | this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_)); |
---|
[2973] | 249 | |
---|
| 250 | if (!this->gtinfo_) |
---|
| 251 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 252 | } |
---|
| 253 | } |
---|
[2072] | 254 | } |
---|