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