[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 |
---|
[2662] | 24 | * Reto Grieder |
---|
[2072] | 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "MovableEntity.h" |
---|
| 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/Executor.h" |
---|
[2896] | 34 | #include "core/GameMode.h" |
---|
[3196] | 35 | #include "core/XMLPort.h" |
---|
[3033] | 36 | #include "objects/worldentities/pawns/Pawn.h" |
---|
[2072] | 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
| 40 | static const float MAX_RESYNCHRONIZE_TIME = 3.0f; |
---|
[2662] | 41 | static const float CONTINUOUS_SYNCHRONIZATION_TIME = 10.0f; |
---|
[2072] | 42 | |
---|
| 43 | CreateFactory(MovableEntity); |
---|
| 44 | |
---|
[2662] | 45 | MovableEntity::MovableEntity(BaseObject* creator) : MobileEntity(creator) |
---|
[2072] | 46 | { |
---|
| 47 | RegisterObject(MovableEntity); |
---|
| 48 | |
---|
[2662] | 49 | this->overwrite_position_ = Vector3::ZERO; |
---|
[2072] | 50 | this->overwrite_orientation_ = Quaternion::IDENTITY; |
---|
| 51 | |
---|
[2662] | 52 | this->continuousResynchroTimer_ = 0; |
---|
| 53 | |
---|
[3280] | 54 | this->setPriority(Priority::Low); |
---|
[2662] | 55 | |
---|
[2072] | 56 | this->registerVariables(); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | MovableEntity::~MovableEntity() |
---|
| 60 | { |
---|
[2662] | 61 | if (this->isInitialized()) |
---|
| 62 | if (this->continuousResynchroTimer_) |
---|
| 63 | delete this->continuousResynchroTimer_; |
---|
[2072] | 64 | } |
---|
| 65 | |
---|
| 66 | void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 67 | { |
---|
| 68 | SUPER(MovableEntity, XMLPort, xmlelement, mode); |
---|
[3033] | 69 | |
---|
| 70 | XMLPortParam(MovableEntity, "enablecollisiondamage", setEnableCollisionDamage, getEnableCollisionDamage, xmlelement, mode).defaultValues(false); |
---|
| 71 | XMLPortParam(MovableEntity, "collisiondamage", setCollisionDamage, getCollisionDamage, xmlelement, mode).defaultValues(1); |
---|
[2072] | 72 | } |
---|
| 73 | |
---|
[3033] | 74 | bool MovableEntity::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint) |
---|
| 75 | { |
---|
| 76 | if (GameMode::isMaster() && enableCollisionDamage_) |
---|
| 77 | { |
---|
[3325] | 78 | Pawn* victim = orxonox_cast<Pawn*>(otherObject); |
---|
[3033] | 79 | if (victim) |
---|
| 80 | { |
---|
[3064] | 81 | victim->damage(this->collisionDamage_ * (victim->getVelocity() - this->getVelocity()).length()); |
---|
[3033] | 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | return false; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
[2072] | 89 | void MovableEntity::registerVariables() |
---|
| 90 | { |
---|
[3280] | 91 | registerVariable(this->linearVelocity_, VariableDirection::ToClient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processLinearVelocity)); |
---|
| 92 | registerVariable(this->angularVelocity_, VariableDirection::ToClient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processAngularVelocity)); |
---|
[2072] | 93 | |
---|
[3280] | 94 | registerVariable(this->overwrite_position_, VariableDirection::ToClient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition)); |
---|
| 95 | registerVariable(this->overwrite_orientation_, VariableDirection::ToClient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation)); |
---|
[2072] | 96 | } |
---|
| 97 | |
---|
| 98 | void MovableEntity::clientConnected(unsigned int clientID) |
---|
| 99 | { |
---|
[2662] | 100 | this->resynchronizeTimer_.setTimer(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&MovableEntity::resynchronize))); |
---|
[2072] | 101 | } |
---|
| 102 | |
---|
| 103 | void MovableEntity::clientDisconnected(unsigned int clientID) |
---|
| 104 | { |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void MovableEntity::resynchronize() |
---|
| 108 | { |
---|
[2896] | 109 | if (GameMode::isMaster() && !this->continuousResynchroTimer_) |
---|
[2662] | 110 | { |
---|
| 111 | // Resynchronise every few seconds because we only work with velocities (no positions) |
---|
| 112 | continuousResynchroTimer_ = new Timer<MovableEntity>(CONTINUOUS_SYNCHRONIZATION_TIME + rnd(-1, 1), |
---|
| 113 | true, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), false); |
---|
| 114 | } |
---|
| 115 | |
---|
[2072] | 116 | this->overwrite_position_ = this->getPosition(); |
---|
| 117 | this->overwrite_orientation_ = this->getOrientation(); |
---|
| 118 | } |
---|
| 119 | } |
---|