[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 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: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * Benjamin Knecht |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | #include "SpaceShip.h" |
---|
| 31 | |
---|
| 32 | #include <OgreCamera.h> |
---|
| 33 | #include <OgreRenderWindow.h> |
---|
| 34 | #include <OgreParticleSystem.h> |
---|
| 35 | #include <OgreSceneNode.h> |
---|
| 36 | |
---|
| 37 | #include "CameraHandler.h" |
---|
| 38 | #include "util/Convert.h" |
---|
| 39 | #include "util/Math.h" |
---|
| 40 | #include "core/CoreIncludes.h" |
---|
| 41 | #include "core/ConfigValueIncludes.h" |
---|
| 42 | #include "core/Debug.h" |
---|
| 43 | #include "GraphicsEngine.h" |
---|
[1535] | 44 | #include "core/input/InputManager.h" |
---|
[1553] | 45 | #include "tools/ParticleInterface.h" |
---|
[1505] | 46 | #include "RotatingProjectile.h" |
---|
[1552] | 47 | #include "ParticleProjectile.h" |
---|
[1505] | 48 | #include "core/XMLPort.h" |
---|
| 49 | #include "core/ConsoleCommand.h" |
---|
[1669] | 50 | #include "network/Host.h" |
---|
[1505] | 51 | #include "hud/HUD.h" |
---|
| 52 | |
---|
| 53 | namespace orxonox |
---|
| 54 | { |
---|
| 55 | SetConsoleCommand(SpaceShip, setMaxSpeedTest, false).setAccessLevel(AccessLevel::Debug); |
---|
| 56 | SetConsoleCommand(SpaceShip, whereAmI, true).setAccessLevel(AccessLevel::User); |
---|
| 57 | SetConsoleCommand(SpaceShip, moveLongitudinal, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold); |
---|
| 58 | SetConsoleCommand(SpaceShip, moveLateral, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold); |
---|
| 59 | SetConsoleCommand(SpaceShip, moveYaw, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold); |
---|
| 60 | SetConsoleCommand(SpaceShip, movePitch, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold); |
---|
| 61 | SetConsoleCommand(SpaceShip, moveRoll, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold); |
---|
| 62 | SetConsoleCommand(SpaceShip, fire, true).setAccessLevel(AccessLevel::User).setKeybindMode(KeybindMode::OnHold); |
---|
| 63 | SetConsoleCommandGeneric(test1, SpaceShip, createConsoleCommand(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxSpeed"), false).setAccessLevel(AccessLevel::Debug); |
---|
| 64 | SetConsoleCommandGeneric(test2, SpaceShip, createConsoleCommand(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxBlubber"), false).setAccessLevel(AccessLevel::Debug); |
---|
| 65 | SetConsoleCommandGeneric(test3, SpaceShip, createConsoleCommand(createFunctor(&SpaceShip::setMaxSpeedTest), "setRofl"), false).setAccessLevel(AccessLevel::Debug); |
---|
| 66 | |
---|
| 67 | CreateFactory(SpaceShip); |
---|
| 68 | |
---|
| 69 | SpaceShip* SpaceShip::instance_s; |
---|
| 70 | |
---|
[1552] | 71 | |
---|
[1505] | 72 | SpaceShip *SpaceShip::getLocalShip(){ |
---|
| 73 | Iterator<SpaceShip> it; |
---|
| 74 | for(it = ObjectList<SpaceShip>::start(); it; ++it){ |
---|
| 75 | if( (it)->myShip_ ) |
---|
| 76 | return *it; |
---|
| 77 | } |
---|
| 78 | return NULL; |
---|
| 79 | } |
---|
| 80 | |
---|
[1559] | 81 | SpaceShip::SpaceShip() |
---|
[1505] | 82 | { |
---|
| 83 | RegisterObject(SpaceShip); |
---|
[1559] | 84 | |
---|
| 85 | this->setRotationAxis(1, 0, 0); |
---|
| 86 | this->setStatic(false); |
---|
| 87 | |
---|
| 88 | this->zeroRadian_ = 0; |
---|
| 89 | this->maxSideAndBackSpeed_ = 0; |
---|
| 90 | this->maxSpeed_ = 0; |
---|
| 91 | this->maxRotation_ = 0; |
---|
| 92 | this->translationAcceleration_ = 0; |
---|
| 93 | this->rotationAcceleration_ = 0; |
---|
| 94 | this->translationDamping_ = 0; |
---|
| 95 | this->rotationDamping_ = 0; |
---|
| 96 | this->maxRotationRadian_ = 0; |
---|
| 97 | this->rotationAccelerationRadian_ = 0; |
---|
| 98 | this->rotationDampingRadian_ = 0; |
---|
| 99 | |
---|
| 100 | this->cam_ = 0; |
---|
| 101 | this->tt1_ = 0; |
---|
| 102 | this->tt2_ = 0; |
---|
| 103 | |
---|
| 104 | this->redNode_ = 0; |
---|
| 105 | this->greenNode_ = 0; |
---|
| 106 | this->blinkTime_ = 0; |
---|
| 107 | |
---|
| 108 | this->timeToReload_ = 0; |
---|
| 109 | |
---|
| 110 | this->setMouseEventCallback_ = false; |
---|
| 111 | this->bLMousePressed_ = false; |
---|
| 112 | this->bRMousePressed_ = false; |
---|
| 113 | this->mouseXRotation_ = 0; |
---|
| 114 | this->mouseYRotation_ = 0; |
---|
| 115 | this->mouseX_ = 0; |
---|
| 116 | this->mouseY_ = 0; |
---|
| 117 | this->myShip_ = false; |
---|
| 118 | |
---|
[1505] | 119 | this->registerAllVariables(); |
---|
| 120 | |
---|
| 121 | SpaceShip::instance_s = this; |
---|
| 122 | |
---|
| 123 | this->setConfigValues(); |
---|
| 124 | |
---|
[1559] | 125 | this->initialDir_ = Vector3(1.0, 0.0, 0.0); |
---|
| 126 | this->currentDir_ = initialDir_; |
---|
| 127 | this->initialOrth_ = Vector3(0.0, 0.0, 1.0); |
---|
| 128 | this->currentOrth_ = initialOrth_; |
---|
[1505] | 129 | |
---|
| 130 | this->camName_ = this->getName() + "CamNode"; |
---|
| 131 | |
---|
[1559] | 132 | this->teamNr_ = 0; |
---|
| 133 | this->health_ = 100; |
---|
[1505] | 134 | |
---|
| 135 | COUT(3) << "Info: SpaceShip was loaded" << std::endl; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | SpaceShip::~SpaceShip() |
---|
| 139 | { |
---|
[1559] | 140 | if (this->isInitialized()) |
---|
| 141 | { |
---|
| 142 | if (this->tt1_) |
---|
| 143 | delete this->tt1_; |
---|
| 144 | if (this->tt2_) |
---|
| 145 | delete this->tt2_; |
---|
| 146 | |
---|
| 147 | if (setMouseEventCallback_) |
---|
| 148 | InputManager::removeMouseHandler("SpaceShip"); |
---|
| 149 | |
---|
| 150 | if (this->cam_) |
---|
| 151 | delete this->cam_; |
---|
| 152 | |
---|
[1562] | 153 | if (!this->myShip_) |
---|
[1564] | 154 | HUD::getSingleton().removeRadarObject(this); |
---|
[1559] | 155 | } |
---|
[1505] | 156 | } |
---|
| 157 | |
---|
| 158 | bool SpaceShip::create(){ |
---|
| 159 | if(!myShip_){ |
---|
[1669] | 160 | if(network::Host::running() && objectID == network::Host::getShipID()) |
---|
[1505] | 161 | myShip_=true; |
---|
| 162 | else |
---|
[1564] | 163 | HUD::getSingleton().addRadarObject(this, this->getProjectileColour()); |
---|
[1505] | 164 | } |
---|
| 165 | if(Model::create()) |
---|
| 166 | this->init(); |
---|
| 167 | else |
---|
| 168 | return false; |
---|
| 169 | return true; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | void SpaceShip::registerAllVariables(){ |
---|
[1534] | 173 | registerVar( &camName_, camName_.length()+1, network::STRING, 0x1 ); |
---|
[1505] | 174 | registerVar( &maxSpeed_, sizeof(maxSpeed_), network::DATA, 0x1); |
---|
| 175 | registerVar( &maxSideAndBackSpeed_, sizeof(maxSideAndBackSpeed_), network::DATA, 0x1); |
---|
| 176 | registerVar( &maxRotation_, sizeof(maxRotation_), network::DATA, 0x1); |
---|
| 177 | registerVar( &translationAcceleration_, sizeof(translationAcceleration_), network::DATA, 0x1); |
---|
| 178 | registerVar( &rotationAcceleration_, sizeof(rotationAcceleration_), network::DATA, 0x1); |
---|
| 179 | registerVar( &rotationAccelerationRadian_, sizeof(rotationAccelerationRadian_), network::DATA, 0x1); |
---|
| 180 | registerVar( &translationDamping_, sizeof(translationDamping_), network::DATA, 0x1); |
---|
| 181 | registerVar( &rotationDamping_, sizeof(rotationDamping_), network::DATA, 0x1); |
---|
| 182 | registerVar( &rotationDampingRadian_, sizeof(rotationDampingRadian_), network::DATA, 0x1); |
---|
| 183 | |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void SpaceShip::init() |
---|
| 187 | { |
---|
| 188 | // START CREATING THRUSTER |
---|
[1563] | 189 | this->tt1_ = new ParticleInterface("Orxonox/thruster1", LODParticle::low); |
---|
[1552] | 190 | this->tt1_->createNewEmitter(); |
---|
| 191 | this->tt1_->getAllEmitters()->setDirection(-this->getInitialDir()); |
---|
| 192 | this->tt1_->getEmitter(0)->setPosition(Vector3(-15, 20, -1)); |
---|
| 193 | this->tt1_->getEmitter(1)->setPosition(Vector3(-15, -20, -1)); |
---|
| 194 | this->tt1_->setSpeedFactor(3.0); |
---|
[1505] | 195 | |
---|
[1552] | 196 | Ogre::SceneNode* node2a = this->getNode()->createChildSceneNode(this->getName() + "particle2a"); |
---|
| 197 | node2a->setInheritScale(false); |
---|
| 198 | node2a->setScale(1, 1, 1); |
---|
| 199 | tt1_->addToSceneNode(node2a); |
---|
[1505] | 200 | |
---|
[1563] | 201 | this->tt2_ = new ParticleInterface("Orxonox/thruster2", LODParticle::normal); |
---|
[1552] | 202 | this->tt2_->createNewEmitter(); |
---|
| 203 | this->tt2_->getAllEmitters()->setDirection(Vector3(-1, 0, 0)); |
---|
| 204 | this->tt2_->getEmitter(0)->setPosition(Vector3(-30, 40, -2)); |
---|
| 205 | this->tt2_->getEmitter(1)->setPosition(Vector3(-30, -40, -2)); |
---|
| 206 | |
---|
| 207 | Ogre::SceneNode* node2b = this->getNode()->createChildSceneNode(this->getName() + "particle2b"); |
---|
| 208 | node2b->setInheritScale(false); |
---|
| 209 | node2b->setScale(0.5, 0.5, 0.5); |
---|
| 210 | tt2_->addToSceneNode(node2b); |
---|
[1505] | 211 | // END CREATING THRUSTER |
---|
| 212 | |
---|
| 213 | // START CREATING BLINKING LIGHTS |
---|
| 214 | this->redBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); |
---|
| 215 | this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1); |
---|
| 216 | |
---|
| 217 | this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.0, -0.3)); |
---|
| 218 | this->redNode_->setInheritScale(false); |
---|
| 219 | this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.0, -0.3)); |
---|
| 220 | this->greenNode_->setInheritScale(false); |
---|
| 221 | |
---|
| 222 | this->redNode_->attachObject(this->redBillboard_.getBillboardSet()); |
---|
| 223 | this->redNode_->setScale(0.3, 0.3, 0.3); |
---|
| 224 | |
---|
| 225 | this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet()); |
---|
| 226 | this->greenNode_->setScale(0.3, 0.3, 0.3); |
---|
| 227 | // END CREATING BLINKING LIGHTS |
---|
| 228 | |
---|
| 229 | if (this->isExactlyA(Class(SpaceShip))) |
---|
| 230 | { |
---|
| 231 | // START of testing crosshair |
---|
| 232 | this->crosshairNear_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
| 233 | this->crosshairFar_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
| 234 | |
---|
| 235 | this->chNearNode_ = this->getNode()->createChildSceneNode(this->getName() + "near", Vector3(50.0, 0.0, 0.0)); |
---|
| 236 | this->chNearNode_->setInheritScale(false); |
---|
| 237 | this->chFarNode_ = this->getNode()->createChildSceneNode(this->getName() + "far", Vector3(200.0, 0.0, 0.0)); |
---|
| 238 | this->chFarNode_->setInheritScale(false); |
---|
| 239 | |
---|
| 240 | this->chNearNode_->attachObject(this->crosshairNear_.getBillboardSet()); |
---|
| 241 | this->chNearNode_->setScale(0.2, 0.2, 0.2); |
---|
| 242 | |
---|
| 243 | this->chFarNode_->attachObject(this->crosshairFar_.getBillboardSet()); |
---|
| 244 | this->chFarNode_->setScale(0.4, 0.4, 0.4); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | createCamera(); |
---|
| 248 | // END of testing crosshair |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | void SpaceShip::setConfigValues() |
---|
| 252 | { |
---|
| 253 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
| 254 | SetConfigValue(reloadTime_, 0.125).description("The reload time of the weapon in seconds"); |
---|
| 255 | SetConfigValue(testvector_, Vector3()).description("asdfblah"); |
---|
| 256 | } |
---|
| 257 | |
---|
[1558] | 258 | void SpaceShip::changedVisibility() |
---|
| 259 | { |
---|
| 260 | Model::changedVisibility(); |
---|
| 261 | |
---|
| 262 | this->tt1_->setEnabled(this->isVisible()); |
---|
| 263 | this->tt2_->setEnabled(this->isVisible()); |
---|
| 264 | this->redBillboard_.setVisible(this->isVisible()); |
---|
| 265 | this->greenBillboard_.setVisible(this->isVisible()); |
---|
| 266 | this->crosshairNear_.setVisible(this->isVisible()); |
---|
| 267 | this->crosshairFar_.setVisible(this->isVisible()); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | void SpaceShip::changedActivity() |
---|
| 271 | { |
---|
| 272 | Model::changedActivity(); |
---|
| 273 | |
---|
| 274 | this->tt1_->setEnabled(this->isVisible()); |
---|
| 275 | this->tt2_->setEnabled(this->isVisible()); |
---|
| 276 | this->redBillboard_.setVisible(this->isVisible()); |
---|
| 277 | this->greenBillboard_.setVisible(this->isVisible()); |
---|
| 278 | this->crosshairNear_.setVisible(this->isVisible()); |
---|
| 279 | this->crosshairFar_.setVisible(this->isVisible()); |
---|
| 280 | } |
---|
| 281 | |
---|
[1505] | 282 | void SpaceShip::setCamera(const std::string& camera) |
---|
| 283 | { |
---|
| 284 | camName_=camera; |
---|
| 285 | // change camera attributes here, if you want to ;) |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | void SpaceShip::getFocus(){ |
---|
| 289 | COUT(4) << "requesting focus" << std::endl; |
---|
[1669] | 290 | //if(!network::Host::running() || network::Host::getShipID()==objectID) |
---|
| 291 | if(myShip_) |
---|
[1505] | 292 | CameraHandler::getInstance()->requestFocus(cam_); |
---|
| 293 | |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | Camera* SpaceShip::getCamera(){ |
---|
| 297 | return cam_; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | void SpaceShip::createCamera(){ |
---|
| 301 | // COUT(4) << "begin camera creation" << std::endl; |
---|
| 302 | this->camNode_ = this->getNode()->createChildSceneNode(camName_); |
---|
| 303 | COUT(4) << "position: (this)" << this->getNode()->getPosition() << std::endl; |
---|
[1552] | 304 | this->camNode_->setPosition(Vector3(-25,0,5)); |
---|
[1505] | 305 | // Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,-1,0)); |
---|
| 306 | // Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(0,0,-1)); |
---|
| 307 | // camNode_->setOrientation(q1*q2); |
---|
| 308 | COUT(4) << "position: (cam)" << this->camNode_->getPosition() << std::endl; |
---|
| 309 | cam_ = new Camera(this->camNode_); |
---|
| 310 | |
---|
| 311 | cam_->setTargetNode(this->getNode()); |
---|
| 312 | // cam->setPosition(Vector3(0,-350,0)); |
---|
[1669] | 313 | // Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,0,1)); |
---|
| 314 | // Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(0,1,0)); |
---|
[1505] | 315 | Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,-1,0)); |
---|
| 316 | Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(1,0,0)); |
---|
[1669] | 317 | |
---|
| 318 | this->camNode_->setOrientation(q2*q1); |
---|
| 319 | //if(!network::Host::running() || network::Host::getShipID()==objectID){ //TODO: check this |
---|
| 320 | if(myShip_){ |
---|
| 321 | COUT(3) << "requesting focus for camera" << std::endl; |
---|
[1505] | 322 | this->setBacksync(true); |
---|
[1669] | 323 | //CameraHandler::getInstance()->requestFocus(cam_); |
---|
| 324 | if(this->isExactlyA(Class(SpaceShip))) |
---|
| 325 | getFocus(); |
---|
[1505] | 326 | } |
---|
| 327 | |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | void SpaceShip::setMaxSpeed(float value) |
---|
| 331 | { this->maxSpeed_ = value; } |
---|
| 332 | void SpaceShip::setMaxSideAndBackSpeed(float value) |
---|
| 333 | { this->maxSideAndBackSpeed_ = value; } |
---|
| 334 | void SpaceShip::setMaxRotation(float value) |
---|
| 335 | { this->maxRotation_ = value; this->maxRotationRadian_ = Radian(value); } |
---|
| 336 | void SpaceShip::setTransAcc(float value) |
---|
| 337 | { this->translationAcceleration_ = value; } |
---|
| 338 | void SpaceShip::setRotAcc(float value) |
---|
| 339 | { this->rotationAcceleration_ = value; this->rotationAccelerationRadian_ = Radian(value); } |
---|
| 340 | void SpaceShip::setTransDamp(float value) |
---|
| 341 | { this->translationDamping_ = value; } |
---|
| 342 | void SpaceShip::setRotDamp(float value) |
---|
| 343 | { this->rotationDamping_ = value; this->rotationDampingRadian_ = Radian(value); } |
---|
| 344 | |
---|
| 345 | /** |
---|
| 346 | @brief XML loading and saving. |
---|
| 347 | @param xmlelement The XML-element |
---|
| 348 | @param loading Loading (true) or saving (false) |
---|
| 349 | @return The XML-element |
---|
| 350 | */ |
---|
| 351 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 352 | { |
---|
| 353 | Model::XMLPort(xmlelement, mode); |
---|
| 354 | |
---|
| 355 | XMLPortParamLoadOnly(SpaceShip, "camera", setCamera, xmlelement, mode); |
---|
| 356 | XMLPortParamLoadOnly(SpaceShip, "maxSpeed", setMaxSpeed, xmlelement, mode); |
---|
| 357 | XMLPortParamLoadOnly(SpaceShip, "maxSideAndBackSpeed", setMaxSideAndBackSpeed, xmlelement, mode); |
---|
| 358 | XMLPortParamLoadOnly(SpaceShip, "maxRotation", setMaxRotation, xmlelement, mode); |
---|
| 359 | XMLPortParamLoadOnly(SpaceShip, "transAcc", setTransAcc, xmlelement, mode); |
---|
| 360 | XMLPortParamLoadOnly(SpaceShip, "rotAcc", setRotAcc, xmlelement, mode); |
---|
| 361 | XMLPortParamLoadOnly(SpaceShip, "transDamp", setTransDamp, xmlelement, mode); |
---|
| 362 | XMLPortParamLoadOnly(SpaceShip, "rotDamp", setRotDamp, xmlelement, mode); |
---|
| 363 | |
---|
[1669] | 364 | myShip_=true; //TODO: this is a hack |
---|
[1505] | 365 | SpaceShip::create(); |
---|
[1669] | 366 | /*if (this->isExactlyA(Class(SpaceShip))) |
---|
| 367 | getFocus();*/ |
---|
[1505] | 368 | } |
---|
| 369 | |
---|
| 370 | int sgn(float x) |
---|
| 371 | { |
---|
| 372 | if (x >= 0) |
---|
| 373 | return 1; |
---|
| 374 | else |
---|
| 375 | return -1; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | std::string SpaceShip::whereAmI() { |
---|
| 379 | return getConvertedValue<float, std::string>(SpaceShip::getLocalShip()->getPosition().x) |
---|
| 380 | + " " + getConvertedValue<float, std::string>(SpaceShip::getLocalShip()->getPosition().y) |
---|
| 381 | + " " + getConvertedValue<float, std::string>(SpaceShip::getLocalShip()->getPosition().z); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | void SpaceShip::tick(float dt) |
---|
| 385 | { |
---|
[1558] | 386 | if (!this->isActive()) |
---|
| 387 | return; |
---|
| 388 | |
---|
[1505] | 389 | currentDir_ = getOrientation()*initialDir_; |
---|
| 390 | currentOrth_ = getOrientation()*initialOrth_; |
---|
| 391 | |
---|
| 392 | if (this->cam_) |
---|
| 393 | this->cam_->tick(dt); |
---|
| 394 | |
---|
| 395 | if (this->redNode_ && this->greenNode_) |
---|
| 396 | { |
---|
| 397 | this->blinkTime_ += dt; |
---|
| 398 | float redScale = 0.15 + 0.15 * sin(this->blinkTime_ * 10.0); |
---|
| 399 | float greenScale = 0.15 - 0.15 * sin(this->blinkTime_ * 10.0); |
---|
| 400 | this->redNode_->setScale(redScale, redScale, redScale); |
---|
| 401 | this->greenNode_->setScale(greenScale, greenScale, greenScale); |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | if (this->timeToReload_ > 0) |
---|
| 405 | this->timeToReload_ -= dt; |
---|
| 406 | else |
---|
| 407 | this->timeToReload_ = 0; |
---|
| 408 | |
---|
| 409 | if (this->bLMousePressed_ && this->timeToReload_ <= 0) |
---|
| 410 | { |
---|
| 411 | |
---|
[1552] | 412 | BillboardProjectile* projectile = new ParticleProjectile(this); |
---|
| 413 | projectile->setColour(this->getProjectileColour()); |
---|
| 414 | projectile->create(); |
---|
| 415 | if (projectile->classID == 0) |
---|
| 416 | { |
---|
[1505] | 417 | COUT(3) << "generated projectile with classid 0" << std::endl; // TODO: remove this output |
---|
[1552] | 418 | } |
---|
[1505] | 419 | |
---|
[1552] | 420 | projectile->setBacksync(true); |
---|
[1505] | 421 | this->timeToReload_ = this->reloadTime_; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | |
---|
| 425 | // ##################################### |
---|
| 426 | // ############# STEERING ############## |
---|
| 427 | // ##################################### |
---|
| 428 | |
---|
| 429 | if (this->velocity_.x > this->maxSpeed_) |
---|
| 430 | this->velocity_.x = this->maxSpeed_; |
---|
| 431 | if (this->velocity_.x < -this->maxSideAndBackSpeed_) |
---|
| 432 | this->velocity_.x = -this->maxSideAndBackSpeed_; |
---|
| 433 | if (this->velocity_.y > this->maxSideAndBackSpeed_) |
---|
| 434 | this->velocity_.y = this->maxSideAndBackSpeed_; |
---|
| 435 | if (this->velocity_.y < -this->maxSideAndBackSpeed_) |
---|
| 436 | this->velocity_.y = -this->maxSideAndBackSpeed_; |
---|
| 437 | if (this->rotationRate_ > this->maxRotationRadian_) |
---|
| 438 | this->rotationRate_ = this->maxRotationRadian_; |
---|
| 439 | if (this->rotationRate_ < -this->maxRotationRadian_) |
---|
| 440 | this->rotationRate_ = -this->maxRotationRadian_; |
---|
| 441 | |
---|
| 442 | if (this->acceleration_.x == 0) |
---|
| 443 | { |
---|
| 444 | if (this->velocity_.x > 0) |
---|
| 445 | { |
---|
| 446 | this->velocity_.x -= (this->translationDamping_ * dt); |
---|
| 447 | if (this->velocity_.x < 0) |
---|
| 448 | this->velocity_.x = 0; |
---|
| 449 | } |
---|
| 450 | else if (this->velocity_.x < 0) |
---|
| 451 | { |
---|
| 452 | this->velocity_.x += (this->translationDamping_ * dt); |
---|
| 453 | if (this->velocity_.x > 0) |
---|
| 454 | this->velocity_.x = 0; |
---|
| 455 | } |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | if (this->acceleration_.y == 0) |
---|
| 459 | { |
---|
| 460 | if (this->velocity_.y > 0) |
---|
| 461 | { |
---|
| 462 | this->velocity_.y -= (this->translationDamping_ * dt); |
---|
| 463 | if (this->velocity_.y < 0) |
---|
| 464 | this->velocity_.y = 0; |
---|
| 465 | } |
---|
| 466 | else if (this->velocity_.y < 0) |
---|
| 467 | { |
---|
| 468 | this->velocity_.y += (this->translationDamping_ * dt); |
---|
| 469 | if (this->velocity_.y > 0) |
---|
| 470 | this->velocity_.y = 0; |
---|
| 471 | } |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | if (this->momentum_ == this->zeroRadian_) |
---|
| 475 | { |
---|
| 476 | if (this->rotationRate_ > this->zeroRadian_) |
---|
| 477 | { |
---|
| 478 | this->rotationRate_ -= (this->rotationDampingRadian_ * dt); |
---|
| 479 | if (this->rotationRate_ < this->zeroRadian_) |
---|
| 480 | this->rotationRate_ = 0; |
---|
| 481 | } |
---|
| 482 | else if (this->rotationRate_ < this->zeroRadian_) |
---|
| 483 | { |
---|
| 484 | this->rotationRate_ += (this->rotationDampingRadian_ * dt); |
---|
| 485 | if (this->rotationRate_ > this->zeroRadian_) |
---|
| 486 | this->rotationRate_ = 0; |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | |
---|
| 491 | WorldEntity::tick(dt); |
---|
| 492 | |
---|
| 493 | this->roll(this->mouseXRotation_ * dt); |
---|
| 494 | if (this->bInvertYAxis_) |
---|
| 495 | this->yaw(Radian(-this->mouseYRotation_ * dt)); |
---|
| 496 | else |
---|
| 497 | this->yaw(Radian(this->mouseYRotation_ * dt)); |
---|
| 498 | |
---|
| 499 | if (this->acceleration_.x > 0) |
---|
[1552] | 500 | { |
---|
| 501 | this->tt1_->setEnabled(true); |
---|
| 502 | this->tt2_->setEnabled(true); |
---|
| 503 | } |
---|
[1505] | 504 | else |
---|
[1552] | 505 | { |
---|
| 506 | this->tt1_->setEnabled(false); |
---|
| 507 | this->tt2_->setEnabled(false); |
---|
| 508 | } |
---|
[1505] | 509 | |
---|
| 510 | if( myShip_ ) |
---|
| 511 | { |
---|
[1534] | 512 | COUT(5) << "steering our ship: " << objectID << std::endl; |
---|
[1505] | 513 | this->acceleration_.x = 0; |
---|
| 514 | this->acceleration_.y = 0; |
---|
| 515 | this->momentum_ = 0; |
---|
| 516 | this->mouseXRotation_ = Radian(0); |
---|
| 517 | this->mouseYRotation_ = Radian(0); |
---|
| 518 | }/*else |
---|
| 519 | COUT(4) << "not steering ship: " << objectID << " our ship: " << network::Client::getSingleton()->getShipID() << std::endl;*/ |
---|
[1564] | 520 | |
---|
| 521 | this->bLMousePressed_ = false; |
---|
[1505] | 522 | } |
---|
| 523 | |
---|
| 524 | void SpaceShip::movePitch(float val) |
---|
| 525 | { getLocalShip()->setMovePitch(val); } |
---|
| 526 | void SpaceShip::moveYaw(float val) |
---|
| 527 | { getLocalShip()->setMoveYaw(val); } |
---|
| 528 | void SpaceShip::moveRoll(float val) |
---|
| 529 | { getLocalShip()->setMoveRoll(val); } |
---|
| 530 | void SpaceShip::moveLongitudinal(float val) |
---|
| 531 | { getLocalShip()->setMoveLongitudinal(val); } |
---|
| 532 | void SpaceShip::moveLateral(float val) |
---|
| 533 | { getLocalShip()->setMoveLateral(val); } |
---|
| 534 | void SpaceShip::fire() |
---|
| 535 | { getLocalShip()->doFire(); } |
---|
| 536 | |
---|
| 537 | void SpaceShip::setMovePitch(float val) |
---|
| 538 | { |
---|
| 539 | val = -val * val * sgn(val) * this->rotationAcceleration_; |
---|
| 540 | if (val > this->maxRotation_) |
---|
| 541 | val = this->maxRotation_; |
---|
| 542 | if (val < -this->maxRotation_) |
---|
| 543 | val = -this->maxRotation_; |
---|
| 544 | this->mouseYRotation_ = Radian(val); |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | void SpaceShip::setMoveYaw(float val) |
---|
| 548 | { |
---|
| 549 | val = -val * val * sgn(val) * this->rotationAcceleration_; |
---|
| 550 | if (val > this->maxRotation_) |
---|
| 551 | val = this->maxRotation_; |
---|
| 552 | if (val < -this->maxRotation_) |
---|
| 553 | val = -this->maxRotation_; |
---|
| 554 | this->mouseXRotation_ = Radian(val); |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | void SpaceShip::setMoveRoll(float val) |
---|
| 558 | { |
---|
| 559 | this->momentum_ = Radian(-this->rotationAccelerationRadian_ * val); |
---|
| 560 | //COUT(3) << "rotating val: " << val << " acceleration: " << this->rotationAccelerationRadian_.valueDegrees() << std::endl; |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | void SpaceShip::setMoveLongitudinal(float val) |
---|
| 564 | { |
---|
| 565 | this->acceleration_.x = this->translationAcceleration_ * val; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | void SpaceShip::setMoveLateral(float val) |
---|
| 569 | { |
---|
| 570 | this->acceleration_.y = -this->translationAcceleration_ * val; |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | void SpaceShip::doFire() |
---|
| 574 | { |
---|
| 575 | this->bLMousePressed_ = true; |
---|
| 576 | } |
---|
| 577 | } |
---|