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->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 | LinearEntity::~LinearEntity() |
---|
59 | { |
---|
60 | } |
---|
61 | |
---|
62 | void LinearEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
63 | { |
---|
64 | SUPER(LinearEntity, XMLPort, xmlelement, mode); |
---|
65 | |
---|
66 | XMLPortParamTemplate(LinearEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&); |
---|
67 | XMLPortParamTemplate(LinearEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode, const Degree&); |
---|
68 | } |
---|
69 | |
---|
70 | void LinearEntity::tick(float dt) |
---|
71 | { |
---|
72 | if (this->isActive()) |
---|
73 | { |
---|
74 | if (!this->isDynamic()) |
---|
75 | { |
---|
76 | // we have to do 'physics' ourselves. |
---|
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 | |
---|
86 | void LinearEntity::registerVariables() |
---|
87 | { |
---|
88 | REGISTERDATA(this->velocity_.x, network::direction::toclient); |
---|
89 | REGISTERDATA(this->velocity_.y, network::direction::toclient); |
---|
90 | REGISTERDATA(this->velocity_.z, network::direction::toclient); |
---|
91 | |
---|
92 | REGISTERDATA(this->rotationAxis_.x, network::direction::toclient); |
---|
93 | REGISTERDATA(this->rotationAxis_.y, network::direction::toclient); |
---|
94 | REGISTERDATA(this->rotationAxis_.z, network::direction::toclient); |
---|
95 | |
---|
96 | REGISTERDATA(this->rotationRate_, network::direction::toclient); |
---|
97 | |
---|
98 | REGISTERDATA(this->overwrite_position_, network::direction::toclient, new network::NetworkCallback<LinearEntity>(this, &LinearEntity::overwritePosition)); |
---|
99 | REGISTERDATA(this->overwrite_orientation_, network::direction::toclient, new network::NetworkCallback<LinearEntity>(this, &LinearEntity::overwriteOrientation)); |
---|
100 | } |
---|
101 | |
---|
102 | void LinearEntity::overwritePosition() |
---|
103 | { |
---|
104 | this->node_->setPosition(this->overwrite_position_); |
---|
105 | } |
---|
106 | |
---|
107 | void LinearEntity::overwriteOrientation() |
---|
108 | { |
---|
109 | this->node_->setOrientation(this->overwrite_orientation_); |
---|
110 | } |
---|
111 | |
---|
112 | void LinearEntity::clientConnected(unsigned int clientID) |
---|
113 | { |
---|
114 | new Timer<LinearEntity>(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&LinearEntity::resynchronize)), true); |
---|
115 | } |
---|
116 | |
---|
117 | void LinearEntity::clientDisconnected(unsigned int clientID) |
---|
118 | { |
---|
119 | } |
---|
120 | |
---|
121 | void LinearEntity::resynchronize() |
---|
122 | { |
---|
123 | this->overwrite_position_ = this->getPosition(); |
---|
124 | this->overwrite_orientation_ = this->getOrientation(); |
---|
125 | } |
---|
126 | |
---|
127 | void LinearEntity::positionChanged() |
---|
128 | { |
---|
129 | this->overwrite_position_ = this->getPosition(); |
---|
130 | } |
---|
131 | |
---|
132 | void LinearEntity::orientationChanged() |
---|
133 | { |
---|
134 | this->overwrite_orientation_ = this->getOrientation(); |
---|
135 | } |
---|
136 | } |
---|