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