[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" |
---|
[8327] | 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 | |
---|
[8327] | 45 | this->clientID_ = NETWORK_PEER_ID_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; |
---|
[6417] | 52 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[2072] | 53 | |
---|
[2973] | 54 | this->gtinfo_ = 0; |
---|
| 55 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 56 | this->updateGametypeInfo(); |
---|
| 57 | |
---|
[2072] | 58 | this->registerVariables(); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | PlayerInfo::~PlayerInfo() |
---|
| 62 | { |
---|
[2171] | 63 | if (this->BaseObject::isInitialized()) |
---|
[2072] | 64 | { |
---|
[3038] | 65 | this->stopControl(); |
---|
[2072] | 66 | |
---|
| 67 | if (this->controller_) |
---|
| 68 | { |
---|
[5929] | 69 | this->controller_->destroy(); |
---|
[2072] | 70 | this->controller_ = 0; |
---|
| 71 | } |
---|
[2662] | 72 | |
---|
| 73 | if (this->getGametype()) |
---|
| 74 | this->getGametype()->playerLeft(this); |
---|
[2072] | 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | void PlayerInfo::registerVariables() |
---|
| 79 | { |
---|
[3280] | 80 | registerVariable(this->name_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); |
---|
| 81 | registerVariable(this->controllableEntityID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID)); |
---|
| 82 | registerVariable(this->gtinfoID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedgtinfoID)); |
---|
[2072] | 83 | } |
---|
| 84 | |
---|
| 85 | void PlayerInfo::changedName() |
---|
| 86 | { |
---|
[2662] | 87 | SUPER(PlayerInfo, changedName); |
---|
| 88 | |
---|
[2171] | 89 | if (this->isInitialized() && this->getGametype()) |
---|
[2072] | 90 | this->getGametype()->playerChangedName(this); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void PlayerInfo::changedGametype() |
---|
| 94 | { |
---|
[2973] | 95 | this->updateGametypeInfo(); |
---|
| 96 | |
---|
[2171] | 97 | if (this->isInitialized()) |
---|
[2072] | 98 | { |
---|
| 99 | if (this->getOldGametype()) |
---|
| 100 | { |
---|
| 101 | if (this->getGametype()) |
---|
| 102 | this->getOldGametype()->playerSwitched(this, this->getGametype()); |
---|
| 103 | else |
---|
| 104 | this->getOldGametype()->playerLeft(this); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if (this->getGametype()) |
---|
| 108 | { |
---|
| 109 | if (this->getOldGametype()) |
---|
| 110 | this->getGametype()->playerSwitchedBack(this, this->getOldGametype()); |
---|
| 111 | else |
---|
| 112 | this->getGametype()->playerEntered(this); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
[2973] | 117 | void PlayerInfo::updateGametypeInfo() |
---|
| 118 | { |
---|
| 119 | this->gtinfo_ = 0; |
---|
| 120 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 121 | |
---|
| 122 | if (this->getGametype() && this->getGametype()->getGametypeInfo()) |
---|
| 123 | { |
---|
| 124 | this->gtinfo_ = this->getGametype()->getGametypeInfo(); |
---|
| 125 | this->gtinfoID_ = this->gtinfo_->getObjectID(); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
[2072] | 129 | void PlayerInfo::createController() |
---|
| 130 | { |
---|
[2839] | 131 | if (this->controller_) |
---|
| 132 | { |
---|
[5929] | 133 | this->controller_->destroy(); |
---|
[2839] | 134 | this->controller_ = 0; |
---|
| 135 | } |
---|
[2072] | 136 | this->controller_ = this->defaultController_.fabricate(this); |
---|
| 137 | assert(this->controller_); |
---|
| 138 | this->controller_->setPlayer(this); |
---|
| 139 | if (this->controllableEntity_) |
---|
[6417] | 140 | { |
---|
[2072] | 141 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
[6417] | 142 | this->controllableEntity_->setController(this->controller_); |
---|
| 143 | } |
---|
[2662] | 144 | this->changedController(); |
---|
[2072] | 145 | } |
---|
| 146 | |
---|
[3038] | 147 | void PlayerInfo::startControl(ControllableEntity* entity) |
---|
[2072] | 148 | { |
---|
[3038] | 149 | if (!entity || entity == this->controllableEntity_) |
---|
[2662] | 150 | return; |
---|
| 151 | |
---|
[8706] | 152 | while (this->previousControllableEntity_.size() > 0) |
---|
[6417] | 153 | this->stopTemporaryControl(); |
---|
[8706] | 154 | |
---|
[2072] | 155 | if (this->controllableEntity_) |
---|
[3038] | 156 | this->stopControl(); |
---|
[2072] | 157 | |
---|
| 158 | this->controllableEntity_ = entity; |
---|
[3038] | 159 | this->controllableEntityID_ = entity->getObjectID(); |
---|
[2072] | 160 | |
---|
[3038] | 161 | entity->setPlayer(this); |
---|
[2072] | 162 | |
---|
[3038] | 163 | this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); |
---|
| 164 | |
---|
[2072] | 165 | if (this->controller_) |
---|
[6417] | 166 | { |
---|
[2072] | 167 | this->controller_->setControllableEntity(entity); |
---|
[6417] | 168 | this->controllableEntity_->setController(this->controller_); |
---|
| 169 | } |
---|
[2826] | 170 | |
---|
| 171 | this->changedControllableEntity(); |
---|
[2072] | 172 | } |
---|
| 173 | |
---|
[6417] | 174 | void PlayerInfo::startTemporaryControl(ControllableEntity* entity) |
---|
| 175 | { |
---|
| 176 | if (!entity) |
---|
| 177 | return; |
---|
| 178 | |
---|
[8706] | 179 | this->controllableEntity_->destroyHud(); // HACK-ish |
---|
| 180 | this->previousControllableEntity_.push_back(WeakPtr<ControllableEntity>(this->controllableEntity_)); |
---|
[6417] | 181 | this->controllableEntity_ = entity; |
---|
| 182 | this->controllableEntityID_ = entity->getObjectID(); |
---|
| 183 | |
---|
| 184 | entity->setPlayer(this); |
---|
[7163] | 185 | entity->setController(this->controller_); |
---|
[6417] | 186 | |
---|
| 187 | if (this->controller_) |
---|
| 188 | this->controller_->setControllableEntity(entity); |
---|
| 189 | |
---|
| 190 | this->changedControllableEntity(); |
---|
| 191 | } |
---|
| 192 | |
---|
[3038] | 193 | void PlayerInfo::stopControl() |
---|
[2072] | 194 | { |
---|
[8706] | 195 | while ( this->previousControllableEntity_.size() > 0) |
---|
[6417] | 196 | this->stopTemporaryControl(); |
---|
| 197 | |
---|
[3038] | 198 | ControllableEntity* entity = this->controllableEntity_; |
---|
[2072] | 199 | |
---|
[3038] | 200 | if (!entity) |
---|
| 201 | return; |
---|
[2072] | 202 | |
---|
[6417] | 203 | this->controllableEntity_->setController(0); |
---|
[3038] | 204 | this->controllableEntity_ = 0; |
---|
| 205 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[2826] | 206 | |
---|
[3038] | 207 | if (this->controller_) |
---|
| 208 | this->controller_->setControllableEntity(0); |
---|
| 209 | |
---|
[6417] | 210 | if ( GameMode::isMaster() ) |
---|
| 211 | entity->removePlayer(); |
---|
[3038] | 212 | |
---|
| 213 | this->changedControllableEntity(); |
---|
[2072] | 214 | } |
---|
| 215 | |
---|
[8706] | 216 | void PlayerInfo::pauseControl() |
---|
| 217 | { |
---|
| 218 | ControllableEntity* entity = this->controllableEntity_; |
---|
| 219 | |
---|
| 220 | if (!entity) |
---|
| 221 | return; |
---|
| 222 | |
---|
| 223 | this->controllableEntity_->getController()->setActive(false); |
---|
| 224 | //this->controllableEntity_->getController()->setControllableEntity(NULL); |
---|
| 225 | this->controllableEntity_->setController(0); |
---|
| 226 | } |
---|
| 227 | |
---|
[6417] | 228 | void PlayerInfo::stopTemporaryControl() |
---|
| 229 | { |
---|
| 230 | ControllableEntity* entity = this->controllableEntity_; |
---|
| 231 | |
---|
[8706] | 232 | assert(this->controllableEntity_ != NULL); |
---|
| 233 | if( !entity || this->previousControllableEntity_.size() == 0 ) |
---|
[6417] | 234 | return; |
---|
| 235 | |
---|
[7163] | 236 | this->controllableEntity_->setController(0); |
---|
[8734] | 237 | if(this->isHumanPlayer()) // TODO: Multiplayer? |
---|
| 238 | this->controllableEntity_->destroyHud(); // HACK-ish |
---|
[7163] | 239 | |
---|
[8706] | 240 | // this->controllableEntity_ = this->previousControllableEntity_.back(); |
---|
| 241 | do { |
---|
| 242 | this->controllableEntity_ = this->previousControllableEntity_.back(); |
---|
| 243 | } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0); |
---|
[6417] | 244 | this->controllableEntityID_ = this->controllableEntity_->getObjectID(); |
---|
[8706] | 245 | this->previousControllableEntity_.pop_back(); |
---|
[6417] | 246 | |
---|
[8706] | 247 | if ( this->controllableEntity_ != NULL && this->controller_ != NULL) |
---|
[6417] | 248 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
| 249 | |
---|
[8706] | 250 | // HACK-ish |
---|
[8734] | 251 | if(this->controllableEntity_ != NULL && this->isHumanPlayer()) |
---|
[8706] | 252 | this->controllableEntity_->createHud(); |
---|
| 253 | |
---|
[6417] | 254 | if ( GameMode::isMaster() ) |
---|
| 255 | entity->removePlayer(); |
---|
| 256 | |
---|
| 257 | this->changedControllableEntity(); |
---|
| 258 | } |
---|
| 259 | |
---|
[2072] | 260 | void PlayerInfo::networkcallback_changedcontrollableentityID() |
---|
| 261 | { |
---|
[2171] | 262 | if (this->controllableEntityID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 263 | { |
---|
| 264 | Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); |
---|
[3325] | 265 | ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp); |
---|
[2072] | 266 | this->startControl(entity); |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | { |
---|
[3038] | 270 | this->stopControl(); |
---|
[2072] | 271 | } |
---|
| 272 | } |
---|
[2973] | 273 | |
---|
[6417] | 274 | |
---|
[2973] | 275 | void PlayerInfo::networkcallback_changedgtinfoID() |
---|
| 276 | { |
---|
| 277 | if (this->gtinfoID_ != OBJECTID_UNKNOWN) |
---|
| 278 | { |
---|
[3325] | 279 | this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_)); |
---|
[2973] | 280 | |
---|
| 281 | if (!this->gtinfo_) |
---|
| 282 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 283 | } |
---|
| 284 | } |
---|
[2072] | 285 | } |
---|