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