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