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