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