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 | #include "graphics/Camera.h" |
---|
39 | #include "CameraManager.h" |
---|
40 | #include "util/Math.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | const float orientationGain = 100; |
---|
45 | CreateFactory(SpaceShip); |
---|
46 | |
---|
47 | SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator) |
---|
48 | { |
---|
49 | RegisterObject(SpaceShip); |
---|
50 | |
---|
51 | this->primaryThrust_ = 100; |
---|
52 | this->auxilaryThrust_ = 30; |
---|
53 | this->rotationThrust_ = 10; |
---|
54 | |
---|
55 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
56 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
57 | this->bBoost_ = false; |
---|
58 | this->steering_ = Vector3::ZERO; |
---|
59 | this->engine_ = 0; |
---|
60 | |
---|
61 | this->boostPower_ = 10.0f; |
---|
62 | this->initialBoostPower_ = 10.0f; |
---|
63 | this->boostRate_ = 5.0; |
---|
64 | this->boostPowerRate_ = 1.0; |
---|
65 | this->boostCooldownDuration_ = 5.0; |
---|
66 | this->bBoostCooldown_ = false; |
---|
67 | |
---|
68 | this->bInvertYAxis_ = false; |
---|
69 | |
---|
70 | this->setDestroyWhenPlayerLeft(true); |
---|
71 | |
---|
72 | // SpaceShip is always a physical object per default |
---|
73 | // Be aware of this call: The collision type legality check will not reach derived classes! |
---|
74 | this->setCollisionType(WorldEntity::Dynamic); |
---|
75 | // Get notification about collisions |
---|
76 | this->enableCollisionCallback(); |
---|
77 | |
---|
78 | this->setConfigValues(); |
---|
79 | this->registerVariables(); |
---|
80 | |
---|
81 | Camera* c = CameraManager::getInstance().getActiveCamera(); |
---|
82 | this->cameraOriginalPosition = c->getPosition(); |
---|
83 | this->cameraOriginalOrientation = c->getOrientation(); |
---|
84 | |
---|
85 | this->shakeFrequency_ = 15; |
---|
86 | this->shakeAmplitude_ = 5; |
---|
87 | this->shakeDt_ = 0; |
---|
88 | } |
---|
89 | |
---|
90 | SpaceShip::~SpaceShip() |
---|
91 | { |
---|
92 | if (this->isInitialized() && this->engine_) |
---|
93 | this->engine_->destroy(); |
---|
94 | } |
---|
95 | |
---|
96 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
97 | { |
---|
98 | SUPER(SpaceShip, XMLPort, xmlelement, mode); |
---|
99 | |
---|
100 | XMLPortParam(SpaceShip, "engine", setEngineTemplate, getEngineTemplate, xmlelement, mode); |
---|
101 | XMLPortParamVariable(SpaceShip, "primaryThrust", primaryThrust_, xmlelement, mode); |
---|
102 | XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); |
---|
103 | XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode); |
---|
104 | XMLPortParamVariable(SpaceShip, "boostPower", initialBoostPower_, xmlelement, mode); |
---|
105 | XMLPortParamVariable(SpaceShip, "boostPowerRate", boostPowerRate_, xmlelement, mode); |
---|
106 | XMLPortParamVariable(SpaceShip, "boostRate", boostRate_, xmlelement, mode); |
---|
107 | XMLPortParamVariable(SpaceShip, "boostCooldownDuration", boostCooldownDuration_, xmlelement, mode); |
---|
108 | XMLPortParamVariable(SpaceShip, "shakeFrequency", shakeFrequency_, 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 | registerVariable(this->shakeFrequency_, VariableDirection::ToClient); |
---|
121 | } |
---|
122 | |
---|
123 | void SpaceShip::setConfigValues() |
---|
124 | { |
---|
125 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
126 | } |
---|
127 | |
---|
128 | bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
129 | { |
---|
130 | if (type != WorldEntity::Dynamic) |
---|
131 | { |
---|
132 | CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl; |
---|
133 | assert(false); // Only in debug mode |
---|
134 | return false; |
---|
135 | } |
---|
136 | else |
---|
137 | return true; |
---|
138 | } |
---|
139 | |
---|
140 | void SpaceShip::tick(float dt) |
---|
141 | { |
---|
142 | SUPER(SpaceShip, tick, dt); |
---|
143 | |
---|
144 | if (this->hasLocalController()) |
---|
145 | { |
---|
146 | |
---|
147 | /* |
---|
148 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_); |
---|
149 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_); |
---|
150 | if (this->localLinearAcceleration_.z() > 0) |
---|
151 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_); |
---|
152 | else |
---|
153 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); |
---|
154 | this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); |
---|
155 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
156 | */ |
---|
157 | if (!this->isInMouseLook()) |
---|
158 | { |
---|
159 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
160 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
161 | } |
---|
162 | |
---|
163 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
164 | |
---|
165 | if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_) |
---|
166 | { |
---|
167 | this->boostPower_ += this->boostPowerRate_*dt; |
---|
168 | } |
---|
169 | |
---|
170 | if(this->bBoost_) |
---|
171 | { |
---|
172 | this->boostPower_ -=this->boostRate_*dt; |
---|
173 | if(this->boostPower_ <= 0.0f) |
---|
174 | { |
---|
175 | this->boost(false); |
---|
176 | this->bBoostCooldown_ = true; |
---|
177 | this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this))); |
---|
178 | |
---|
179 | } |
---|
180 | |
---|
181 | shakeCamera(dt); |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | void SpaceShip::moveFrontBack(const Vector2& value) |
---|
187 | { |
---|
188 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); |
---|
189 | this->steering_.z = -value.x; |
---|
190 | } |
---|
191 | |
---|
192 | void SpaceShip::moveRightLeft(const Vector2& value) |
---|
193 | { |
---|
194 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); |
---|
195 | this->steering_.x = value.x; |
---|
196 | } |
---|
197 | |
---|
198 | void SpaceShip::moveUpDown(const Vector2& value) |
---|
199 | { |
---|
200 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); |
---|
201 | this->steering_.y = value.x; |
---|
202 | } |
---|
203 | |
---|
204 | void SpaceShip::rotateYaw(const Vector2& value) |
---|
205 | { |
---|
206 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x); |
---|
207 | |
---|
208 | Pawn::rotateYaw(value); |
---|
209 | } |
---|
210 | |
---|
211 | void SpaceShip::rotatePitch(const Vector2& value) |
---|
212 | { |
---|
213 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); |
---|
214 | |
---|
215 | Pawn::rotatePitch(value); |
---|
216 | } |
---|
217 | |
---|
218 | void SpaceShip::rotateRoll(const Vector2& value) |
---|
219 | { |
---|
220 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); |
---|
221 | |
---|
222 | Pawn::rotateRoll(value); |
---|
223 | } |
---|
224 | |
---|
225 | void SpaceShip::fire() |
---|
226 | { |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | @brief |
---|
231 | Starts or stops boosting. |
---|
232 | @param bBoost |
---|
233 | Whether to start or stop boosting. |
---|
234 | */ |
---|
235 | void SpaceShip::boost(bool bBoost) |
---|
236 | { |
---|
237 | if(bBoost && !this->bBoostCooldown_) |
---|
238 | { |
---|
239 | //COUT(0) << "Boost startet!\n"; |
---|
240 | this->bBoost_ = true; |
---|
241 | } |
---|
242 | if(!bBoost) |
---|
243 | { |
---|
244 | //COUT(0) << "Boost stoppt\n"; |
---|
245 | this->resetCamera(); |
---|
246 | this->bBoost_ = false; |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | void SpaceShip::boostCooledDown(void) |
---|
251 | { |
---|
252 | this->bBoostCooldown_ = false; |
---|
253 | } |
---|
254 | |
---|
255 | void SpaceShip::shakeCamera(float dt) |
---|
256 | { |
---|
257 | //make sure the ship is only shaking if it's moving |
---|
258 | if (this->getVelocity().squaredLength() > 80) |
---|
259 | { |
---|
260 | this->shakeDt_ += dt; |
---|
261 | |
---|
262 | int frequency = this->shakeFrequency_ * (this->getVelocity().squaredLength()); |
---|
263 | |
---|
264 | if (this->shakeDt_ >= 1 /(frequency)) |
---|
265 | { |
---|
266 | this->shakeDt_ -= 1/(frequency); |
---|
267 | } |
---|
268 | |
---|
269 | Degree angle = Degree(sin(this->shakeDt_ * 2* math::pi * frequency) * this->shakeAmplitude_); |
---|
270 | |
---|
271 | // COUT(0) << "Angle: " << angle << std::endl; |
---|
272 | Camera* c = this->getCamera(); |
---|
273 | |
---|
274 | //Shaking Camera effect |
---|
275 | if (c != 0) |
---|
276 | { |
---|
277 | c->setOrientation(Vector3::UNIT_X, angle); |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | void SpaceShip::resetCamera() |
---|
283 | { |
---|
284 | |
---|
285 | // COUT(0) << "Resetting camera\n"; |
---|
286 | Camera *c = this->getCamera(); |
---|
287 | |
---|
288 | if (c == 0) |
---|
289 | { |
---|
290 | COUT(2) << "Failed to reset camera!"; |
---|
291 | return; |
---|
292 | } |
---|
293 | |
---|
294 | shakeDt_ = 0; |
---|
295 | // |
---|
296 | c->setPosition(this->cameraOriginalPosition); |
---|
297 | c->setOrientation(this->cameraOriginalOrientation); |
---|
298 | } |
---|
299 | |
---|
300 | void SpaceShip::loadEngineTemplate() |
---|
301 | { |
---|
302 | if (!this->enginetemplate_.empty()) |
---|
303 | { |
---|
304 | Template* temp = Template::getTemplate(this->enginetemplate_); |
---|
305 | |
---|
306 | if (temp) |
---|
307 | { |
---|
308 | Identifier* identifier = temp->getBaseclassIdentifier(); |
---|
309 | |
---|
310 | if (identifier) |
---|
311 | { |
---|
312 | BaseObject* object = identifier->fabricate(this); |
---|
313 | this->engine_ = orxonox_cast<Engine*>(object); |
---|
314 | |
---|
315 | if (this->engine_) |
---|
316 | { |
---|
317 | this->engine_->addTemplate(temp); |
---|
318 | this->engine_->addToSpaceShip(this); |
---|
319 | } |
---|
320 | else |
---|
321 | { |
---|
322 | object->destroy(); |
---|
323 | } |
---|
324 | } |
---|
325 | } |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | void SpaceShip::setEngine(Engine* engine) |
---|
330 | { |
---|
331 | this->engine_ = engine; |
---|
332 | if (engine && engine->getShip() != this) |
---|
333 | engine->addToSpaceShip(this); |
---|
334 | } |
---|
335 | |
---|
336 | std::vector<PickupCarrier*>* SpaceShip::getCarrierChildren(void) const |
---|
337 | { |
---|
338 | std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>(); |
---|
339 | list->push_back(this->engine_); |
---|
340 | return list; |
---|
341 | } |
---|
342 | |
---|
343 | |
---|
344 | } |
---|