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