[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; |
---|
[8733] | 76 | this->bIsRocket_ = false; |
---|
[2072] | 77 | |
---|
[2662] | 78 | this->server_position_ = Vector3::ZERO; |
---|
| 79 | this->client_position_ = Vector3::ZERO; |
---|
| 80 | this->server_linear_velocity_ = Vector3::ZERO; |
---|
| 81 | this->client_linear_velocity_ = Vector3::ZERO; |
---|
| 82 | this->server_orientation_ = Quaternion::IDENTITY; |
---|
| 83 | this->client_orientation_ = Quaternion::IDENTITY; |
---|
| 84 | this->server_angular_velocity_ = Vector3::ZERO; |
---|
| 85 | this->client_angular_velocity_ = Vector3::ZERO; |
---|
[2072] | 86 | |
---|
[2662] | 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); |
---|
[8706] | 123 | XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemplate, xmlelement, mode); |
---|
[2072] | 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 | |
---|
[8706] | 175 | unsigned int ControllableEntity::getCurrentCameraIndex() const |
---|
| 176 | { |
---|
| 177 | if (this->cameraPositions_.size() <= 0) |
---|
| 178 | return 0; |
---|
| 179 | |
---|
| 180 | unsigned int counter = 0; |
---|
| 181 | for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) |
---|
| 182 | { |
---|
| 183 | if ((*it) == this->currentCameraPosition_) |
---|
| 184 | break; |
---|
| 185 | counter++; |
---|
| 186 | } |
---|
| 187 | if (counter >= this->cameraPositions_.size()) |
---|
| 188 | return 0; |
---|
| 189 | |
---|
| 190 | return counter; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | bool ControllableEntity::setCameraPosition(unsigned int index) |
---|
| 194 | { |
---|
| 195 | if(this->camera_ != NULL && this->cameraPositions_.size() > 0) |
---|
| 196 | { |
---|
| 197 | if(index >= this->cameraPositions_.size()) |
---|
| 198 | index = 0; |
---|
| 199 | |
---|
| 200 | CameraPosition* position = this->getCameraPosition(index); |
---|
| 201 | position->attachCamera(this->camera_); |
---|
| 202 | this->currentCameraPosition_ = position; |
---|
| 203 | return true; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | return false; |
---|
| 207 | } |
---|
| 208 | |
---|
[2072] | 209 | void ControllableEntity::switchCamera() |
---|
| 210 | { |
---|
| 211 | if (this->camera_) |
---|
| 212 | { |
---|
| 213 | if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0) |
---|
| 214 | { |
---|
| 215 | this->cameraPositions_.front()->attachCamera(this->camera_); |
---|
[6417] | 216 | this->currentCameraPosition_ = this->cameraPositions_.front().get(); |
---|
[2072] | 217 | } |
---|
| 218 | else if (this->cameraPositions_.size() > 0) |
---|
| 219 | { |
---|
[5929] | 220 | for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) |
---|
[2072] | 221 | { |
---|
| 222 | if ((*it) == this->camera_->getParent()) |
---|
| 223 | { |
---|
| 224 | ++it; |
---|
| 225 | if (it != this->cameraPositions_.end()) |
---|
[6417] | 226 | { |
---|
[2072] | 227 | (*it)->attachCamera(this->camera_); |
---|
[6417] | 228 | this->currentCameraPosition_ = *it; |
---|
| 229 | } |
---|
[2072] | 230 | else |
---|
[6417] | 231 | { |
---|
[2072] | 232 | (*this->cameraPositions_.begin())->attachCamera(this->camera_); |
---|
[6417] | 233 | this->currentCameraPosition_ = *this->cameraPositions_.begin(); |
---|
| 234 | } |
---|
[2072] | 235 | break; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | else |
---|
| 240 | { |
---|
[2662] | 241 | this->camera_->attachToNode(this->cameraPositionRootNode_); |
---|
[6417] | 242 | this->currentCameraPosition_ = 0; |
---|
[2072] | 243 | } |
---|
[7860] | 244 | |
---|
[7857] | 245 | // disable mouse look if the new camera position doesn't allow it |
---|
| 246 | if (this->currentCameraPosition_ && !this->currentCameraPosition_->getAllowMouseLook() && this->bMouseLook_) |
---|
| 247 | this->mouseLook(); |
---|
[7860] | 248 | |
---|
| 249 | // disable drag if in mouse look |
---|
| 250 | if (this->bMouseLook_) |
---|
| 251 | this->getCamera()->setDrag(false); |
---|
[2072] | 252 | } |
---|
| 253 | } |
---|
| 254 | |
---|
[2662] | 255 | void ControllableEntity::mouseLook() |
---|
| 256 | { |
---|
[7857] | 257 | // enable mouse look only if allowed - disabling it works always |
---|
| 258 | if (this->currentCameraPosition_ && (this->currentCameraPosition_->getAllowMouseLook() || this->bMouseLook_)) |
---|
| 259 | { |
---|
| 260 | this->bMouseLook_ = !this->bMouseLook_; |
---|
[2662] | 261 | |
---|
[7857] | 262 | if (!this->bMouseLook_) |
---|
[7860] | 263 | { |
---|
[7857] | 264 | this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY); |
---|
[7860] | 265 | this->cameraPositionRootNode_->_update(true, false); // update the camera node because otherwise the camera will drag back in position which looks strange |
---|
| 266 | |
---|
| 267 | NewHumanController* controller = dynamic_cast<NewHumanController*>(this->getController()); |
---|
| 268 | if (controller) |
---|
| 269 | controller->centerCursor(); |
---|
| 270 | } |
---|
| 271 | |
---|
[7857] | 272 | if (this->getCamera()) |
---|
| 273 | { |
---|
| 274 | if (!this->bMouseLook_ && this->currentCameraPosition_->getDrag()) |
---|
| 275 | this->getCamera()->setDrag(true); |
---|
| 276 | else |
---|
| 277 | this->getCamera()->setDrag(false); |
---|
| 278 | } |
---|
[6417] | 279 | } |
---|
[2662] | 280 | } |
---|
| 281 | |
---|
| 282 | void ControllableEntity::rotateYaw(const Vector2& value) |
---|
| 283 | { |
---|
| 284 | if (this->bMouseLook_) |
---|
| 285 | this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | void ControllableEntity::rotatePitch(const Vector2& value) |
---|
| 289 | { |
---|
| 290 | if (this->bMouseLook_) |
---|
| 291 | this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | void ControllableEntity::rotateRoll(const Vector2& value) |
---|
| 295 | { |
---|
| 296 | if (this->bMouseLook_) |
---|
| 297 | this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); |
---|
| 298 | } |
---|
| 299 | |
---|
[6417] | 300 | void ControllableEntity::fire(unsigned int firemode) |
---|
| 301 | { |
---|
| 302 | if(GameMode::isMaster()) |
---|
| 303 | { |
---|
| 304 | this->fired(firemode); |
---|
| 305 | } |
---|
| 306 | else |
---|
| 307 | { |
---|
| 308 | callMemberNetworkFunction(ControllableEntity, fire, this->getObjectID(), 0, firemode); |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | void ControllableEntity::setTarget( WorldEntity* target ) |
---|
| 313 | { |
---|
| 314 | this->target_ = target; |
---|
| 315 | if ( !GameMode::isMaster() ) |
---|
| 316 | { |
---|
| 317 | if ( target != 0 ) |
---|
| 318 | { |
---|
| 319 | callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, target->getObjectID() ); |
---|
| 320 | } |
---|
| 321 | else |
---|
| 322 | { |
---|
| 323 | callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, OBJECTID_UNKNOWN ); |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | void ControllableEntity::setTargetInternal( uint32_t targetID ) |
---|
| 329 | { |
---|
| 330 | this->setTarget( orxonox_cast<WorldEntity*>(Synchronisable::getSynchronisable(targetID)) ); |
---|
| 331 | } |
---|
| 332 | |
---|
[2072] | 333 | void ControllableEntity::setPlayer(PlayerInfo* player) |
---|
| 334 | { |
---|
| 335 | if (!player) |
---|
| 336 | { |
---|
| 337 | this->removePlayer(); |
---|
| 338 | return; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | this->player_ = player; |
---|
[7533] | 342 | this->formerPlayer_ = player; |
---|
[2072] | 343 | this->playerID_ = player->getObjectID(); |
---|
[2662] | 344 | this->bHasLocalController_ = player->isLocalPlayer(); |
---|
| 345 | this->bHasHumanController_ = player->isHumanPlayer(); |
---|
| 346 | |
---|
| 347 | if (this->bHasLocalController_ && this->bHasHumanController_) |
---|
[2072] | 348 | { |
---|
[2662] | 349 | this->startLocalHumanControl(); |
---|
[2072] | 350 | |
---|
[2896] | 351 | if (!GameMode::isMaster()) |
---|
[2072] | 352 | { |
---|
| 353 | this->client_overwrite_ = this->server_overwrite_; |
---|
[5929] | 354 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
[2072] | 355 | } |
---|
| 356 | } |
---|
[2839] | 357 | |
---|
| 358 | this->changedPlayer(); |
---|
[2072] | 359 | } |
---|
| 360 | |
---|
| 361 | void ControllableEntity::removePlayer() |
---|
| 362 | { |
---|
[2662] | 363 | if (this->bHasLocalController_ && this->bHasHumanController_) |
---|
| 364 | this->stopLocalHumanControl(); |
---|
[2072] | 365 | |
---|
| 366 | this->player_ = 0; |
---|
[2171] | 367 | this->playerID_ = OBJECTID_UNKNOWN; |
---|
[2662] | 368 | this->bHasLocalController_ = false; |
---|
| 369 | this->bHasHumanController_ = false; |
---|
[5929] | 370 | this->setSyncMode(ObjectDirection::ToClient); |
---|
[2072] | 371 | |
---|
[2839] | 372 | this->changedPlayer(); |
---|
| 373 | |
---|
[2072] | 374 | if (this->bDestroyWhenPlayerLeft_) |
---|
[5929] | 375 | this->destroy(); |
---|
[2072] | 376 | } |
---|
| 377 | |
---|
| 378 | void ControllableEntity::networkcallback_changedplayerID() |
---|
| 379 | { |
---|
| 380 | // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID |
---|
[2171] | 381 | if (this->playerID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 382 | { |
---|
[3325] | 383 | this->player_ = orxonox_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_)); |
---|
[2072] | 384 | if (this->player_ && (this->player_->getControllableEntity() != this)) |
---|
| 385 | this->player_->startControl(this); |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | |
---|
[2662] | 389 | void ControllableEntity::startLocalHumanControl() |
---|
| 390 | { |
---|
[5929] | 391 | if (!this->camera_ && GameMode::showsGraphics()) |
---|
[2072] | 392 | { |
---|
[2662] | 393 | this->camera_ = new Camera(this); |
---|
| 394 | this->camera_->requestFocus(); |
---|
[6417] | 395 | if (!this->cameraPositionTemplate_.empty()) |
---|
[2662] | 396 | this->addTemplate(this->cameraPositionTemplate_); |
---|
| 397 | if (this->cameraPositions_.size() > 0) |
---|
[6417] | 398 | { |
---|
[2662] | 399 | this->cameraPositions_.front()->attachCamera(this->camera_); |
---|
[6417] | 400 | this->currentCameraPosition_ = this->cameraPositions_.front(); |
---|
| 401 | } |
---|
[2662] | 402 | else |
---|
[6417] | 403 | { |
---|
[2662] | 404 | this->camera_->attachToNode(this->cameraPositionRootNode_); |
---|
[6417] | 405 | this->currentCameraPosition_ = 0; |
---|
| 406 | } |
---|
[2072] | 407 | } |
---|
[2662] | 408 | |
---|
[8706] | 409 | this->createHud(); |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | // HACK-ish |
---|
| 413 | void ControllableEntity::createHud(void) |
---|
| 414 | { |
---|
[5929] | 415 | if (!this->hud_ && GameMode::showsGraphics()) |
---|
[2662] | 416 | { |
---|
[6417] | 417 | if (!this->hudtemplate_.empty()) |
---|
[2662] | 418 | { |
---|
| 419 | this->hud_ = new OverlayGroup(this); |
---|
| 420 | this->hud_->addTemplate(this->hudtemplate_); |
---|
| 421 | this->hud_->setOwner(this); |
---|
| 422 | } |
---|
| 423 | } |
---|
[2072] | 424 | } |
---|
| 425 | |
---|
[8706] | 426 | void ControllableEntity::destroyHud(void) |
---|
| 427 | { |
---|
| 428 | if (this->hud_ != NULL) |
---|
| 429 | { |
---|
| 430 | this->hud_->destroy(); |
---|
| 431 | this->hud_ = NULL; |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | |
---|
[2662] | 435 | void ControllableEntity::stopLocalHumanControl() |
---|
[2072] | 436 | { |
---|
[2662] | 437 | if (this->camera_) |
---|
| 438 | { |
---|
| 439 | this->camera_->detachFromParent(); |
---|
[5929] | 440 | this->camera_->destroy(); |
---|
[2662] | 441 | this->camera_ = 0; |
---|
| 442 | } |
---|
[2072] | 443 | |
---|
[2662] | 444 | if (this->hud_) |
---|
| 445 | { |
---|
[5929] | 446 | this->hud_->destroy(); |
---|
[2662] | 447 | this->hud_ = 0; |
---|
| 448 | } |
---|
[2072] | 449 | } |
---|
| 450 | |
---|
[3049] | 451 | void ControllableEntity::setXMLController(Controller* controller) |
---|
| 452 | { |
---|
| 453 | if (!this->xmlcontroller_) |
---|
| 454 | { |
---|
| 455 | this->xmlcontroller_ = controller; |
---|
| 456 | this->bHasLocalController_ = true; |
---|
| 457 | this->xmlcontroller_->setControllableEntity(this); |
---|
| 458 | } |
---|
| 459 | else |
---|
| 460 | COUT(2) << "Warning: ControllableEntity \"" << this->getName() << "\" already has a Controller." << std::endl; |
---|
| 461 | } |
---|
| 462 | |
---|
[2851] | 463 | void ControllableEntity::parentChanged() |
---|
| 464 | { |
---|
| 465 | WorldEntity::parentChanged(); |
---|
| 466 | |
---|
| 467 | WorldEntity* parent = this->getParent(); |
---|
| 468 | if (parent) |
---|
| 469 | { |
---|
[5929] | 470 | for (std::list<SmartPtr<CameraPosition> >::iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) |
---|
[2851] | 471 | if ((*it)->getIsAbsolute()) |
---|
| 472 | parent->attach((*it)); |
---|
| 473 | } |
---|
| 474 | } |
---|
| 475 | |
---|
[2072] | 476 | void ControllableEntity::tick(float dt) |
---|
| 477 | { |
---|
[2662] | 478 | MobileEntity::tick(dt); |
---|
| 479 | |
---|
[2072] | 480 | if (this->isActive()) |
---|
| 481 | { |
---|
[2662] | 482 | // Check whether Bullet doesn't do the physics for us |
---|
| 483 | if (!this->isDynamic()) |
---|
[2072] | 484 | { |
---|
[2896] | 485 | if (GameMode::isMaster()) |
---|
[2662] | 486 | { |
---|
| 487 | this->server_position_ = this->getPosition(); |
---|
| 488 | this->server_orientation_ = this->getOrientation(); |
---|
| 489 | this->server_linear_velocity_ = this->getVelocity(); |
---|
| 490 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
| 491 | } |
---|
| 492 | else if (this->bHasLocalController_) |
---|
| 493 | { |
---|
| 494 | this->client_position_ = this->getPosition(); |
---|
| 495 | this->client_orientation_ = this->getOrientation(); |
---|
| 496 | this->client_linear_velocity_ = this->getVelocity(); |
---|
| 497 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
| 498 | } |
---|
[2072] | 499 | } |
---|
| 500 | } |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | void ControllableEntity::registerVariables() |
---|
| 504 | { |
---|
[3280] | 505 | registerVariable(this->cameraPositionTemplate_, VariableDirection::ToClient); |
---|
| 506 | registerVariable(this->hudtemplate_, VariableDirection::ToClient); |
---|
[2072] | 507 | |
---|
[3280] | 508 | registerVariable(this->server_position_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition)); |
---|
| 509 | registerVariable(this->server_linear_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity)); |
---|
| 510 | registerVariable(this->server_orientation_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation)); |
---|
| 511 | registerVariable(this->server_angular_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity)); |
---|
[2072] | 512 | |
---|
[3280] | 513 | registerVariable(this->server_overwrite_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite)); |
---|
| 514 | registerVariable(this->client_overwrite_, VariableDirection::ToServer); |
---|
[2072] | 515 | |
---|
[3280] | 516 | registerVariable(this->client_position_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition)); |
---|
| 517 | registerVariable(this->client_linear_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity)); |
---|
| 518 | registerVariable(this->client_orientation_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation)); |
---|
| 519 | registerVariable(this->client_angular_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity)); |
---|
[2072] | 520 | |
---|
[5735] | 521 | |
---|
[3280] | 522 | registerVariable(this->playerID_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID)); |
---|
[2072] | 523 | } |
---|
| 524 | |
---|
| 525 | void ControllableEntity::processServerPosition() |
---|
| 526 | { |
---|
[2662] | 527 | if (!this->bHasLocalController_) |
---|
| 528 | MobileEntity::setPosition(this->server_position_); |
---|
[2072] | 529 | } |
---|
| 530 | |
---|
[2662] | 531 | void ControllableEntity::processServerLinearVelocity() |
---|
[2072] | 532 | { |
---|
[2662] | 533 | if (!this->bHasLocalController_) |
---|
| 534 | MobileEntity::setVelocity(this->server_linear_velocity_); |
---|
[2072] | 535 | } |
---|
| 536 | |
---|
| 537 | void ControllableEntity::processServerOrientation() |
---|
| 538 | { |
---|
[2662] | 539 | if (!this->bHasLocalController_) |
---|
| 540 | MobileEntity::setOrientation(this->server_orientation_); |
---|
[2072] | 541 | } |
---|
| 542 | |
---|
[2662] | 543 | void ControllableEntity::processServerAngularVelocity() |
---|
| 544 | { |
---|
| 545 | if (!this->bHasLocalController_) |
---|
| 546 | MobileEntity::setAngularVelocity(this->server_angular_velocity_); |
---|
| 547 | } |
---|
| 548 | |
---|
[2072] | 549 | void ControllableEntity::processOverwrite() |
---|
| 550 | { |
---|
[2662] | 551 | if (this->bHasLocalController_) |
---|
[2072] | 552 | { |
---|
| 553 | this->setPosition(this->server_position_); |
---|
| 554 | this->setOrientation(this->server_orientation_); |
---|
[2662] | 555 | this->setVelocity(this->server_linear_velocity_); |
---|
| 556 | this->setAngularVelocity(this->server_angular_velocity_); |
---|
[2072] | 557 | |
---|
| 558 | this->client_overwrite_ = this->server_overwrite_; |
---|
| 559 | } |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | void ControllableEntity::processClientPosition() |
---|
| 563 | { |
---|
| 564 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 565 | { |
---|
[2662] | 566 | MobileEntity::setPosition(this->client_position_); |
---|
| 567 | this->server_position_ = this->getPosition(); |
---|
[2072] | 568 | } |
---|
| 569 | } |
---|
| 570 | |
---|
[2662] | 571 | void ControllableEntity::processClientLinearVelocity() |
---|
[2072] | 572 | { |
---|
| 573 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 574 | { |
---|
[2662] | 575 | MobileEntity::setVelocity(this->client_linear_velocity_); |
---|
| 576 | this->server_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 577 | } |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | void ControllableEntity::processClientOrientation() |
---|
| 581 | { |
---|
| 582 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 583 | { |
---|
[2662] | 584 | MobileEntity::setOrientation(this->client_orientation_); |
---|
| 585 | this->server_orientation_ = this->getOrientation(); |
---|
[2072] | 586 | } |
---|
| 587 | } |
---|
| 588 | |
---|
[2662] | 589 | void ControllableEntity::processClientAngularVelocity() |
---|
[2072] | 590 | { |
---|
[2662] | 591 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
[2072] | 592 | { |
---|
[2662] | 593 | MobileEntity::setAngularVelocity(this->client_angular_velocity_); |
---|
| 594 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 595 | } |
---|
| 596 | } |
---|
| 597 | |
---|
[2662] | 598 | void ControllableEntity::setPosition(const Vector3& position) |
---|
[2072] | 599 | { |
---|
[2896] | 600 | if (GameMode::isMaster()) |
---|
[2072] | 601 | { |
---|
[2662] | 602 | MobileEntity::setPosition(position); |
---|
| 603 | this->server_position_ = this->getPosition(); |
---|
[2072] | 604 | ++this->server_overwrite_; |
---|
| 605 | } |
---|
[2662] | 606 | else if (this->bHasLocalController_) |
---|
[2072] | 607 | { |
---|
[2662] | 608 | MobileEntity::setPosition(position); |
---|
| 609 | this->client_position_ = this->getPosition(); |
---|
[2072] | 610 | } |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | void ControllableEntity::setOrientation(const Quaternion& orientation) |
---|
| 614 | { |
---|
[2896] | 615 | if (GameMode::isMaster()) |
---|
[2072] | 616 | { |
---|
[2662] | 617 | MobileEntity::setOrientation(orientation); |
---|
| 618 | this->server_orientation_ = this->getOrientation(); |
---|
[2072] | 619 | ++this->server_overwrite_; |
---|
| 620 | } |
---|
[2662] | 621 | else if (this->bHasLocalController_) |
---|
[2072] | 622 | { |
---|
[2662] | 623 | MobileEntity::setOrientation(orientation); |
---|
| 624 | this->client_orientation_ = this->getOrientation(); |
---|
[2072] | 625 | } |
---|
| 626 | } |
---|
| 627 | |
---|
[2662] | 628 | void ControllableEntity::setVelocity(const Vector3& velocity) |
---|
[2072] | 629 | { |
---|
[2896] | 630 | if (GameMode::isMaster()) |
---|
[2072] | 631 | { |
---|
[2662] | 632 | MobileEntity::setVelocity(velocity); |
---|
| 633 | this->server_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 634 | ++this->server_overwrite_; |
---|
| 635 | } |
---|
[2662] | 636 | else if (this->bHasLocalController_) |
---|
[2072] | 637 | { |
---|
[2662] | 638 | MobileEntity::setVelocity(velocity); |
---|
| 639 | this->client_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 640 | } |
---|
| 641 | } |
---|
| 642 | |
---|
[2662] | 643 | void ControllableEntity::setAngularVelocity(const Vector3& velocity) |
---|
[2072] | 644 | { |
---|
[2896] | 645 | if (GameMode::isMaster()) |
---|
[2072] | 646 | { |
---|
[2662] | 647 | MobileEntity::setAngularVelocity(velocity); |
---|
| 648 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 649 | ++this->server_overwrite_; |
---|
| 650 | } |
---|
[2662] | 651 | else if (this->bHasLocalController_) |
---|
[2072] | 652 | { |
---|
[2662] | 653 | MobileEntity::setAngularVelocity(velocity); |
---|
| 654 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 655 | } |
---|
| 656 | } |
---|
| 657 | |
---|
[2662] | 658 | void ControllableEntity::setWorldTransform(const btTransform& worldTrans) |
---|
[2072] | 659 | { |
---|
[2662] | 660 | MobileEntity::setWorldTransform(worldTrans); |
---|
[2896] | 661 | if (GameMode::isMaster()) |
---|
[2072] | 662 | { |
---|
[2662] | 663 | this->server_position_ = this->getPosition(); |
---|
| 664 | this->server_orientation_ = this->getOrientation(); |
---|
| 665 | this->server_linear_velocity_ = this->getVelocity(); |
---|
| 666 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 667 | } |
---|
[2662] | 668 | else if (this->bHasLocalController_) |
---|
[2072] | 669 | { |
---|
[2662] | 670 | this->client_position_ = this->getPosition(); |
---|
| 671 | this->client_orientation_ = this->getOrientation(); |
---|
| 672 | this->client_linear_velocity_ = this->getVelocity(); |
---|
| 673 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 674 | } |
---|
| 675 | } |
---|
| 676 | } |
---|