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 "OrxonoxStableHeaders.h" |
---|
30 | #include "MovableEntity.h" |
---|
31 | |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | #include "core/XMLPort.h" |
---|
34 | #include "core/Executor.h" |
---|
35 | #include "tools/Timer.h" |
---|
36 | |
---|
37 | namespace orxonox |
---|
38 | { |
---|
39 | static const float MAX_RESYNCHRONIZE_TIME = 3.0f; |
---|
40 | |
---|
41 | CreateFactory(MovableEntity); |
---|
42 | |
---|
43 | MovableEntity::MovableEntity(BaseObject* creator) : WorldEntity(creator) |
---|
44 | { |
---|
45 | RegisterObject(MovableEntity); |
---|
46 | |
---|
47 | this->velocity_ = Vector3::ZERO; |
---|
48 | this->acceleration_ = Vector3::ZERO; |
---|
49 | this->rotationAxis_ = Vector3::ZERO; |
---|
50 | this->rotationRate_ = 0; |
---|
51 | this->momentum_ = 0; |
---|
52 | |
---|
53 | this->overwrite_position_ = Vector3::ZERO; |
---|
54 | this->overwrite_orientation_ = Quaternion::IDENTITY; |
---|
55 | |
---|
56 | this->setPriority( priority::low ); |
---|
57 | |
---|
58 | this->registerVariables(); |
---|
59 | } |
---|
60 | |
---|
61 | MovableEntity::~MovableEntity() |
---|
62 | { |
---|
63 | } |
---|
64 | |
---|
65 | void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
66 | { |
---|
67 | SUPER(MovableEntity, XMLPort, xmlelement, mode); |
---|
68 | |
---|
69 | XMLPortParamTemplate(MovableEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&); |
---|
70 | XMLPortParamTemplate(MovableEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&); |
---|
71 | XMLPortParamTemplate(MovableEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode, const Degree&); |
---|
72 | } |
---|
73 | |
---|
74 | void MovableEntity::tick(float dt) |
---|
75 | { |
---|
76 | if (this->isActive()) |
---|
77 | { |
---|
78 | this->velocity_ += (dt * this->acceleration_); |
---|
79 | this->node_->translate(dt * this->velocity_); |
---|
80 | |
---|
81 | this->rotationRate_ += (dt * this->momentum_); |
---|
82 | this->node_->rotate(this->rotationAxis_, this->rotationRate_ * dt); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | void MovableEntity::registerVariables() |
---|
87 | { |
---|
88 | registerVariable(this->velocity_.x, variableDirection::toclient); |
---|
89 | registerVariable(this->velocity_.y, variableDirection::toclient); |
---|
90 | registerVariable(this->velocity_.z, variableDirection::toclient); |
---|
91 | |
---|
92 | registerVariable(this->rotationAxis_.x, variableDirection::toclient); |
---|
93 | registerVariable(this->rotationAxis_.y, variableDirection::toclient); |
---|
94 | registerVariable(this->rotationAxis_.z, variableDirection::toclient); |
---|
95 | |
---|
96 | registerVariable(this->rotationRate_, variableDirection::toclient); |
---|
97 | |
---|
98 | registerVariable(this->overwrite_position_, variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition)); |
---|
99 | registerVariable(this->overwrite_orientation_, variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation)); |
---|
100 | } |
---|
101 | |
---|
102 | void MovableEntity::overwritePosition() |
---|
103 | { |
---|
104 | this->node_->setPosition(this->overwrite_position_); |
---|
105 | } |
---|
106 | |
---|
107 | void MovableEntity::overwriteOrientation() |
---|
108 | { |
---|
109 | this->node_->setOrientation(this->overwrite_orientation_); |
---|
110 | } |
---|
111 | |
---|
112 | void MovableEntity::clientConnected(unsigned int clientID) |
---|
113 | { |
---|
114 | new Timer<MovableEntity>(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), true); |
---|
115 | } |
---|
116 | |
---|
117 | void MovableEntity::clientDisconnected(unsigned int clientID) |
---|
118 | { |
---|
119 | } |
---|
120 | |
---|
121 | void MovableEntity::resynchronize() |
---|
122 | { |
---|
123 | this->overwrite_position_ = this->getPosition(); |
---|
124 | this->overwrite_orientation_ = this->getOrientation(); |
---|
125 | } |
---|
126 | |
---|
127 | void MovableEntity::setPosition(const Vector3& position) |
---|
128 | { |
---|
129 | this->node_->setPosition(position); |
---|
130 | this->overwrite_position_ = this->node_->getPosition(); |
---|
131 | } |
---|
132 | |
---|
133 | void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo) |
---|
134 | { |
---|
135 | this->node_->translate(distance, relativeTo); |
---|
136 | this->overwrite_position_ = this->node_->getPosition(); |
---|
137 | } |
---|
138 | |
---|
139 | void MovableEntity::setOrientation(const Quaternion& orientation) |
---|
140 | { |
---|
141 | this->node_->setOrientation(orientation); |
---|
142 | this->overwrite_orientation_ = this->node_->getOrientation(); |
---|
143 | } |
---|
144 | |
---|
145 | void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo) |
---|
146 | { |
---|
147 | this->node_->rotate(rotation, relativeTo); |
---|
148 | this->overwrite_orientation_ = this->node_->getOrientation(); |
---|
149 | } |
---|
150 | |
---|
151 | void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
152 | { |
---|
153 | this->node_->yaw(angle, relativeTo); |
---|
154 | this->overwrite_orientation_ = this->node_->getOrientation(); |
---|
155 | } |
---|
156 | |
---|
157 | void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
158 | { |
---|
159 | this->node_->pitch(angle, relativeTo); |
---|
160 | this->overwrite_orientation_ = this->node_->getOrientation(); |
---|
161 | } |
---|
162 | |
---|
163 | void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
164 | { |
---|
165 | this->node_->roll(angle, relativeTo); |
---|
166 | this->overwrite_orientation_ = this->node_->getOrientation(); |
---|
167 | } |
---|
168 | |
---|
169 | void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
170 | { |
---|
171 | this->node_->lookAt(target, relativeTo, localDirectionVector); |
---|
172 | this->overwrite_orientation_ = this->node_->getOrientation(); |
---|
173 | } |
---|
174 | |
---|
175 | void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
176 | { |
---|
177 | this->node_->setDirection(direction, relativeTo, localDirectionVector); |
---|
178 | this->overwrite_orientation_ = this->node_->getOrientation(); |
---|
179 | } |
---|
180 | } |
---|