[608] | 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: |
---|
[670] | 22 | * Fabian 'x3n' Landau |
---|
[608] | 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[786] | 28 | #include "OrxonoxStableHeaders.h" |
---|
[784] | 29 | |
---|
[715] | 30 | #include <string> |
---|
| 31 | |
---|
[708] | 32 | #include <OIS/OIS.h> |
---|
| 33 | #include <OgreCamera.h> |
---|
| 34 | #include <OgreRenderWindow.h> |
---|
| 35 | #include <OgreParticleSystem.h> |
---|
| 36 | #include <OgreSceneNode.h> |
---|
[608] | 37 | |
---|
[742] | 38 | #include "util/tinyxml/tinyxml.h" |
---|
| 39 | #include "util/String2Number.h" |
---|
| 40 | #include "util/Math.h" |
---|
[608] | 41 | #include "../core/CoreIncludes.h" |
---|
[708] | 42 | #include "../core/Debug.h" |
---|
[614] | 43 | #include "../Orxonox.h" |
---|
[708] | 44 | #include "../particle/ParticleInterface.h" |
---|
| 45 | #include "Projectile.h" |
---|
[871] | 46 | #include "core/XMLPort.h" |
---|
[947] | 47 | #include "core/ConsoleCommand.h" |
---|
[608] | 48 | |
---|
[708] | 49 | #include "SpaceShip.h" |
---|
[608] | 50 | |
---|
| 51 | namespace orxonox |
---|
| 52 | { |
---|
[947] | 53 | ConsoleCommand(SpaceShip, setMaxSpeedTest, AccessLevel::Debug, false); |
---|
[955] | 54 | ConsoleCommandGeneric(test1, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxSpeed", AccessLevel::Debug), false); |
---|
| 55 | ConsoleCommandGeneric(test2, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxBlubber", AccessLevel::Debug), false); |
---|
| 56 | ConsoleCommandGeneric(test3, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setRofl", AccessLevel::Debug), false); |
---|
[947] | 57 | |
---|
[608] | 58 | CreateFactory(SpaceShip); |
---|
| 59 | |
---|
[947] | 60 | SpaceShip* SpaceShip::instance_s; |
---|
| 61 | |
---|
[608] | 62 | SpaceShip::SpaceShip() |
---|
| 63 | { |
---|
| 64 | RegisterObject(SpaceShip); |
---|
| 65 | |
---|
[947] | 66 | SpaceShip::instance_s = this; |
---|
| 67 | |
---|
[697] | 68 | this->setConfigValues(); |
---|
[608] | 69 | |
---|
| 70 | this->setMouseEventCallback_ = false; |
---|
[644] | 71 | this->bLMousePressed_ = false; |
---|
| 72 | this->bRMousePressed_ = false; |
---|
[647] | 73 | this->mouseX_ = 0; |
---|
| 74 | this->mouseY_ = 0; |
---|
[608] | 75 | |
---|
[644] | 76 | this->camNode_ = 0; |
---|
| 77 | |
---|
[633] | 78 | this->tt_ = 0; |
---|
| 79 | this->redNode_ = 0; |
---|
| 80 | this->greenNode_ = 0; |
---|
| 81 | this->blinkTime_ = 0; |
---|
[623] | 82 | |
---|
[643] | 83 | this->timeToReload_ = 0; |
---|
| 84 | |
---|
[647] | 85 | this->maxSpeed_ = 0; |
---|
| 86 | this->maxSideAndBackSpeed_ = 0; |
---|
| 87 | this->maxRotation_ = 0; |
---|
| 88 | this->translationAcceleration_ = 0; |
---|
| 89 | this->rotationAcceleration_ = 0; |
---|
| 90 | this->translationDamping_ = 0; |
---|
| 91 | this->rotationDamping_ = 0; |
---|
| 92 | |
---|
| 93 | this->maxRotationRadian_ = 0; |
---|
| 94 | this->rotationAccelerationRadian_ = 0; |
---|
| 95 | this->rotationDampingRadian_ = 0; |
---|
| 96 | this->zeroRadian_ = Radian(0); |
---|
| 97 | |
---|
| 98 | this->setRotationAxis(1, 0, 0); |
---|
| 99 | this->setStatic(false); |
---|
| 100 | /* |
---|
[608] | 101 | this->moveForward_ = 0; |
---|
| 102 | this->rotateUp_ = 0; |
---|
| 103 | this->rotateDown_ = 0; |
---|
| 104 | this->rotateRight_ = 0; |
---|
| 105 | this->rotateLeft_ = 0; |
---|
| 106 | this->loopRight_ = 0; |
---|
| 107 | this->loopLeft_ = 0; |
---|
| 108 | this->brakeForward_ = 0; |
---|
| 109 | this->brakeRotate_ = 0; |
---|
| 110 | this->brakeLoop_ = 0; |
---|
| 111 | this->speedForward_ = 0; |
---|
| 112 | this->speedRotateUpDown_ = 0; |
---|
| 113 | this->speedRotateRightLeft_ = 0; |
---|
| 114 | this->speedLoopRightLeft_ = 0; |
---|
| 115 | this->maxSpeedForward_ = 0; |
---|
| 116 | this->maxSpeedRotateUpDown_ = 0; |
---|
| 117 | this->maxSpeedRotateRightLeft_ = 0; |
---|
| 118 | this->maxSpeedLoopRightLeft_ = 0; |
---|
| 119 | this->accelerationForward_ = 0; |
---|
| 120 | this->accelerationRotateUpDown_ = 0; |
---|
| 121 | this->accelerationRotateRightLeft_ = 0; |
---|
| 122 | this->accelerationLoopRightLeft_ = 0; |
---|
| 123 | |
---|
| 124 | this->speed = 250; |
---|
| 125 | this->loop = 100; |
---|
| 126 | this->rotate = 10; |
---|
| 127 | this->mouseX = 0; |
---|
| 128 | this->mouseY = 0; |
---|
| 129 | this->maxMouseX = 0; |
---|
| 130 | this->minMouseX = 0; |
---|
| 131 | this->moved = false; |
---|
| 132 | |
---|
| 133 | this->brakeRotate(rotate*10); |
---|
| 134 | this->brakeLoop(loop); |
---|
[647] | 135 | */ |
---|
[871] | 136 | this->init(); |
---|
| 137 | |
---|
[608] | 138 | COUT(3) << "Info: SpaceShip was loaded" << std::endl; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | SpaceShip::~SpaceShip() |
---|
| 142 | { |
---|
[647] | 143 | if (this->tt_) |
---|
| 144 | delete this->tt_; |
---|
[608] | 145 | } |
---|
| 146 | |
---|
[871] | 147 | void SpaceShip::init() |
---|
[697] | 148 | { |
---|
[633] | 149 | // START CREATING THRUSTER |
---|
[708] | 150 | this->tt_ = new ParticleInterface(Orxonox::getSingleton()->getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow"); |
---|
[647] | 151 | this->tt_->getParticleSystem()->setParameter("local_space","true"); |
---|
| 152 | this->tt_->newEmitter(); |
---|
[608] | 153 | /* |
---|
[647] | 154 | this->tt_->setDirection(Vector3(0,0,1)); |
---|
| 155 | this->tt_->setPositionOfEmitter(0, Vector3(20,-1,-15)); |
---|
| 156 | this->tt_->setPositionOfEmitter(1, Vector3(-20,-1,-15)); |
---|
[608] | 157 | */ |
---|
[647] | 158 | this->tt_->setDirection(Vector3(-1,0,0)); |
---|
| 159 | this->tt_->setPositionOfEmitter(0, Vector3(-15,20,-1)); |
---|
| 160 | this->tt_->setPositionOfEmitter(1, Vector3(-15,-20,-1)); |
---|
| 161 | this->tt_->setVelocity(50); |
---|
[608] | 162 | |
---|
[633] | 163 | emitterRate_ = tt_->getRate(); |
---|
[626] | 164 | |
---|
[633] | 165 | Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2"); |
---|
| 166 | node2->setInheritScale(false); |
---|
| 167 | tt_->addToSceneNode(node2); |
---|
| 168 | // END CREATING THRUSTER |
---|
[608] | 169 | |
---|
[633] | 170 | // START CREATING BLINKING LIGHTS |
---|
| 171 | this->redBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); |
---|
| 172 | this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1); |
---|
[608] | 173 | |
---|
[644] | 174 | this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.7, -0.3)); |
---|
[633] | 175 | this->redNode_->setInheritScale(false); |
---|
[644] | 176 | this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.7, -0.3)); |
---|
[633] | 177 | this->greenNode_->setInheritScale(false); |
---|
[608] | 178 | |
---|
[633] | 179 | this->redNode_->attachObject(this->redBillboard_.getBillboardSet()); |
---|
| 180 | this->redNode_->setScale(0.3, 0.3, 0.3); |
---|
[608] | 181 | |
---|
[633] | 182 | this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet()); |
---|
| 183 | this->greenNode_->setScale(0.3, 0.3, 0.3); |
---|
| 184 | // END CREATING BLINKING LIGHTS |
---|
[608] | 185 | |
---|
[661] | 186 | // START of testing crosshair |
---|
| 187 | this->crosshairNear_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
| 188 | this->crosshairFar_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
[608] | 189 | |
---|
[661] | 190 | this->chNearNode_ = this->getNode()->createChildSceneNode(this->getName() + "near", Vector3(50.0, 0.0, 0.0)); |
---|
| 191 | this->chNearNode_->setInheritScale(false); |
---|
| 192 | this->chFarNode_ = this->getNode()->createChildSceneNode(this->getName() + "far", Vector3(200.0, 0.0, 0.0)); |
---|
| 193 | this->chFarNode_->setInheritScale(false); |
---|
| 194 | |
---|
| 195 | this->chNearNode_->attachObject(this->crosshairNear_.getBillboardSet()); |
---|
| 196 | this->chNearNode_->setScale(0.2, 0.2, 0.2); |
---|
| 197 | |
---|
| 198 | this->chFarNode_->attachObject(this->crosshairFar_.getBillboardSet()); |
---|
| 199 | this->chFarNode_->setScale(0.4, 0.4, 0.4); |
---|
| 200 | |
---|
| 201 | // END of testing crosshair |
---|
[871] | 202 | } |
---|
[661] | 203 | |
---|
[871] | 204 | void SpaceShip::setConfigValues() |
---|
| 205 | { |
---|
| 206 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
| 207 | SetConfigValue(reloadTime_, 0.125).description("The reload time of the weapon in seconds"); |
---|
| 208 | SetConfigValue(testvector_, Vector3()).description("asdfblah"); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void SpaceShip::loadParams(TiXmlElement* xmlElem) |
---|
| 212 | { |
---|
| 213 | Model::loadParams(xmlElem); |
---|
[647] | 214 | /* |
---|
[608] | 215 | if (xmlElem->Attribute("forward") && xmlElem->Attribute("rotateupdown") && xmlElem->Attribute("rotaterightleft") && xmlElem->Attribute("looprightleft")) |
---|
| 216 | { |
---|
[715] | 217 | std::string forwardStr = xmlElem->Attribute("forward"); |
---|
| 218 | std::string rotateupdownStr = xmlElem->Attribute("rotateupdown"); |
---|
| 219 | std::string rotaterightleftStr = xmlElem->Attribute("rotaterightleft"); |
---|
| 220 | std::string looprightleftStr = xmlElem->Attribute("looprightleft"); |
---|
[608] | 221 | |
---|
| 222 | String2Number<float>(this->maxSpeedForward_, forwardStr); |
---|
| 223 | String2Number<float>(this->maxSpeedRotateUpDown_, rotateupdownStr); |
---|
| 224 | String2Number<float>(this->maxSpeedRotateRightLeft_, rotaterightleftStr); |
---|
| 225 | String2Number<float>(this->maxSpeedLoopRightLeft_, looprightleftStr); |
---|
| 226 | |
---|
| 227 | COUT(4) << "Loader: Initialized spaceship steering with values " << maxSpeedForward_ << " " << maxSpeedRotateUpDown_ << " " << maxSpeedRotateRightLeft_ << " " << maxSpeedLoopRightLeft_ << " " << std::endl; |
---|
| 228 | } |
---|
[647] | 229 | */ |
---|
| 230 | if (xmlElem->Attribute("maxSpeed") && xmlElem->Attribute("maxSideAndBackSpeed") && xmlElem->Attribute("maxRotation") && xmlElem->Attribute("transAcc") && xmlElem->Attribute("rotAcc") && xmlElem->Attribute("transDamp") && xmlElem->Attribute("rotDamp")) |
---|
| 231 | { |
---|
[608] | 232 | |
---|
[715] | 233 | std::string msStr = xmlElem->Attribute("maxSpeed"); |
---|
| 234 | std::string msabsStr = xmlElem->Attribute("maxSideAndBackSpeed"); |
---|
| 235 | std::string mrStr = xmlElem->Attribute("maxRotation"); |
---|
| 236 | std::string taStr = xmlElem->Attribute("transAcc"); |
---|
| 237 | std::string raStr = xmlElem->Attribute("rotAcc"); |
---|
| 238 | std::string tdStr = xmlElem->Attribute("transDamp"); |
---|
| 239 | std::string rdStr = xmlElem->Attribute("rotDamp"); |
---|
[647] | 240 | |
---|
| 241 | String2Number<float>(this->maxSpeed_, msStr); |
---|
| 242 | String2Number<float>(this->maxSideAndBackSpeed_, msabsStr); |
---|
| 243 | String2Number<float>(this->maxRotation_, mrStr); |
---|
| 244 | String2Number<float>(this->translationAcceleration_, taStr); |
---|
| 245 | String2Number<float>(this->rotationAcceleration_, raStr); |
---|
| 246 | String2Number<float>(this->translationDamping_, tdStr); |
---|
| 247 | String2Number<float>(this->rotationDamping_, rdStr); |
---|
| 248 | |
---|
| 249 | this->maxRotationRadian_ = Radian(this->maxRotation_); |
---|
| 250 | this->rotationAccelerationRadian_ = Radian(this->rotationAcceleration_); |
---|
| 251 | this->rotationDampingRadian_ = Radian(this->rotationDamping_); |
---|
| 252 | |
---|
| 253 | COUT(4) << "Loader: Initialized SpaceShip" << std::endl; |
---|
| 254 | } |
---|
| 255 | |
---|
[608] | 256 | if (xmlElem->Attribute("camera")) |
---|
| 257 | { |
---|
[871] | 258 | this->setCamera(); |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | void SpaceShip::setCamera(const std::string& camera) |
---|
| 263 | { |
---|
| 264 | Ogre::Camera *cam = Orxonox::getSingleton()->getSceneManager()->createCamera("ShipCam"); |
---|
| 265 | this->camNode_ = this->getNode()->createChildSceneNode("CamNode"); |
---|
[608] | 266 | /* |
---|
[871] | 267 | // node->setInheritOrientation(false); |
---|
| 268 | cam->setPosition(Vector3(0,50,-150)); |
---|
| 269 | cam->lookAt(Vector3(0,20,0)); |
---|
| 270 | cam->roll(Degree(0)); |
---|
[608] | 271 | */ |
---|
| 272 | |
---|
[871] | 273 | cam->setPosition(Vector3(-200,0,35)); |
---|
| 274 | // cam->setPosition(Vector3(0,-350,0)); |
---|
| 275 | cam->lookAt(Vector3(0,0,35)); |
---|
| 276 | cam->roll(Degree(-90)); |
---|
[608] | 277 | |
---|
[871] | 278 | this->camNode_->attachObject(cam); |
---|
| 279 | Orxonox::getSingleton()->getOgrePointer()->getRoot()->getAutoCreatedWindow()->addViewport(cam); |
---|
[608] | 280 | } |
---|
| 281 | |
---|
[871] | 282 | void SpaceShip::setMaxSpeed(float value) |
---|
| 283 | { this->maxSpeed_ = value; } |
---|
| 284 | void SpaceShip::setMaxSideAndBackSpeed(float value) |
---|
| 285 | { this->maxSideAndBackSpeed_ = value; } |
---|
| 286 | void SpaceShip::setMaxRotation(float value) |
---|
| 287 | { this->maxRotation_ = value; this->maxRotationRadian_ = Radian(value); } |
---|
| 288 | void SpaceShip::setTransAcc(float value) |
---|
| 289 | { this->translationAcceleration_ = value; } |
---|
| 290 | void SpaceShip::setRotAcc(float value) |
---|
| 291 | { this->rotationAcceleration_ = value; this->rotationAccelerationRadian_ = Radian(value); } |
---|
| 292 | void SpaceShip::setTransDamp(float value) |
---|
| 293 | { this->translationDamping_ = value; } |
---|
| 294 | void SpaceShip::setRotDamp(float value) |
---|
| 295 | { this->rotationDamping_ = value; this->rotationDampingRadian_ = Radian(value); } |
---|
| 296 | |
---|
| 297 | /** |
---|
| 298 | @brief XML loading and saving. |
---|
| 299 | @param xmlelement The XML-element |
---|
| 300 | @param loading Loading (true) or saving (false) |
---|
| 301 | @return The XML-element |
---|
| 302 | */ |
---|
[902] | 303 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[871] | 304 | { |
---|
[902] | 305 | Model::XMLPort(xmlelement, mode); |
---|
[871] | 306 | |
---|
[902] | 307 | XMLPortParamLoadOnly(SpaceShip, "camera", setCamera, xmlelement, mode); |
---|
| 308 | XMLPortParamLoadOnly(SpaceShip, "maxSpeed", setMaxSpeed, xmlelement, mode); |
---|
| 309 | XMLPortParamLoadOnly(SpaceShip, "maxSideAndBackSpeed", setMaxSideAndBackSpeed, xmlelement, mode); |
---|
| 310 | XMLPortParamLoadOnly(SpaceShip, "maxRotation", setMaxRotation, xmlelement, mode); |
---|
| 311 | XMLPortParamLoadOnly(SpaceShip, "transAcc", setTransAcc, xmlelement, mode); |
---|
| 312 | XMLPortParamLoadOnly(SpaceShip, "rotAcc", setRotAcc, xmlelement, mode); |
---|
| 313 | XMLPortParamLoadOnly(SpaceShip, "transDamp", setTransDamp, xmlelement, mode); |
---|
| 314 | XMLPortParamLoadOnly(SpaceShip, "rotDamp", setRotDamp, xmlelement, mode); |
---|
[871] | 315 | } |
---|
| 316 | |
---|
[647] | 317 | int sgn(float x) |
---|
| 318 | { |
---|
| 319 | if (x >= 0) |
---|
| 320 | return 1; |
---|
| 321 | else |
---|
| 322 | return -1; |
---|
| 323 | } |
---|
| 324 | |
---|
[608] | 325 | bool SpaceShip::mouseMoved(const OIS::MouseEvent &e) |
---|
| 326 | { |
---|
[647] | 327 | /* |
---|
[608] | 328 | this->mouseX += e.state.X.rel; |
---|
| 329 | if (this->bInvertMouse_) |
---|
| 330 | this->mouseY += e.state.Y.rel; |
---|
| 331 | else |
---|
| 332 | this->mouseY -= e.state.Y.rel; |
---|
| 333 | |
---|
| 334 | // if(mouseX>maxMouseX) maxMouseX = mouseX; |
---|
| 335 | // if(mouseX<minMouseX) minMouseX = mouseX; |
---|
| 336 | // cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl; |
---|
| 337 | |
---|
| 338 | this->moved = true; |
---|
[647] | 339 | */ |
---|
[644] | 340 | if (this->bRMousePressed_) |
---|
| 341 | { |
---|
| 342 | this->camNode_->roll(Degree(-e.state.X.rel * 0.10)); |
---|
| 343 | this->camNode_->yaw(Degree(e.state.Y.rel * 0.10)); |
---|
| 344 | } |
---|
[647] | 345 | else |
---|
| 346 | { |
---|
| 347 | float minDimension = e.state.height; |
---|
| 348 | if (e.state.width < minDimension) |
---|
| 349 | minDimension = e.state.width; |
---|
[644] | 350 | |
---|
[647] | 351 | this->mouseX_ += e.state.X.rel; |
---|
| 352 | if (this->mouseX_ < -minDimension) |
---|
| 353 | this->mouseX_ = -minDimension; |
---|
| 354 | if (this->mouseX_ > minDimension) |
---|
| 355 | this->mouseX_ = minDimension; |
---|
| 356 | |
---|
| 357 | this->mouseY_ += e.state.Y.rel; |
---|
| 358 | if (this->mouseY_ < -minDimension) |
---|
| 359 | this->mouseY_ = -minDimension; |
---|
| 360 | if (this->mouseY_ > minDimension) |
---|
| 361 | this->mouseY_ = minDimension; |
---|
| 362 | |
---|
| 363 | float xRotation = this->mouseX_ / minDimension; |
---|
| 364 | xRotation = xRotation*xRotation * sgn(xRotation); |
---|
| 365 | xRotation *= -this->rotationAcceleration_; |
---|
| 366 | if (xRotation > this->maxRotation_) |
---|
| 367 | xRotation = this->maxRotation_; |
---|
| 368 | if (xRotation < -this->maxRotation_) |
---|
| 369 | xRotation = -this->maxRotation_; |
---|
| 370 | this->mouseXRotation_ = Radian(xRotation); |
---|
| 371 | |
---|
| 372 | float yRotation = this->mouseY_ / minDimension; |
---|
| 373 | yRotation = yRotation*yRotation * sgn(yRotation); |
---|
| 374 | yRotation *= this->rotationAcceleration_; |
---|
| 375 | if (yRotation > this->maxRotation_) |
---|
| 376 | yRotation = this->maxRotation_; |
---|
| 377 | if (yRotation < -this->maxRotation_) |
---|
| 378 | yRotation = -this->maxRotation_; |
---|
| 379 | this->mouseYRotation_ = Radian(yRotation); |
---|
| 380 | } |
---|
| 381 | |
---|
[608] | 382 | return true; |
---|
| 383 | } |
---|
| 384 | |
---|
[643] | 385 | bool SpaceShip::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 386 | { |
---|
[644] | 387 | if (id == OIS::MB_Left) |
---|
| 388 | this->bLMousePressed_ = true; |
---|
| 389 | else if (id == OIS::MB_Right) |
---|
| 390 | this->bRMousePressed_ = true; |
---|
[643] | 391 | |
---|
| 392 | return true; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | bool SpaceShip::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 396 | { |
---|
[644] | 397 | if (id == OIS::MB_Left) |
---|
| 398 | this->bLMousePressed_ = false; |
---|
| 399 | else if (id == OIS::MB_Right) |
---|
| 400 | { |
---|
| 401 | this->bRMousePressed_ = false; |
---|
| 402 | this->camNode_->resetOrientation(); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | return true; |
---|
[643] | 406 | } |
---|
| 407 | |
---|
[608] | 408 | void SpaceShip::tick(float dt) |
---|
| 409 | { |
---|
| 410 | if (!this->setMouseEventCallback_) |
---|
| 411 | { |
---|
| 412 | if (Orxonox::getSingleton()->getMouse()) |
---|
| 413 | { |
---|
| 414 | Orxonox::getSingleton()->getMouse()->setEventCallback(this); |
---|
| 415 | this->setMouseEventCallback_ = true; |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
[633] | 419 | if (this->redNode_ && this->greenNode_) |
---|
| 420 | { |
---|
| 421 | this->blinkTime_ += dt; |
---|
| 422 | float redScale = 0.15 + 0.15 * sin(this->blinkTime_ * 10.0); |
---|
| 423 | float greenScale = 0.15 - 0.15 * sin(this->blinkTime_ * 10.0); |
---|
| 424 | this->redNode_->setScale(redScale, redScale, redScale); |
---|
| 425 | this->greenNode_->setScale(greenScale, greenScale, greenScale); |
---|
| 426 | } |
---|
| 427 | |
---|
[643] | 428 | if (this->timeToReload_ > 0) |
---|
| 429 | this->timeToReload_ -= dt; |
---|
| 430 | else |
---|
| 431 | this->timeToReload_ = 0; |
---|
| 432 | |
---|
[644] | 433 | if (this->bLMousePressed_ && this->timeToReload_ <= 0) |
---|
[643] | 434 | { |
---|
[697] | 435 | new Projectile(this); |
---|
[643] | 436 | this->timeToReload_ = this->reloadTime_; |
---|
| 437 | } |
---|
| 438 | |
---|
[608] | 439 | OIS::Keyboard* mKeyboard = Orxonox::getSingleton()->getKeyboard(); |
---|
| 440 | OIS::Mouse* mMouse = Orxonox::getSingleton()->getMouse(); |
---|
| 441 | |
---|
| 442 | mKeyboard->capture(); |
---|
| 443 | mMouse->capture(); |
---|
| 444 | |
---|
[647] | 445 | |
---|
| 446 | // ##################################### |
---|
| 447 | // ############# STEERING ############## |
---|
| 448 | // ##################################### |
---|
| 449 | |
---|
| 450 | if (this->velocity_.x > this->maxSpeed_) |
---|
| 451 | this->velocity_.x = this->maxSpeed_; |
---|
| 452 | if (this->velocity_.x < -this->maxSideAndBackSpeed_) |
---|
| 453 | this->velocity_.x = -this->maxSideAndBackSpeed_; |
---|
| 454 | if (this->velocity_.y > this->maxSideAndBackSpeed_) |
---|
| 455 | this->velocity_.y = this->maxSideAndBackSpeed_; |
---|
| 456 | if (this->velocity_.y < -this->maxSideAndBackSpeed_) |
---|
| 457 | this->velocity_.y = -this->maxSideAndBackSpeed_; |
---|
| 458 | if (this->rotationRate_ > this->maxRotationRadian_) |
---|
| 459 | this->rotationRate_ = this->maxRotationRadian_; |
---|
| 460 | if (this->rotationRate_ < -this->maxRotationRadian_) |
---|
| 461 | this->rotationRate_ = -this->maxRotationRadian_; |
---|
| 462 | |
---|
| 463 | if (this->acceleration_.x == 0) |
---|
| 464 | { |
---|
| 465 | if (this->velocity_.x > 0) |
---|
| 466 | { |
---|
| 467 | this->velocity_.x -= (this->translationDamping_ * dt); |
---|
| 468 | if (this->velocity_.x < 0) |
---|
| 469 | this->velocity_.x = 0; |
---|
| 470 | } |
---|
| 471 | else if (this->velocity_.x < 0) |
---|
| 472 | { |
---|
| 473 | this->velocity_.x += (this->translationDamping_ * dt); |
---|
| 474 | if (this->velocity_.x > 0) |
---|
| 475 | this->velocity_.x = 0; |
---|
| 476 | } |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | if (this->acceleration_.y == 0) |
---|
| 480 | { |
---|
| 481 | if (this->velocity_.y > 0) |
---|
| 482 | { |
---|
| 483 | this->velocity_.y -= (this->translationDamping_ * dt); |
---|
| 484 | if (this->velocity_.y < 0) |
---|
| 485 | this->velocity_.y = 0; |
---|
| 486 | } |
---|
| 487 | else if (this->velocity_.y < 0) |
---|
| 488 | { |
---|
| 489 | this->velocity_.y += (this->translationDamping_ * dt); |
---|
| 490 | if (this->velocity_.y > 0) |
---|
| 491 | this->velocity_.y = 0; |
---|
| 492 | } |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | if (this->momentum_ == this->zeroRadian_) |
---|
| 496 | { |
---|
| 497 | if (this->rotationRate_ > this->zeroRadian_) |
---|
| 498 | { |
---|
| 499 | this->rotationRate_ -= (this->rotationDampingRadian_ * dt); |
---|
| 500 | if (this->rotationRate_ < this->zeroRadian_) |
---|
| 501 | this->rotationRate_ = 0; |
---|
| 502 | } |
---|
| 503 | else if (this->rotationRate_ < this->zeroRadian_) |
---|
| 504 | { |
---|
| 505 | this->rotationRate_ += (this->rotationDampingRadian_ * dt); |
---|
| 506 | if (this->rotationRate_ > this->zeroRadian_) |
---|
| 507 | this->rotationRate_ = 0; |
---|
| 508 | } |
---|
| 509 | } |
---|
| 510 | |
---|
[608] | 511 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
---|
[647] | 512 | this->acceleration_.x = this->translationAcceleration_; |
---|
| 513 | else if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
---|
| 514 | this->acceleration_.x = -this->translationAcceleration_; |
---|
| 515 | else |
---|
| 516 | this->acceleration_.x = 0; |
---|
| 517 | |
---|
| 518 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
---|
| 519 | this->acceleration_.y = -this->translationAcceleration_; |
---|
| 520 | else if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
---|
| 521 | this->acceleration_.y = this->translationAcceleration_; |
---|
| 522 | else |
---|
| 523 | this->acceleration_.y = 0; |
---|
| 524 | |
---|
| 525 | if (mKeyboard->isKeyDown(OIS::KC_DELETE) || mKeyboard->isKeyDown(OIS::KC_Q)) |
---|
| 526 | this->momentum_ = Radian(-this->rotationAccelerationRadian_); |
---|
| 527 | else if (mKeyboard->isKeyDown(OIS::KC_PGDOWN) || mKeyboard->isKeyDown(OIS::KC_E)) |
---|
| 528 | this->momentum_ = Radian(this->rotationAccelerationRadian_); |
---|
| 529 | else |
---|
| 530 | this->momentum_ = 0; |
---|
| 531 | |
---|
| 532 | WorldEntity::tick(dt); |
---|
| 533 | |
---|
| 534 | this->roll(this->mouseXRotation_ * dt); |
---|
| 535 | if (this->bInvertYAxis_) |
---|
| 536 | this->yaw(Radian(-this->mouseYRotation_ * dt)); |
---|
| 537 | else |
---|
| 538 | this->yaw(Radian(this->mouseYRotation_ * dt)); |
---|
| 539 | |
---|
| 540 | if (this->acceleration_.x > 0) |
---|
| 541 | this->tt_->setRate(emitterRate_); |
---|
| 542 | else |
---|
| 543 | this->tt_->setRate(0); |
---|
| 544 | |
---|
| 545 | /* |
---|
| 546 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
---|
[608] | 547 | this->moveForward(speed); |
---|
| 548 | else |
---|
| 549 | this->moveForward(0); |
---|
| 550 | |
---|
| 551 | if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
---|
| 552 | this->brakeForward(speed); |
---|
| 553 | else |
---|
| 554 | this->brakeForward(speed/10); |
---|
| 555 | |
---|
| 556 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
---|
| 557 | this->loopRight(loop); |
---|
| 558 | else |
---|
| 559 | this->loopRight(0); |
---|
| 560 | |
---|
| 561 | if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
---|
| 562 | this->loopLeft(loop); |
---|
| 563 | else |
---|
| 564 | this->loopLeft(0); |
---|
| 565 | |
---|
| 566 | if(moved) |
---|
| 567 | { |
---|
| 568 | if (mouseY<=0) |
---|
| 569 | this->rotateUp(-mouseY*rotate); |
---|
| 570 | if (mouseY>0) |
---|
| 571 | this->rotateDown(mouseY*rotate); |
---|
| 572 | if (mouseX>0) |
---|
| 573 | this->rotateRight(mouseX*rotate); |
---|
| 574 | if (mouseX<=0) |
---|
| 575 | this->rotateLeft(-mouseX*rotate); |
---|
| 576 | |
---|
| 577 | mouseY = 0; |
---|
| 578 | mouseX = 0; |
---|
| 579 | moved = false; |
---|
[647] | 580 | }*/ |
---|
| 581 | /* else |
---|
[608] | 582 | { |
---|
| 583 | this->rotateUp(0); |
---|
| 584 | this->rotateDown(0); |
---|
| 585 | this->rotateRight(0); |
---|
| 586 | this->rotateLeft(0); |
---|
[647] | 587 | }*/ |
---|
| 588 | /* |
---|
[608] | 589 | if(moveForward_ > 0) |
---|
| 590 | { |
---|
| 591 | accelerationForward_ = moveForward_; |
---|
| 592 | if(speedForward_ < maxSpeedForward_) |
---|
| 593 | speedForward_ += accelerationForward_*dt; |
---|
| 594 | if(speedForward_ > maxSpeedForward_) |
---|
| 595 | speedForward_ = maxSpeedForward_; |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | if(moveForward_ <= 0) |
---|
| 599 | { |
---|
[626] | 600 | accelerationForward_ = -brakeForward_; |
---|
[608] | 601 | if(speedForward_ > 0) |
---|
[626] | 602 | speedForward_ += accelerationForward_*dt; |
---|
[608] | 603 | if(speedForward_ < 0) |
---|
| 604 | speedForward_ = 0; |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | if(rotateUp_ > 0) |
---|
| 608 | { |
---|
| 609 | accelerationRotateUpDown_ = rotateUp_; |
---|
| 610 | if(speedRotateUpDown_ < maxSpeedRotateUpDown_) |
---|
| 611 | speedRotateUpDown_ += accelerationRotateUpDown_*dt; |
---|
| 612 | if(speedRotateUpDown_ > maxSpeedRotateUpDown_) |
---|
| 613 | speedRotateUpDown_ = maxSpeedRotateUpDown_; |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | if(rotateDown_ > 0) |
---|
| 617 | { |
---|
| 618 | accelerationRotateUpDown_ = rotateDown_; |
---|
| 619 | if(speedRotateUpDown_ > -maxSpeedRotateUpDown_) |
---|
| 620 | speedRotateUpDown_ -= accelerationRotateUpDown_*dt; |
---|
| 621 | if(speedRotateUpDown_ < -maxSpeedRotateUpDown_) |
---|
| 622 | speedRotateUpDown_ = -maxSpeedRotateUpDown_; |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | if(rotateUp_ == 0 && rotateDown_ == 0) |
---|
| 626 | { |
---|
| 627 | accelerationRotateUpDown_ = brakeRotate_; |
---|
| 628 | if(speedRotateUpDown_ > 0) |
---|
| 629 | speedRotateUpDown_ -= accelerationRotateUpDown_*dt; |
---|
| 630 | if(speedRotateUpDown_ < 0) |
---|
| 631 | speedRotateUpDown_ += accelerationRotateUpDown_*dt; |
---|
| 632 | if(fabs(speedRotateUpDown_) < accelerationRotateUpDown_*dt) |
---|
| 633 | speedRotateUpDown_ = 0; |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | if(rotateRight_ > 0) |
---|
| 637 | { |
---|
| 638 | accelerationRotateRightLeft_ = rotateRight_; |
---|
| 639 | if(speedRotateRightLeft_ > -maxSpeedRotateRightLeft_) |
---|
| 640 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt; |
---|
| 641 | if(speedRotateRightLeft_ < -maxSpeedRotateRightLeft_) |
---|
| 642 | speedRotateRightLeft_ = -maxSpeedRotateRightLeft_; |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | if(rotateLeft_ > 0) |
---|
| 646 | { |
---|
| 647 | accelerationRotateRightLeft_ = rotateLeft_; |
---|
| 648 | if(speedRotateRightLeft_ < maxSpeedRotateRightLeft_) |
---|
| 649 | speedRotateRightLeft_ += accelerationRotateRightLeft_*dt; |
---|
| 650 | if(speedRotateRightLeft_ > maxSpeedRotateRightLeft_) |
---|
| 651 | speedRotateRightLeft_ = maxSpeedRotateRightLeft_; |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | if(rotateRight_ == 0 && rotateLeft_ == 0) |
---|
| 655 | { |
---|
| 656 | accelerationRotateRightLeft_ = brakeRotate_; |
---|
| 657 | if(speedRotateRightLeft_ > 0) |
---|
| 658 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt; |
---|
| 659 | if(speedRotateRightLeft_ < 0) |
---|
| 660 | speedRotateRightLeft_ += accelerationRotateRightLeft_*dt; |
---|
| 661 | if(fabs(speedRotateRightLeft_) < accelerationRotateRightLeft_*dt) |
---|
| 662 | speedRotateRightLeft_ = 0; |
---|
| 663 | } |
---|
| 664 | |
---|
| 665 | if(loopRight_ > 0) |
---|
| 666 | { |
---|
| 667 | accelerationLoopRightLeft_ = loopRight_; |
---|
| 668 | if(speedLoopRightLeft_ < maxSpeedLoopRightLeft_) |
---|
| 669 | speedLoopRightLeft_ += accelerationLoopRightLeft_*dt; |
---|
| 670 | if(speedLoopRightLeft_ > maxSpeedLoopRightLeft_) |
---|
| 671 | speedLoopRightLeft_ = maxSpeedLoopRightLeft_; |
---|
| 672 | } |
---|
| 673 | |
---|
| 674 | if(loopLeft_ > 0) |
---|
| 675 | { |
---|
| 676 | accelerationLoopRightLeft_ = loopLeft_; |
---|
| 677 | if(speedLoopRightLeft_ > -maxSpeedLoopRightLeft_) |
---|
| 678 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt; |
---|
| 679 | if(speedLoopRightLeft_ < -maxSpeedLoopRightLeft_) |
---|
| 680 | speedLoopRightLeft_ = -maxSpeedLoopRightLeft_; |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | if(loopLeft_ == 0 && loopRight_ == 0) |
---|
| 684 | { |
---|
| 685 | accelerationLoopRightLeft_ = brakeLoop_; |
---|
| 686 | if(speedLoopRightLeft_ > 0) |
---|
| 687 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt; |
---|
| 688 | if(speedLoopRightLeft_ < 0) |
---|
| 689 | speedLoopRightLeft_ += accelerationLoopRightLeft_*dt; |
---|
| 690 | if(fabs(speedLoopRightLeft_) < accelerationLoopRightLeft_*dt) |
---|
| 691 | speedLoopRightLeft_ = 0; |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | Vector3 transVector = Vector3::ZERO; |
---|
[647] | 695 | */ |
---|
[608] | 696 | /* |
---|
| 697 | transVector.z = 1; |
---|
| 698 | this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL); |
---|
| 699 | this->pitch(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL); |
---|
| 700 | this->yaw(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
| 701 | this->roll(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
| 702 | */ |
---|
[647] | 703 | /* |
---|
[608] | 704 | transVector.x = 1; |
---|
| 705 | this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL); |
---|
| 706 | this->yaw(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL); |
---|
| 707 | this->roll(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
| 708 | this->pitch(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
[647] | 709 | */ |
---|
[608] | 710 | } |
---|
[647] | 711 | /* |
---|
[608] | 712 | void SpaceShip::moveForward(float moveForward) { |
---|
| 713 | moveForward_ = moveForward; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | void SpaceShip::rotateUp(float rotateUp) { |
---|
| 717 | rotateUp_ = rotateUp; |
---|
| 718 | } |
---|
| 719 | |
---|
| 720 | void SpaceShip::rotateDown(float rotateDown) { |
---|
| 721 | rotateDown_ = rotateDown; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | void SpaceShip::rotateLeft(float rotateLeft) { |
---|
| 725 | rotateLeft_ = rotateLeft; |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | void SpaceShip::rotateRight(float rotateRight) { |
---|
| 729 | rotateRight_ = rotateRight; |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | void SpaceShip::loopLeft(float loopLeft) { |
---|
| 733 | loopLeft_ = loopLeft; |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | void SpaceShip::loopRight(float loopRight) { |
---|
| 737 | loopRight_ = loopRight; |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | void SpaceShip::brakeForward(float brakeForward) { |
---|
| 741 | brakeForward_ = brakeForward; |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | void SpaceShip::brakeRotate(float brakeRotate) { |
---|
| 745 | brakeRotate_ = brakeRotate; |
---|
| 746 | } |
---|
| 747 | |
---|
| 748 | void SpaceShip::brakeLoop(float brakeLoop) { |
---|
| 749 | brakeLoop_ = brakeLoop; |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | void SpaceShip::maxSpeedForward(float maxSpeedForward) { |
---|
| 753 | maxSpeedForward_ = maxSpeedForward; |
---|
| 754 | } |
---|
| 755 | |
---|
| 756 | void SpaceShip::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) { |
---|
| 757 | maxSpeedRotateUpDown_ = maxSpeedRotateUpDown; |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | void SpaceShip::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) { |
---|
| 761 | maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft; |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | void SpaceShip::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) { |
---|
| 765 | maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft; |
---|
| 766 | } |
---|
[647] | 767 | */ |
---|
[608] | 768 | } |
---|