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