Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 15, 2008, 5:44:55 PM (16 years ago)
Author:
scheusso
Message:

merged changes from input & camera & network branch into trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/orxonox/objects/SpaceShip.cc

    r1219 r1293  
    2323 *      Fabian 'x3n' Landau
    2424 *   Co-authors:
    25  *      ...
     25 *      Benjamin Knecht
    2626 *
    2727 */
     
    3737#include <OgreSceneNode.h>
    3838
     39#include "CameraHandler.h"
    3940#include "tinyxml/tinyxml.h"
    4041#include "ois/OIS.h"
     
    5051#include "core/XMLPort.h"
    5152#include "core/ConsoleCommand.h"
     53#include "network/Client.h"
    5254
    5355namespace orxonox
     
    6264    SpaceShip* SpaceShip::instance_s;
    6365
    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)
    65100    {
    66101        RegisterObject(SpaceShip);
     
    71106        this->setConfigValues();
    72107
    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);
    100108
    101109        this->setRotationAxis(1, 0, 0);
    102110        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 
    141111
    142112        COUT(3) << "Info: SpaceShip was loaded" << std::endl;
     
    147117        if (this->tt_)
    148118            delete this->tt_;
     119        if(setMouseEventCallback_)
     120          InputManager::removeMouseHandler("SpaceShip");
     121        if (this->cam_)
     122          delete this->cam_;
    149123    }
    150124
     
    158132
    159133    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);
    163144
    164145    }
     
    166147    void SpaceShip::init()
    167148    {
     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
    168158        // START CREATING THRUSTER
    169159        this->tt_ = new ParticleInterface(GraphicsEngine::getSingleton().getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow");
     
    191181        this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1);
    192182
    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));
    194184        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));
    196186        this->greenNode_->setInheritScale(false);
    197187
     
    218208        this->chFarNode_->setScale(0.4, 0.4, 0.4);
    219209
     210        createCamera();
    220211        // END of testing crosshair
    221212    }
     
    232223        Model::loadParams(xmlElem);
    233224        this->create();
     225        this->getFocus();
    234226/*
    235227        if (xmlElem->Attribute("forward") && xmlElem->Attribute("rotateupdown") && xmlElem->Attribute("rotaterightleft") && xmlElem->Attribute("looprightleft"))
     
    246238
    247239            COUT(4) << "Loader: Initialized spaceship steering with values " << maxSpeedForward_ << " " << maxSpeedRotateUpDown_ << " " << maxSpeedRotateRightLeft_ << " " << maxSpeedLoopRightLeft_ << " " << std::endl;
    248         }
     240      }
    249241*/
    250242        if (xmlElem->Attribute("maxSpeed") && xmlElem->Attribute("maxSideAndBackSpeed") && xmlElem->Attribute("maxRotation") && xmlElem->Attribute("transAcc") && xmlElem->Attribute("rotAcc") && xmlElem->Attribute("transDamp") && xmlElem->Attribute("rotDamp"))
     
    272264
    273265            COUT(4) << "Loader: Initialized SpaceShip" << std::endl;
    274         }
    275 
    276         if (xmlElem->Attribute("camera"))
    277         {
    278             this->setCamera();
    279         }
     266        }
     267
     268        if (xmlElem->Attribute("camera"))
     269        {
     270            this->setCamera();
     271        }
    280272    }
    281273
    282274    void SpaceShip::setCamera(const std::string& camera)
    283275    {
    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());
    294299//        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
    300305    }
    301306
     
    333338        XMLPortParamLoadOnly(SpaceShip, "transDamp", setTransDamp, xmlelement, mode);
    334339        XMLPortParamLoadOnly(SpaceShip, "rotDamp", setRotDamp, xmlelement, mode);
     340        server_=true; // TODO: this is only a hack
     341        SpaceShip::create();
     342        getFocus();
    335343    }
    336344
     
    343351    }
    344352
    345     bool SpaceShip::mouseMoved(const OIS::MouseEvent &e)
     353    bool SpaceShip::mouseMoved(const MouseState& state)
    346354    {
    347355/*
     
    360368        if (this->bRMousePressed_)
    361369        {
    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;
    372380            if (this->mouseX_ < -minDimension)
    373381                this->mouseX_ = -minDimension;
     
    375383                this->mouseX_ = minDimension;
    376384
    377             this->mouseY_ += e.state.Y.rel;
     385            this->mouseY_ += state.Y.rel;
    378386            if (this->mouseY_ < -minDimension)
    379387                this->mouseY_ = -minDimension;
     
    403411    }
    404412
    405     bool SpaceShip::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
    406     {
    407         if (id == OIS::MB_Left)
     413    bool SpaceShip::mouseButtonPressed(const MouseState& state, MouseButton::Enum id)
     414    {
     415        if (id == MouseButton::Left)
    408416            this->bLMousePressed_ = true;
    409         else if (id == OIS::MB_Right)
     417        else if (id == MouseButton::Right)
    410418            this->bRMousePressed_ = true;
    411419
     
    413421    }
    414422
    415     bool SpaceShip::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
    416     {
    417         if (id == OIS::MB_Left)
     423    bool SpaceShip::mouseButtonReleased(const MouseState& state, MouseButton::Enum id)
     424    {
     425        if (id == MouseButton::Left)
    418426            this->bLMousePressed_ = false;
    419         else if (id == OIS::MB_Right)
     427        else if (id == MouseButton::Right)
    420428        {
    421429            this->bRMousePressed_ = false;
     
    428436    void SpaceShip::tick(float dt)
    429437    {
    430         if (!setMouseEventCallback_)
    431         {
    432           InputManager::addMouseHandler(this, "SpaceShip");
    433           setMouseEventCallback_ = true;
    434         }
     438        if (this->cam_)
     439            this->cam_->tick(dt);
    435440
    436441        if (this->redNode_ && this->greenNode_)
     
    450455        if (this->bLMousePressed_ && this->timeToReload_ <= 0)
    451456        {
    452             new Projectile(this);
     457            Projectile *p = new Projectile(this);
     458            p->setBacksync(true);
    453459            this->timeToReload_ = this->reloadTime_;
    454460        }
    455 
    456         OIS::Keyboard* mKeyboard = InputManager::getKeyboard();
    457461
    458462
     
    522526        }
    523527
    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))
    525532            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))
    527534            this->acceleration_.x = -this->translationAcceleration_;
    528         else
     535          else
    529536            this->acceleration_.x = 0;
    530537
    531         if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
     538          if (InputManager::isKeyDown(KeyCode::Right) || InputManager::isKeyDown(KeyCode::D))
    532539            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))
    534541            this->acceleration_.y = this->translationAcceleration_;
    535         else
     542          else
    536543            this->acceleration_.y = 0;
    537544
    538         if (mKeyboard->isKeyDown(OIS::KC_DELETE) || mKeyboard->isKeyDown(OIS::KC_Q))
     545          if (InputManager::isKeyDown(KeyCode::Delete) || InputManager::isKeyDown(KeyCode::Q))
    539546            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))
    541548            this->momentum_ = Radian(this->rotationAccelerationRadian_);
    542         else
     549          else
    543550            this->momentum_ = 0;
     551        }/*else
     552          COUT(4) << "not steering ship: " << objectID << " our ship: " << network::Client::getSingleton()->getShipID() << std::endl;*/
    544553
    545554        WorldEntity::tick(dt);
Note: See TracChangeset for help on using the changeset viewer.