[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" |
---|
[2662] | 35 | #include "core/ConfigValueIncludes.h" |
---|
[2896] | 36 | #include "core/GameMode.h" |
---|
[2072] | 37 | #include "core/XMLPort.h" |
---|
[6417] | 38 | #include "network/NetworkFunction.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 | { |
---|
| 49 | CreateFactory(ControllableEntity); |
---|
| 50 | |
---|
[6417] | 51 | registerMemberNetworkFunction( ControllableEntity, fire ); |
---|
| 52 | registerMemberNetworkFunction( ControllableEntity, setTargetInternal ); |
---|
| 53 | |
---|
[2662] | 54 | ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator) |
---|
[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; |
---|
| 63 | this->player_ = 0; |
---|
[7534] | 64 | this->formerPlayer_ = NULL; |
---|
[2171] | 65 | this->playerID_ = OBJECTID_UNKNOWN; |
---|
[2072] | 66 | this->hud_ = 0; |
---|
| 67 | this->camera_ = 0; |
---|
[3049] | 68 | this->xmlcontroller_ = 0; |
---|
[6417] | 69 | this->controller_ = 0; |
---|
[3089] | 70 | this->reverseCamera_ = 0; |
---|
[2072] | 71 | this->bDestroyWhenPlayerLeft_ = false; |
---|
[2662] | 72 | this->cameraPositionRootNode_ = this->node_->createChildSceneNode(); |
---|
[6417] | 73 | this->currentCameraPosition_ = 0; |
---|
[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 | |
---|
| 87 | this->setConfigValues(); |
---|
[3280] | 88 | this->setPriority( Priority::VeryHigh ); |
---|
[2072] | 89 | this->registerVariables(); |
---|
| 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 | |
---|
[5929] | 110 | for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) |
---|
| 111 | (*it)->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 | |
---|
| 122 | XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode); |
---|
| 123 | XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode); |
---|
| 124 | |
---|
| 125 | XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode); |
---|
[3049] | 126 | XMLPortObject(ControllableEntity, Controller, "controller", setXMLController, getXMLController, xmlelement, mode); |
---|
[2072] | 127 | } |
---|
| 128 | |
---|
[2662] | 129 | void ControllableEntity::setConfigValues() |
---|
| 130 | { |
---|
| 131 | SetConfigValue(mouseLookSpeed_, 3.0f); |
---|
| 132 | } |
---|
| 133 | |
---|
[7889] | 134 | void ControllableEntity::preDestroy() |
---|
| 135 | { |
---|
| 136 | // HACK - solve this clean and without preDestroy hook for multiplayer where removePlayer() isn't called |
---|
[7892] | 137 | if (this->isInitialized() && this->bHasLocalController_ && this->bHasHumanController_) |
---|
[7889] | 138 | this->stopLocalHumanControl(); |
---|
| 139 | } |
---|
| 140 | |
---|
[2072] | 141 | void ControllableEntity::addCameraPosition(CameraPosition* position) |
---|
| 142 | { |
---|
[2826] | 143 | if (!position->getIsAbsolute()) |
---|
| 144 | { |
---|
| 145 | if (position->getAllowMouseLook()) |
---|
| 146 | position->attachToNode(this->cameraPositionRootNode_); |
---|
| 147 | else |
---|
| 148 | this->attach(position); |
---|
| 149 | } |
---|
[2662] | 150 | else |
---|
[2826] | 151 | { |
---|
| 152 | WorldEntity* parent = this->getParent(); |
---|
| 153 | if (parent) |
---|
| 154 | parent->attach(position); |
---|
| 155 | } |
---|
[3089] | 156 | |
---|
| 157 | if (!position->getRenderCamera()) |
---|
| 158 | this->cameraPositions_.push_back(position); |
---|
| 159 | else |
---|
| 160 | this->setReverseCamera(position); |
---|
[2072] | 161 | } |
---|
| 162 | |
---|
| 163 | CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const |
---|
| 164 | { |
---|
| 165 | unsigned int i = 0; |
---|
[5929] | 166 | for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) |
---|
[2072] | 167 | { |
---|
| 168 | if (i == index) |
---|
| 169 | return (*it); |
---|
| 170 | ++i; |
---|
| 171 | } |
---|
| 172 | return 0; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void ControllableEntity::switchCamera() |
---|
| 176 | { |
---|
| 177 | if (this->camera_) |
---|
| 178 | { |
---|
| 179 | if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0) |
---|
| 180 | { |
---|
| 181 | this->cameraPositions_.front()->attachCamera(this->camera_); |
---|
[6417] | 182 | this->currentCameraPosition_ = this->cameraPositions_.front().get(); |
---|
[2072] | 183 | } |
---|
| 184 | else if (this->cameraPositions_.size() > 0) |
---|
| 185 | { |
---|
[5929] | 186 | for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) |
---|
[2072] | 187 | { |
---|
| 188 | if ((*it) == this->camera_->getParent()) |
---|
| 189 | { |
---|
| 190 | ++it; |
---|
| 191 | if (it != this->cameraPositions_.end()) |
---|
[6417] | 192 | { |
---|
[2072] | 193 | (*it)->attachCamera(this->camera_); |
---|
[6417] | 194 | this->currentCameraPosition_ = *it; |
---|
| 195 | } |
---|
[2072] | 196 | else |
---|
[6417] | 197 | { |
---|
[2072] | 198 | (*this->cameraPositions_.begin())->attachCamera(this->camera_); |
---|
[6417] | 199 | this->currentCameraPosition_ = *this->cameraPositions_.begin(); |
---|
| 200 | } |
---|
[2072] | 201 | break; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | else |
---|
| 206 | { |
---|
[2662] | 207 | this->camera_->attachToNode(this->cameraPositionRootNode_); |
---|
[6417] | 208 | this->currentCameraPosition_ = 0; |
---|
[2072] | 209 | } |
---|
[7860] | 210 | |
---|
[7857] | 211 | // disable mouse look if the new camera position doesn't allow it |
---|
| 212 | if (this->currentCameraPosition_ && !this->currentCameraPosition_->getAllowMouseLook() && this->bMouseLook_) |
---|
| 213 | this->mouseLook(); |
---|
[7860] | 214 | |
---|
| 215 | // disable drag if in mouse look |
---|
| 216 | if (this->bMouseLook_) |
---|
| 217 | this->getCamera()->setDrag(false); |
---|
[2072] | 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
[2662] | 221 | void ControllableEntity::mouseLook() |
---|
| 222 | { |
---|
[7857] | 223 | // enable mouse look only if allowed - disabling it works always |
---|
| 224 | if (this->currentCameraPosition_ && (this->currentCameraPosition_->getAllowMouseLook() || this->bMouseLook_)) |
---|
| 225 | { |
---|
| 226 | this->bMouseLook_ = !this->bMouseLook_; |
---|
[2662] | 227 | |
---|
[7857] | 228 | if (!this->bMouseLook_) |
---|
[7860] | 229 | { |
---|
[7857] | 230 | this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY); |
---|
[7860] | 231 | this->cameraPositionRootNode_->_update(true, false); // update the camera node because otherwise the camera will drag back in position which looks strange |
---|
| 232 | |
---|
| 233 | NewHumanController* controller = dynamic_cast<NewHumanController*>(this->getController()); |
---|
| 234 | if (controller) |
---|
| 235 | controller->centerCursor(); |
---|
| 236 | } |
---|
| 237 | |
---|
[7857] | 238 | if (this->getCamera()) |
---|
| 239 | { |
---|
| 240 | if (!this->bMouseLook_ && this->currentCameraPosition_->getDrag()) |
---|
| 241 | this->getCamera()->setDrag(true); |
---|
| 242 | else |
---|
| 243 | this->getCamera()->setDrag(false); |
---|
| 244 | } |
---|
[6417] | 245 | } |
---|
[2662] | 246 | } |
---|
| 247 | |
---|
| 248 | void ControllableEntity::rotateYaw(const Vector2& value) |
---|
| 249 | { |
---|
| 250 | if (this->bMouseLook_) |
---|
| 251 | this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | void ControllableEntity::rotatePitch(const Vector2& value) |
---|
| 255 | { |
---|
| 256 | if (this->bMouseLook_) |
---|
| 257 | this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | void ControllableEntity::rotateRoll(const Vector2& value) |
---|
| 261 | { |
---|
| 262 | if (this->bMouseLook_) |
---|
| 263 | this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); |
---|
| 264 | } |
---|
| 265 | |
---|
[6417] | 266 | void ControllableEntity::fire(unsigned int firemode) |
---|
| 267 | { |
---|
| 268 | if(GameMode::isMaster()) |
---|
| 269 | { |
---|
| 270 | this->fired(firemode); |
---|
| 271 | } |
---|
| 272 | else |
---|
| 273 | { |
---|
| 274 | callMemberNetworkFunction(ControllableEntity, fire, this->getObjectID(), 0, firemode); |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | void ControllableEntity::setTarget( WorldEntity* target ) |
---|
| 279 | { |
---|
| 280 | this->target_ = target; |
---|
| 281 | if ( !GameMode::isMaster() ) |
---|
| 282 | { |
---|
| 283 | if ( target != 0 ) |
---|
| 284 | { |
---|
| 285 | callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, target->getObjectID() ); |
---|
| 286 | } |
---|
| 287 | else |
---|
| 288 | { |
---|
| 289 | callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, OBJECTID_UNKNOWN ); |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | void ControllableEntity::setTargetInternal( uint32_t targetID ) |
---|
| 295 | { |
---|
| 296 | this->setTarget( orxonox_cast<WorldEntity*>(Synchronisable::getSynchronisable(targetID)) ); |
---|
| 297 | } |
---|
| 298 | |
---|
[2072] | 299 | void ControllableEntity::setPlayer(PlayerInfo* player) |
---|
| 300 | { |
---|
| 301 | if (!player) |
---|
| 302 | { |
---|
| 303 | this->removePlayer(); |
---|
| 304 | return; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | this->player_ = player; |
---|
[7533] | 308 | this->formerPlayer_ = player; |
---|
[2072] | 309 | this->playerID_ = player->getObjectID(); |
---|
[2662] | 310 | this->bHasLocalController_ = player->isLocalPlayer(); |
---|
| 311 | this->bHasHumanController_ = player->isHumanPlayer(); |
---|
| 312 | |
---|
| 313 | if (this->bHasLocalController_ && this->bHasHumanController_) |
---|
[2072] | 314 | { |
---|
[2662] | 315 | this->startLocalHumanControl(); |
---|
[2072] | 316 | |
---|
[2896] | 317 | if (!GameMode::isMaster()) |
---|
[2072] | 318 | { |
---|
| 319 | this->client_overwrite_ = this->server_overwrite_; |
---|
[5929] | 320 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
[2072] | 321 | } |
---|
| 322 | } |
---|
[2839] | 323 | |
---|
| 324 | this->changedPlayer(); |
---|
[2072] | 325 | } |
---|
| 326 | |
---|
| 327 | void ControllableEntity::removePlayer() |
---|
| 328 | { |
---|
[2662] | 329 | if (this->bHasLocalController_ && this->bHasHumanController_) |
---|
| 330 | this->stopLocalHumanControl(); |
---|
[2072] | 331 | |
---|
| 332 | this->player_ = 0; |
---|
[2171] | 333 | this->playerID_ = OBJECTID_UNKNOWN; |
---|
[2662] | 334 | this->bHasLocalController_ = false; |
---|
| 335 | this->bHasHumanController_ = false; |
---|
[5929] | 336 | this->setSyncMode(ObjectDirection::ToClient); |
---|
[2072] | 337 | |
---|
[2839] | 338 | this->changedPlayer(); |
---|
| 339 | |
---|
[2072] | 340 | if (this->bDestroyWhenPlayerLeft_) |
---|
[5929] | 341 | this->destroy(); |
---|
[2072] | 342 | } |
---|
| 343 | |
---|
| 344 | void ControllableEntity::networkcallback_changedplayerID() |
---|
| 345 | { |
---|
| 346 | // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID |
---|
[2171] | 347 | if (this->playerID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 348 | { |
---|
[3325] | 349 | this->player_ = orxonox_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_)); |
---|
[2072] | 350 | if (this->player_ && (this->player_->getControllableEntity() != this)) |
---|
| 351 | this->player_->startControl(this); |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
[2662] | 355 | void ControllableEntity::startLocalHumanControl() |
---|
| 356 | { |
---|
[5929] | 357 | if (!this->camera_ && GameMode::showsGraphics()) |
---|
[2072] | 358 | { |
---|
[2662] | 359 | this->camera_ = new Camera(this); |
---|
| 360 | this->camera_->requestFocus(); |
---|
[6417] | 361 | if (!this->cameraPositionTemplate_.empty()) |
---|
[2662] | 362 | this->addTemplate(this->cameraPositionTemplate_); |
---|
| 363 | if (this->cameraPositions_.size() > 0) |
---|
[6417] | 364 | { |
---|
[2662] | 365 | this->cameraPositions_.front()->attachCamera(this->camera_); |
---|
[6417] | 366 | this->currentCameraPosition_ = this->cameraPositions_.front(); |
---|
| 367 | } |
---|
[2662] | 368 | else |
---|
[6417] | 369 | { |
---|
[2662] | 370 | this->camera_->attachToNode(this->cameraPositionRootNode_); |
---|
[6417] | 371 | this->currentCameraPosition_ = 0; |
---|
| 372 | } |
---|
[2072] | 373 | } |
---|
[2662] | 374 | |
---|
[5929] | 375 | if (!this->hud_ && GameMode::showsGraphics()) |
---|
[2662] | 376 | { |
---|
[6417] | 377 | if (!this->hudtemplate_.empty()) |
---|
[2662] | 378 | { |
---|
| 379 | this->hud_ = new OverlayGroup(this); |
---|
| 380 | this->hud_->addTemplate(this->hudtemplate_); |
---|
| 381 | this->hud_->setOwner(this); |
---|
| 382 | } |
---|
| 383 | } |
---|
[2072] | 384 | } |
---|
| 385 | |
---|
[2662] | 386 | void ControllableEntity::stopLocalHumanControl() |
---|
[2072] | 387 | { |
---|
[2662] | 388 | if (this->camera_) |
---|
| 389 | { |
---|
| 390 | this->camera_->detachFromParent(); |
---|
[5929] | 391 | this->camera_->destroy(); |
---|
[2662] | 392 | this->camera_ = 0; |
---|
| 393 | } |
---|
[2072] | 394 | |
---|
[2662] | 395 | if (this->hud_) |
---|
| 396 | { |
---|
[5929] | 397 | this->hud_->destroy(); |
---|
[2662] | 398 | this->hud_ = 0; |
---|
| 399 | } |
---|
[2072] | 400 | } |
---|
| 401 | |
---|
[3049] | 402 | void ControllableEntity::setXMLController(Controller* controller) |
---|
| 403 | { |
---|
| 404 | if (!this->xmlcontroller_) |
---|
| 405 | { |
---|
| 406 | this->xmlcontroller_ = controller; |
---|
| 407 | this->bHasLocalController_ = true; |
---|
| 408 | this->xmlcontroller_->setControllableEntity(this); |
---|
| 409 | } |
---|
| 410 | else |
---|
| 411 | COUT(2) << "Warning: ControllableEntity \"" << this->getName() << "\" already has a Controller." << std::endl; |
---|
| 412 | } |
---|
| 413 | |
---|
[2851] | 414 | void ControllableEntity::parentChanged() |
---|
| 415 | { |
---|
| 416 | WorldEntity::parentChanged(); |
---|
| 417 | |
---|
| 418 | WorldEntity* parent = this->getParent(); |
---|
| 419 | if (parent) |
---|
| 420 | { |
---|
[5929] | 421 | for (std::list<SmartPtr<CameraPosition> >::iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) |
---|
[2851] | 422 | if ((*it)->getIsAbsolute()) |
---|
| 423 | parent->attach((*it)); |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | |
---|
[2072] | 427 | void ControllableEntity::tick(float dt) |
---|
| 428 | { |
---|
[2662] | 429 | MobileEntity::tick(dt); |
---|
| 430 | |
---|
[2072] | 431 | if (this->isActive()) |
---|
| 432 | { |
---|
[2662] | 433 | // Check whether Bullet doesn't do the physics for us |
---|
| 434 | if (!this->isDynamic()) |
---|
[2072] | 435 | { |
---|
[2896] | 436 | if (GameMode::isMaster()) |
---|
[2662] | 437 | { |
---|
| 438 | this->server_position_ = this->getPosition(); |
---|
| 439 | this->server_orientation_ = this->getOrientation(); |
---|
| 440 | this->server_linear_velocity_ = this->getVelocity(); |
---|
| 441 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
| 442 | } |
---|
| 443 | else if (this->bHasLocalController_) |
---|
| 444 | { |
---|
| 445 | this->client_position_ = this->getPosition(); |
---|
| 446 | this->client_orientation_ = this->getOrientation(); |
---|
| 447 | this->client_linear_velocity_ = this->getVelocity(); |
---|
| 448 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
| 449 | } |
---|
[2072] | 450 | } |
---|
| 451 | } |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | void ControllableEntity::registerVariables() |
---|
| 455 | { |
---|
[3280] | 456 | registerVariable(this->cameraPositionTemplate_, VariableDirection::ToClient); |
---|
| 457 | registerVariable(this->hudtemplate_, VariableDirection::ToClient); |
---|
[2072] | 458 | |
---|
[3280] | 459 | registerVariable(this->server_position_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition)); |
---|
| 460 | registerVariable(this->server_linear_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity)); |
---|
| 461 | registerVariable(this->server_orientation_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation)); |
---|
| 462 | registerVariable(this->server_angular_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity)); |
---|
[2072] | 463 | |
---|
[3280] | 464 | registerVariable(this->server_overwrite_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite)); |
---|
| 465 | registerVariable(this->client_overwrite_, VariableDirection::ToServer); |
---|
[2072] | 466 | |
---|
[3280] | 467 | registerVariable(this->client_position_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition)); |
---|
| 468 | registerVariable(this->client_linear_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity)); |
---|
| 469 | registerVariable(this->client_orientation_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation)); |
---|
| 470 | registerVariable(this->client_angular_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity)); |
---|
[2072] | 471 | |
---|
[5735] | 472 | |
---|
[3280] | 473 | registerVariable(this->playerID_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID)); |
---|
[2072] | 474 | } |
---|
| 475 | |
---|
| 476 | void ControllableEntity::processServerPosition() |
---|
| 477 | { |
---|
[2662] | 478 | if (!this->bHasLocalController_) |
---|
| 479 | MobileEntity::setPosition(this->server_position_); |
---|
[2072] | 480 | } |
---|
| 481 | |
---|
[2662] | 482 | void ControllableEntity::processServerLinearVelocity() |
---|
[2072] | 483 | { |
---|
[2662] | 484 | if (!this->bHasLocalController_) |
---|
| 485 | MobileEntity::setVelocity(this->server_linear_velocity_); |
---|
[2072] | 486 | } |
---|
| 487 | |
---|
| 488 | void ControllableEntity::processServerOrientation() |
---|
| 489 | { |
---|
[2662] | 490 | if (!this->bHasLocalController_) |
---|
| 491 | MobileEntity::setOrientation(this->server_orientation_); |
---|
[2072] | 492 | } |
---|
| 493 | |
---|
[2662] | 494 | void ControllableEntity::processServerAngularVelocity() |
---|
| 495 | { |
---|
| 496 | if (!this->bHasLocalController_) |
---|
| 497 | MobileEntity::setAngularVelocity(this->server_angular_velocity_); |
---|
| 498 | } |
---|
| 499 | |
---|
[2072] | 500 | void ControllableEntity::processOverwrite() |
---|
| 501 | { |
---|
[2662] | 502 | if (this->bHasLocalController_) |
---|
[2072] | 503 | { |
---|
| 504 | this->setPosition(this->server_position_); |
---|
| 505 | this->setOrientation(this->server_orientation_); |
---|
[2662] | 506 | this->setVelocity(this->server_linear_velocity_); |
---|
| 507 | this->setAngularVelocity(this->server_angular_velocity_); |
---|
[2072] | 508 | |
---|
| 509 | this->client_overwrite_ = this->server_overwrite_; |
---|
| 510 | } |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | void ControllableEntity::processClientPosition() |
---|
| 514 | { |
---|
| 515 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 516 | { |
---|
[2662] | 517 | MobileEntity::setPosition(this->client_position_); |
---|
| 518 | this->server_position_ = this->getPosition(); |
---|
[2072] | 519 | } |
---|
| 520 | } |
---|
| 521 | |
---|
[2662] | 522 | void ControllableEntity::processClientLinearVelocity() |
---|
[2072] | 523 | { |
---|
| 524 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 525 | { |
---|
[2662] | 526 | MobileEntity::setVelocity(this->client_linear_velocity_); |
---|
| 527 | this->server_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 528 | } |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | void ControllableEntity::processClientOrientation() |
---|
| 532 | { |
---|
| 533 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 534 | { |
---|
[2662] | 535 | MobileEntity::setOrientation(this->client_orientation_); |
---|
| 536 | this->server_orientation_ = this->getOrientation(); |
---|
[2072] | 537 | } |
---|
| 538 | } |
---|
| 539 | |
---|
[2662] | 540 | void ControllableEntity::processClientAngularVelocity() |
---|
[2072] | 541 | { |
---|
[2662] | 542 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
[2072] | 543 | { |
---|
[2662] | 544 | MobileEntity::setAngularVelocity(this->client_angular_velocity_); |
---|
| 545 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 546 | } |
---|
| 547 | } |
---|
| 548 | |
---|
[2662] | 549 | void ControllableEntity::setPosition(const Vector3& position) |
---|
[2072] | 550 | { |
---|
[2896] | 551 | if (GameMode::isMaster()) |
---|
[2072] | 552 | { |
---|
[2662] | 553 | MobileEntity::setPosition(position); |
---|
| 554 | this->server_position_ = this->getPosition(); |
---|
[2072] | 555 | ++this->server_overwrite_; |
---|
| 556 | } |
---|
[2662] | 557 | else if (this->bHasLocalController_) |
---|
[2072] | 558 | { |
---|
[2662] | 559 | MobileEntity::setPosition(position); |
---|
| 560 | this->client_position_ = this->getPosition(); |
---|
[2072] | 561 | } |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | void ControllableEntity::setOrientation(const Quaternion& orientation) |
---|
| 565 | { |
---|
[2896] | 566 | if (GameMode::isMaster()) |
---|
[2072] | 567 | { |
---|
[2662] | 568 | MobileEntity::setOrientation(orientation); |
---|
| 569 | this->server_orientation_ = this->getOrientation(); |
---|
[2072] | 570 | ++this->server_overwrite_; |
---|
| 571 | } |
---|
[2662] | 572 | else if (this->bHasLocalController_) |
---|
[2072] | 573 | { |
---|
[2662] | 574 | MobileEntity::setOrientation(orientation); |
---|
| 575 | this->client_orientation_ = this->getOrientation(); |
---|
[2072] | 576 | } |
---|
| 577 | } |
---|
| 578 | |
---|
[2662] | 579 | void ControllableEntity::setVelocity(const Vector3& velocity) |
---|
[2072] | 580 | { |
---|
[2896] | 581 | if (GameMode::isMaster()) |
---|
[2072] | 582 | { |
---|
[2662] | 583 | MobileEntity::setVelocity(velocity); |
---|
| 584 | this->server_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 585 | ++this->server_overwrite_; |
---|
| 586 | } |
---|
[2662] | 587 | else if (this->bHasLocalController_) |
---|
[2072] | 588 | { |
---|
[2662] | 589 | MobileEntity::setVelocity(velocity); |
---|
| 590 | this->client_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 591 | } |
---|
| 592 | } |
---|
| 593 | |
---|
[2662] | 594 | void ControllableEntity::setAngularVelocity(const Vector3& velocity) |
---|
[2072] | 595 | { |
---|
[2896] | 596 | if (GameMode::isMaster()) |
---|
[2072] | 597 | { |
---|
[2662] | 598 | MobileEntity::setAngularVelocity(velocity); |
---|
| 599 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 600 | ++this->server_overwrite_; |
---|
| 601 | } |
---|
[2662] | 602 | else if (this->bHasLocalController_) |
---|
[2072] | 603 | { |
---|
[2662] | 604 | MobileEntity::setAngularVelocity(velocity); |
---|
| 605 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 606 | } |
---|
| 607 | } |
---|
| 608 | |
---|
[2662] | 609 | void ControllableEntity::setWorldTransform(const btTransform& worldTrans) |
---|
[2072] | 610 | { |
---|
[2662] | 611 | MobileEntity::setWorldTransform(worldTrans); |
---|
[2896] | 612 | if (GameMode::isMaster()) |
---|
[2072] | 613 | { |
---|
[2662] | 614 | this->server_position_ = this->getPosition(); |
---|
| 615 | this->server_orientation_ = this->getOrientation(); |
---|
| 616 | this->server_linear_velocity_ = this->getVelocity(); |
---|
| 617 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 618 | } |
---|
[2662] | 619 | else if (this->bHasLocalController_) |
---|
[2072] | 620 | { |
---|
[2662] | 621 | this->client_position_ = this->getPosition(); |
---|
| 622 | this->client_orientation_ = this->getOrientation(); |
---|
| 623 | this->client_linear_velocity_ = this->getVelocity(); |
---|
| 624 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 625 | } |
---|
| 626 | } |
---|
| 627 | } |
---|