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 "items/Engine.h" |
---|
38 | |
---|
39 | // New as of Booster integration |
---|
40 | #include "Scene.h" |
---|
41 | #include "tools/Shader.h" |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | const float orientationGain = 100; |
---|
46 | CreateFactory(SpaceShip); |
---|
47 | |
---|
48 | SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator) |
---|
49 | { |
---|
50 | RegisterObject(SpaceShip); |
---|
51 | |
---|
52 | this->primaryThrust_ = 100; |
---|
53 | this->auxilaryThrust_ = 30; |
---|
54 | this->rotationThrust_ = 10; |
---|
55 | |
---|
56 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
57 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
58 | this->bBoost_ = false; |
---|
59 | this->steering_ = Vector3::ZERO; |
---|
60 | //this->engine_ = 0; |
---|
61 | |
---|
62 | this->boostBlur_ = 0; |
---|
63 | |
---|
64 | this->boostPower_ = 10.0f; |
---|
65 | this->initialBoostPower_ = 10.0f; |
---|
66 | this->boostRate_ = 5.0; |
---|
67 | this->boostPowerRate_ = 1.0; |
---|
68 | this->boostCooldownDuration_ = 5.0; |
---|
69 | this->bBoostCooldown_ = false; |
---|
70 | |
---|
71 | this->bInvertYAxis_ = false; |
---|
72 | |
---|
73 | this->setDestroyWhenPlayerLeft(true); |
---|
74 | |
---|
75 | // SpaceShip is always a physical object per default |
---|
76 | // Be aware of this call: The collision type legality check will not reach derived classes! |
---|
77 | this->setCollisionType(WorldEntity::Dynamic); |
---|
78 | // Get notification about collisions |
---|
79 | this->enableCollisionCallback(); |
---|
80 | |
---|
81 | this->engineTicksNotDone = 0; |
---|
82 | this->setConfigValues(); |
---|
83 | this->registerVariables(); |
---|
84 | } |
---|
85 | |
---|
86 | SpaceShip::~SpaceShip() |
---|
87 | { |
---|
88 | if (this->isInitialized()) |
---|
89 | this->removeAllEngines(); |
---|
90 | |
---|
91 | if (this->boostBlur_) |
---|
92 | this->boostBlur_->destroy(); |
---|
93 | } |
---|
94 | |
---|
95 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
96 | { |
---|
97 | SUPER(SpaceShip, XMLPort, xmlelement, mode); |
---|
98 | |
---|
99 | //XMLPortParam(SpaceShip, "engine", setEngineTemplate, getEngineTemplate, xmlelement, mode); |
---|
100 | XMLPortParamVariable(SpaceShip, "primaryThrust", primaryThrust_, xmlelement, mode); |
---|
101 | XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); |
---|
102 | XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode); |
---|
103 | XMLPortParamVariable(SpaceShip, "boostPower", initialBoostPower_, xmlelement, mode); |
---|
104 | XMLPortParamVariable(SpaceShip, "boostPowerRate", boostPowerRate_, xmlelement, mode); |
---|
105 | XMLPortParamVariable(SpaceShip, "boostRate", boostRate_, xmlelement, mode); |
---|
106 | XMLPortParamVariable(SpaceShip, "boostCooldownDuration", boostCooldownDuration_, xmlelement, mode); |
---|
107 | |
---|
108 | XMLPortObject(SpaceShip, Engine, "engines", addEngine, getEngine, xmlelement, mode); |
---|
109 | } |
---|
110 | |
---|
111 | void SpaceShip::registerVariables() |
---|
112 | { |
---|
113 | registerVariable(this->primaryThrust_, VariableDirection::ToClient); |
---|
114 | registerVariable(this->auxilaryThrust_, VariableDirection::ToClient); |
---|
115 | registerVariable(this->rotationThrust_, VariableDirection::ToClient); |
---|
116 | registerVariable(this->boostPower_, VariableDirection::ToClient); |
---|
117 | registerVariable(this->boostPowerRate_, VariableDirection::ToClient); |
---|
118 | registerVariable(this->boostRate_, VariableDirection::ToClient); |
---|
119 | registerVariable(this->boostCooldownDuration_, VariableDirection::ToClient); |
---|
120 | } |
---|
121 | |
---|
122 | void SpaceShip::setConfigValues() |
---|
123 | { |
---|
124 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
125 | |
---|
126 | SetConfigValueExternal(bEnableMotionBlur_, "GraphicsSettings", "enableMotionBlur", true) |
---|
127 | .description("Enable or disable the motion blur effect when moving very fast") |
---|
128 | .callback(this, &SpaceShip::changedEnableMotionBlur); |
---|
129 | SetConfigValueExternal(blurStrength_, "GraphicsSettings", "blurStrength", 3.0f) |
---|
130 | .description("Defines the strength of the motion blur effect"); |
---|
131 | } |
---|
132 | |
---|
133 | bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
134 | { |
---|
135 | if (type != WorldEntity::Dynamic) |
---|
136 | { |
---|
137 | CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl; |
---|
138 | assert(false); // Only in debug mode |
---|
139 | return false; |
---|
140 | } |
---|
141 | else |
---|
142 | return true; |
---|
143 | } |
---|
144 | |
---|
145 | void SpaceShip::tick(float dt) |
---|
146 | { |
---|
147 | SUPER(SpaceShip, tick, dt); |
---|
148 | |
---|
149 | if (this->hasLocalController()) |
---|
150 | { |
---|
151 | // Handle mouse look |
---|
152 | if (!this->isInMouseLook()) |
---|
153 | { |
---|
154 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
155 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
156 | } |
---|
157 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
158 | |
---|
159 | // Charge boostPower |
---|
160 | if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_) |
---|
161 | { |
---|
162 | this->boostPower_ += this->boostPowerRate_*dt; |
---|
163 | } |
---|
164 | // Use boostPower |
---|
165 | if(this->bBoost_) |
---|
166 | { |
---|
167 | this->boostPower_ -=this->boostRate_*dt; |
---|
168 | if(this->boostPower_ <= 0.0f) |
---|
169 | { |
---|
170 | this->bBoost_ = false; |
---|
171 | this->bBoostCooldown_ = true; |
---|
172 | this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this))); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | // Enable Blur depending on settings |
---|
177 | if (this->bEnableMotionBlur_ && !this->boostBlur_ && this->hasLocalController() && this->hasHumanController()) |
---|
178 | { |
---|
179 | this->boostBlur_ = new Shader(this->getScene()->getSceneManager()); |
---|
180 | this->boostBlur_->setCompositorName("Radial Blur"); |
---|
181 | } |
---|
182 | |
---|
183 | if (this->boostBlur_) // && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1) |
---|
184 | { |
---|
185 | // TODO: this->maxSpeedFront_ gets fastest engine |
---|
186 | float blur = this->blurStrength_ * clamp((-this->getLocalVelocity().z - 0.0f /*this->maxSpeedFront_*/) / ((150.0f /*boostFactor_*/ - 1) * 1.5f /*this->maxSpeedFront_*/), 0.0f, 1.0f); |
---|
187 | |
---|
188 | // Show and hide blur effect depending on state of booster |
---|
189 | if(this->bBoost_) |
---|
190 | this->boostBlur_->setVisible(blur > 0); |
---|
191 | else |
---|
192 | this->boostBlur_->setVisible(false); |
---|
193 | |
---|
194 | this->boostBlur_->setParameter(0, 0, "sampleStrength", blur); |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | void SpaceShip::boostCooledDown(void) |
---|
200 | { |
---|
201 | this->bBoostCooldown_ = false; |
---|
202 | } |
---|
203 | |
---|
204 | void SpaceShip::moveFrontBack(const Vector2& value) |
---|
205 | { |
---|
206 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); |
---|
207 | this->steering_.z = -value.x; |
---|
208 | } |
---|
209 | |
---|
210 | void SpaceShip::moveRightLeft(const Vector2& value) |
---|
211 | { |
---|
212 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); |
---|
213 | this->steering_.x = value.x; |
---|
214 | } |
---|
215 | |
---|
216 | void SpaceShip::moveUpDown(const Vector2& value) |
---|
217 | { |
---|
218 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); |
---|
219 | this->steering_.y = value.x; |
---|
220 | } |
---|
221 | |
---|
222 | void SpaceShip::rotateYaw(const Vector2& value) |
---|
223 | { |
---|
224 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x); |
---|
225 | |
---|
226 | Pawn::rotateYaw(value); |
---|
227 | } |
---|
228 | |
---|
229 | void SpaceShip::rotatePitch(const Vector2& value) |
---|
230 | { |
---|
231 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); |
---|
232 | |
---|
233 | Pawn::rotatePitch(value); |
---|
234 | } |
---|
235 | |
---|
236 | void SpaceShip::rotateRoll(const Vector2& value) |
---|
237 | { |
---|
238 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); |
---|
239 | |
---|
240 | Pawn::rotateRoll(value); |
---|
241 | } |
---|
242 | |
---|
243 | void SpaceShip::fire() |
---|
244 | { |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | @brief |
---|
249 | Starts or stops boosting. |
---|
250 | @param bBoost |
---|
251 | Whether to start or stop boosting. |
---|
252 | */ |
---|
253 | void SpaceShip::boost(bool bBoost) |
---|
254 | { |
---|
255 | if(bBoost && !this->bBoostCooldown_) |
---|
256 | this->bBoost_ = true; |
---|
257 | if(!bBoost) |
---|
258 | this->bBoost_ = false; |
---|
259 | } |
---|
260 | |
---|
261 | void SpaceShip::addEngine(orxonox::Engine* engine) |
---|
262 | { |
---|
263 | //COUT(0)<<"Adding an Engine: " << engine << endl; |
---|
264 | this->engineList_.push_back(engine); |
---|
265 | engine->addToSpaceShip(this); |
---|
266 | this->resetEngineTicks(); |
---|
267 | } |
---|
268 | bool SpaceShip::hasEngine(Engine* engine) |
---|
269 | { |
---|
270 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
271 | { |
---|
272 | if(this->engineList_[i]==engine) |
---|
273 | return true; |
---|
274 | } |
---|
275 | return false; |
---|
276 | } |
---|
277 | Engine* SpaceShip::getEngine(unsigned int i) |
---|
278 | { |
---|
279 | if(this->engineList_.size()>=i) |
---|
280 | return 0; |
---|
281 | else |
---|
282 | return this->engineList_[i]; |
---|
283 | } |
---|
284 | void SpaceShip::removeAllEngines() |
---|
285 | { |
---|
286 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
287 | this->engineList_[i]->~Engine(); |
---|
288 | } |
---|
289 | |
---|
290 | void SpaceShip::setSpeedFactor(float factor) |
---|
291 | { |
---|
292 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
293 | this->engineList_[i]->setSpeedFactor(factor); |
---|
294 | } |
---|
295 | float SpaceShip::getSpeedFactor() // Calculate mean SpeedFactor. |
---|
296 | { |
---|
297 | float ret = 0; unsigned int i = 0; |
---|
298 | for(; i<this->engineList_.size(); i++) |
---|
299 | ret += this->engineList_[i]->getSpeedFactor(); |
---|
300 | ret /= (float)i; |
---|
301 | return ret; |
---|
302 | } |
---|
303 | float SpaceShip::getMaxSpeedFront() |
---|
304 | { |
---|
305 | float ret=0; |
---|
306 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
307 | { |
---|
308 | if(this->engineList_[i]->getMaxSpeedFront() > ret) |
---|
309 | ret = this->engineList_[i]->getMaxSpeedFront(); |
---|
310 | } |
---|
311 | return ret; |
---|
312 | } |
---|
313 | float SpaceShip::getBoostFactor() |
---|
314 | { |
---|
315 | float ret = 0; unsigned int i=0; |
---|
316 | for(; i<this->engineList_.size(); i++) |
---|
317 | ret += this->engineList_[i]->getBoostFactor(); |
---|
318 | ret /= (float)i; |
---|
319 | return ret; |
---|
320 | } |
---|
321 | |
---|
322 | std::vector<PickupCarrier*>* SpaceShip::getCarrierChildren(void) const |
---|
323 | { |
---|
324 | std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>(); |
---|
325 | for(unsigned int i=0; i<this->engineList_.size(); i++) |
---|
326 | list->push_back(this->engineList_[i]); |
---|
327 | return list; |
---|
328 | } |
---|
329 | |
---|
330 | void SpaceShip::changedEnableMotionBlur() |
---|
331 | { |
---|
332 | if (!this->bEnableMotionBlur_) |
---|
333 | { |
---|
334 | this->boostBlur_->destroy(); |
---|
335 | this->boostBlur_ = 0; |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | } |
---|