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