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