[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; |
---|
[8891] | 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; |
---|
[8891] | 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 | } |
---|
[8891] | 192 | |
---|
[8706] | 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 | |
---|
[8891] | 312 | void ControllableEntity::setController(Controller* val) |
---|
| 313 | { |
---|
| 314 | this->controller_ = val; |
---|
| 315 | } |
---|
| 316 | |
---|
[6417] | 317 | void ControllableEntity::setTarget( WorldEntity* target ) |
---|
| 318 | { |
---|
| 319 | this->target_ = target; |
---|
| 320 | if ( !GameMode::isMaster() ) |
---|
| 321 | { |
---|
| 322 | if ( target != 0 ) |
---|
| 323 | { |
---|
| 324 | callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, target->getObjectID() ); |
---|
| 325 | } |
---|
| 326 | else |
---|
| 327 | { |
---|
| 328 | callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, OBJECTID_UNKNOWN ); |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | void ControllableEntity::setTargetInternal( uint32_t targetID ) |
---|
| 334 | { |
---|
| 335 | this->setTarget( orxonox_cast<WorldEntity*>(Synchronisable::getSynchronisable(targetID)) ); |
---|
| 336 | } |
---|
| 337 | |
---|
[2072] | 338 | void ControllableEntity::setPlayer(PlayerInfo* player) |
---|
| 339 | { |
---|
| 340 | if (!player) |
---|
| 341 | { |
---|
| 342 | this->removePlayer(); |
---|
| 343 | return; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | this->player_ = player; |
---|
[7533] | 347 | this->formerPlayer_ = player; |
---|
[2072] | 348 | this->playerID_ = player->getObjectID(); |
---|
[2662] | 349 | this->bHasLocalController_ = player->isLocalPlayer(); |
---|
| 350 | this->bHasHumanController_ = player->isHumanPlayer(); |
---|
| 351 | |
---|
| 352 | if (this->bHasLocalController_ && this->bHasHumanController_) |
---|
[2072] | 353 | { |
---|
[2662] | 354 | this->startLocalHumanControl(); |
---|
[2072] | 355 | |
---|
[2896] | 356 | if (!GameMode::isMaster()) |
---|
[2072] | 357 | { |
---|
| 358 | this->client_overwrite_ = this->server_overwrite_; |
---|
[5929] | 359 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
[2072] | 360 | } |
---|
| 361 | } |
---|
[2839] | 362 | |
---|
| 363 | this->changedPlayer(); |
---|
[2072] | 364 | } |
---|
| 365 | |
---|
| 366 | void ControllableEntity::removePlayer() |
---|
| 367 | { |
---|
[2662] | 368 | if (this->bHasLocalController_ && this->bHasHumanController_) |
---|
| 369 | this->stopLocalHumanControl(); |
---|
[2072] | 370 | |
---|
| 371 | this->player_ = 0; |
---|
[2171] | 372 | this->playerID_ = OBJECTID_UNKNOWN; |
---|
[2662] | 373 | this->bHasLocalController_ = false; |
---|
| 374 | this->bHasHumanController_ = false; |
---|
[5929] | 375 | this->setSyncMode(ObjectDirection::ToClient); |
---|
[2072] | 376 | |
---|
[2839] | 377 | this->changedPlayer(); |
---|
| 378 | |
---|
[2072] | 379 | if (this->bDestroyWhenPlayerLeft_) |
---|
[5929] | 380 | this->destroy(); |
---|
[2072] | 381 | } |
---|
| 382 | |
---|
| 383 | void ControllableEntity::networkcallback_changedplayerID() |
---|
| 384 | { |
---|
| 385 | // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID |
---|
[2171] | 386 | if (this->playerID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 387 | { |
---|
[3325] | 388 | this->player_ = orxonox_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_)); |
---|
[2072] | 389 | if (this->player_ && (this->player_->getControllableEntity() != this)) |
---|
| 390 | this->player_->startControl(this); |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | |
---|
[2662] | 394 | void ControllableEntity::startLocalHumanControl() |
---|
| 395 | { |
---|
[5929] | 396 | if (!this->camera_ && GameMode::showsGraphics()) |
---|
[2072] | 397 | { |
---|
[2662] | 398 | this->camera_ = new Camera(this); |
---|
| 399 | this->camera_->requestFocus(); |
---|
[6417] | 400 | if (!this->cameraPositionTemplate_.empty()) |
---|
[2662] | 401 | this->addTemplate(this->cameraPositionTemplate_); |
---|
| 402 | if (this->cameraPositions_.size() > 0) |
---|
[6417] | 403 | { |
---|
[2662] | 404 | this->cameraPositions_.front()->attachCamera(this->camera_); |
---|
[6417] | 405 | this->currentCameraPosition_ = this->cameraPositions_.front(); |
---|
| 406 | } |
---|
[2662] | 407 | else |
---|
[6417] | 408 | { |
---|
[2662] | 409 | this->camera_->attachToNode(this->cameraPositionRootNode_); |
---|
[6417] | 410 | this->currentCameraPosition_ = 0; |
---|
| 411 | } |
---|
[2072] | 412 | } |
---|
[2662] | 413 | |
---|
[8706] | 414 | this->createHud(); |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | // HACK-ish |
---|
| 418 | void ControllableEntity::createHud(void) |
---|
| 419 | { |
---|
[5929] | 420 | if (!this->hud_ && GameMode::showsGraphics()) |
---|
[2662] | 421 | { |
---|
[6417] | 422 | if (!this->hudtemplate_.empty()) |
---|
[2662] | 423 | { |
---|
| 424 | this->hud_ = new OverlayGroup(this); |
---|
| 425 | this->hud_->addTemplate(this->hudtemplate_); |
---|
| 426 | this->hud_->setOwner(this); |
---|
| 427 | } |
---|
| 428 | } |
---|
[2072] | 429 | } |
---|
| 430 | |
---|
[8706] | 431 | void ControllableEntity::destroyHud(void) |
---|
| 432 | { |
---|
| 433 | if (this->hud_ != NULL) |
---|
| 434 | { |
---|
| 435 | this->hud_->destroy(); |
---|
| 436 | this->hud_ = NULL; |
---|
| 437 | } |
---|
| 438 | } |
---|
| 439 | |
---|
[2662] | 440 | void ControllableEntity::stopLocalHumanControl() |
---|
[2072] | 441 | { |
---|
[2662] | 442 | if (this->camera_) |
---|
| 443 | { |
---|
| 444 | this->camera_->detachFromParent(); |
---|
[5929] | 445 | this->camera_->destroy(); |
---|
[2662] | 446 | this->camera_ = 0; |
---|
| 447 | } |
---|
[2072] | 448 | |
---|
[2662] | 449 | if (this->hud_) |
---|
| 450 | { |
---|
[5929] | 451 | this->hud_->destroy(); |
---|
[2662] | 452 | this->hud_ = 0; |
---|
| 453 | } |
---|
[2072] | 454 | } |
---|
| 455 | |
---|
[3049] | 456 | void ControllableEntity::setXMLController(Controller* controller) |
---|
| 457 | { |
---|
| 458 | if (!this->xmlcontroller_) |
---|
| 459 | { |
---|
| 460 | this->xmlcontroller_ = controller; |
---|
| 461 | this->bHasLocalController_ = true; |
---|
| 462 | this->xmlcontroller_->setControllableEntity(this); |
---|
| 463 | } |
---|
| 464 | else |
---|
[8858] | 465 | orxout(internal_warning) << "ControllableEntity \"" << this->getName() << "\" already has a Controller." << endl; |
---|
[3049] | 466 | } |
---|
| 467 | |
---|
[2851] | 468 | void ControllableEntity::parentChanged() |
---|
| 469 | { |
---|
| 470 | WorldEntity::parentChanged(); |
---|
| 471 | |
---|
| 472 | WorldEntity* parent = this->getParent(); |
---|
| 473 | if (parent) |
---|
| 474 | { |
---|
[5929] | 475 | for (std::list<SmartPtr<CameraPosition> >::iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) |
---|
[2851] | 476 | if ((*it)->getIsAbsolute()) |
---|
| 477 | parent->attach((*it)); |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
[2072] | 481 | void ControllableEntity::tick(float dt) |
---|
| 482 | { |
---|
[2662] | 483 | MobileEntity::tick(dt); |
---|
| 484 | |
---|
[2072] | 485 | if (this->isActive()) |
---|
| 486 | { |
---|
[2662] | 487 | // Check whether Bullet doesn't do the physics for us |
---|
| 488 | if (!this->isDynamic()) |
---|
[2072] | 489 | { |
---|
[2896] | 490 | if (GameMode::isMaster()) |
---|
[2662] | 491 | { |
---|
| 492 | this->server_position_ = this->getPosition(); |
---|
| 493 | this->server_orientation_ = this->getOrientation(); |
---|
| 494 | this->server_linear_velocity_ = this->getVelocity(); |
---|
| 495 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
| 496 | } |
---|
| 497 | else if (this->bHasLocalController_) |
---|
| 498 | { |
---|
| 499 | this->client_position_ = this->getPosition(); |
---|
| 500 | this->client_orientation_ = this->getOrientation(); |
---|
| 501 | this->client_linear_velocity_ = this->getVelocity(); |
---|
| 502 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
| 503 | } |
---|
[2072] | 504 | } |
---|
| 505 | } |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | void ControllableEntity::registerVariables() |
---|
| 509 | { |
---|
[3280] | 510 | registerVariable(this->cameraPositionTemplate_, VariableDirection::ToClient); |
---|
| 511 | registerVariable(this->hudtemplate_, VariableDirection::ToClient); |
---|
[2072] | 512 | |
---|
[3280] | 513 | registerVariable(this->server_position_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition)); |
---|
| 514 | registerVariable(this->server_linear_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity)); |
---|
| 515 | registerVariable(this->server_orientation_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation)); |
---|
| 516 | registerVariable(this->server_angular_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity)); |
---|
[2072] | 517 | |
---|
[3280] | 518 | registerVariable(this->server_overwrite_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite)); |
---|
| 519 | registerVariable(this->client_overwrite_, VariableDirection::ToServer); |
---|
[2072] | 520 | |
---|
[3280] | 521 | registerVariable(this->client_position_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition)); |
---|
| 522 | registerVariable(this->client_linear_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity)); |
---|
| 523 | registerVariable(this->client_orientation_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation)); |
---|
| 524 | registerVariable(this->client_angular_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity)); |
---|
[2072] | 525 | |
---|
[5735] | 526 | |
---|
[3280] | 527 | registerVariable(this->playerID_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID)); |
---|
[2072] | 528 | } |
---|
| 529 | |
---|
| 530 | void ControllableEntity::processServerPosition() |
---|
| 531 | { |
---|
[2662] | 532 | if (!this->bHasLocalController_) |
---|
| 533 | MobileEntity::setPosition(this->server_position_); |
---|
[2072] | 534 | } |
---|
| 535 | |
---|
[2662] | 536 | void ControllableEntity::processServerLinearVelocity() |
---|
[2072] | 537 | { |
---|
[2662] | 538 | if (!this->bHasLocalController_) |
---|
| 539 | MobileEntity::setVelocity(this->server_linear_velocity_); |
---|
[2072] | 540 | } |
---|
| 541 | |
---|
| 542 | void ControllableEntity::processServerOrientation() |
---|
| 543 | { |
---|
[2662] | 544 | if (!this->bHasLocalController_) |
---|
| 545 | MobileEntity::setOrientation(this->server_orientation_); |
---|
[2072] | 546 | } |
---|
| 547 | |
---|
[2662] | 548 | void ControllableEntity::processServerAngularVelocity() |
---|
| 549 | { |
---|
| 550 | if (!this->bHasLocalController_) |
---|
| 551 | MobileEntity::setAngularVelocity(this->server_angular_velocity_); |
---|
| 552 | } |
---|
| 553 | |
---|
[2072] | 554 | void ControllableEntity::processOverwrite() |
---|
| 555 | { |
---|
[2662] | 556 | if (this->bHasLocalController_) |
---|
[2072] | 557 | { |
---|
| 558 | this->setPosition(this->server_position_); |
---|
| 559 | this->setOrientation(this->server_orientation_); |
---|
[2662] | 560 | this->setVelocity(this->server_linear_velocity_); |
---|
| 561 | this->setAngularVelocity(this->server_angular_velocity_); |
---|
[2072] | 562 | |
---|
| 563 | this->client_overwrite_ = this->server_overwrite_; |
---|
| 564 | } |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | void ControllableEntity::processClientPosition() |
---|
| 568 | { |
---|
| 569 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 570 | { |
---|
[2662] | 571 | MobileEntity::setPosition(this->client_position_); |
---|
| 572 | this->server_position_ = this->getPosition(); |
---|
[2072] | 573 | } |
---|
| 574 | } |
---|
| 575 | |
---|
[2662] | 576 | void ControllableEntity::processClientLinearVelocity() |
---|
[2072] | 577 | { |
---|
| 578 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 579 | { |
---|
[2662] | 580 | MobileEntity::setVelocity(this->client_linear_velocity_); |
---|
| 581 | this->server_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 582 | } |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | void ControllableEntity::processClientOrientation() |
---|
| 586 | { |
---|
| 587 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 588 | { |
---|
[2662] | 589 | MobileEntity::setOrientation(this->client_orientation_); |
---|
| 590 | this->server_orientation_ = this->getOrientation(); |
---|
[2072] | 591 | } |
---|
| 592 | } |
---|
| 593 | |
---|
[2662] | 594 | void ControllableEntity::processClientAngularVelocity() |
---|
[2072] | 595 | { |
---|
[2662] | 596 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
[2072] | 597 | { |
---|
[2662] | 598 | MobileEntity::setAngularVelocity(this->client_angular_velocity_); |
---|
| 599 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 600 | } |
---|
| 601 | } |
---|
| 602 | |
---|
[2662] | 603 | void ControllableEntity::setPosition(const Vector3& position) |
---|
[2072] | 604 | { |
---|
[2896] | 605 | if (GameMode::isMaster()) |
---|
[2072] | 606 | { |
---|
[2662] | 607 | MobileEntity::setPosition(position); |
---|
| 608 | this->server_position_ = this->getPosition(); |
---|
[2072] | 609 | ++this->server_overwrite_; |
---|
| 610 | } |
---|
[2662] | 611 | else if (this->bHasLocalController_) |
---|
[2072] | 612 | { |
---|
[2662] | 613 | MobileEntity::setPosition(position); |
---|
| 614 | this->client_position_ = this->getPosition(); |
---|
[2072] | 615 | } |
---|
| 616 | } |
---|
| 617 | |
---|
| 618 | void ControllableEntity::setOrientation(const Quaternion& orientation) |
---|
| 619 | { |
---|
[2896] | 620 | if (GameMode::isMaster()) |
---|
[2072] | 621 | { |
---|
[2662] | 622 | MobileEntity::setOrientation(orientation); |
---|
| 623 | this->server_orientation_ = this->getOrientation(); |
---|
[2072] | 624 | ++this->server_overwrite_; |
---|
| 625 | } |
---|
[2662] | 626 | else if (this->bHasLocalController_) |
---|
[2072] | 627 | { |
---|
[2662] | 628 | MobileEntity::setOrientation(orientation); |
---|
| 629 | this->client_orientation_ = this->getOrientation(); |
---|
[2072] | 630 | } |
---|
| 631 | } |
---|
| 632 | |
---|
[2662] | 633 | void ControllableEntity::setVelocity(const Vector3& velocity) |
---|
[2072] | 634 | { |
---|
[2896] | 635 | if (GameMode::isMaster()) |
---|
[2072] | 636 | { |
---|
[2662] | 637 | MobileEntity::setVelocity(velocity); |
---|
| 638 | this->server_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 639 | ++this->server_overwrite_; |
---|
| 640 | } |
---|
[2662] | 641 | else if (this->bHasLocalController_) |
---|
[2072] | 642 | { |
---|
[2662] | 643 | MobileEntity::setVelocity(velocity); |
---|
| 644 | this->client_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 645 | } |
---|
| 646 | } |
---|
| 647 | |
---|
[2662] | 648 | void ControllableEntity::setAngularVelocity(const Vector3& velocity) |
---|
[2072] | 649 | { |
---|
[2896] | 650 | if (GameMode::isMaster()) |
---|
[2072] | 651 | { |
---|
[2662] | 652 | MobileEntity::setAngularVelocity(velocity); |
---|
| 653 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 654 | ++this->server_overwrite_; |
---|
| 655 | } |
---|
[2662] | 656 | else if (this->bHasLocalController_) |
---|
[2072] | 657 | { |
---|
[2662] | 658 | MobileEntity::setAngularVelocity(velocity); |
---|
| 659 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 660 | } |
---|
| 661 | } |
---|
| 662 | |
---|
[2662] | 663 | void ControllableEntity::setWorldTransform(const btTransform& worldTrans) |
---|
[2072] | 664 | { |
---|
[2662] | 665 | MobileEntity::setWorldTransform(worldTrans); |
---|
[2896] | 666 | if (GameMode::isMaster()) |
---|
[2072] | 667 | { |
---|
[2662] | 668 | this->server_position_ = this->getPosition(); |
---|
| 669 | this->server_orientation_ = this->getOrientation(); |
---|
| 670 | this->server_linear_velocity_ = this->getVelocity(); |
---|
| 671 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 672 | } |
---|
[2662] | 673 | else if (this->bHasLocalController_) |
---|
[2072] | 674 | { |
---|
[2662] | 675 | this->client_position_ = this->getPosition(); |
---|
| 676 | this->client_orientation_ = this->getOrientation(); |
---|
| 677 | this->client_linear_velocity_ = this->getVelocity(); |
---|
| 678 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 679 | } |
---|
| 680 | } |
---|
| 681 | } |
---|