[1989] | 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: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ControllableEntity.h" |
---|
| 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
| 32 | #include "core/Core.h" |
---|
| 33 | #include "core/XMLPort.h" |
---|
| 34 | #include "core/Template.h" |
---|
| 35 | |
---|
| 36 | #include "objects/infos/PlayerInfo.h" |
---|
| 37 | #include "objects/Camera.h" |
---|
| 38 | #include "overlays/OverlayGroup.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | CreateFactory(ControllableEntity); |
---|
| 43 | |
---|
| 44 | ControllableEntity::ControllableEntity() |
---|
| 45 | { |
---|
| 46 | RegisterObject(ControllableEntity); |
---|
| 47 | |
---|
| 48 | this->bControlled_ = false; |
---|
| 49 | this->server_overwrite_ = 0; |
---|
| 50 | this->client_overwrite_ = 0; |
---|
| 51 | this->player_ = 0; |
---|
| 52 | this->playerID_ = network::OBJECTID_UNKNOWN; |
---|
| 53 | this->hud_ = 0; |
---|
| 54 | this->camera_ = 0; |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | this->velocity_ = Vector3::ZERO; |
---|
| 58 | this->acceleration_ = Vector3::ZERO; |
---|
| 59 | |
---|
| 60 | this->server_position_ = Vector3::ZERO; |
---|
| 61 | this->client_position_ = Vector3::ZERO; |
---|
| 62 | this->server_velocity_ = Vector3::ZERO; |
---|
| 63 | this->client_velocity_ = Vector3::ZERO; |
---|
| 64 | this->server_orientation_ = Quaternion::IDENTITY; |
---|
| 65 | this->client_orientation_ = Quaternion::IDENTITY; |
---|
| 66 | |
---|
| 67 | this->registerVariables(); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | ControllableEntity::~ControllableEntity() |
---|
| 71 | { |
---|
| 72 | if (this->isInitialized() && this->bControlled_) |
---|
| 73 | this->stopLocalControl(); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 77 | { |
---|
| 78 | SUPER(ControllableEntity, XMLPort, xmlelement, mode); |
---|
| 79 | |
---|
| 80 | XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | void ControllableEntity::setPlayer(PlayerInfo* player) |
---|
| 84 | { |
---|
| 85 | if (!player) |
---|
| 86 | { |
---|
| 87 | this->removePlayer(); |
---|
| 88 | return; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | this->player_ = player; |
---|
| 92 | this->playerID_ = player->getObjectID(); |
---|
| 93 | this->bControlled_ = player->isLocalPlayer(); |
---|
| 94 | |
---|
| 95 | if (this->bControlled_) |
---|
| 96 | this->startLocalControl(); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void ControllableEntity::removePlayer() |
---|
| 100 | { |
---|
| 101 | if (this->bControlled_) |
---|
| 102 | this->stopLocalControl(); |
---|
| 103 | |
---|
| 104 | this->player_ = 0; |
---|
| 105 | this->playerID_ = network::OBJECTID_UNKNOWN; |
---|
| 106 | this->bControlled_ = false; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | void ControllableEntity::updatePlayer() |
---|
| 110 | { |
---|
| 111 | this->player_ = dynamic_cast<PlayerInfo*>(network::Synchronisable::getSynchronisable(this->playerID_)); |
---|
| 112 | if (this->player_ && (this->player_->getPawn() != this)) |
---|
| 113 | this->player_->startControl(this); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void ControllableEntity::startLocalControl() |
---|
| 117 | { |
---|
| 118 | this->camera_ = new Camera(); |
---|
| 119 | this->camera_->requestFocus(); |
---|
| 120 | |
---|
| 121 | this->hud_ = new OverlayGroup(); |
---|
| 122 | this->hud_->addTemplate(this->hudtemplate_); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | void ControllableEntity::stopLocalControl() |
---|
| 126 | { |
---|
| 127 | delete this->camera_; |
---|
| 128 | this->camera_ = 0; |
---|
| 129 | |
---|
| 130 | delete this->hud_; |
---|
| 131 | this->hud_ = 0; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | void ControllableEntity::tick(float dt) |
---|
| 135 | { |
---|
| 136 | if (this->isActive()) |
---|
| 137 | { |
---|
| 138 | this->velocity_ += (dt * this->acceleration_); |
---|
| 139 | this->node_->translate(dt * this->velocity_); |
---|
| 140 | |
---|
| 141 | if (Core::isMaster()) |
---|
| 142 | { |
---|
| 143 | this->server_velocity_ = this->velocity_; |
---|
| 144 | this->server_position_ = this->node_->getPosition(); |
---|
| 145 | } |
---|
| 146 | else if (this->bControlled_) |
---|
| 147 | { |
---|
| 148 | this->client_velocity_ = this->velocity_; |
---|
| 149 | this->client_position_ = this->node_->getPosition(); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | // std::cout << this->getPosition() << std::endl; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | void ControllableEntity::registerVariables() |
---|
| 157 | { |
---|
| 158 | REGISTERDATA(this->server_position_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition)); |
---|
| 159 | REGISTERDATA(this->server_velocity_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerVelocity)); |
---|
| 160 | REGISTERDATA(this->server_orientation_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation)); |
---|
| 161 | |
---|
| 162 | REGISTERDATA(this->server_overwrite_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite)); |
---|
| 163 | |
---|
| 164 | REGISTERDATA(this->client_position_, network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition)); |
---|
| 165 | REGISTERDATA(this->client_velocity_, network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientVelocity)); |
---|
| 166 | REGISTERDATA(this->client_orientation_, network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation)); |
---|
| 167 | |
---|
| 168 | REGISTERDATA(this->client_overwrite_, network::direction::toserver); |
---|
| 169 | |
---|
| 170 | REGISTERDATA(this->playerID_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::updatePlayer)); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void ControllableEntity::processServerPosition() |
---|
| 174 | { |
---|
| 175 | if (!this->bControlled_) |
---|
| 176 | this->node_->setPosition(this->server_position_); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | void ControllableEntity::processServerVelocity() |
---|
| 180 | { |
---|
| 181 | if (!this->bControlled_) |
---|
| 182 | this->velocity_ = this->server_velocity_; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void ControllableEntity::processServerOrientation() |
---|
| 186 | { |
---|
| 187 | if (!this->bControlled_) |
---|
| 188 | this->node_->setOrientation(this->server_orientation_); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | void ControllableEntity::processOverwrite() |
---|
| 192 | { |
---|
| 193 | if (this->bControlled_) |
---|
| 194 | { |
---|
| 195 | this->setPosition(this->server_position_); |
---|
| 196 | this->setVelocity(this->server_velocity_); |
---|
| 197 | this->setOrientation(this->server_orientation_); |
---|
| 198 | |
---|
| 199 | this->client_overwrite_ = this->server_overwrite_; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | void ControllableEntity::processClientPosition() |
---|
| 204 | { |
---|
| 205 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 206 | { |
---|
| 207 | this->node_->setPosition(this->client_position_); |
---|
| 208 | this->server_position_ = this->client_position_; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | void ControllableEntity::processClientVelocity() |
---|
| 213 | { |
---|
| 214 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 215 | { |
---|
| 216 | this->velocity_ = this->client_velocity_; |
---|
| 217 | this->server_velocity_ = this->client_velocity_; |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | void ControllableEntity::processClientOrientation() |
---|
| 222 | { |
---|
| 223 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 224 | { |
---|
| 225 | this->node_->setOrientation(this->client_orientation_); |
---|
| 226 | this->server_orientation_ = this->client_orientation_; |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | void ControllableEntity::setPosition(const Vector3& position) |
---|
| 232 | { |
---|
| 233 | if (Core::isMaster()) |
---|
| 234 | { |
---|
| 235 | this->node_->setPosition(position); |
---|
| 236 | this->server_position_ = position; |
---|
| 237 | ++this->server_overwrite_; |
---|
| 238 | } |
---|
| 239 | else if (this->bControlled_) |
---|
| 240 | { |
---|
| 241 | this->node_->setPosition(position); |
---|
| 242 | this->client_position_ = position; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | void ControllableEntity::setVelocity(const Vector3& velocity) |
---|
| 247 | { |
---|
| 248 | if (Core::isMaster()) |
---|
| 249 | { |
---|
| 250 | this->velocity_ = velocity; |
---|
| 251 | this->server_velocity_ = velocity; |
---|
| 252 | ++this->server_overwrite_; |
---|
| 253 | } |
---|
| 254 | else if (this->bControlled_) |
---|
| 255 | { |
---|
| 256 | this->velocity_ = velocity; |
---|
| 257 | this->client_velocity_ = velocity; |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | void ControllableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo) |
---|
| 262 | { |
---|
| 263 | if (Core::isMaster()) |
---|
| 264 | { |
---|
| 265 | this->node_->translate(distance, relativeTo); |
---|
| 266 | this->server_position_ = this->node_->getPosition(); |
---|
| 267 | ++this->server_overwrite_; |
---|
| 268 | } |
---|
| 269 | else if (this->bControlled_) |
---|
| 270 | { |
---|
| 271 | this->node_->translate(distance, relativeTo); |
---|
| 272 | this->client_position_ = this->node_->getPosition(); |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | void ControllableEntity::setOrientation(const Quaternion& orientation) |
---|
| 277 | { |
---|
| 278 | if (Core::isMaster()) |
---|
| 279 | { |
---|
| 280 | this->node_->setOrientation(orientation); |
---|
| 281 | this->server_orientation_ = orientation; |
---|
| 282 | ++this->server_overwrite_; |
---|
| 283 | } |
---|
| 284 | else if (this->bControlled_) |
---|
| 285 | { |
---|
| 286 | this->node_->setOrientation(orientation); |
---|
| 287 | this->client_orientation_ = orientation; |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | void ControllableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo) |
---|
| 292 | { |
---|
| 293 | if (Core::isMaster()) |
---|
| 294 | { |
---|
| 295 | this->node_->rotate(rotation, relativeTo); |
---|
| 296 | this->server_orientation_ = this->node_->getOrientation(); |
---|
| 297 | ++this->server_overwrite_; |
---|
| 298 | } |
---|
| 299 | else if (this->bControlled_) |
---|
| 300 | { |
---|
| 301 | this->node_->rotate(rotation, relativeTo); |
---|
| 302 | this->client_orientation_ = this->node_->getOrientation(); |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | void ControllableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
| 307 | { |
---|
| 308 | if (Core::isMaster()) |
---|
| 309 | { |
---|
| 310 | this->node_->yaw(angle, relativeTo); |
---|
| 311 | this->server_orientation_ = this->node_->getOrientation(); |
---|
| 312 | ++this->server_overwrite_; |
---|
| 313 | } |
---|
| 314 | else if (this->bControlled_) |
---|
| 315 | { |
---|
| 316 | this->node_->yaw(angle, relativeTo); |
---|
| 317 | this->client_orientation_ = this->node_->getOrientation(); |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | void ControllableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
| 322 | { |
---|
| 323 | if (Core::isMaster()) |
---|
| 324 | { |
---|
| 325 | this->node_->pitch(angle, relativeTo); |
---|
| 326 | this->server_orientation_ = this->node_->getOrientation(); |
---|
| 327 | ++this->server_overwrite_; |
---|
| 328 | } |
---|
| 329 | else if (this->bControlled_) |
---|
| 330 | { |
---|
| 331 | this->node_->pitch(angle, relativeTo); |
---|
| 332 | this->client_orientation_ = this->node_->getOrientation(); |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | void ControllableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
| 337 | { |
---|
| 338 | if (Core::isMaster()) |
---|
| 339 | { |
---|
| 340 | this->node_->roll(angle, relativeTo); |
---|
| 341 | this->server_orientation_ = this->node_->getOrientation(); |
---|
| 342 | ++this->server_overwrite_; |
---|
| 343 | } |
---|
| 344 | else if (this->bControlled_) |
---|
| 345 | { |
---|
| 346 | this->node_->roll(angle, relativeTo); |
---|
| 347 | this->client_orientation_ = this->node_->getOrientation(); |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | void ControllableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
| 352 | { |
---|
| 353 | if (Core::isMaster()) |
---|
| 354 | { |
---|
| 355 | this->node_->lookAt(target, relativeTo, localDirectionVector); |
---|
| 356 | this->server_orientation_ = this->node_->getOrientation(); |
---|
| 357 | ++this->server_overwrite_; |
---|
| 358 | } |
---|
| 359 | else if (this->bControlled_) |
---|
| 360 | { |
---|
| 361 | this->node_->lookAt(target, relativeTo, localDirectionVector); |
---|
| 362 | this->client_orientation_ = this->node_->getOrientation(); |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | void ControllableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
| 367 | { |
---|
| 368 | if (Core::isMaster()) |
---|
| 369 | { |
---|
| 370 | this->node_->setDirection(direction, relativeTo, localDirectionVector); |
---|
| 371 | this->server_orientation_ = this->node_->getOrientation(); |
---|
| 372 | ++this->server_overwrite_; |
---|
| 373 | } |
---|
| 374 | else if (this->bControlled_) |
---|
| 375 | { |
---|
| 376 | this->node_->setDirection(direction, relativeTo, localDirectionVector); |
---|
| 377 | this->client_orientation_ = this->node_->getOrientation(); |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | } |
---|