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