[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 | { |
---|
| 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(); |
---|
[9257] | 60 | |
---|
[2072] | 61 | } |
---|
| 62 | |
---|
| 63 | PlayerInfo::~PlayerInfo() |
---|
| 64 | { |
---|
[2171] | 65 | if (this->BaseObject::isInitialized()) |
---|
[2072] | 66 | { |
---|
[3038] | 67 | this->stopControl(); |
---|
[2072] | 68 | |
---|
| 69 | if (this->controller_) |
---|
| 70 | { |
---|
[5929] | 71 | this->controller_->destroy(); |
---|
[2072] | 72 | this->controller_ = 0; |
---|
| 73 | } |
---|
[2662] | 74 | |
---|
| 75 | if (this->getGametype()) |
---|
| 76 | this->getGametype()->playerLeft(this); |
---|
[2072] | 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void PlayerInfo::registerVariables() |
---|
| 81 | { |
---|
[3280] | 82 | registerVariable(this->name_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); |
---|
| 83 | registerVariable(this->controllableEntityID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID)); |
---|
| 84 | registerVariable(this->gtinfoID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedgtinfoID)); |
---|
[2072] | 85 | } |
---|
| 86 | |
---|
| 87 | void PlayerInfo::changedName() |
---|
| 88 | { |
---|
[2662] | 89 | SUPER(PlayerInfo, changedName); |
---|
| 90 | |
---|
[2171] | 91 | if (this->isInitialized() && this->getGametype()) |
---|
[2072] | 92 | this->getGametype()->playerChangedName(this); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void PlayerInfo::changedGametype() |
---|
| 96 | { |
---|
[2973] | 97 | this->updateGametypeInfo(); |
---|
| 98 | |
---|
[2171] | 99 | if (this->isInitialized()) |
---|
[2072] | 100 | { |
---|
| 101 | if (this->getOldGametype()) |
---|
| 102 | { |
---|
| 103 | if (this->getGametype()) |
---|
| 104 | this->getOldGametype()->playerSwitched(this, this->getGametype()); |
---|
| 105 | else |
---|
| 106 | this->getOldGametype()->playerLeft(this); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | if (this->getGametype()) |
---|
| 110 | { |
---|
| 111 | if (this->getOldGametype()) |
---|
| 112 | this->getGametype()->playerSwitchedBack(this, this->getOldGametype()); |
---|
| 113 | else |
---|
| 114 | this->getGametype()->playerEntered(this); |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
[2973] | 119 | void PlayerInfo::updateGametypeInfo() |
---|
| 120 | { |
---|
| 121 | this->gtinfo_ = 0; |
---|
| 122 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 123 | |
---|
| 124 | if (this->getGametype() && this->getGametype()->getGametypeInfo()) |
---|
| 125 | { |
---|
| 126 | this->gtinfo_ = this->getGametype()->getGametypeInfo(); |
---|
| 127 | this->gtinfoID_ = this->gtinfo_->getObjectID(); |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
[2072] | 131 | void PlayerInfo::createController() |
---|
| 132 | { |
---|
[2839] | 133 | if (this->controller_) |
---|
| 134 | { |
---|
[5929] | 135 | this->controller_->destroy(); |
---|
[2839] | 136 | this->controller_ = 0; |
---|
| 137 | } |
---|
[2072] | 138 | this->controller_ = this->defaultController_.fabricate(this); |
---|
| 139 | assert(this->controller_); |
---|
| 140 | this->controller_->setPlayer(this); |
---|
| 141 | if (this->controllableEntity_) |
---|
[6417] | 142 | { |
---|
[2072] | 143 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
[6417] | 144 | this->controllableEntity_->setController(this->controller_); |
---|
| 145 | } |
---|
[2662] | 146 | this->changedController(); |
---|
[2072] | 147 | } |
---|
| 148 | |
---|
[3038] | 149 | void PlayerInfo::startControl(ControllableEntity* entity) |
---|
[2072] | 150 | { |
---|
[3038] | 151 | if (!entity || entity == this->controllableEntity_) |
---|
[2662] | 152 | return; |
---|
| 153 | |
---|
[8706] | 154 | while (this->previousControllableEntity_.size() > 0) |
---|
[6417] | 155 | this->stopTemporaryControl(); |
---|
[9257] | 156 | |
---|
[2072] | 157 | if (this->controllableEntity_) |
---|
[3038] | 158 | this->stopControl(); |
---|
[2072] | 159 | |
---|
| 160 | this->controllableEntity_ = entity; |
---|
[3038] | 161 | this->controllableEntityID_ = entity->getObjectID(); |
---|
[2072] | 162 | |
---|
[3038] | 163 | entity->setPlayer(this); |
---|
[2072] | 164 | |
---|
[3038] | 165 | this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_); |
---|
| 166 | |
---|
[2072] | 167 | if (this->controller_) |
---|
[6417] | 168 | { |
---|
[2072] | 169 | this->controller_->setControllableEntity(entity); |
---|
[6417] | 170 | this->controllableEntity_->setController(this->controller_); |
---|
| 171 | } |
---|
[2826] | 172 | |
---|
| 173 | this->changedControllableEntity(); |
---|
[9257] | 174 | |
---|
| 175 | RadarViewable* radarviewable = orxonox_cast<RadarViewable*>(entity); |
---|
| 176 | if (radarviewable != NULL) |
---|
| 177 | radarviewable->setRadarName(this->getName()); |
---|
[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); |
---|
[8891] | 243 | if(this->isHumanPlayer()) // TODO: Multiplayer? |
---|
| 244 | this->controllableEntity_->destroyHud(); // HACK-ish |
---|
[9257] | 245 | |
---|
[8706] | 246 | // this->controllableEntity_ = this->previousControllableEntity_.back(); |
---|
| 247 | do { |
---|
| 248 | this->controllableEntity_ = this->previousControllableEntity_.back(); |
---|
| 249 | } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0); |
---|
[6417] | 250 | this->controllableEntityID_ = this->controllableEntity_->getObjectID(); |
---|
[8706] | 251 | this->previousControllableEntity_.pop_back(); |
---|
[6417] | 252 | |
---|
[8706] | 253 | if ( this->controllableEntity_ != NULL && this->controller_ != NULL) |
---|
[6417] | 254 | this->controller_->setControllableEntity(this->controllableEntity_); |
---|
| 255 | |
---|
[8706] | 256 | // HACK-ish |
---|
[8891] | 257 | if(this->controllableEntity_ != NULL && this->isHumanPlayer()) |
---|
[8706] | 258 | this->controllableEntity_->createHud(); |
---|
| 259 | |
---|
[6417] | 260 | if ( GameMode::isMaster() ) |
---|
| 261 | entity->removePlayer(); |
---|
| 262 | |
---|
| 263 | this->changedControllableEntity(); |
---|
| 264 | } |
---|
| 265 | |
---|
[2072] | 266 | void PlayerInfo::networkcallback_changedcontrollableentityID() |
---|
| 267 | { |
---|
[2171] | 268 | if (this->controllableEntityID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 269 | { |
---|
| 270 | Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_); |
---|
[3325] | 271 | ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp); |
---|
[2072] | 272 | this->startControl(entity); |
---|
| 273 | } |
---|
| 274 | else |
---|
| 275 | { |
---|
[3038] | 276 | this->stopControl(); |
---|
[2072] | 277 | } |
---|
| 278 | } |
---|
[2973] | 279 | |
---|
[6417] | 280 | |
---|
[2973] | 281 | void PlayerInfo::networkcallback_changedgtinfoID() |
---|
| 282 | { |
---|
| 283 | if (this->gtinfoID_ != OBJECTID_UNKNOWN) |
---|
| 284 | { |
---|
[3325] | 285 | this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_)); |
---|
[2973] | 286 | |
---|
| 287 | if (!this->gtinfo_) |
---|
| 288 | this->gtinfoID_ = OBJECTID_UNKNOWN; |
---|
| 289 | } |
---|
| 290 | } |
---|
[2072] | 291 | } |
---|