[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: |
---|
[1293] | 25 | * Benjamin Knecht |
---|
[608] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[786] | 29 | #include "OrxonoxStableHeaders.h" |
---|
[1039] | 30 | #include "SpaceShip.h" |
---|
[784] | 31 | |
---|
[708] | 32 | #include <OgreCamera.h> |
---|
| 33 | #include <OgreRenderWindow.h> |
---|
| 34 | #include <OgreParticleSystem.h> |
---|
| 35 | #include <OgreSceneNode.h> |
---|
[608] | 36 | |
---|
[1293] | 37 | #include "CameraHandler.h" |
---|
[1362] | 38 | #include "util/Convert.h" |
---|
[742] | 39 | #include "util/Math.h" |
---|
[1021] | 40 | #include "core/CoreIncludes.h" |
---|
[1052] | 41 | #include "core/ConfigValueIncludes.h" |
---|
[1021] | 42 | #include "core/Debug.h" |
---|
[1032] | 43 | #include "GraphicsEngine.h" |
---|
[1024] | 44 | #include "core/InputManager.h" |
---|
[1021] | 45 | #include "particle/ParticleInterface.h" |
---|
[708] | 46 | #include "Projectile.h" |
---|
[871] | 47 | #include "core/XMLPort.h" |
---|
[1052] | 48 | #include "core/ConsoleCommand.h" |
---|
[1293] | 49 | #include "network/Client.h" |
---|
[608] | 50 | |
---|
| 51 | namespace orxonox |
---|
| 52 | { |
---|
[1362] | 53 | ConsoleCommand(SpaceShip, setMaxSpeedTest, AccessLevel::Debug, false); |
---|
| 54 | ConsoleCommand(SpaceShip, whereAmI, AccessLevel::User, true); |
---|
[1052] | 55 | ConsoleCommandGeneric(test1, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxSpeed", AccessLevel::Debug), false); |
---|
| 56 | ConsoleCommandGeneric(test2, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxBlubber", AccessLevel::Debug), false); |
---|
[1362] | 57 | ConsoleCommandGeneric(test3, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setRofl", AccessLevel::Debug), false); |
---|
[1052] | 58 | |
---|
[608] | 59 | CreateFactory(SpaceShip); |
---|
| 60 | |
---|
[1052] | 61 | SpaceShip* SpaceShip::instance_s; |
---|
| 62 | |
---|
[1360] | 63 | SpaceShip *SpaceShip::getLocalShip(){ |
---|
| 64 | Iterator<SpaceShip> it; |
---|
| 65 | for(it = ObjectList<SpaceShip>::start(); it; ++it){ |
---|
| 66 | if((it)->server_ || ( network::Client::getSingleton() && network::Client::getSingleton()->getShipID()==it->objectID ) ) |
---|
| 67 | return *it; |
---|
| 68 | } |
---|
| 69 | return NULL; |
---|
| 70 | } |
---|
[1407] | 71 | |
---|
[1293] | 72 | SpaceShip::SpaceShip() : |
---|
| 73 | //testvector_(0,0,0), |
---|
| 74 | //bInvertYAxis_(false), |
---|
| 75 | setMouseEventCallback_(false), |
---|
| 76 | bLMousePressed_(false), |
---|
| 77 | bRMousePressed_(false), |
---|
| 78 | camNode_(0), |
---|
| 79 | cam_(0), |
---|
| 80 | camName_("CamNode"), |
---|
| 81 | tt_(0), |
---|
| 82 | redNode_(0), |
---|
| 83 | greenNode_(0), |
---|
| 84 | blinkTime_(0.0f), |
---|
| 85 | chNearNode_(0), |
---|
| 86 | chFarNode_(0), |
---|
| 87 | timeToReload_(0.0f), |
---|
| 88 | //reloadTime_(0.0f), |
---|
| 89 | maxSideAndBackSpeed_(0.0f), |
---|
| 90 | maxSpeed_(0.0f), |
---|
| 91 | maxRotation_(0.0f), |
---|
| 92 | translationAcceleration_(0.0f), |
---|
| 93 | rotationAcceleration_(0.0f), |
---|
| 94 | translationDamping_(0.0f), |
---|
| 95 | rotationDamping_(0.0f), |
---|
| 96 | maxRotationRadian_(0), |
---|
| 97 | rotationAccelerationRadian_(0), |
---|
| 98 | rotationDampingRadian_(0), |
---|
| 99 | zeroRadian_(0), |
---|
| 100 | mouseXRotation_(0), |
---|
| 101 | mouseYRotation_(0), |
---|
| 102 | mouseX_(0.0f), |
---|
| 103 | mouseY_(0.0f), |
---|
| 104 | emitterRate_(0.0f), |
---|
| 105 | server_(false) |
---|
[608] | 106 | { |
---|
| 107 | RegisterObject(SpaceShip); |
---|
[1021] | 108 | this->registerAllVariables(); |
---|
[608] | 109 | |
---|
[1052] | 110 | SpaceShip::instance_s = this; |
---|
| 111 | |
---|
[1407] | 112 | this->setConfigValues(); |
---|
[608] | 113 | |
---|
[1407] | 114 | |
---|
[647] | 115 | this->setRotationAxis(1, 0, 0); |
---|
| 116 | this->setStatic(false); |
---|
[608] | 117 | |
---|
| 118 | COUT(3) << "Info: SpaceShip was loaded" << std::endl; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | SpaceShip::~SpaceShip() |
---|
| 122 | { |
---|
[647] | 123 | if (this->tt_) |
---|
| 124 | delete this->tt_; |
---|
[1293] | 125 | if(setMouseEventCallback_) |
---|
| 126 | InputManager::removeMouseHandler("SpaceShip"); |
---|
| 127 | if (this->cam_) |
---|
| 128 | delete this->cam_; |
---|
[608] | 129 | } |
---|
| 130 | |
---|
[1021] | 131 | bool SpaceShip::create(){ |
---|
| 132 | if(Model::create()) |
---|
| 133 | this->init(); |
---|
| 134 | else |
---|
| 135 | return false; |
---|
| 136 | return true; |
---|
| 137 | } |
---|
[1056] | 138 | |
---|
[1021] | 139 | void SpaceShip::registerAllVariables(){ |
---|
[1293] | 140 | registerVar( &camName_, camName_.length()+1, network::STRING, 0x1); |
---|
| 141 | registerVar( &maxSpeed_, sizeof(maxSpeed_), network::DATA, 0x1); |
---|
| 142 | registerVar( &maxSideAndBackSpeed_, sizeof(maxSideAndBackSpeed_), network::DATA, 0x1); |
---|
| 143 | registerVar( &maxRotation_, sizeof(maxRotation_), network::DATA, 0x1); |
---|
| 144 | registerVar( &translationAcceleration_, sizeof(translationAcceleration_), network::DATA, 0x1); |
---|
| 145 | registerVar( &rotationAcceleration_, sizeof(rotationAcceleration_), network::DATA, 0x1); |
---|
| 146 | registerVar( &rotationAccelerationRadian_, sizeof(rotationAccelerationRadian_), network::DATA, 0x1); |
---|
| 147 | registerVar( &translationDamping_, sizeof(translationDamping_), network::DATA, 0x1); |
---|
| 148 | registerVar( &rotationDamping_, sizeof(rotationDamping_), network::DATA, 0x1); |
---|
| 149 | registerVar( &rotationDampingRadian_, sizeof(rotationDampingRadian_), network::DATA, 0x1); |
---|
[1056] | 150 | |
---|
[1021] | 151 | } |
---|
[1056] | 152 | |
---|
[871] | 153 | void SpaceShip::init() |
---|
[697] | 154 | { |
---|
[1360] | 155 | if ((server_ || ( network::Client::getSingleton() && network::Client::getSingleton()->getShipID()==objectID ) )) |
---|
| 156 | { |
---|
| 157 | if (!setMouseEventCallback_) |
---|
| 158 | { |
---|
| 159 | InputManager::addMouseHandler(this, "SpaceShip"); |
---|
| 160 | //InputManager::enableMouseHandler("SpaceShip"); |
---|
| 161 | setMouseEventCallback_ = true; |
---|
| 162 | } |
---|
| 163 | } |
---|
[1293] | 164 | |
---|
[633] | 165 | // START CREATING THRUSTER |
---|
[1032] | 166 | this->tt_ = new ParticleInterface(GraphicsEngine::getSingleton().getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow"); |
---|
[647] | 167 | this->tt_->getParticleSystem()->setParameter("local_space","true"); |
---|
[978] | 168 | this->tt_->newEmitter(); |
---|
[608] | 169 | /* |
---|
[647] | 170 | this->tt_->setDirection(Vector3(0,0,1)); |
---|
| 171 | this->tt_->setPositionOfEmitter(0, Vector3(20,-1,-15)); |
---|
| 172 | this->tt_->setPositionOfEmitter(1, Vector3(-20,-1,-15)); |
---|
[608] | 173 | */ |
---|
[647] | 174 | this->tt_->setDirection(Vector3(-1,0,0)); |
---|
[978] | 175 | this->tt_->setPositionOfEmitter(0, Vector3(-15,20,-1)); |
---|
| 176 | this->tt_->setPositionOfEmitter(1, Vector3(-15,-20,-1)); |
---|
[647] | 177 | this->tt_->setVelocity(50); |
---|
[608] | 178 | |
---|
[633] | 179 | emitterRate_ = tt_->getRate(); |
---|
[626] | 180 | |
---|
[633] | 181 | Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2"); |
---|
| 182 | node2->setInheritScale(false); |
---|
| 183 | tt_->addToSceneNode(node2); |
---|
| 184 | // END CREATING THRUSTER |
---|
[608] | 185 | |
---|
[633] | 186 | // START CREATING BLINKING LIGHTS |
---|
| 187 | this->redBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); |
---|
| 188 | this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1); |
---|
[608] | 189 | |
---|
[1293] | 190 | this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.0, -0.3)); |
---|
[633] | 191 | this->redNode_->setInheritScale(false); |
---|
[1293] | 192 | this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.0, -0.3)); |
---|
[633] | 193 | this->greenNode_->setInheritScale(false); |
---|
[608] | 194 | |
---|
[633] | 195 | this->redNode_->attachObject(this->redBillboard_.getBillboardSet()); |
---|
| 196 | this->redNode_->setScale(0.3, 0.3, 0.3); |
---|
[608] | 197 | |
---|
[633] | 198 | this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet()); |
---|
| 199 | this->greenNode_->setScale(0.3, 0.3, 0.3); |
---|
| 200 | // END CREATING BLINKING LIGHTS |
---|
[608] | 201 | |
---|
[661] | 202 | // START of testing crosshair |
---|
| 203 | this->crosshairNear_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
| 204 | this->crosshairFar_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
[608] | 205 | |
---|
[661] | 206 | this->chNearNode_ = this->getNode()->createChildSceneNode(this->getName() + "near", Vector3(50.0, 0.0, 0.0)); |
---|
| 207 | this->chNearNode_->setInheritScale(false); |
---|
| 208 | this->chFarNode_ = this->getNode()->createChildSceneNode(this->getName() + "far", Vector3(200.0, 0.0, 0.0)); |
---|
| 209 | this->chFarNode_->setInheritScale(false); |
---|
| 210 | |
---|
| 211 | this->chNearNode_->attachObject(this->crosshairNear_.getBillboardSet()); |
---|
| 212 | this->chNearNode_->setScale(0.2, 0.2, 0.2); |
---|
| 213 | |
---|
| 214 | this->chFarNode_->attachObject(this->crosshairFar_.getBillboardSet()); |
---|
| 215 | this->chFarNode_->setScale(0.4, 0.4, 0.4); |
---|
| 216 | |
---|
[1293] | 217 | createCamera(); |
---|
[661] | 218 | // END of testing crosshair |
---|
[871] | 219 | } |
---|
[661] | 220 | |
---|
[871] | 221 | void SpaceShip::setConfigValues() |
---|
| 222 | { |
---|
| 223 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
| 224 | SetConfigValue(reloadTime_, 0.125).description("The reload time of the weapon in seconds"); |
---|
| 225 | SetConfigValue(testvector_, Vector3()).description("asdfblah"); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void SpaceShip::setCamera(const std::string& camera) |
---|
| 229 | { |
---|
[1293] | 230 | camName_=camera; |
---|
| 231 | // change camera attributes here, if you want to ;) |
---|
| 232 | } |
---|
[1362] | 233 | |
---|
[1293] | 234 | void SpaceShip::getFocus(){ |
---|
| 235 | COUT(4) << "requesting focus" << std::endl; |
---|
| 236 | if(network::Client::getSingleton()==0 || network::Client::getSingleton()->getShipID()==objectID) |
---|
| 237 | CameraHandler::getInstance()->requestFocus(cam_); |
---|
[1362] | 238 | |
---|
[1293] | 239 | } |
---|
[1362] | 240 | |
---|
[1293] | 241 | void SpaceShip::createCamera(){ |
---|
| 242 | // COUT(4) << "begin camera creation" << std::endl; |
---|
| 243 | this->camNode_ = this->getNode()->createChildSceneNode(camName_); |
---|
| 244 | COUT(4) << "position: (this)" << this->getNode()->getPosition() << std::endl; |
---|
[1401] | 245 | this->camNode_->setPosition(Vector3(-50,0,10)); |
---|
[1362] | 246 | // Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,-1,0)); |
---|
| 247 | // Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(0,0,-1)); |
---|
| 248 | // camNode_->setOrientation(q1*q2); |
---|
[1293] | 249 | COUT(4) << "position: (cam)" << this->camNode_->getPosition() << std::endl; |
---|
| 250 | cam_ = new Camera(this->camNode_); |
---|
[608] | 251 | |
---|
[1293] | 252 | cam_->setTargetNode(this->getNode()); |
---|
[1362] | 253 | // cam->setPosition(Vector3(0,-350,0)); |
---|
| 254 | Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,-1,0)); |
---|
| 255 | Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(1,0,0)); |
---|
| 256 | camNode_->setOrientation(q2*q1); |
---|
[1293] | 257 | if(network::Client::getSingleton()!=0 && network::Client::getSingleton()->getShipID()==objectID){ |
---|
| 258 | this->setBacksync(true); |
---|
| 259 | CameraHandler::getInstance()->requestFocus(cam_); |
---|
| 260 | } |
---|
[608] | 261 | |
---|
| 262 | } |
---|
| 263 | |
---|
[871] | 264 | void SpaceShip::setMaxSpeed(float value) |
---|
| 265 | { this->maxSpeed_ = value; } |
---|
| 266 | void SpaceShip::setMaxSideAndBackSpeed(float value) |
---|
| 267 | { this->maxSideAndBackSpeed_ = value; } |
---|
| 268 | void SpaceShip::setMaxRotation(float value) |
---|
| 269 | { this->maxRotation_ = value; this->maxRotationRadian_ = Radian(value); } |
---|
| 270 | void SpaceShip::setTransAcc(float value) |
---|
| 271 | { this->translationAcceleration_ = value; } |
---|
| 272 | void SpaceShip::setRotAcc(float value) |
---|
| 273 | { this->rotationAcceleration_ = value; this->rotationAccelerationRadian_ = Radian(value); } |
---|
| 274 | void SpaceShip::setTransDamp(float value) |
---|
| 275 | { this->translationDamping_ = value; } |
---|
| 276 | void SpaceShip::setRotDamp(float value) |
---|
| 277 | { this->rotationDamping_ = value; this->rotationDampingRadian_ = Radian(value); } |
---|
| 278 | |
---|
| 279 | /** |
---|
| 280 | @brief XML loading and saving. |
---|
| 281 | @param xmlelement The XML-element |
---|
| 282 | @param loading Loading (true) or saving (false) |
---|
| 283 | @return The XML-element |
---|
| 284 | */ |
---|
[1052] | 285 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[871] | 286 | { |
---|
[1052] | 287 | Model::XMLPort(xmlelement, mode); |
---|
[871] | 288 | |
---|
[1052] | 289 | XMLPortParamLoadOnly(SpaceShip, "camera", setCamera, xmlelement, mode); |
---|
| 290 | XMLPortParamLoadOnly(SpaceShip, "maxSpeed", setMaxSpeed, xmlelement, mode); |
---|
| 291 | XMLPortParamLoadOnly(SpaceShip, "maxSideAndBackSpeed", setMaxSideAndBackSpeed, xmlelement, mode); |
---|
| 292 | XMLPortParamLoadOnly(SpaceShip, "maxRotation", setMaxRotation, xmlelement, mode); |
---|
| 293 | XMLPortParamLoadOnly(SpaceShip, "transAcc", setTransAcc, xmlelement, mode); |
---|
| 294 | XMLPortParamLoadOnly(SpaceShip, "rotAcc", setRotAcc, xmlelement, mode); |
---|
| 295 | XMLPortParamLoadOnly(SpaceShip, "transDamp", setTransDamp, xmlelement, mode); |
---|
| 296 | XMLPortParamLoadOnly(SpaceShip, "rotDamp", setRotDamp, xmlelement, mode); |
---|
[1293] | 297 | server_=true; // TODO: this is only a hack |
---|
| 298 | SpaceShip::create(); |
---|
| 299 | getFocus(); |
---|
[871] | 300 | } |
---|
| 301 | |
---|
[647] | 302 | int sgn(float x) |
---|
| 303 | { |
---|
| 304 | if (x >= 0) |
---|
| 305 | return 1; |
---|
| 306 | else |
---|
| 307 | return -1; |
---|
| 308 | } |
---|
| 309 | |
---|
[1349] | 310 | void SpaceShip::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
[608] | 311 | { |
---|
[647] | 312 | /* |
---|
[608] | 313 | this->mouseX += e.state.X.rel; |
---|
| 314 | if (this->bInvertMouse_) |
---|
| 315 | this->mouseY += e.state.Y.rel; |
---|
| 316 | else |
---|
| 317 | this->mouseY -= e.state.Y.rel; |
---|
| 318 | |
---|
| 319 | // if(mouseX>maxMouseX) maxMouseX = mouseX; |
---|
| 320 | // if(mouseX<minMouseX) minMouseX = mouseX; |
---|
| 321 | // cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl; |
---|
| 322 | |
---|
| 323 | this->moved = true; |
---|
[647] | 324 | */ |
---|
[644] | 325 | if (this->bRMousePressed_) |
---|
| 326 | { |
---|
[1349] | 327 | this->camNode_->roll(Degree(-rel.x * 0.10)); |
---|
| 328 | this->camNode_->yaw(Degree(rel.y * 0.10)); |
---|
[644] | 329 | } |
---|
[647] | 330 | else |
---|
| 331 | { |
---|
[1349] | 332 | float minDimension = clippingSize.y; |
---|
| 333 | if (clippingSize.x < minDimension) |
---|
| 334 | minDimension = clippingSize.x; |
---|
[644] | 335 | |
---|
[1349] | 336 | this->mouseX_ += rel.x; |
---|
[647] | 337 | if (this->mouseX_ < -minDimension) |
---|
| 338 | this->mouseX_ = -minDimension; |
---|
| 339 | if (this->mouseX_ > minDimension) |
---|
| 340 | this->mouseX_ = minDimension; |
---|
| 341 | |
---|
[1349] | 342 | this->mouseY_ += rel.y; |
---|
[647] | 343 | if (this->mouseY_ < -minDimension) |
---|
| 344 | this->mouseY_ = -minDimension; |
---|
| 345 | if (this->mouseY_ > minDimension) |
---|
| 346 | this->mouseY_ = minDimension; |
---|
| 347 | |
---|
| 348 | float xRotation = this->mouseX_ / minDimension; |
---|
| 349 | xRotation = xRotation*xRotation * sgn(xRotation); |
---|
| 350 | xRotation *= -this->rotationAcceleration_; |
---|
| 351 | if (xRotation > this->maxRotation_) |
---|
| 352 | xRotation = this->maxRotation_; |
---|
| 353 | if (xRotation < -this->maxRotation_) |
---|
| 354 | xRotation = -this->maxRotation_; |
---|
| 355 | this->mouseXRotation_ = Radian(xRotation); |
---|
| 356 | |
---|
| 357 | float yRotation = this->mouseY_ / minDimension; |
---|
| 358 | yRotation = yRotation*yRotation * sgn(yRotation); |
---|
| 359 | yRotation *= this->rotationAcceleration_; |
---|
| 360 | if (yRotation > this->maxRotation_) |
---|
| 361 | yRotation = this->maxRotation_; |
---|
| 362 | if (yRotation < -this->maxRotation_) |
---|
| 363 | yRotation = -this->maxRotation_; |
---|
| 364 | this->mouseYRotation_ = Radian(yRotation); |
---|
| 365 | } |
---|
[608] | 366 | } |
---|
| 367 | |
---|
[1349] | 368 | void SpaceShip::mouseButtonPressed(MouseButton::Enum id) |
---|
[643] | 369 | { |
---|
[1293] | 370 | if (id == MouseButton::Left) |
---|
[644] | 371 | this->bLMousePressed_ = true; |
---|
[1293] | 372 | else if (id == MouseButton::Right) |
---|
[644] | 373 | this->bRMousePressed_ = true; |
---|
[643] | 374 | } |
---|
| 375 | |
---|
[1349] | 376 | void SpaceShip::mouseButtonReleased(MouseButton::Enum id) |
---|
[643] | 377 | { |
---|
[1293] | 378 | if (id == MouseButton::Left) |
---|
[644] | 379 | this->bLMousePressed_ = false; |
---|
[1293] | 380 | else if (id == MouseButton::Right) |
---|
[644] | 381 | { |
---|
| 382 | this->bRMousePressed_ = false; |
---|
| 383 | this->camNode_->resetOrientation(); |
---|
| 384 | } |
---|
[643] | 385 | } |
---|
[1362] | 386 | |
---|
| 387 | std::string SpaceShip::whereAmI() { |
---|
| 388 | return getConvertedValue<float, std::string>(SpaceShip::getLocalShip()->getPosition().x) |
---|
| 389 | + " " + getConvertedValue<float, std::string>(SpaceShip::getLocalShip()->getPosition().y) |
---|
| 390 | + " " + getConvertedValue<float, std::string>(SpaceShip::getLocalShip()->getPosition().z); |
---|
| 391 | } |
---|
| 392 | |
---|
[1407] | 393 | Vector3 SpaceShip::getSPosition() { |
---|
| 394 | return SpaceShip::getLocalShip()->getPosition(); |
---|
[1362] | 395 | } |
---|
| 396 | |
---|
[1407] | 397 | Quaternion SpaceShip::getSOrientation() { |
---|
| 398 | return SpaceShip::getLocalShip()->getOrientation(); |
---|
[1362] | 399 | } |
---|
| 400 | |
---|
| 401 | float SpaceShip::getMaxSpeed() { return maxSpeed_; } |
---|
[643] | 402 | |
---|
[608] | 403 | void SpaceShip::tick(float dt) |
---|
[1407] | 404 | { |
---|
[1293] | 405 | if (this->cam_) |
---|
| 406 | this->cam_->tick(dt); |
---|
[608] | 407 | |
---|
[633] | 408 | if (this->redNode_ && this->greenNode_) |
---|
| 409 | { |
---|
| 410 | this->blinkTime_ += dt; |
---|
| 411 | float redScale = 0.15 + 0.15 * sin(this->blinkTime_ * 10.0); |
---|
| 412 | float greenScale = 0.15 - 0.15 * sin(this->blinkTime_ * 10.0); |
---|
| 413 | this->redNode_->setScale(redScale, redScale, redScale); |
---|
| 414 | this->greenNode_->setScale(greenScale, greenScale, greenScale); |
---|
| 415 | } |
---|
| 416 | |
---|
[643] | 417 | if (this->timeToReload_ > 0) |
---|
| 418 | this->timeToReload_ -= dt; |
---|
| 419 | else |
---|
| 420 | this->timeToReload_ = 0; |
---|
| 421 | |
---|
[644] | 422 | if (this->bLMousePressed_ && this->timeToReload_ <= 0) |
---|
[643] | 423 | { |
---|
[1407] | 424 | |
---|
[1293] | 425 | Projectile *p = new Projectile(this); |
---|
[1407] | 426 | |
---|
[1293] | 427 | p->setBacksync(true); |
---|
[643] | 428 | this->timeToReload_ = this->reloadTime_; |
---|
| 429 | } |
---|
| 430 | |
---|
[608] | 431 | |
---|
[647] | 432 | // ##################################### |
---|
| 433 | // ############# STEERING ############## |
---|
| 434 | // ##################################### |
---|
| 435 | |
---|
| 436 | if (this->velocity_.x > this->maxSpeed_) |
---|
| 437 | this->velocity_.x = this->maxSpeed_; |
---|
| 438 | if (this->velocity_.x < -this->maxSideAndBackSpeed_) |
---|
| 439 | this->velocity_.x = -this->maxSideAndBackSpeed_; |
---|
| 440 | if (this->velocity_.y > this->maxSideAndBackSpeed_) |
---|
| 441 | this->velocity_.y = this->maxSideAndBackSpeed_; |
---|
| 442 | if (this->velocity_.y < -this->maxSideAndBackSpeed_) |
---|
| 443 | this->velocity_.y = -this->maxSideAndBackSpeed_; |
---|
| 444 | if (this->rotationRate_ > this->maxRotationRadian_) |
---|
| 445 | this->rotationRate_ = this->maxRotationRadian_; |
---|
| 446 | if (this->rotationRate_ < -this->maxRotationRadian_) |
---|
| 447 | this->rotationRate_ = -this->maxRotationRadian_; |
---|
| 448 | |
---|
| 449 | if (this->acceleration_.x == 0) |
---|
| 450 | { |
---|
| 451 | if (this->velocity_.x > 0) |
---|
| 452 | { |
---|
| 453 | this->velocity_.x -= (this->translationDamping_ * dt); |
---|
| 454 | if (this->velocity_.x < 0) |
---|
| 455 | this->velocity_.x = 0; |
---|
| 456 | } |
---|
| 457 | else if (this->velocity_.x < 0) |
---|
| 458 | { |
---|
| 459 | this->velocity_.x += (this->translationDamping_ * dt); |
---|
| 460 | if (this->velocity_.x > 0) |
---|
| 461 | this->velocity_.x = 0; |
---|
| 462 | } |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | if (this->acceleration_.y == 0) |
---|
| 466 | { |
---|
| 467 | if (this->velocity_.y > 0) |
---|
| 468 | { |
---|
| 469 | this->velocity_.y -= (this->translationDamping_ * dt); |
---|
| 470 | if (this->velocity_.y < 0) |
---|
| 471 | this->velocity_.y = 0; |
---|
| 472 | } |
---|
| 473 | else if (this->velocity_.y < 0) |
---|
| 474 | { |
---|
| 475 | this->velocity_.y += (this->translationDamping_ * dt); |
---|
| 476 | if (this->velocity_.y > 0) |
---|
| 477 | this->velocity_.y = 0; |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | if (this->momentum_ == this->zeroRadian_) |
---|
| 482 | { |
---|
| 483 | if (this->rotationRate_ > this->zeroRadian_) |
---|
| 484 | { |
---|
| 485 | this->rotationRate_ -= (this->rotationDampingRadian_ * dt); |
---|
| 486 | if (this->rotationRate_ < this->zeroRadian_) |
---|
| 487 | this->rotationRate_ = 0; |
---|
| 488 | } |
---|
| 489 | else if (this->rotationRate_ < this->zeroRadian_) |
---|
| 490 | { |
---|
| 491 | this->rotationRate_ += (this->rotationDampingRadian_ * dt); |
---|
| 492 | if (this->rotationRate_ > this->zeroRadian_) |
---|
| 493 | this->rotationRate_ = 0; |
---|
| 494 | } |
---|
| 495 | } |
---|
| 496 | |
---|
[1293] | 497 | if( (network::Client::getSingleton() && network::Client::getSingleton()->getShipID() == objectID) || server_ ) |
---|
| 498 | { |
---|
| 499 | COUT(4) << "steering our ship: " << objectID << std::endl; |
---|
| 500 | if (InputManager::isKeyDown(KeyCode::Up) || InputManager::isKeyDown(KeyCode::W)) |
---|
[647] | 501 | this->acceleration_.x = this->translationAcceleration_; |
---|
[1293] | 502 | else if(InputManager::isKeyDown(KeyCode::Down) || InputManager::isKeyDown(KeyCode::S)) |
---|
[647] | 503 | this->acceleration_.x = -this->translationAcceleration_; |
---|
[1293] | 504 | else |
---|
[647] | 505 | this->acceleration_.x = 0; |
---|
| 506 | |
---|
[1293] | 507 | if (InputManager::isKeyDown(KeyCode::Right) || InputManager::isKeyDown(KeyCode::D)) |
---|
[647] | 508 | this->acceleration_.y = -this->translationAcceleration_; |
---|
[1293] | 509 | else if (InputManager::isKeyDown(KeyCode::Left) || InputManager::isKeyDown(KeyCode::A)) |
---|
[647] | 510 | this->acceleration_.y = this->translationAcceleration_; |
---|
[1293] | 511 | else |
---|
[647] | 512 | this->acceleration_.y = 0; |
---|
| 513 | |
---|
[1293] | 514 | if (InputManager::isKeyDown(KeyCode::Delete) || InputManager::isKeyDown(KeyCode::Q)) |
---|
[647] | 515 | this->momentum_ = Radian(-this->rotationAccelerationRadian_); |
---|
[1293] | 516 | else if (InputManager::isKeyDown(KeyCode::PageDown) || InputManager::isKeyDown(KeyCode::E)) |
---|
[647] | 517 | this->momentum_ = Radian(this->rotationAccelerationRadian_); |
---|
[1293] | 518 | else |
---|
[647] | 519 | this->momentum_ = 0; |
---|
[1293] | 520 | }/*else |
---|
| 521 | COUT(4) << "not steering ship: " << objectID << " our ship: " << network::Client::getSingleton()->getShipID() << std::endl;*/ |
---|
[647] | 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); |
---|
[608] | 535 | } |
---|
| 536 | |
---|
| 537 | } |
---|