1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Fabian 'x3n' Landau |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "OrxonoxStableHeaders.h" |
---|
29 | |
---|
30 | #include <string> |
---|
31 | |
---|
32 | #include <OIS/OIS.h> |
---|
33 | #include <OgreCamera.h> |
---|
34 | #include <OgreRenderWindow.h> |
---|
35 | #include <OgreParticleSystem.h> |
---|
36 | #include <OgreSceneNode.h> |
---|
37 | |
---|
38 | #include "util/tinyxml/tinyxml.h" |
---|
39 | #include "util/String2Number.h" |
---|
40 | #include "util/Math.h" |
---|
41 | #include "../core/CoreIncludes.h" |
---|
42 | #include "../core/Debug.h" |
---|
43 | #include "../Orxonox.h" |
---|
44 | #include "../particle/ParticleInterface.h" |
---|
45 | #include "Projectile.h" |
---|
46 | |
---|
47 | #include "SpaceShip.h" |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | CreateFactory(SpaceShip); |
---|
52 | |
---|
53 | SpaceShip::SpaceShip() |
---|
54 | { |
---|
55 | RegisterObject(SpaceShip); |
---|
56 | |
---|
57 | this->setConfigValues(); |
---|
58 | |
---|
59 | this->setMouseEventCallback_ = false; |
---|
60 | this->bLMousePressed_ = false; |
---|
61 | this->bRMousePressed_ = false; |
---|
62 | this->mouseX_ = 0; |
---|
63 | this->mouseY_ = 0; |
---|
64 | |
---|
65 | this->camNode_ = 0; |
---|
66 | |
---|
67 | this->tt_ = 0; |
---|
68 | this->redNode_ = 0; |
---|
69 | this->greenNode_ = 0; |
---|
70 | this->blinkTime_ = 0; |
---|
71 | |
---|
72 | this->timeToReload_ = 0; |
---|
73 | |
---|
74 | this->maxSpeed_ = 0; |
---|
75 | this->maxSideAndBackSpeed_ = 0; |
---|
76 | this->maxRotation_ = 0; |
---|
77 | this->translationAcceleration_ = 0; |
---|
78 | this->rotationAcceleration_ = 0; |
---|
79 | this->translationDamping_ = 0; |
---|
80 | this->rotationDamping_ = 0; |
---|
81 | |
---|
82 | this->maxRotationRadian_ = 0; |
---|
83 | this->rotationAccelerationRadian_ = 0; |
---|
84 | this->rotationDampingRadian_ = 0; |
---|
85 | this->zeroRadian_ = Radian(0); |
---|
86 | |
---|
87 | this->setRotationAxis(1, 0, 0); |
---|
88 | this->setStatic(false); |
---|
89 | |
---|
90 | COUT(3) << "Info: SpaceShip was loaded" << std::endl; |
---|
91 | } |
---|
92 | |
---|
93 | SpaceShip::~SpaceShip() |
---|
94 | { |
---|
95 | if (this->tt_) |
---|
96 | delete this->tt_; |
---|
97 | } |
---|
98 | |
---|
99 | void SpaceShip::setConfigValues() |
---|
100 | { |
---|
101 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
102 | SetConfigValue(reloadTime_, 0.125).description("The reload time of the weapon in seconds"); |
---|
103 | SetConfigValue(projectileName_, "Projectile").description("Projectile of the ship-weapon."); |
---|
104 | } |
---|
105 | |
---|
106 | void SpaceShip::loadParams(TiXmlElement* xmlElem) |
---|
107 | { |
---|
108 | Model::loadParams(xmlElem); |
---|
109 | |
---|
110 | // START CREATING THRUSTER |
---|
111 | this->tt_ = new ParticleInterface(Orxonox::getSingleton()->getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow"); |
---|
112 | this->tt_->getParticleSystem()->setParameter("local_space","true"); |
---|
113 | this->tt_->newEmitter(); |
---|
114 | |
---|
115 | this->tt_->setDirection(Vector3(-1,0,0)); |
---|
116 | this->tt_->setPositionOfEmitter(0, Vector3(-15,20,-1)); |
---|
117 | this->tt_->setPositionOfEmitter(1, Vector3(-15,-20,-1)); |
---|
118 | this->tt_->setVelocity(50); |
---|
119 | |
---|
120 | emitterRate_ = tt_->getRate(); |
---|
121 | |
---|
122 | Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2"); |
---|
123 | node2->setInheritScale(false); |
---|
124 | tt_->addToSceneNode(node2); |
---|
125 | // END CREATING THRUSTER |
---|
126 | |
---|
127 | // START CREATING BLINKING LIGHTS |
---|
128 | this->redBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); |
---|
129 | this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1); |
---|
130 | |
---|
131 | this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.7, -0.3)); |
---|
132 | this->redNode_->setInheritScale(false); |
---|
133 | this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.7, -0.3)); |
---|
134 | this->greenNode_->setInheritScale(false); |
---|
135 | |
---|
136 | this->redNode_->attachObject(this->redBillboard_.getBillboardSet()); |
---|
137 | this->redNode_->setScale(0.3, 0.3, 0.3); |
---|
138 | |
---|
139 | this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet()); |
---|
140 | this->greenNode_->setScale(0.3, 0.3, 0.3); |
---|
141 | // END CREATING BLINKING LIGHTS |
---|
142 | |
---|
143 | // START of testing crosshair |
---|
144 | this->crosshairNear_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
145 | this->crosshairFar_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
146 | |
---|
147 | this->chNearNode_ = this->getNode()->createChildSceneNode(this->getName() + "near", Vector3(50.0, 0.0, 0.0)); |
---|
148 | this->chNearNode_->setInheritScale(false); |
---|
149 | this->chFarNode_ = this->getNode()->createChildSceneNode(this->getName() + "far", Vector3(200.0, 0.0, 0.0)); |
---|
150 | this->chFarNode_->setInheritScale(false); |
---|
151 | |
---|
152 | this->chNearNode_->attachObject(this->crosshairNear_.getBillboardSet()); |
---|
153 | this->chNearNode_->setScale(0.2, 0.2, 0.2); |
---|
154 | |
---|
155 | this->chFarNode_->attachObject(this->crosshairFar_.getBillboardSet()); |
---|
156 | this->chFarNode_->setScale(0.4, 0.4, 0.4); |
---|
157 | |
---|
158 | // END of testing crosshair |
---|
159 | |
---|
160 | if (xmlElem->Attribute("maxSpeed") && xmlElem->Attribute("maxSideAndBackSpeed") && xmlElem->Attribute("maxRotation") && xmlElem->Attribute("transAcc") && xmlElem->Attribute("rotAcc") && xmlElem->Attribute("transDamp") && xmlElem->Attribute("rotDamp")) |
---|
161 | { |
---|
162 | |
---|
163 | std::string msStr = xmlElem->Attribute("maxSpeed"); |
---|
164 | std::string msabsStr = xmlElem->Attribute("maxSideAndBackSpeed"); |
---|
165 | std::string mrStr = xmlElem->Attribute("maxRotation"); |
---|
166 | std::string taStr = xmlElem->Attribute("transAcc"); |
---|
167 | std::string raStr = xmlElem->Attribute("rotAcc"); |
---|
168 | std::string tdStr = xmlElem->Attribute("transDamp"); |
---|
169 | std::string rdStr = xmlElem->Attribute("rotDamp"); |
---|
170 | |
---|
171 | String2Number<float>(this->maxSpeed_, msStr); |
---|
172 | String2Number<float>(this->maxSideAndBackSpeed_, msabsStr); |
---|
173 | String2Number<float>(this->maxRotation_, mrStr); |
---|
174 | String2Number<float>(this->translationAcceleration_, taStr); |
---|
175 | String2Number<float>(this->rotationAcceleration_, raStr); |
---|
176 | String2Number<float>(this->translationDamping_, tdStr); |
---|
177 | String2Number<float>(this->rotationDamping_, rdStr); |
---|
178 | |
---|
179 | this->maxRotationRadian_ = Radian(this->maxRotation_); |
---|
180 | this->rotationAccelerationRadian_ = Radian(this->rotationAcceleration_); |
---|
181 | this->rotationDampingRadian_ = Radian(this->rotationDamping_); |
---|
182 | |
---|
183 | COUT(4) << "Loader: Initialized SpaceShip" << std::endl; |
---|
184 | } |
---|
185 | |
---|
186 | if (xmlElem->Attribute("camera")) |
---|
187 | { |
---|
188 | Ogre::Camera *cam = Orxonox::getSingleton()->getSceneManager()->createCamera("ShipCam"); |
---|
189 | this->camNode_ = this->getNode()->createChildSceneNode("CamNode"); |
---|
190 | |
---|
191 | cam->setPosition(Vector3(-200,0,35)); |
---|
192 | cam->lookAt(Vector3(0,0,35)); |
---|
193 | cam->roll(Degree(-90)); |
---|
194 | |
---|
195 | this->camNode_->attachObject(cam); |
---|
196 | Orxonox::getSingleton()->getOgrePointer()->getRoot()->getAutoCreatedWindow()->addViewport(cam); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | int sgn(float x) |
---|
201 | { |
---|
202 | if (x >= 0) |
---|
203 | return 1; |
---|
204 | else |
---|
205 | return -1; |
---|
206 | } |
---|
207 | |
---|
208 | bool SpaceShip::mouseMoved(const OIS::MouseEvent &e) |
---|
209 | { |
---|
210 | if (this->bRMousePressed_) |
---|
211 | { |
---|
212 | this->camNode_->roll(Degree(-e.state.X.rel * 0.10)); |
---|
213 | this->camNode_->yaw(Degree(e.state.Y.rel * 0.10)); |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | float minDimension = e.state.height; |
---|
218 | if (e.state.width < minDimension) |
---|
219 | minDimension = e.state.width; |
---|
220 | |
---|
221 | this->mouseX_ += e.state.X.rel; |
---|
222 | if (this->mouseX_ < -minDimension) |
---|
223 | this->mouseX_ = -minDimension; |
---|
224 | if (this->mouseX_ > minDimension) |
---|
225 | this->mouseX_ = minDimension; |
---|
226 | |
---|
227 | this->mouseY_ += e.state.Y.rel; |
---|
228 | if (this->mouseY_ < -minDimension) |
---|
229 | this->mouseY_ = -minDimension; |
---|
230 | if (this->mouseY_ > minDimension) |
---|
231 | this->mouseY_ = minDimension; |
---|
232 | |
---|
233 | float xRotation = this->mouseX_ / minDimension; |
---|
234 | xRotation = xRotation*xRotation * sgn(xRotation); |
---|
235 | xRotation *= -this->rotationAcceleration_; |
---|
236 | if (xRotation > this->maxRotation_) |
---|
237 | xRotation = this->maxRotation_; |
---|
238 | if (xRotation < -this->maxRotation_) |
---|
239 | xRotation = -this->maxRotation_; |
---|
240 | this->mouseXRotation_ = Radian(xRotation); |
---|
241 | |
---|
242 | float yRotation = this->mouseY_ / minDimension; |
---|
243 | yRotation = yRotation*yRotation * sgn(yRotation); |
---|
244 | yRotation *= this->rotationAcceleration_; |
---|
245 | if (yRotation > this->maxRotation_) |
---|
246 | yRotation = this->maxRotation_; |
---|
247 | if (yRotation < -this->maxRotation_) |
---|
248 | yRotation = -this->maxRotation_; |
---|
249 | this->mouseYRotation_ = Radian(yRotation); |
---|
250 | } |
---|
251 | |
---|
252 | return true; |
---|
253 | } |
---|
254 | |
---|
255 | bool SpaceShip::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
256 | { |
---|
257 | if (id == OIS::MB_Left) |
---|
258 | this->bLMousePressed_ = true; |
---|
259 | else if (id == OIS::MB_Right) |
---|
260 | this->bRMousePressed_ = true; |
---|
261 | |
---|
262 | return true; |
---|
263 | } |
---|
264 | |
---|
265 | bool SpaceShip::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
266 | { |
---|
267 | if (id == OIS::MB_Left) |
---|
268 | this->bLMousePressed_ = false; |
---|
269 | else if (id == OIS::MB_Right) |
---|
270 | { |
---|
271 | this->bRMousePressed_ = false; |
---|
272 | this->camNode_->resetOrientation(); |
---|
273 | } |
---|
274 | |
---|
275 | return true; |
---|
276 | } |
---|
277 | |
---|
278 | void SpaceShip::tick(float dt) |
---|
279 | { |
---|
280 | if (!this->setMouseEventCallback_) |
---|
281 | { |
---|
282 | if (Orxonox::getSingleton()->getMouse()) |
---|
283 | { |
---|
284 | Orxonox::getSingleton()->getMouse()->setEventCallback(this); |
---|
285 | this->setMouseEventCallback_ = true; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | if (this->redNode_ && this->greenNode_) |
---|
290 | { |
---|
291 | this->blinkTime_ += dt; |
---|
292 | float redScale = 0.15 + 0.15 * sin(this->blinkTime_ * 10.0); |
---|
293 | float greenScale = 0.15 - 0.15 * sin(this->blinkTime_ * 10.0); |
---|
294 | this->redNode_->setScale(redScale, redScale, redScale); |
---|
295 | this->greenNode_->setScale(greenScale, greenScale, greenScale); |
---|
296 | } |
---|
297 | |
---|
298 | if (this->timeToReload_ > 0) |
---|
299 | this->timeToReload_ -= dt; |
---|
300 | else |
---|
301 | this->timeToReload_ = 0; |
---|
302 | |
---|
303 | if (this->bLMousePressed_ && this->timeToReload_ <= 0) |
---|
304 | { |
---|
305 | SubclassIdentifier<Projectile> projectile = ID(this->projectileName_); |
---|
306 | Projectile* newProjectile = projectile.fabricate(); |
---|
307 | newProjectile->setOwner(this); |
---|
308 | |
---|
309 | this->timeToReload_ = this->reloadTime_; |
---|
310 | } |
---|
311 | |
---|
312 | OIS::Keyboard* mKeyboard = Orxonox::getSingleton()->getKeyboard(); |
---|
313 | OIS::Mouse* mMouse = Orxonox::getSingleton()->getMouse(); |
---|
314 | |
---|
315 | mKeyboard->capture(); |
---|
316 | mMouse->capture(); |
---|
317 | |
---|
318 | |
---|
319 | // ##################################### |
---|
320 | // ############# STEERING ############## |
---|
321 | // ##################################### |
---|
322 | |
---|
323 | if (this->velocity_.x > this->maxSpeed_) |
---|
324 | this->velocity_.x = this->maxSpeed_; |
---|
325 | if (this->velocity_.x < -this->maxSideAndBackSpeed_) |
---|
326 | this->velocity_.x = -this->maxSideAndBackSpeed_; |
---|
327 | if (this->velocity_.y > this->maxSideAndBackSpeed_) |
---|
328 | this->velocity_.y = this->maxSideAndBackSpeed_; |
---|
329 | if (this->velocity_.y < -this->maxSideAndBackSpeed_) |
---|
330 | this->velocity_.y = -this->maxSideAndBackSpeed_; |
---|
331 | if (this->rotationRate_ > this->maxRotationRadian_) |
---|
332 | this->rotationRate_ = this->maxRotationRadian_; |
---|
333 | if (this->rotationRate_ < -this->maxRotationRadian_) |
---|
334 | this->rotationRate_ = -this->maxRotationRadian_; |
---|
335 | |
---|
336 | if (this->acceleration_.x == 0) |
---|
337 | { |
---|
338 | if (this->velocity_.x > 0) |
---|
339 | { |
---|
340 | this->velocity_.x -= (this->translationDamping_ * dt); |
---|
341 | if (this->velocity_.x < 0) |
---|
342 | this->velocity_.x = 0; |
---|
343 | } |
---|
344 | else if (this->velocity_.x < 0) |
---|
345 | { |
---|
346 | this->velocity_.x += (this->translationDamping_ * dt); |
---|
347 | if (this->velocity_.x > 0) |
---|
348 | this->velocity_.x = 0; |
---|
349 | } |
---|
350 | } |
---|
351 | |
---|
352 | if (this->acceleration_.y == 0) |
---|
353 | { |
---|
354 | if (this->velocity_.y > 0) |
---|
355 | { |
---|
356 | this->velocity_.y -= (this->translationDamping_ * dt); |
---|
357 | if (this->velocity_.y < 0) |
---|
358 | this->velocity_.y = 0; |
---|
359 | } |
---|
360 | else if (this->velocity_.y < 0) |
---|
361 | { |
---|
362 | this->velocity_.y += (this->translationDamping_ * dt); |
---|
363 | if (this->velocity_.y > 0) |
---|
364 | this->velocity_.y = 0; |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | if (this->momentum_ == this->zeroRadian_) |
---|
369 | { |
---|
370 | if (this->rotationRate_ > this->zeroRadian_) |
---|
371 | { |
---|
372 | this->rotationRate_ -= (this->rotationDampingRadian_ * dt); |
---|
373 | if (this->rotationRate_ < this->zeroRadian_) |
---|
374 | this->rotationRate_ = 0; |
---|
375 | } |
---|
376 | else if (this->rotationRate_ < this->zeroRadian_) |
---|
377 | { |
---|
378 | this->rotationRate_ += (this->rotationDampingRadian_ * dt); |
---|
379 | if (this->rotationRate_ > this->zeroRadian_) |
---|
380 | this->rotationRate_ = 0; |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
---|
385 | this->acceleration_.x = this->translationAcceleration_; |
---|
386 | else if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
---|
387 | this->acceleration_.x = -this->translationAcceleration_; |
---|
388 | else |
---|
389 | this->acceleration_.x = 0; |
---|
390 | |
---|
391 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
---|
392 | this->acceleration_.y = -this->translationAcceleration_; |
---|
393 | else if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
---|
394 | this->acceleration_.y = this->translationAcceleration_; |
---|
395 | else |
---|
396 | this->acceleration_.y = 0; |
---|
397 | |
---|
398 | if (mKeyboard->isKeyDown(OIS::KC_DELETE) || mKeyboard->isKeyDown(OIS::KC_Q)) |
---|
399 | this->momentum_ = Radian(-this->rotationAccelerationRadian_); |
---|
400 | else if (mKeyboard->isKeyDown(OIS::KC_PGDOWN) || mKeyboard->isKeyDown(OIS::KC_E)) |
---|
401 | this->momentum_ = Radian(this->rotationAccelerationRadian_); |
---|
402 | else |
---|
403 | this->momentum_ = 0; |
---|
404 | |
---|
405 | WorldEntity::tick(dt); |
---|
406 | |
---|
407 | this->roll(this->mouseXRotation_ * dt); |
---|
408 | if (this->bInvertYAxis_) |
---|
409 | this->yaw(Radian(-this->mouseYRotation_ * dt)); |
---|
410 | else |
---|
411 | this->yaw(Radian(this->mouseYRotation_ * dt)); |
---|
412 | |
---|
413 | if (this->acceleration_.x > 0) |
---|
414 | this->tt_->setRate(emitterRate_); |
---|
415 | else |
---|
416 | this->tt_->setRate(0); |
---|
417 | } |
---|
418 | } |
---|