[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: |
---|
[2662] | 25 | * Reto Grieder |
---|
[2072] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ControllableEntity.h" |
---|
| 30 | |
---|
[2662] | 31 | #include <OgreSceneManager.h> |
---|
[3196] | 32 | #include <OgreSceneNode.h> |
---|
[2662] | 33 | |
---|
[2072] | 34 | #include "core/CoreIncludes.h" |
---|
[9667] | 35 | #include "core/config/ConfigValueIncludes.h" |
---|
[2896] | 36 | #include "core/GameMode.h" |
---|
[2072] | 37 | #include "core/XMLPort.h" |
---|
[10624] | 38 | #include "network/NetworkFunctionIncludes.h" |
---|
[2072] | 39 | |
---|
[5735] | 40 | #include "Scene.h" |
---|
| 41 | #include "infos/PlayerInfo.h" |
---|
[7860] | 42 | #include "controllers/NewHumanController.h" |
---|
[5737] | 43 | #include "graphics/Camera.h" |
---|
[5735] | 44 | #include "worldentities/CameraPosition.h" |
---|
[2072] | 45 | #include "overlays/OverlayGroup.h" |
---|
| 46 | |
---|
| 47 | namespace orxonox |
---|
| 48 | { |
---|
[9667] | 49 | RegisterClass(ControllableEntity); |
---|
[2072] | 50 | |
---|
[11175] | 51 | registerMemberNetworkFunction( ControllableEntity, push ); |
---|
[6417] | 52 | registerMemberNetworkFunction( ControllableEntity, setTargetInternal ); |
---|
| 53 | |
---|
[9667] | 54 | ControllableEntity::ControllableEntity(Context* context) : MobileEntity(context) |
---|
[2072] | 55 | { |
---|
| 56 | RegisterObject(ControllableEntity); |
---|
| 57 | |
---|
[2662] | 58 | this->bHasLocalController_ = false; |
---|
| 59 | this->bHasHumanController_ = false; |
---|
| 60 | |
---|
[2072] | 61 | this->server_overwrite_ = 0; |
---|
| 62 | this->client_overwrite_ = 0; |
---|
[11071] | 63 | this->player_ = nullptr; |
---|
| 64 | this->formerPlayer_ = nullptr; |
---|
[2171] | 65 | this->playerID_ = OBJECTID_UNKNOWN; |
---|
[11071] | 66 | this->hud_ = nullptr; |
---|
| 67 | this->camera_ = nullptr; |
---|
| 68 | this->xmlcontroller_ = nullptr; |
---|
| 69 | //this->controller_ = nullptr; |
---|
| 70 | this->reverseCamera_ = nullptr; |
---|
[2072] | 71 | this->bDestroyWhenPlayerLeft_ = false; |
---|
[2662] | 72 | this->cameraPositionRootNode_ = this->node_->createChildSceneNode(); |
---|
[11071] | 73 | this->currentCameraPosition_ = nullptr; |
---|
[2662] | 74 | this->bMouseLook_ = false; |
---|
| 75 | this->mouseLookSpeed_ = 200; |
---|
[2072] | 76 | |
---|
[2662] | 77 | this->server_position_ = Vector3::ZERO; |
---|
| 78 | this->client_position_ = Vector3::ZERO; |
---|
| 79 | this->server_linear_velocity_ = Vector3::ZERO; |
---|
| 80 | this->client_linear_velocity_ = Vector3::ZERO; |
---|
| 81 | this->server_orientation_ = Quaternion::IDENTITY; |
---|
| 82 | this->client_orientation_ = Quaternion::IDENTITY; |
---|
| 83 | this->server_angular_velocity_ = Vector3::ZERO; |
---|
| 84 | this->client_angular_velocity_ = Vector3::ZERO; |
---|
[2072] | 85 | |
---|
[2662] | 86 | this->setConfigValues(); |
---|
[3280] | 87 | this->setPriority( Priority::VeryHigh ); |
---|
[2072] | 88 | this->registerVariables(); |
---|
[9348] | 89 | this->team_ = -1; |
---|
[2072] | 90 | } |
---|
| 91 | |
---|
| 92 | ControllableEntity::~ControllableEntity() |
---|
| 93 | { |
---|
| 94 | if (this->isInitialized()) |
---|
| 95 | { |
---|
[2662] | 96 | this->bDestroyWhenPlayerLeft_ = false; |
---|
[2072] | 97 | |
---|
[2662] | 98 | if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this) |
---|
[3038] | 99 | this->getPlayer()->stopControl(); |
---|
[2662] | 100 | |
---|
[3049] | 101 | if (this->xmlcontroller_) |
---|
[5929] | 102 | this->xmlcontroller_->destroy(); |
---|
[3049] | 103 | |
---|
[2072] | 104 | if (this->hud_) |
---|
[5929] | 105 | this->hud_->destroy(); |
---|
[2072] | 106 | |
---|
| 107 | if (this->camera_) |
---|
[5929] | 108 | this->camera_->destroy(); |
---|
[2072] | 109 | |
---|
[11071] | 110 | for (CameraPosition* cameraPosition : this->cameraPositions_) |
---|
| 111 | cameraPosition->destroy(); |
---|
[2662] | 112 | |
---|
| 113 | if (this->getScene()->getSceneManager()) |
---|
| 114 | this->getScene()->getSceneManager()->destroySceneNode(this->cameraPositionRootNode_->getName()); |
---|
[2072] | 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 119 | { |
---|
| 120 | SUPER(ControllableEntity, XMLPort, xmlelement, mode); |
---|
| 121 | |
---|
[9016] | 122 | XMLPortParam(ControllableEntity, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1); |
---|
[2072] | 123 | XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode); |
---|
[8706] | 124 | XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemplate, xmlelement, mode); |
---|
[2072] | 125 | |
---|
| 126 | XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode); |
---|
[3049] | 127 | XMLPortObject(ControllableEntity, Controller, "controller", setXMLController, getXMLController, xmlelement, mode); |
---|
[2072] | 128 | } |
---|
| 129 | |
---|
[2662] | 130 | void ControllableEntity::setConfigValues() |
---|
| 131 | { |
---|
| 132 | SetConfigValue(mouseLookSpeed_, 3.0f); |
---|
| 133 | } |
---|
| 134 | |
---|
[7889] | 135 | void ControllableEntity::preDestroy() |
---|
| 136 | { |
---|
| 137 | // HACK - solve this clean and without preDestroy hook for multiplayer where removePlayer() isn't called |
---|
[7892] | 138 | if (this->isInitialized() && this->bHasLocalController_ && this->bHasHumanController_) |
---|
[7889] | 139 | this->stopLocalHumanControl(); |
---|
| 140 | } |
---|
| 141 | |
---|
[2072] | 142 | void ControllableEntity::addCameraPosition(CameraPosition* position) |
---|
| 143 | { |
---|
[2826] | 144 | if (!position->getIsAbsolute()) |
---|
| 145 | { |
---|
| 146 | if (position->getAllowMouseLook()) |
---|
| 147 | position->attachToNode(this->cameraPositionRootNode_); |
---|
| 148 | else |
---|
| 149 | this->attach(position); |
---|
| 150 | } |
---|
[2662] | 151 | else |
---|
[2826] | 152 | { |
---|
| 153 | WorldEntity* parent = this->getParent(); |
---|
| 154 | if (parent) |
---|
| 155 | parent->attach(position); |
---|
| 156 | } |
---|
[3089] | 157 | |
---|
| 158 | if (!position->getRenderCamera()) |
---|
| 159 | this->cameraPositions_.push_back(position); |
---|
| 160 | else |
---|
| 161 | this->setReverseCamera(position); |
---|
[2072] | 162 | } |
---|
| 163 | |
---|
| 164 | CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const |
---|
| 165 | { |
---|
| 166 | unsigned int i = 0; |
---|
[11071] | 167 | for (CameraPosition* cameraPosition : this->cameraPositions_) |
---|
[2072] | 168 | { |
---|
| 169 | if (i == index) |
---|
[11071] | 170 | return cameraPosition; |
---|
[2072] | 171 | ++i; |
---|
| 172 | } |
---|
[11071] | 173 | return nullptr; |
---|
[2072] | 174 | } |
---|
| 175 | |
---|
[8706] | 176 | unsigned int ControllableEntity::getCurrentCameraIndex() const |
---|
| 177 | { |
---|
| 178 | if (this->cameraPositions_.size() <= 0) |
---|
| 179 | return 0; |
---|
| 180 | |
---|
| 181 | unsigned int counter = 0; |
---|
[11071] | 182 | for (CameraPosition* cameraPosition : this->cameraPositions_) |
---|
[8706] | 183 | { |
---|
[11071] | 184 | if (cameraPosition == this->currentCameraPosition_) |
---|
[8706] | 185 | break; |
---|
| 186 | counter++; |
---|
| 187 | } |
---|
| 188 | if (counter >= this->cameraPositions_.size()) |
---|
| 189 | return 0; |
---|
| 190 | |
---|
| 191 | return counter; |
---|
| 192 | } |
---|
[8891] | 193 | |
---|
[8706] | 194 | bool ControllableEntity::setCameraPosition(unsigned int index) |
---|
| 195 | { |
---|
[11071] | 196 | if(this->camera_ != nullptr && this->cameraPositions_.size() > 0) |
---|
[8706] | 197 | { |
---|
| 198 | if(index >= this->cameraPositions_.size()) |
---|
| 199 | index = 0; |
---|
| 200 | |
---|
| 201 | CameraPosition* position = this->getCameraPosition(index); |
---|
| 202 | position->attachCamera(this->camera_); |
---|
| 203 | this->currentCameraPosition_ = position; |
---|
| 204 | return true; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | return false; |
---|
| 208 | } |
---|
| 209 | |
---|
[2072] | 210 | void ControllableEntity::switchCamera() |
---|
| 211 | { |
---|
| 212 | if (this->camera_) |
---|
| 213 | { |
---|
| 214 | if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0) |
---|
| 215 | { |
---|
| 216 | this->cameraPositions_.front()->attachCamera(this->camera_); |
---|
[10624] | 217 | this->currentCameraPosition_ = this->cameraPositions_.front(); |
---|
[2072] | 218 | } |
---|
| 219 | else if (this->cameraPositions_.size() > 0) |
---|
| 220 | { |
---|
[11071] | 221 | for (std::list<StrongPtr<CameraPosition>>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) |
---|
[2072] | 222 | { |
---|
| 223 | if ((*it) == this->camera_->getParent()) |
---|
| 224 | { |
---|
| 225 | ++it; |
---|
| 226 | if (it != this->cameraPositions_.end()) |
---|
[6417] | 227 | { |
---|
[2072] | 228 | (*it)->attachCamera(this->camera_); |
---|
[6417] | 229 | this->currentCameraPosition_ = *it; |
---|
| 230 | } |
---|
[2072] | 231 | else |
---|
[6417] | 232 | { |
---|
[2072] | 233 | (*this->cameraPositions_.begin())->attachCamera(this->camera_); |
---|
[6417] | 234 | this->currentCameraPosition_ = *this->cameraPositions_.begin(); |
---|
| 235 | } |
---|
[2072] | 236 | break; |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | else |
---|
| 241 | { |
---|
[2662] | 242 | this->camera_->attachToNode(this->cameraPositionRootNode_); |
---|
[11071] | 243 | this->currentCameraPosition_ = nullptr; |
---|
[2072] | 244 | } |
---|
[7860] | 245 | |
---|
[7857] | 246 | // disable mouse look if the new camera position doesn't allow it |
---|
| 247 | if (this->currentCameraPosition_ && !this->currentCameraPosition_->getAllowMouseLook() && this->bMouseLook_) |
---|
| 248 | this->mouseLook(); |
---|
[7860] | 249 | |
---|
| 250 | // disable drag if in mouse look |
---|
| 251 | if (this->bMouseLook_) |
---|
| 252 | this->getCamera()->setDrag(false); |
---|
[2072] | 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
[2662] | 256 | void ControllableEntity::mouseLook() |
---|
| 257 | { |
---|
[7857] | 258 | // enable mouse look only if allowed - disabling it works always |
---|
| 259 | if (this->currentCameraPosition_ && (this->currentCameraPosition_->getAllowMouseLook() || this->bMouseLook_)) |
---|
| 260 | { |
---|
| 261 | this->bMouseLook_ = !this->bMouseLook_; |
---|
[2662] | 262 | |
---|
[7857] | 263 | if (!this->bMouseLook_) |
---|
[7860] | 264 | { |
---|
[7857] | 265 | this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY); |
---|
[7860] | 266 | this->cameraPositionRootNode_->_update(true, false); // update the camera node because otherwise the camera will drag back in position which looks strange |
---|
| 267 | |
---|
[9348] | 268 | NewHumanController* controller = orxonox_cast<NewHumanController*>(this->getController()); |
---|
[7860] | 269 | if (controller) |
---|
| 270 | controller->centerCursor(); |
---|
| 271 | } |
---|
| 272 | |
---|
[7857] | 273 | if (this->getCamera()) |
---|
| 274 | { |
---|
| 275 | if (!this->bMouseLook_ && this->currentCameraPosition_->getDrag()) |
---|
| 276 | this->getCamera()->setDrag(true); |
---|
| 277 | else |
---|
| 278 | this->getCamera()->setDrag(false); |
---|
| 279 | } |
---|
[6417] | 280 | } |
---|
[2662] | 281 | } |
---|
| 282 | |
---|
| 283 | void ControllableEntity::rotateYaw(const Vector2& value) |
---|
| 284 | { |
---|
| 285 | if (this->bMouseLook_) |
---|
| 286 | this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | void ControllableEntity::rotatePitch(const Vector2& value) |
---|
| 290 | { |
---|
| 291 | if (this->bMouseLook_) |
---|
| 292 | this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | void ControllableEntity::rotateRoll(const Vector2& value) |
---|
| 296 | { |
---|
| 297 | if (this->bMouseLook_) |
---|
| 298 | this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); |
---|
| 299 | } |
---|
| 300 | |
---|
[11174] | 301 | void ControllableEntity::push(unsigned int firemode) |
---|
[6417] | 302 | { |
---|
| 303 | if(GameMode::isMaster()) |
---|
| 304 | { |
---|
[11174] | 305 | this->pushed(firemode); |
---|
[6417] | 306 | } |
---|
| 307 | else |
---|
| 308 | { |
---|
[11175] | 309 | callMemberNetworkFunction(&ControllableEntity::push, this->getObjectID(), 0, firemode); |
---|
[6417] | 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
[11164] | 313 | void ControllableEntity::release(unsigned int firemode) |
---|
| 314 | { |
---|
| 315 | if(GameMode::isMaster()) |
---|
| 316 | { |
---|
| 317 | this->released(firemode); |
---|
| 318 | } |
---|
| 319 | else |
---|
| 320 | { |
---|
[11175] | 321 | callMemberNetworkFunction(&ControllableEntity::release, this->getObjectID(), 0, firemode); |
---|
[11164] | 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
[8891] | 325 | void ControllableEntity::setController(Controller* val) |
---|
| 326 | { |
---|
| 327 | this->controller_ = val; |
---|
| 328 | } |
---|
| 329 | |
---|
[6417] | 330 | void ControllableEntity::setTarget( WorldEntity* target ) |
---|
| 331 | { |
---|
| 332 | this->target_ = target; |
---|
| 333 | if ( !GameMode::isMaster() ) |
---|
| 334 | { |
---|
[11071] | 335 | if ( target != nullptr ) |
---|
[6417] | 336 | { |
---|
[10624] | 337 | callMemberNetworkFunction(&ControllableEntity::setTargetInternal, this->getObjectID(), 0, target->getObjectID() ); |
---|
[6417] | 338 | } |
---|
| 339 | else |
---|
| 340 | { |
---|
[10624] | 341 | callMemberNetworkFunction(&ControllableEntity::setTargetInternal, this->getObjectID(), 0, OBJECTID_UNKNOWN ); |
---|
[6417] | 342 | } |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | void ControllableEntity::setTargetInternal( uint32_t targetID ) |
---|
| 347 | { |
---|
| 348 | this->setTarget( orxonox_cast<WorldEntity*>(Synchronisable::getSynchronisable(targetID)) ); |
---|
| 349 | } |
---|
| 350 | |
---|
[2072] | 351 | void ControllableEntity::setPlayer(PlayerInfo* player) |
---|
| 352 | { |
---|
| 353 | if (!player) |
---|
| 354 | { |
---|
| 355 | this->removePlayer(); |
---|
| 356 | return; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | this->player_ = player; |
---|
[7533] | 360 | this->formerPlayer_ = player; |
---|
[2072] | 361 | this->playerID_ = player->getObjectID(); |
---|
[2662] | 362 | this->bHasLocalController_ = player->isLocalPlayer(); |
---|
| 363 | this->bHasHumanController_ = player->isHumanPlayer(); |
---|
[11071] | 364 | if(controller_ != nullptr) |
---|
[9799] | 365 | this->team_ = controller_->getTeam(); // forward controller team number |
---|
[2662] | 366 | |
---|
| 367 | if (this->bHasLocalController_ && this->bHasHumanController_) |
---|
[2072] | 368 | { |
---|
[2662] | 369 | this->startLocalHumanControl(); |
---|
[2072] | 370 | |
---|
[2896] | 371 | if (!GameMode::isMaster()) |
---|
[2072] | 372 | { |
---|
| 373 | this->client_overwrite_ = this->server_overwrite_; |
---|
[5929] | 374 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
[2072] | 375 | } |
---|
| 376 | } |
---|
[2839] | 377 | |
---|
| 378 | this->changedPlayer(); |
---|
[2072] | 379 | } |
---|
| 380 | |
---|
| 381 | void ControllableEntity::removePlayer() |
---|
| 382 | { |
---|
[2662] | 383 | if (this->bHasLocalController_ && this->bHasHumanController_) |
---|
| 384 | this->stopLocalHumanControl(); |
---|
[2072] | 385 | |
---|
[11071] | 386 | this->player_ = nullptr; |
---|
[2171] | 387 | this->playerID_ = OBJECTID_UNKNOWN; |
---|
[2662] | 388 | this->bHasLocalController_ = false; |
---|
| 389 | this->bHasHumanController_ = false; |
---|
[5929] | 390 | this->setSyncMode(ObjectDirection::ToClient); |
---|
[2072] | 391 | |
---|
[2839] | 392 | this->changedPlayer(); |
---|
| 393 | |
---|
[2072] | 394 | if (this->bDestroyWhenPlayerLeft_) |
---|
[5929] | 395 | this->destroy(); |
---|
[2072] | 396 | } |
---|
| 397 | |
---|
| 398 | void ControllableEntity::networkcallback_changedplayerID() |
---|
| 399 | { |
---|
| 400 | // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID |
---|
[2171] | 401 | if (this->playerID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 402 | { |
---|
[3325] | 403 | this->player_ = orxonox_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_)); |
---|
[2072] | 404 | if (this->player_ && (this->player_->getControllableEntity() != this)) |
---|
| 405 | this->player_->startControl(this); |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
[2662] | 409 | void ControllableEntity::startLocalHumanControl() |
---|
| 410 | { |
---|
[5929] | 411 | if (!this->camera_ && GameMode::showsGraphics()) |
---|
[2072] | 412 | { |
---|
[9667] | 413 | this->camera_ = new Camera(this->getContext()); |
---|
[2662] | 414 | this->camera_->requestFocus(); |
---|
[6417] | 415 | if (!this->cameraPositionTemplate_.empty()) |
---|
[2662] | 416 | this->addTemplate(this->cameraPositionTemplate_); |
---|
| 417 | if (this->cameraPositions_.size() > 0) |
---|
[6417] | 418 | { |
---|
[2662] | 419 | this->cameraPositions_.front()->attachCamera(this->camera_); |
---|
[6417] | 420 | this->currentCameraPosition_ = this->cameraPositions_.front(); |
---|
| 421 | } |
---|
[2662] | 422 | else |
---|
[6417] | 423 | { |
---|
[2662] | 424 | this->camera_->attachToNode(this->cameraPositionRootNode_); |
---|
[11071] | 425 | this->currentCameraPosition_ = nullptr; |
---|
[6417] | 426 | } |
---|
[2072] | 427 | } |
---|
[2662] | 428 | |
---|
[8706] | 429 | this->createHud(); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | // HACK-ish |
---|
| 433 | void ControllableEntity::createHud(void) |
---|
| 434 | { |
---|
[5929] | 435 | if (!this->hud_ && GameMode::showsGraphics()) |
---|
[2662] | 436 | { |
---|
[6417] | 437 | if (!this->hudtemplate_.empty()) |
---|
[2662] | 438 | { |
---|
[9667] | 439 | this->hud_ = new OverlayGroup(this->getContext()); |
---|
[2662] | 440 | this->hud_->addTemplate(this->hudtemplate_); |
---|
| 441 | this->hud_->setOwner(this); |
---|
| 442 | } |
---|
| 443 | } |
---|
[2072] | 444 | } |
---|
| 445 | |
---|
[8706] | 446 | void ControllableEntity::destroyHud(void) |
---|
| 447 | { |
---|
[11071] | 448 | if (this->hud_ != nullptr) |
---|
[8706] | 449 | { |
---|
| 450 | this->hud_->destroy(); |
---|
[11071] | 451 | this->hud_ = nullptr; |
---|
[8706] | 452 | } |
---|
| 453 | } |
---|
| 454 | |
---|
[2662] | 455 | void ControllableEntity::stopLocalHumanControl() |
---|
[2072] | 456 | { |
---|
[2662] | 457 | if (this->camera_) |
---|
| 458 | { |
---|
| 459 | this->camera_->detachFromParent(); |
---|
[5929] | 460 | this->camera_->destroy(); |
---|
[11071] | 461 | this->camera_ = nullptr; |
---|
[2662] | 462 | } |
---|
[2072] | 463 | |
---|
[2662] | 464 | if (this->hud_) |
---|
| 465 | { |
---|
[5929] | 466 | this->hud_->destroy(); |
---|
[11071] | 467 | this->hud_ = nullptr; |
---|
[2662] | 468 | } |
---|
[2072] | 469 | } |
---|
| 470 | |
---|
[3049] | 471 | void ControllableEntity::setXMLController(Controller* controller) |
---|
| 472 | { |
---|
| 473 | if (!this->xmlcontroller_) |
---|
| 474 | { |
---|
| 475 | this->xmlcontroller_ = controller; |
---|
| 476 | this->bHasLocalController_ = true; |
---|
| 477 | this->xmlcontroller_->setControllableEntity(this); |
---|
[9799] | 478 | this->team_ = this->xmlcontroller_->getTeam(); //forward the team number |
---|
[3049] | 479 | } |
---|
| 480 | else |
---|
[8858] | 481 | orxout(internal_warning) << "ControllableEntity \"" << this->getName() << "\" already has a Controller." << endl; |
---|
[3049] | 482 | } |
---|
| 483 | |
---|
[2851] | 484 | void ControllableEntity::parentChanged() |
---|
| 485 | { |
---|
| 486 | WorldEntity::parentChanged(); |
---|
| 487 | |
---|
| 488 | WorldEntity* parent = this->getParent(); |
---|
| 489 | if (parent) |
---|
| 490 | { |
---|
[11071] | 491 | for (CameraPosition* cameraPosition : this->cameraPositions_) |
---|
| 492 | if (cameraPosition->getIsAbsolute()) |
---|
| 493 | parent->attach(cameraPosition); |
---|
[2851] | 494 | } |
---|
| 495 | } |
---|
| 496 | |
---|
[2072] | 497 | void ControllableEntity::tick(float dt) |
---|
| 498 | { |
---|
[2662] | 499 | MobileEntity::tick(dt); |
---|
| 500 | |
---|
[2072] | 501 | if (this->isActive()) |
---|
| 502 | { |
---|
[2662] | 503 | // Check whether Bullet doesn't do the physics for us |
---|
| 504 | if (!this->isDynamic()) |
---|
[2072] | 505 | { |
---|
[2896] | 506 | if (GameMode::isMaster()) |
---|
[2662] | 507 | { |
---|
| 508 | this->server_position_ = this->getPosition(); |
---|
| 509 | this->server_orientation_ = this->getOrientation(); |
---|
| 510 | this->server_linear_velocity_ = this->getVelocity(); |
---|
| 511 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
| 512 | } |
---|
| 513 | else if (this->bHasLocalController_) |
---|
| 514 | { |
---|
| 515 | this->client_position_ = this->getPosition(); |
---|
| 516 | this->client_orientation_ = this->getOrientation(); |
---|
| 517 | this->client_linear_velocity_ = this->getVelocity(); |
---|
| 518 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
| 519 | } |
---|
[2072] | 520 | } |
---|
| 521 | } |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | void ControllableEntity::registerVariables() |
---|
| 525 | { |
---|
[3280] | 526 | registerVariable(this->cameraPositionTemplate_, VariableDirection::ToClient); |
---|
| 527 | registerVariable(this->hudtemplate_, VariableDirection::ToClient); |
---|
[2072] | 528 | |
---|
[3280] | 529 | registerVariable(this->server_position_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition)); |
---|
| 530 | registerVariable(this->server_linear_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity)); |
---|
| 531 | registerVariable(this->server_orientation_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation)); |
---|
| 532 | registerVariable(this->server_angular_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity)); |
---|
[2072] | 533 | |
---|
[3280] | 534 | registerVariable(this->server_overwrite_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite)); |
---|
| 535 | registerVariable(this->client_overwrite_, VariableDirection::ToServer); |
---|
[2072] | 536 | |
---|
[3280] | 537 | registerVariable(this->client_position_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition)); |
---|
| 538 | registerVariable(this->client_linear_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity)); |
---|
| 539 | registerVariable(this->client_orientation_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation)); |
---|
| 540 | registerVariable(this->client_angular_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity)); |
---|
[2072] | 541 | |
---|
[5735] | 542 | |
---|
[3280] | 543 | registerVariable(this->playerID_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID)); |
---|
[2072] | 544 | } |
---|
| 545 | |
---|
| 546 | void ControllableEntity::processServerPosition() |
---|
| 547 | { |
---|
[2662] | 548 | if (!this->bHasLocalController_) |
---|
| 549 | MobileEntity::setPosition(this->server_position_); |
---|
[2072] | 550 | } |
---|
| 551 | |
---|
[2662] | 552 | void ControllableEntity::processServerLinearVelocity() |
---|
[2072] | 553 | { |
---|
[2662] | 554 | if (!this->bHasLocalController_) |
---|
| 555 | MobileEntity::setVelocity(this->server_linear_velocity_); |
---|
[2072] | 556 | } |
---|
| 557 | |
---|
| 558 | void ControllableEntity::processServerOrientation() |
---|
| 559 | { |
---|
[2662] | 560 | if (!this->bHasLocalController_) |
---|
| 561 | MobileEntity::setOrientation(this->server_orientation_); |
---|
[2072] | 562 | } |
---|
| 563 | |
---|
[2662] | 564 | void ControllableEntity::processServerAngularVelocity() |
---|
| 565 | { |
---|
| 566 | if (!this->bHasLocalController_) |
---|
| 567 | MobileEntity::setAngularVelocity(this->server_angular_velocity_); |
---|
| 568 | } |
---|
| 569 | |
---|
[2072] | 570 | void ControllableEntity::processOverwrite() |
---|
| 571 | { |
---|
[2662] | 572 | if (this->bHasLocalController_) |
---|
[2072] | 573 | { |
---|
| 574 | this->setPosition(this->server_position_); |
---|
| 575 | this->setOrientation(this->server_orientation_); |
---|
[2662] | 576 | this->setVelocity(this->server_linear_velocity_); |
---|
| 577 | this->setAngularVelocity(this->server_angular_velocity_); |
---|
[2072] | 578 | |
---|
| 579 | this->client_overwrite_ = this->server_overwrite_; |
---|
| 580 | } |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | void ControllableEntity::processClientPosition() |
---|
| 584 | { |
---|
| 585 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 586 | { |
---|
[2662] | 587 | MobileEntity::setPosition(this->client_position_); |
---|
| 588 | this->server_position_ = this->getPosition(); |
---|
[2072] | 589 | } |
---|
| 590 | } |
---|
| 591 | |
---|
[2662] | 592 | void ControllableEntity::processClientLinearVelocity() |
---|
[2072] | 593 | { |
---|
| 594 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 595 | { |
---|
[2662] | 596 | MobileEntity::setVelocity(this->client_linear_velocity_); |
---|
| 597 | this->server_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 598 | } |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | void ControllableEntity::processClientOrientation() |
---|
| 602 | { |
---|
| 603 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 604 | { |
---|
[2662] | 605 | MobileEntity::setOrientation(this->client_orientation_); |
---|
| 606 | this->server_orientation_ = this->getOrientation(); |
---|
[2072] | 607 | } |
---|
| 608 | } |
---|
| 609 | |
---|
[2662] | 610 | void ControllableEntity::processClientAngularVelocity() |
---|
[2072] | 611 | { |
---|
[2662] | 612 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
[2072] | 613 | { |
---|
[2662] | 614 | MobileEntity::setAngularVelocity(this->client_angular_velocity_); |
---|
| 615 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 616 | } |
---|
| 617 | } |
---|
| 618 | |
---|
[2662] | 619 | void ControllableEntity::setPosition(const Vector3& position) |
---|
[2072] | 620 | { |
---|
[2896] | 621 | if (GameMode::isMaster()) |
---|
[2072] | 622 | { |
---|
[2662] | 623 | MobileEntity::setPosition(position); |
---|
| 624 | this->server_position_ = this->getPosition(); |
---|
[2072] | 625 | ++this->server_overwrite_; |
---|
| 626 | } |
---|
[2662] | 627 | else if (this->bHasLocalController_) |
---|
[2072] | 628 | { |
---|
[2662] | 629 | MobileEntity::setPosition(position); |
---|
| 630 | this->client_position_ = this->getPosition(); |
---|
[2072] | 631 | } |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | void ControllableEntity::setOrientation(const Quaternion& orientation) |
---|
| 635 | { |
---|
[2896] | 636 | if (GameMode::isMaster()) |
---|
[2072] | 637 | { |
---|
[2662] | 638 | MobileEntity::setOrientation(orientation); |
---|
| 639 | this->server_orientation_ = this->getOrientation(); |
---|
[2072] | 640 | ++this->server_overwrite_; |
---|
| 641 | } |
---|
[2662] | 642 | else if (this->bHasLocalController_) |
---|
[2072] | 643 | { |
---|
[2662] | 644 | MobileEntity::setOrientation(orientation); |
---|
| 645 | this->client_orientation_ = this->getOrientation(); |
---|
[2072] | 646 | } |
---|
| 647 | } |
---|
| 648 | |
---|
[2662] | 649 | void ControllableEntity::setVelocity(const Vector3& velocity) |
---|
[2072] | 650 | { |
---|
[2896] | 651 | if (GameMode::isMaster()) |
---|
[2072] | 652 | { |
---|
[2662] | 653 | MobileEntity::setVelocity(velocity); |
---|
| 654 | this->server_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 655 | ++this->server_overwrite_; |
---|
| 656 | } |
---|
[2662] | 657 | else if (this->bHasLocalController_) |
---|
[2072] | 658 | { |
---|
[2662] | 659 | MobileEntity::setVelocity(velocity); |
---|
| 660 | this->client_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 661 | } |
---|
| 662 | } |
---|
| 663 | |
---|
[2662] | 664 | void ControllableEntity::setAngularVelocity(const Vector3& velocity) |
---|
[2072] | 665 | { |
---|
[2896] | 666 | if (GameMode::isMaster()) |
---|
[2072] | 667 | { |
---|
[2662] | 668 | MobileEntity::setAngularVelocity(velocity); |
---|
| 669 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 670 | ++this->server_overwrite_; |
---|
| 671 | } |
---|
[2662] | 672 | else if (this->bHasLocalController_) |
---|
[2072] | 673 | { |
---|
[2662] | 674 | MobileEntity::setAngularVelocity(velocity); |
---|
| 675 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 676 | } |
---|
| 677 | } |
---|
| 678 | |
---|
[2662] | 679 | void ControllableEntity::setWorldTransform(const btTransform& worldTrans) |
---|
[2072] | 680 | { |
---|
[2662] | 681 | MobileEntity::setWorldTransform(worldTrans); |
---|
[2896] | 682 | if (GameMode::isMaster()) |
---|
[2072] | 683 | { |
---|
[2662] | 684 | this->server_position_ = this->getPosition(); |
---|
| 685 | this->server_orientation_ = this->getOrientation(); |
---|
| 686 | this->server_linear_velocity_ = this->getVelocity(); |
---|
| 687 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 688 | } |
---|
[2662] | 689 | else if (this->bHasLocalController_) |
---|
[2072] | 690 | { |
---|
[2662] | 691 | this->client_position_ = this->getPosition(); |
---|
| 692 | this->client_orientation_ = this->getOrientation(); |
---|
| 693 | this->client_linear_velocity_ = this->getVelocity(); |
---|
| 694 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 695 | } |
---|
| 696 | } |
---|
| 697 | } |
---|