Changeset 1293 for code/trunk/src/orxonox/objects/SpaceShip.cc
- Timestamp:
- May 15, 2008, 5:44:55 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/orxonox/objects/SpaceShip.cc
r1219 r1293 23 23 * Fabian 'x3n' Landau 24 24 * Co-authors: 25 * ...25 * Benjamin Knecht 26 26 * 27 27 */ … … 37 37 #include <OgreSceneNode.h> 38 38 39 #include "CameraHandler.h" 39 40 #include "tinyxml/tinyxml.h" 40 41 #include "ois/OIS.h" … … 50 51 #include "core/XMLPort.h" 51 52 #include "core/ConsoleCommand.h" 53 #include "network/Client.h" 52 54 53 55 namespace orxonox … … 62 64 SpaceShip* SpaceShip::instance_s; 63 65 64 SpaceShip::SpaceShip() 66 SpaceShip::SpaceShip() : 67 //testvector_(0,0,0), 68 //bInvertYAxis_(false), 69 setMouseEventCallback_(false), 70 bLMousePressed_(false), 71 bRMousePressed_(false), 72 camNode_(0), 73 cam_(0), 74 camName_("CamNode"), 75 tt_(0), 76 redNode_(0), 77 greenNode_(0), 78 blinkTime_(0.0f), 79 chNearNode_(0), 80 chFarNode_(0), 81 timeToReload_(0.0f), 82 //reloadTime_(0.0f), 83 maxSideAndBackSpeed_(0.0f), 84 maxSpeed_(0.0f), 85 maxRotation_(0.0f), 86 translationAcceleration_(0.0f), 87 rotationAcceleration_(0.0f), 88 translationDamping_(0.0f), 89 rotationDamping_(0.0f), 90 maxRotationRadian_(0), 91 rotationAccelerationRadian_(0), 92 rotationDampingRadian_(0), 93 zeroRadian_(0), 94 mouseXRotation_(0), 95 mouseYRotation_(0), 96 mouseX_(0.0f), 97 mouseY_(0.0f), 98 emitterRate_(0.0f), 99 server_(false) 65 100 { 66 101 RegisterObject(SpaceShip); … … 71 106 this->setConfigValues(); 72 107 73 this->setMouseEventCallback_ = false;74 this->bLMousePressed_ = false;75 this->bRMousePressed_ = false;76 this->mouseX_ = 0;77 this->mouseY_ = 0;78 79 this->camNode_ = 0;80 81 this->tt_ = 0;82 this->redNode_ = 0;83 this->greenNode_ = 0;84 this->blinkTime_ = 0;85 86 this->timeToReload_ = 0;87 88 this->maxSpeed_ = 0;89 this->maxSideAndBackSpeed_ = 0;90 this->maxRotation_ = 0;91 this->translationAcceleration_ = 0;92 this->rotationAcceleration_ = 0;93 this->translationDamping_ = 0;94 this->rotationDamping_ = 0;95 96 this->maxRotationRadian_ = 0;97 this->rotationAccelerationRadian_ = 0;98 this->rotationDampingRadian_ = 0;99 this->zeroRadian_ = Radian(0);100 108 101 109 this->setRotationAxis(1, 0, 0); 102 110 this->setStatic(false); 103 /*104 this->moveForward_ = 0;105 this->rotateUp_ = 0;106 this->rotateDown_ = 0;107 this->rotateRight_ = 0;108 this->rotateLeft_ = 0;109 this->loopRight_ = 0;110 this->loopLeft_ = 0;111 this->brakeForward_ = 0;112 this->brakeRotate_ = 0;113 this->brakeLoop_ = 0;114 this->speedForward_ = 0;115 this->speedRotateUpDown_ = 0;116 this->speedRotateRightLeft_ = 0;117 this->speedLoopRightLeft_ = 0;118 this->maxSpeedForward_ = 0;119 this->maxSpeedRotateUpDown_ = 0;120 this->maxSpeedRotateRightLeft_ = 0;121 this->maxSpeedLoopRightLeft_ = 0;122 this->accelerationForward_ = 0;123 this->accelerationRotateUpDown_ = 0;124 this->accelerationRotateRightLeft_ = 0;125 this->accelerationLoopRightLeft_ = 0;126 127 this->speed = 250;128 this->loop = 100;129 this->rotate = 10;130 this->mouseX = 0;131 this->mouseY = 0;132 this->maxMouseX = 0;133 this->minMouseX = 0;134 this->moved = false;135 136 this->brakeRotate(rotate*10);137 this->brakeLoop(loop);138 */139 // this->create();140 141 111 142 112 COUT(3) << "Info: SpaceShip was loaded" << std::endl; … … 147 117 if (this->tt_) 148 118 delete this->tt_; 119 if(setMouseEventCallback_) 120 InputManager::removeMouseHandler("SpaceShip"); 121 if (this->cam_) 122 delete this->cam_; 149 123 } 150 124 … … 158 132 159 133 void SpaceShip::registerAllVariables(){ 160 Model::registerAllVariables(); 161 162 134 registerVar( &camName_, camName_.length()+1, network::STRING, 0x1); 135 registerVar( &maxSpeed_, sizeof(maxSpeed_), network::DATA, 0x1); 136 registerVar( &maxSideAndBackSpeed_, sizeof(maxSideAndBackSpeed_), network::DATA, 0x1); 137 registerVar( &maxRotation_, sizeof(maxRotation_), network::DATA, 0x1); 138 registerVar( &translationAcceleration_, sizeof(translationAcceleration_), network::DATA, 0x1); 139 registerVar( &rotationAcceleration_, sizeof(rotationAcceleration_), network::DATA, 0x1); 140 registerVar( &rotationAccelerationRadian_, sizeof(rotationAccelerationRadian_), network::DATA, 0x1); 141 registerVar( &translationDamping_, sizeof(translationDamping_), network::DATA, 0x1); 142 registerVar( &rotationDamping_, sizeof(rotationDamping_), network::DATA, 0x1); 143 registerVar( &rotationDampingRadian_, sizeof(rotationDampingRadian_), network::DATA, 0x1); 163 144 164 145 } … … 166 147 void SpaceShip::init() 167 148 { 149 if ((server_ || ( network::Client::getSingleton() && network::Client::getSingleton()->getShipID()==objectID ) )) 150 { 151 if (!setMouseEventCallback_) 152 { 153 InputManager::addMouseHandler(this, "SpaceShip"); 154 setMouseEventCallback_ = true; 155 } 156 } 157 168 158 // START CREATING THRUSTER 169 159 this->tt_ = new ParticleInterface(GraphicsEngine::getSingleton().getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow"); … … 191 181 this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1); 192 182 193 this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4. 7, -0.3));183 this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.0, -0.3)); 194 184 this->redNode_->setInheritScale(false); 195 this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4. 7, -0.3));185 this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.0, -0.3)); 196 186 this->greenNode_->setInheritScale(false); 197 187 … … 218 208 this->chFarNode_->setScale(0.4, 0.4, 0.4); 219 209 210 createCamera(); 220 211 // END of testing crosshair 221 212 } … … 232 223 Model::loadParams(xmlElem); 233 224 this->create(); 225 this->getFocus(); 234 226 /* 235 227 if (xmlElem->Attribute("forward") && xmlElem->Attribute("rotateupdown") && xmlElem->Attribute("rotaterightleft") && xmlElem->Attribute("looprightleft")) … … 246 238 247 239 COUT(4) << "Loader: Initialized spaceship steering with values " << maxSpeedForward_ << " " << maxSpeedRotateUpDown_ << " " << maxSpeedRotateRightLeft_ << " " << maxSpeedLoopRightLeft_ << " " << std::endl; 248 240 } 249 241 */ 250 242 if (xmlElem->Attribute("maxSpeed") && xmlElem->Attribute("maxSideAndBackSpeed") && xmlElem->Attribute("maxRotation") && xmlElem->Attribute("transAcc") && xmlElem->Attribute("rotAcc") && xmlElem->Attribute("transDamp") && xmlElem->Attribute("rotDamp")) … … 272 264 273 265 COUT(4) << "Loader: Initialized SpaceShip" << std::endl; 274 275 276 277 278 279 266 } 267 268 if (xmlElem->Attribute("camera")) 269 { 270 this->setCamera(); 271 } 280 272 } 281 273 282 274 void SpaceShip::setCamera(const std::string& camera) 283 275 { 284 Ogre::Camera *cam = GraphicsEngine::getSingleton().getSceneManager()->createCamera("ShipCam"); 285 this->camNode_ = this->getNode()->createChildSceneNode("CamNode"); 286 /* 287 // node->setInheritOrientation(false); 288 cam->setPosition(Vector3(0,50,-150)); 289 cam->lookAt(Vector3(0,20,0)); 290 cam->roll(Degree(0)); 291 */ 292 293 cam->setPosition(Vector3(-200,0,35)); 276 camName_=camera; 277 // change camera attributes here, if you want to ;) 278 } 279 280 void SpaceShip::getFocus(){ 281 COUT(4) << "requesting focus" << std::endl; 282 if(network::Client::getSingleton()==0 || network::Client::getSingleton()->getShipID()==objectID) 283 CameraHandler::getInstance()->requestFocus(cam_); 284 285 } 286 287 void SpaceShip::createCamera(){ 288 // COUT(4) << "begin camera creation" << std::endl; 289 this->camNode_ = this->getNode()->createChildSceneNode(camName_); 290 COUT(4) << "position: (this)" << this->getNode()->getPosition() << std::endl; 291 this->camNode_->setPosition(Vector3(-50,0,10)); 292 Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,-1,0)); 293 Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(0,0,-1)); 294 camNode_->setOrientation(q1*q2); 295 COUT(4) << "position: (cam)" << this->camNode_->getPosition() << std::endl; 296 cam_ = new Camera(this->camNode_); 297 298 cam_->setTargetNode(this->getNode()); 294 299 // cam->setPosition(Vector3(0,-350,0)); 295 cam->lookAt(Vector3(0,0,35));296 cam->roll(Degree(-90));297 298 this->camNode_->attachObject(cam);299 GraphicsEngine::getSingleton().getRenderWindow()->addViewport(cam); 300 if(network::Client::getSingleton()!=0 && network::Client::getSingleton()->getShipID()==objectID){ 301 this->setBacksync(true); 302 CameraHandler::getInstance()->requestFocus(cam_); 303 } 304 300 305 } 301 306 … … 333 338 XMLPortParamLoadOnly(SpaceShip, "transDamp", setTransDamp, xmlelement, mode); 334 339 XMLPortParamLoadOnly(SpaceShip, "rotDamp", setRotDamp, xmlelement, mode); 340 server_=true; // TODO: this is only a hack 341 SpaceShip::create(); 342 getFocus(); 335 343 } 336 344 … … 343 351 } 344 352 345 bool SpaceShip::mouseMoved(const OIS::MouseEvent &e)353 bool SpaceShip::mouseMoved(const MouseState& state) 346 354 { 347 355 /* … … 360 368 if (this->bRMousePressed_) 361 369 { 362 this->camNode_->roll(Degree(- e.state.X.rel * 0.10));363 this->camNode_->yaw(Degree( e.state.Y.rel * 0.10));364 } 365 else 366 { 367 float minDimension = e.state.height;368 if ( e.state.width < minDimension)369 minDimension = e.state.width;370 371 this->mouseX_ += e.state.X.rel;370 this->camNode_->roll(Degree(-state.X.rel * 0.10)); 371 this->camNode_->yaw(Degree(state.Y.rel * 0.10)); 372 } 373 else 374 { 375 float minDimension = state.height; 376 if (state.width < minDimension) 377 minDimension = state.width; 378 379 this->mouseX_ += state.X.rel; 372 380 if (this->mouseX_ < -minDimension) 373 381 this->mouseX_ = -minDimension; … … 375 383 this->mouseX_ = minDimension; 376 384 377 this->mouseY_ += e.state.Y.rel;385 this->mouseY_ += state.Y.rel; 378 386 if (this->mouseY_ < -minDimension) 379 387 this->mouseY_ = -minDimension; … … 403 411 } 404 412 405 bool SpaceShip::mouse Pressed(const OIS::MouseEvent &e, OIS::MouseButtonIDid)406 { 407 if (id == OIS::MB_Left)413 bool SpaceShip::mouseButtonPressed(const MouseState& state, MouseButton::Enum id) 414 { 415 if (id == MouseButton::Left) 408 416 this->bLMousePressed_ = true; 409 else if (id == OIS::MB_Right)417 else if (id == MouseButton::Right) 410 418 this->bRMousePressed_ = true; 411 419 … … 413 421 } 414 422 415 bool SpaceShip::mouse Released(const OIS::MouseEvent &e, OIS::MouseButtonIDid)416 { 417 if (id == OIS::MB_Left)423 bool SpaceShip::mouseButtonReleased(const MouseState& state, MouseButton::Enum id) 424 { 425 if (id == MouseButton::Left) 418 426 this->bLMousePressed_ = false; 419 else if (id == OIS::MB_Right)427 else if (id == MouseButton::Right) 420 428 { 421 429 this->bRMousePressed_ = false; … … 428 436 void SpaceShip::tick(float dt) 429 437 { 430 if (!setMouseEventCallback_) 431 { 432 InputManager::addMouseHandler(this, "SpaceShip"); 433 setMouseEventCallback_ = true; 434 } 438 if (this->cam_) 439 this->cam_->tick(dt); 435 440 436 441 if (this->redNode_ && this->greenNode_) … … 450 455 if (this->bLMousePressed_ && this->timeToReload_ <= 0) 451 456 { 452 new Projectile(this); 457 Projectile *p = new Projectile(this); 458 p->setBacksync(true); 453 459 this->timeToReload_ = this->reloadTime_; 454 460 } 455 456 OIS::Keyboard* mKeyboard = InputManager::getKeyboard();457 461 458 462 … … 522 526 } 523 527 524 if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) 528 if( (network::Client::getSingleton() && network::Client::getSingleton()->getShipID() == objectID) || server_ ) 529 { 530 COUT(4) << "steering our ship: " << objectID << std::endl; 531 if (InputManager::isKeyDown(KeyCode::Up) || InputManager::isKeyDown(KeyCode::W)) 525 532 this->acceleration_.x = this->translationAcceleration_; 526 else if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))533 else if(InputManager::isKeyDown(KeyCode::Down) || InputManager::isKeyDown(KeyCode::S)) 527 534 this->acceleration_.x = -this->translationAcceleration_; 528 else535 else 529 536 this->acceleration_.x = 0; 530 537 531 if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))538 if (InputManager::isKeyDown(KeyCode::Right) || InputManager::isKeyDown(KeyCode::D)) 532 539 this->acceleration_.y = -this->translationAcceleration_; 533 else if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))540 else if (InputManager::isKeyDown(KeyCode::Left) || InputManager::isKeyDown(KeyCode::A)) 534 541 this->acceleration_.y = this->translationAcceleration_; 535 else542 else 536 543 this->acceleration_.y = 0; 537 544 538 if (mKeyboard->isKeyDown(OIS::KC_DELETE) || mKeyboard->isKeyDown(OIS::KC_Q))545 if (InputManager::isKeyDown(KeyCode::Delete) || InputManager::isKeyDown(KeyCode::Q)) 539 546 this->momentum_ = Radian(-this->rotationAccelerationRadian_); 540 else if (mKeyboard->isKeyDown(OIS::KC_PGDOWN) || mKeyboard->isKeyDown(OIS::KC_E))547 else if (InputManager::isKeyDown(KeyCode::PageDown) || InputManager::isKeyDown(KeyCode::E)) 541 548 this->momentum_ = Radian(this->rotationAccelerationRadian_); 542 else549 else 543 550 this->momentum_ = 0; 551 }/*else 552 COUT(4) << "not steering ship: " << objectID << " our ship: " << network::Client::getSingleton()->getShipID() << std::endl;*/ 544 553 545 554 WorldEntity::tick(dt);
Note: See TracChangeset
for help on using the changeset viewer.