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 "Engine.h" |
---|
31 | |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | #include "core/XMLPort.h" |
---|
34 | #include "objects/Scene.h" |
---|
35 | #include "objects/worldentities/pawns/SpaceShip.h" |
---|
36 | #include "tools/Shader.h" |
---|
37 | |
---|
38 | namespace orxonox |
---|
39 | { |
---|
40 | CreateFactory(Engine); |
---|
41 | |
---|
42 | Engine::Engine(BaseObject* creator) : Item(creator) |
---|
43 | { |
---|
44 | RegisterObject(Engine); |
---|
45 | |
---|
46 | this->ship_ = 0; |
---|
47 | this->shipID_ = OBJECTID_UNKNOWN; |
---|
48 | |
---|
49 | this->boostFactor_ = 1.5; |
---|
50 | this->speedFactor_ = 1.0; |
---|
51 | |
---|
52 | this->maxSpeedFront_ = 0.0; |
---|
53 | this->maxSpeedBack_ = 0.0; |
---|
54 | this->maxSpeedLeftRight_ = 0.0; |
---|
55 | this->maxSpeedUpDown_ = 0.0; |
---|
56 | |
---|
57 | this->accelerationFront_ = 0.0; |
---|
58 | this->accelerationBrake_ = 0.0; |
---|
59 | this->accelerationBack_ = 0.0; |
---|
60 | this->accelerationLeftRight_ = 0.0; |
---|
61 | this->accelerationUpDown_ = 0.0; |
---|
62 | |
---|
63 | this->boostBlur_ = 0; |
---|
64 | |
---|
65 | this->registerVariables(); |
---|
66 | } |
---|
67 | |
---|
68 | Engine::~Engine() |
---|
69 | { |
---|
70 | if (this->isInitialized() && this->ship_) |
---|
71 | { |
---|
72 | this->ship_->setEngine(0); |
---|
73 | |
---|
74 | if (this->boostBlur_) |
---|
75 | delete this->boostBlur_; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
80 | { |
---|
81 | SUPER(Engine, XMLPort, xmlelement, mode); |
---|
82 | |
---|
83 | XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode); |
---|
84 | |
---|
85 | XMLPortParam(Engine, "speedfront", setMaxSpeedFront, setMaxSpeedFront, xmlelement, mode); |
---|
86 | XMLPortParam(Engine, "speedback", setMaxSpeedBack, setMaxSpeedBack, xmlelement, mode); |
---|
87 | XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode); |
---|
88 | XMLPortParam(Engine, "speedupdown", setMaxSpeedUpDown, setMaxSpeedUpDown, xmlelement, mode); |
---|
89 | |
---|
90 | XMLPortParam(Engine, "accelerationfront", setAccelerationFront, setAccelerationFront, xmlelement, mode); |
---|
91 | XMLPortParam(Engine, "accelerationbrake", setAccelerationBrake, setAccelerationBrake, xmlelement, mode); |
---|
92 | XMLPortParam(Engine, "accelerationback", setAccelerationBack, setAccelerationBack, xmlelement, mode); |
---|
93 | XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode); |
---|
94 | XMLPortParam(Engine, "accelerationupdown", setAccelerationUpDown, setAccelerationUpDown, xmlelement, mode); |
---|
95 | } |
---|
96 | |
---|
97 | void Engine::registerVariables() |
---|
98 | { |
---|
99 | REGISTERDATA(this->shipID_, direction::toclient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID)); |
---|
100 | |
---|
101 | REGISTERDATA(this->speedFactor_, direction::toclient); |
---|
102 | REGISTERDATA(this->boostFactor_, direction::toclient); |
---|
103 | |
---|
104 | REGISTERDATA(this->maxSpeedFront_, direction::toclient); |
---|
105 | REGISTERDATA(this->maxSpeedBack_, direction::toclient); |
---|
106 | REGISTERDATA(this->maxSpeedLeftRight_, direction::toclient); |
---|
107 | REGISTERDATA(this->maxSpeedUpDown_, direction::toclient); |
---|
108 | |
---|
109 | REGISTERDATA(this->accelerationFront_, direction::toclient); |
---|
110 | REGISTERDATA(this->accelerationBrake_, direction::toclient); |
---|
111 | REGISTERDATA(this->accelerationBack_, direction::toclient); |
---|
112 | REGISTERDATA(this->accelerationLeftRight_, direction::toclient); |
---|
113 | REGISTERDATA(this->accelerationUpDown_, direction::toclient); |
---|
114 | } |
---|
115 | |
---|
116 | void Engine::networkcallback_shipID() |
---|
117 | { |
---|
118 | this->ship_ = 0; |
---|
119 | |
---|
120 | if (this->shipID_ != OBJECTID_UNKNOWN) |
---|
121 | { |
---|
122 | Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_); |
---|
123 | if (object) |
---|
124 | this->addToSpaceShip(dynamic_cast<SpaceShip*>(object)); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | void Engine::tick(float dt) |
---|
129 | { |
---|
130 | if (!this->ship_) |
---|
131 | { |
---|
132 | if (this->shipID_) |
---|
133 | { |
---|
134 | this->networkcallback_shipID(); |
---|
135 | |
---|
136 | if (!this->ship_) |
---|
137 | return; |
---|
138 | } |
---|
139 | else |
---|
140 | return; |
---|
141 | } |
---|
142 | |
---|
143 | if (!this->isActive()) |
---|
144 | return; |
---|
145 | |
---|
146 | SUPER(Engine, tick, dt); |
---|
147 | |
---|
148 | const Vector3& direction = this->getDirection(); |
---|
149 | Vector3 velocity = this->ship_->getVelocity(); |
---|
150 | Vector3 acceleration = Vector3::ZERO; |
---|
151 | |
---|
152 | float factor = 1.0f / this->speedFactor_; |
---|
153 | velocity *= factor; |
---|
154 | |
---|
155 | if (direction.z < 0) |
---|
156 | { |
---|
157 | if (this->maxSpeedFront_ != 0) |
---|
158 | { |
---|
159 | float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f); |
---|
160 | acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f); |
---|
161 | } |
---|
162 | } |
---|
163 | else if (direction.z > 0) |
---|
164 | { |
---|
165 | if (velocity.z < 0) |
---|
166 | acceleration.z = direction.z * this->accelerationBrake_; |
---|
167 | else if (this->maxSpeedBack_ != 0) |
---|
168 | acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f); |
---|
169 | } |
---|
170 | |
---|
171 | if (this->maxSpeedLeftRight_ != 0) |
---|
172 | { |
---|
173 | if (direction.x < 0) |
---|
174 | acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); |
---|
175 | else if (direction.x > 0) |
---|
176 | acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); |
---|
177 | } |
---|
178 | |
---|
179 | if (this->maxSpeedUpDown_ != 0) |
---|
180 | { |
---|
181 | if (direction.y < 0) |
---|
182 | acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); |
---|
183 | else if (direction.y > 0) |
---|
184 | acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); |
---|
185 | } |
---|
186 | |
---|
187 | this->ship_->setAcceleration(acceleration); |
---|
188 | |
---|
189 | this->ship_->setBoost(false); |
---|
190 | this->ship_->setSteeringDirection(Vector3::ZERO); |
---|
191 | |
---|
192 | if (!this->boostBlur_ && this->ship_->hasLocalController() && this->ship_->hasHumanController()) |
---|
193 | { |
---|
194 | this->boostBlur_ = new Shader(this->ship_->getScene()->getSceneManager()); |
---|
195 | this->boostBlur_->setCompositor("Radial Blur"); |
---|
196 | } |
---|
197 | |
---|
198 | if (this->boostBlur_ && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1) |
---|
199 | this->boostBlur_->setParameter("Ogre/Compositor/Radial_Blur", 0, 0, "sampleStrength", 5.0f * clamp((-velocity.z - this->maxSpeedFront_) / ((this->boostFactor_ - 1) * this->maxSpeedFront_), 0.0f, 1.0f)); |
---|
200 | } |
---|
201 | |
---|
202 | void Engine::changedActivity() |
---|
203 | { |
---|
204 | SUPER(Engine, changedActivity); |
---|
205 | |
---|
206 | if (this->boostBlur_) |
---|
207 | this->boostBlur_->setVisible(this->isVisible()); |
---|
208 | } |
---|
209 | |
---|
210 | void Engine::addToSpaceShip(SpaceShip* ship) |
---|
211 | { |
---|
212 | this->ship_ = ship; |
---|
213 | if (ship) |
---|
214 | { |
---|
215 | this->shipID_ = ship->getObjectID(); |
---|
216 | if (ship->getEngine() != this) |
---|
217 | ship->setEngine(this); |
---|
218 | |
---|
219 | if (this->boostBlur_) |
---|
220 | { |
---|
221 | delete this->boostBlur_; |
---|
222 | this->boostBlur_ = 0; |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | const Vector3& Engine::getDirection() const |
---|
228 | { |
---|
229 | if (this->ship_) |
---|
230 | return this->ship_->getSteeringDirection(); |
---|
231 | else |
---|
232 | return Vector3::ZERO; |
---|
233 | } |
---|
234 | } |
---|