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 "SpaceShip.h" |
---|
30 | |
---|
31 | #include <BulletDynamics/Dynamics/btRigidBody.h> |
---|
32 | |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/ConfigValueIncludes.h" |
---|
35 | #include "core/Template.h" |
---|
36 | #include "core/XMLPort.h" |
---|
37 | #include "tools/Shader.h" |
---|
38 | #include "util/Debug.h" // TODO: Needed? |
---|
39 | #include "util/Math.h" |
---|
40 | |
---|
41 | #include "graphics/Camera.h" |
---|
42 | #include "items/Engine.h" |
---|
43 | |
---|
44 | #include "CameraManager.h" |
---|
45 | #include "Scene.h" |
---|
46 | |
---|
47 | namespace orxonox |
---|
48 | { |
---|
49 | const float orientationGain = 100; |
---|
50 | CreateFactory(SpaceShip); |
---|
51 | |
---|
52 | SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator), boostBlur_(NULL) |
---|
53 | { |
---|
54 | RegisterObject(SpaceShip); |
---|
55 | |
---|
56 | this->primaryThrust_ = 100; |
---|
57 | this->auxilaryThrust_ = 30; |
---|
58 | this->rotationThrust_ = 10; |
---|
59 | |
---|
60 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
61 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
62 | this->bBoost_ = false; |
---|
63 | this->steering_ = Vector3::ZERO; |
---|
64 | |
---|
65 | this->boostPower_ = 10.0f; |
---|
66 | this->initialBoostPower_ = 10.0f; |
---|
67 | this->boostRate_ = 5.0; |
---|
68 | this->boostPowerRate_ = 1.0; |
---|
69 | this->boostCooldownDuration_ = 5.0; |
---|
70 | this->bBoostCooldown_ = false; |
---|
71 | |
---|
72 | this->lift_ = 1.0f; // factor of the lift, standard is 1 |
---|
73 | this->stallSpeed_ = 220.0f; // max speed where lift is added |
---|
74 | |
---|
75 | this->bInvertYAxis_ = false; |
---|
76 | |
---|
77 | this->setDestroyWhenPlayerLeft(true); |
---|
78 | |
---|
79 | // SpaceShip is always a physical object per default |
---|
80 | // Be aware of this call: The collision type legality check will not reach derived classes! |
---|
81 | this->setCollisionType(WorldEntity::Dynamic); |
---|
82 | // Get notification about collisions |
---|
83 | this->enableCollisionCallback(); |
---|
84 | |
---|
85 | this->engineTicksNotDone = 0; |
---|
86 | this->setConfigValues(); |
---|
87 | this->registerVariables(); |
---|
88 | |
---|
89 | this->cameraOriginalPosition_ = Vector3::UNIT_SCALE; |
---|
90 | this->cameraOriginalOrientation_ = Quaternion::IDENTITY; |
---|
91 | |
---|
92 | this->shakeFrequency_ = 15; |
---|
93 | this->shakeAmplitude_ = 5; |
---|
94 | this->shakeDt_ = 0; |
---|
95 | } |
---|
96 | |
---|
97 | SpaceShip::~SpaceShip() |
---|
98 | { |
---|
99 | if (this->isInitialized()) |
---|
100 | { |
---|
101 | this->removeAllEngines(); |
---|
102 | |
---|
103 | if (this->boostBlur_) |
---|
104 | this->boostBlur_->destroy(); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
109 | { |
---|
110 | SUPER(SpaceShip, XMLPort, xmlelement, mode); |
---|
111 | |
---|
112 | //XMLPortParam(SpaceShip, "engine", setEngineTemplate, getEngineTemplate, xmlelement, mode); |
---|
113 | XMLPortParamVariable(SpaceShip, "primaryThrust", primaryThrust_, xmlelement, mode); |
---|
114 | XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); |
---|
115 | XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode); |
---|
116 | XMLPortParamVariable(SpaceShip, "boostPower", initialBoostPower_, xmlelement, mode); |
---|
117 | XMLPortParamVariable(SpaceShip, "boostPowerRate", boostPowerRate_, xmlelement, mode); |
---|
118 | XMLPortParamVariable(SpaceShip, "boostRate", boostRate_, xmlelement, mode); |
---|
119 | XMLPortParamVariable(SpaceShip, "boostCooldownDuration", boostCooldownDuration_, xmlelement, mode); |
---|
120 | XMLPortParamVariable(SpaceShip, "shakeFrequency", shakeFrequency_, xmlelement, mode); |
---|
121 | XMLPortParamVariable(SpaceShip, "shakeAmplitude", shakeAmplitude_, xmlelement, mode); |
---|
122 | XMLPortParamVariable(SpaceShip, "lift", lift_, xmlelement, mode); |
---|
123 | XMLPortParamVariable(SpaceShip, "stallSpeed", stallSpeed_, xmlelement, mode); |
---|
124 | |
---|
125 | XMLPortObject(SpaceShip, Engine, "engines", addEngine, getEngine, xmlelement, mode); |
---|
126 | } |
---|
127 | |
---|
128 | void SpaceShip::registerVariables() |
---|
129 | { |
---|
130 | registerVariable(this->primaryThrust_, VariableDirection::ToClient); |
---|
131 | registerVariable(this->auxilaryThrust_, VariableDirection::ToClient); |
---|
132 | registerVariable(this->rotationThrust_, VariableDirection::ToClient); |
---|
133 | // TODO: Synchronization of boost needed? |
---|
134 | registerVariable(this->boostPower_, VariableDirection::ToClient); |
---|
135 | registerVariable(this->boostPowerRate_, VariableDirection::ToClient); |
---|
136 | registerVariable(this->boostRate_, VariableDirection::ToClient); |
---|
137 | registerVariable(this->boostCooldownDuration_, VariableDirection::ToClient); |
---|
138 | registerVariable(this->shakeFrequency_, VariableDirection::ToClient); |
---|
139 | registerVariable(this->shakeAmplitude_, VariableDirection::ToClient); |
---|
140 | registerVariable(this->lift_, VariableDirection::ToClient); |
---|
141 | registerVariable(this->stallSpeed_, VariableDirection::ToClient); |
---|
142 | } |
---|
143 | |
---|
144 | void SpaceShip::setConfigValues() |
---|
145 | { |
---|
146 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
147 | |
---|
148 | SetConfigValueExternal(bEnableMotionBlur_, "GraphicsSettings", "enableMotionBlur", true) |
---|
149 | .description("Enable or disable the motion blur effect when moving very fast") |
---|
150 | .callback(this, &SpaceShip::changedEnableMotionBlur); |
---|
151 | SetConfigValueExternal(blurStrength_, "GraphicsSettings", "blurStrength", 3.0f) |
---|
152 | .description("Defines the strength of the motion blur effect"); |
---|
153 | } |
---|
154 | |
---|
155 | bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
156 | { |
---|
157 | if (type != WorldEntity::Dynamic) |
---|
158 | { |
---|
159 | CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl; |
---|
160 | assert(false); // Only in debug mode |
---|
161 | return false; |
---|
162 | } |
---|
163 | else |
---|
164 | return true; |
---|
165 | } |
---|
166 | |
---|
167 | void SpaceShip::tick(float dt) |
---|
168 | { |
---|
169 | SUPER(SpaceShip, tick, dt); |
---|
170 | |
---|
171 | if (this->hasLocalController()) |
---|
172 | { |
---|
173 | // Handle mouse look |
---|
174 | if (!this->isInMouseLook()) |
---|
175 | { |
---|
176 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
177 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
178 | } |
---|
179 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
180 | |
---|
181 | // Charge boostPower |
---|
182 | if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_) |
---|
183 | { |
---|
184 | this->boostPower_ += this->boostPowerRate_*dt; |
---|
185 | } |
---|
186 | // Use boostPower |
---|
187 | if(this->bBoost_) |
---|
188 | { |
---|
189 | this->boostPower_ -=this->boostRate_*dt; |
---|
190 | if(this->boostPower_ <= 0.0f) |
---|
191 | { |
---|
192 | this->boost(false); |
---|
193 | this->bBoostCooldown_ = true; |
---|
194 | this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this))); |
---|
195 | } |
---|
196 | |
---|
197 | this->shakeCamera(dt); |
---|
198 | } |
---|
199 | |
---|
200 | // Enable Blur depending on settings |
---|
201 | if (this->bEnableMotionBlur_ && !this->boostBlur_ && this->hasLocalController() && this->hasHumanController()) |
---|
202 | { |
---|
203 | this->boostBlur_ = new Shader(this->getScene()->getSceneManager()); |
---|
204 | this->boostBlur_->setCompositorName("Radial Blur"); |
---|
205 | } |
---|
206 | |
---|
207 | if (this->boostBlur_) // && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1) |
---|
208 | { |
---|
209 | // TODO: this->maxSpeedFront_ gets fastest engine |
---|
210 | float blur = this->blurStrength_ * clamp((-this->getLocalVelocity().z - 0.0f /*this->maxSpeedFront_*/) / ((150.0f /*boostFactor_*/ - 1) * 1.5f /*this->maxSpeedFront_*/), 0.0f, 1.0f); |
---|
211 | |
---|
212 | // Show and hide blur effect depending on state of booster |
---|
213 | if(this->bBoost_) |
---|
214 | this->boostBlur_->setVisible(blur > 0); |
---|
215 | else |
---|
216 | this->boostBlur_->setVisible(false); |
---|
217 | |
---|
218 | this->boostBlur_->setParameter(0, 0, "sampleStrength", blur); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | void SpaceShip::moveFrontBack(const Vector2& value) |
---|
224 | { |
---|
225 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); |
---|
226 | this->steering_.z = -value.x; |
---|
227 | } |
---|
228 | |
---|
229 | void SpaceShip::moveRightLeft(const Vector2& value) |
---|
230 | { |
---|
231 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); |
---|
232 | this->steering_.x = value.x; |
---|
233 | } |
---|
234 | |
---|
235 | void SpaceShip::moveUpDown(const Vector2& value) |
---|
236 | { |
---|
237 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); |
---|
238 | this->steering_.y = value.x; |
---|
239 | } |
---|
240 | |
---|
241 | void SpaceShip::rotateYaw(const Vector2& value) |
---|
242 | { |
---|
243 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x); |
---|
244 | |
---|
245 | Pawn::rotateYaw(value); |
---|
246 | } |
---|
247 | |
---|
248 | void SpaceShip::rotatePitch(const Vector2& value) |
---|
249 | { |
---|
250 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); |
---|
251 | |
---|
252 | Pawn::rotatePitch(value); |
---|
253 | |
---|
254 | //This function call adds a lift to the ship when it is pitching to make it's movement more "realistic" and enhance the feeling. |
---|
255 | if (abs(this-> getLocalVelocity().z) < stallSpeed_) {this->moveUpDown(lift_ / 5 * value * sqrt(abs(this-> getLocalVelocity().z)));} |
---|
256 | } |
---|
257 | |
---|
258 | void SpaceShip::rotateRoll(const Vector2& value) |
---|
259 | { |
---|
260 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); |
---|
261 | |
---|
262 | Pawn::rotateRoll(value); |
---|
263 | } |
---|
264 | |
---|
265 | void SpaceShip::fire() |
---|
266 | { |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | @brief |
---|
271 | Starts or stops boosting. |
---|
272 | @param bBoost |
---|
273 | Whether to start or stop boosting. |
---|
274 | */ |
---|
275 | void SpaceShip::boost(bool bBoost) |
---|
276 | { |
---|
277 | if(bBoost && !this->bBoostCooldown_) |
---|
278 | { |
---|
279 | this->bBoost_ = true; |
---|
280 | Camera* camera = CameraManager::getInstance().getActiveCamera(); |
---|
281 | this->cameraOriginalPosition_ = camera->getPosition(); |
---|
282 | this->cameraOriginalOrientation_ = camera->getOrientation(); |
---|
283 | } |
---|
284 | if(!bBoost) |
---|
285 | { |
---|
286 | this->bBoost_ = false; |
---|
287 | this->resetCamera(); |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | void SpaceShip::boostCooledDown(void) |
---|
292 | { |
---|
293 | this->bBoostCooldown_ = false; |
---|
294 | } |
---|
295 | |
---|
296 | void SpaceShip::shakeCamera(float dt) |
---|
297 | { |
---|
298 | //make sure the ship is only shaking if it's moving |
---|
299 | if (this->getVelocity().squaredLength() > 80.0f) |
---|
300 | { |
---|
301 | this->shakeDt_ += dt; |
---|
302 | |
---|
303 | float frequency = this->shakeFrequency_ * (this->getVelocity().squaredLength()); |
---|
304 | |
---|
305 | if (this->shakeDt_ >= 1.0f/frequency) |
---|
306 | { |
---|
307 | this->shakeDt_ -= 1.0f/frequency; |
---|
308 | } |
---|
309 | |
---|
310 | Degree angle = Degree(sin(this->shakeDt_ *2.0f* math::pi * frequency) * this->shakeAmplitude_); |
---|
311 | |
---|
312 | //COUT(0) << "Angle: " << angle << std::endl; |
---|
313 | Camera* camera = this->getCamera(); |
---|
314 | |
---|
315 | //Shaking Camera effect |
---|
316 | if (camera != 0) |
---|
317 | { |
---|
318 | camera->setOrientation(Vector3::UNIT_X, angle); |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | void SpaceShip::resetCamera() |
---|
324 | { |
---|
325 | Camera *camera = this->getCamera(); |
---|
326 | |
---|
327 | if (camera == 0) |
---|
328 | { |
---|
329 | COUT(2) << "Failed to reset camera!"; |
---|
330 | return; |
---|
331 | } |
---|
332 | |
---|
333 | this->shakeDt_ = 0; |
---|
334 | camera->setPosition(this->cameraOriginalPosition_); |
---|
335 | camera->setOrientation(this->cameraOriginalOrientation_); |
---|
336 | } |
---|
337 | |
---|
338 | void SpaceShip::backupCamera() |
---|
339 | { |
---|
340 | Camera* camera = CameraManager::getInstance().getActiveCamera(); |
---|
341 | if(camera != NULL) |
---|
342 | { |
---|
343 | this->cameraOriginalPosition_ = camera->getPosition(); |
---|
344 | this->cameraOriginalOrientation_ = camera->getOrientation(); |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | void SpaceShip::addEngine(orxonox::Engine* engine) |
---|
349 | { |
---|
350 | //COUT(0)<<"Adding an Engine: " << engine << endl; |
---|
351 | this->engineList_.push_back(engine); |
---|
352 | engine->addToSpaceShip(this); |
---|
353 | this->resetEngineTicks(); |
---|
354 | } |
---|
355 | |
---|
356 | bool SpaceShip::hasEngine(Engine* engine) |
---|
357 | { |
---|
358 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
359 | { |
---|
360 | if(this->engineList_[i]==engine) |
---|
361 | return true; |
---|
362 | } |
---|
363 | return false; |
---|
364 | } |
---|
365 | |
---|
366 | Engine* SpaceShip::getEngine(unsigned int i) |
---|
367 | { |
---|
368 | if(this->engineList_.size()>=i) |
---|
369 | return 0; |
---|
370 | else |
---|
371 | return this->engineList_[i]; |
---|
372 | } |
---|
373 | |
---|
374 | void SpaceShip::removeAllEngines() |
---|
375 | { |
---|
376 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
377 | this->engineList_[i]->destroy(); |
---|
378 | } |
---|
379 | |
---|
380 | void SpaceShip::setSpeedFactor(float factor) |
---|
381 | { |
---|
382 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
383 | this->engineList_[i]->setSpeedFactor(factor); |
---|
384 | } |
---|
385 | float SpaceShip::getSpeedFactor() // Calculate mean SpeedFactor. |
---|
386 | { |
---|
387 | float ret = 0; unsigned int i = 0; |
---|
388 | for(; i<this->engineList_.size(); i++) |
---|
389 | ret += this->engineList_[i]->getSpeedFactor(); |
---|
390 | ret /= (float)i; |
---|
391 | return ret; |
---|
392 | } |
---|
393 | float SpaceShip::getMaxSpeedFront() |
---|
394 | { |
---|
395 | float ret=0; |
---|
396 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
397 | { |
---|
398 | if(this->engineList_[i]->getMaxSpeedFront() > ret) |
---|
399 | ret = this->engineList_[i]->getMaxSpeedFront(); |
---|
400 | } |
---|
401 | return ret; |
---|
402 | } |
---|
403 | |
---|
404 | float SpaceShip::getBoostFactor() |
---|
405 | { |
---|
406 | float ret = 0; unsigned int i=0; |
---|
407 | for(; i<this->engineList_.size(); i++) |
---|
408 | ret += this->engineList_[i]->getBoostFactor(); |
---|
409 | ret /= (float)i; |
---|
410 | return ret; |
---|
411 | } |
---|
412 | |
---|
413 | std::vector<PickupCarrier*>* SpaceShip::getCarrierChildren(void) const |
---|
414 | { |
---|
415 | std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>(); |
---|
416 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
417 | list->push_back(this->engineList_[i]); |
---|
418 | return list; |
---|
419 | } |
---|
420 | |
---|
421 | void SpaceShip::changedEnableMotionBlur() |
---|
422 | { |
---|
423 | if (!this->bEnableMotionBlur_) |
---|
424 | { |
---|
425 | this->boostBlur_->destroy(); |
---|
426 | this->boostBlur_ = 0; |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | } |
---|