[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" |
---|
[8896] | 38 | #include "worldentities/pawns/SpaceShip.h" |
---|
[2072] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator) |
---|
| 43 | { |
---|
| 44 | RegisterObject(PlayerInfo); |
---|
| 45 | |
---|
[8327] | 46 | this->clientID_ = NETWORK_PEER_ID_UNKNOWN; |
---|
[2072] | 47 | this->bHumanPlayer_ = false; |
---|
| 48 | this->bLocalPlayer_ = false; |
---|
| 49 | this->bReadyToSpawn_ = false; |
---|
[2662] | 50 | this->bSetUnreadyAfterSpawn_ = true; |
---|
[2072] | 51 | this->controller_ = 0; |
---|
| 52 | this->controllableEntity_ = 0; |
---|
[6417] | 53 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[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_) |
---|
[6417] | 141 | { |
---|
[2072] | 142 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
[6417] | 143 | this->controllableEntity_->setController(this->controller_); |
---|
| 144 | } |
---|
[2662] | 145 | this->changedController(); |
---|
[2072] | 146 | } |
---|
| 147 | |
---|
[3038] | 148 | void PlayerInfo::startControl(ControllableEntity* entity) |
---|
[2072] | 149 | { |
---|
[3038] | 150 | if (!entity || entity == this->controllableEntity_) |
---|
[2662] | 151 | return; |
---|
| 152 | |
---|
[8706] | 153 | while (this->previousControllableEntity_.size() > 0) |
---|
[6417] | 154 | this->stopTemporaryControl(); |
---|
[8706] | 155 | |
---|
[2072] | 156 | if (this->controllableEntity_) |
---|
[3038] | 157 | this->stopControl(); |
---|
[2072] | 158 | |
---|
| 159 | this->controllableEntity_ = entity; |
---|
[3038] | 160 | this->controllableEntityID_ = entity->getObjectID(); |
---|
[2072] | 161 | |
---|
[3038] | 162 | entity->setPlayer(this); |
---|
[2072] | 163 | |
---|
[3038] | 164 | this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); |
---|
| 165 | |
---|
[2072] | 166 | if (this->controller_) |
---|
[6417] | 167 | { |
---|
[2072] | 168 | this->controller_->setControllableEntity(entity); |
---|
[6417] | 169 | this->controllableEntity_->setController(this->controller_); |
---|
| 170 | } |
---|
[2826] | 171 | |
---|
| 172 | this->changedControllableEntity(); |
---|
[8906] | 173 | SpaceShip* spaceship = dynamic_cast<SpaceShip*>(entity); |
---|
| 174 | if (spaceship != NULL) |
---|
| 175 | { |
---|
| 176 | spaceship->setRVName(this->getName()); |
---|
| 177 | } |
---|
[2072] | 178 | } |
---|
| 179 | |
---|
[6417] | 180 | void PlayerInfo::startTemporaryControl(ControllableEntity* entity) |
---|
| 181 | { |
---|
| 182 | if (!entity) |
---|
| 183 | return; |
---|
| 184 | |
---|
[8706] | 185 | this->controllableEntity_->destroyHud(); // HACK-ish |
---|
| 186 | this->previousControllableEntity_.push_back(WeakPtr<ControllableEntity>(this->controllableEntity_)); |
---|
[6417] | 187 | this->controllableEntity_ = entity; |
---|
| 188 | this->controllableEntityID_ = entity->getObjectID(); |
---|
| 189 | |
---|
| 190 | entity->setPlayer(this); |
---|
[7163] | 191 | entity->setController(this->controller_); |
---|
[6417] | 192 | |
---|
| 193 | if (this->controller_) |
---|
| 194 | this->controller_->setControllableEntity(entity); |
---|
| 195 | |
---|
| 196 | this->changedControllableEntity(); |
---|
| 197 | } |
---|
| 198 | |
---|
[3038] | 199 | void PlayerInfo::stopControl() |
---|
[2072] | 200 | { |
---|
[8706] | 201 | while ( this->previousControllableEntity_.size() > 0) |
---|
[6417] | 202 | this->stopTemporaryControl(); |
---|
| 203 | |
---|
[3038] | 204 | ControllableEntity* entity = this->controllableEntity_; |
---|
[2072] | 205 | |
---|
[3038] | 206 | if (!entity) |
---|
| 207 | return; |
---|
[2072] | 208 | |
---|
[6417] | 209 | this->controllableEntity_->setController(0); |
---|
[3038] | 210 | this->controllableEntity_ = 0; |
---|
| 211 | this->controllableEntityID_ = OBJECTID_UNKNOWN; |
---|
[2826] | 212 | |
---|
[3038] | 213 | if (this->controller_) |
---|
| 214 | this->controller_->setControllableEntity(0); |
---|
| 215 | |
---|
[6417] | 216 | if ( GameMode::isMaster() ) |
---|
| 217 | entity->removePlayer(); |
---|
[3038] | 218 | |
---|
| 219 | this->changedControllableEntity(); |
---|
[2072] | 220 | } |
---|
| 221 | |
---|
[8706] | 222 | void PlayerInfo::pauseControl() |
---|
| 223 | { |
---|
| 224 | ControllableEntity* entity = this->controllableEntity_; |
---|
| 225 | |
---|
| 226 | if (!entity) |
---|
| 227 | return; |
---|
| 228 | |
---|
| 229 | this->controllableEntity_->getController()->setActive(false); |
---|
| 230 | //this->controllableEntity_->getController()->setControllableEntity(NULL); |
---|
| 231 | this->controllableEntity_->setController(0); |
---|
| 232 | } |
---|
| 233 | |
---|
[6417] | 234 | void PlayerInfo::stopTemporaryControl() |
---|
| 235 | { |
---|
| 236 | ControllableEntity* entity = this->controllableEntity_; |
---|
| 237 | |
---|
[8706] | 238 | assert(this->controllableEntity_ != NULL); |
---|
| 239 | if( !entity || this->previousControllableEntity_.size() == 0 ) |
---|
[6417] | 240 | return; |
---|
| 241 | |
---|
[7163] | 242 | this->controllableEntity_->setController(0); |
---|
[8706] | 243 | this->controllableEntity_->destroyHud(); // HACK-ish |
---|
[7163] | 244 | |
---|
[8706] | 245 | // this->controllableEntity_ = this->previousControllableEntity_.back(); |
---|
| 246 | do { |
---|
| 247 | this->controllableEntity_ = this->previousControllableEntity_.back(); |
---|
| 248 | } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0); |
---|
[6417] | 249 | this->controllableEntityID_ = this->controllableEntity_->getObjectID(); |
---|
[8706] | 250 | this->previousControllableEntity_.pop_back(); |
---|
[6417] | 251 | |
---|
[8706] | 252 | if ( this->controllableEntity_ != NULL && this->controller_ != NULL) |
---|
[6417] | 253 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
| 254 | |
---|
[8706] | 255 | // HACK-ish |
---|
| 256 | if(this->controllableEntity_ != NULL) |
---|
| 257 | this->controllableEntity_->createHud(); |
---|
| 258 | |
---|
[6417] | 259 | if ( GameMode::isMaster() ) |
---|
| 260 | entity->removePlayer(); |
---|
| 261 | |
---|
| 262 | this->changedControllableEntity(); |
---|
| 263 | } |
---|
| 264 | |
---|
[2072] | 265 | void PlayerInfo::networkcallback_changedcontrollableentityID() |
---|
| 266 | { |
---|
[2171] | 267 | if (this->controllableEntityID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 268 | { |
---|
| 269 | Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); |
---|
[3325] | 270 | ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp); |
---|
[2072] | 271 | this->startControl(entity); |
---|
| 272 | } |
---|
| 273 | else |
---|
| 274 | { |
---|
[3038] | 275 | this->stopControl(); |
---|
[2072] | 276 | } |
---|
| 277 | } |
---|
[2973] | 278 | |
---|
[6417] | 279 | |
---|
[2973] | 280 | void PlayerInfo::networkcallback_changedgtinfoID() |
---|
| 281 | { |
---|
| 282 | if (this->gtinfoID_ != OBJECTID_UNKNOWN) |
---|
| 283 | { |
---|
[3325] | 284 | this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_)); |
---|
[2973] | 285 | |
---|
| 286 | if (!this->gtinfo_) |
---|
| 287 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 288 | } |
---|
| 289 | } |
---|
[2072] | 290 | } |
---|