Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 27, 2008, 4:08:51 AM (16 years ago)
Author:
landauf
Message:

many changes, most important: BaseObject takes now a pointer to it's creator which is needed to build a level hierarchy (with different scenes)

Location:
code/branches/objecthierarchy/src/orxonox/objects
Files:
6 added
37 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/orxonox/objects/Camera.cc

    r2006 r2019  
    3232
    3333#include <string>
     34#include <cassert>
    3435
    3536#include <OgreSceneManager.h>
     
    4142#include "util/SubString.h"
    4243#include "util/Math.h"
     44#include "util/String.h"
    4345#include "util/Debug.h"
    4446#include "core/CoreIncludes.h"
     47#include "core/ConfigValueIncludes.h"
    4548#include "GraphicsEngine.h"
     49#include "objects/Scene.h"
    4650
    4751namespace orxonox
    4852{
    49   CreateUnloadableFactory(Camera);
     53    CreateFactory(Camera);
    5054
    51   Camera::Camera()
    52   {
    53     RegisterObject(Camera);
     55    Camera::Camera(BaseObject* creator) : PositionableEntity(creator)
     56    {
     57        RegisterObject(Camera);
    5458
    55     this->bHasFocus_ = false;
    56     this->bDrag_ = false;
    57     this->cameraNode_ = GraphicsEngine::getInstance().getLevelSceneManager()->getRootSceneNode()->createChildSceneNode();
    58     this->setObjectMode(0x0);
    59   }
     59        assert(this->getScene());
     60        assert(this->getScene()->getSceneManager());
    6061
    61   Camera::~Camera()
    62   {
    63     if (this->isInitialized())
     62        this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString());
     63        this->getNode()->attachObject(this->camera_);
     64
     65        this->bHasFocus_ = false;
     66        this->bDrag_ = false;
     67        this->nearClipDistance_ = 1;
     68
     69        this->setObjectMode(0x0);
     70
     71        this->setConfigValues();
     72        this->configvaluecallback_changedNearClipDistance();
     73
     74        this->requestFocus(); // ! HACK ! REMOVE THIS !
     75    }
     76
     77    Camera::~Camera()
    6478    {
    65       CameraHandler::getInstance()->releaseFocus(this);
    66       GraphicsEngine::getInstance().getLevelSceneManager()->getRootSceneNode()->removeAndDestroyChild(this->cameraNode_->getName());
     79        if (this->isInitialized())
     80        {
     81            this->releaseFocus();
     82        }
    6783    }
    68   }
    6984
    70   void Camera::tick(float dt)
    71   {
    72       // this stuff here may need some adjustments
    73       float coeff = (this->bDrag_) ? min(1.0f, 15.0f * dt) : (1.0f);
     85    void Camera::setConfigValues()
     86    {
     87        SetConfigValue(nearClipDistance_, 1.0f).callback(this, &Camera::configvaluecallback_changedNearClipDistance);
     88    }
    7489
    75       Vector3 offset = this->getNode()->getWorldPosition() - this->cameraNode_->getWorldPosition();
    76       this->cameraNode_->translate(coeff * offset);
     90    void Camera::configvaluecallback_changedNearClipDistance()
     91    {
     92        this->camera_->setNearClipDistance(this->nearClipDistance_);
     93    }
    7794
    78       this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->getWorldOrientation(), this->getWorldOrientation(), false));
    79   }
     95    void Camera::tick(float dt)
     96    {
     97/*
     98        // this stuff here may need some adjustments
     99        float coeff = (this->bDrag_) ? min(1.0f, 15.0f * dt) : (1.0f);
    80100
    81   /**
    82     don't move anything before here! here the Ogre camera is set to values of this camera
    83     always call update after changes
    84   */
    85   void Camera::update()
    86   {
    87       this->cameraNode_->setPosition(this->getWorldPosition());
    88       this->cameraNode_->setOrientation(this->getWorldOrientation());
    89   }
     101        Vector3 offset = this->getNode()->getWorldPosition() - this->cameraNode_->getWorldPosition();
     102        this->cameraNode_->translate(coeff * offset);
    90103
    91   /**
    92     what to do when camera loses focus (do not request focus in this function!!)
    93     this is called by the CameraHandler singleton class to notify the camera
    94   */
    95   void Camera::removeFocus()
    96   {
    97     this->bHasFocus_ = false;
    98     this->cameraNode_->detachObject(this->cam_);
    99   }
     104        this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->getWorldOrientation(), this->getWorldOrientation(), false));
     105*/
     106    }
    100107
    101   void Camera::setFocus(Ogre::Camera* ogreCam)
    102   {
    103     this->bHasFocus_ = true;
    104     this->cam_ = ogreCam;
    105     this->cam_->setOrientation(this->cameraNode_->getWorldOrientation());
    106     this->cameraNode_->attachObject(this->cam_);
    107   }
     108    void Camera::requestFocus()
     109    {
     110        CameraHandler::getInstance().requestFocus(this);
     111    }
    108112
    109   void Camera::requestFocus()
    110   {
    111     CameraHandler::getInstance()->requestFocus(this);
    112   }
     113    void Camera::releaseFocus()
     114    {
     115        CameraHandler::getInstance().releaseFocus(this);
     116    }
     117
     118    /**
     119        what to do when camera loses focus (do not request focus in this function!!)
     120        this is called by the CameraHandler singleton class to notify the camera
     121    */
     122    void Camera::removeFocus()
     123    {
     124        this->bHasFocus_ = false;
     125    }
     126
     127    void Camera::setFocus(Ogre::Viewport* viewport)
     128    {
     129        this->bHasFocus_ = true;
     130        viewport->setCamera(this->camera_);
     131    }
    113132}
  • code/branches/objecthierarchy/src/orxonox/objects/Camera.h

    r2006 r2019  
    4242    class _OrxonoxExport Camera : public PositionableEntity, public Tickable
    4343    {
    44       friend class CameraHandler;
     44        friend class CameraHandler;
    4545
    46       public:
    47         Camera();
    48         virtual ~Camera();
     46        public:
     47            Camera(BaseObject* creator);
     48            virtual ~Camera();
    4949
    50         virtual void tick(float dt);
    51         void update();
     50            void setConfigValues();
     51            virtual void tick(float dt);
    5252
    53         void requestFocus();
    54         inline bool hasFocus()
    55             { return this->bHasFocus_; }
     53            void requestFocus();
     54            void releaseFocus();
    5655
    57         inline void setDrag(bool bDrag)
    58             { this->bDrag_ = bDrag; }
    59         inline bool getDrag() const
    60             { return this->bDrag_; }
     56            inline bool hasFocus()
     57                { return this->bHasFocus_; }
    6158
    62       private:
    63         void removeFocus();
    64         void setFocus(Ogre::Camera* ogreCam);
     59            inline void setDrag(bool bDrag)
     60                { this->bDrag_ = bDrag; }
     61            inline bool getDrag() const
     62                { return this->bDrag_; }
    6563
    66         Ogre::Camera* cam_;
    67         Ogre::SceneNode* cameraNode_;
    68         Ogre::Vector3 oldPos;
    69         bool bHasFocus_;
    70         bool bDrag_;
     64        private:
     65            void removeFocus();
     66            void setFocus(Ogre::Viewport* viewport);
     67            void configvaluecallback_changedNearClipDistance();
     68
     69            Ogre::Camera*   camera_;
     70            float           nearClipDistance_;
     71            bool            bHasFocus_;
     72            bool            bDrag_;
    7173    };
    7274}
  • code/branches/objecthierarchy/src/orxonox/objects/CameraHandler.cc

    r2006 r2019  
    3333
    3434#include "core/ObjectList.h"
     35#include "core/Core.h"
    3536#include "Camera.h"
    3637#include "GraphicsEngine.h"
     
    3839#include <OgreCamera.h>
    3940
    40 namespace orxonox {
     41namespace orxonox
     42{
     43    CameraHandler::CameraHandler()
     44    {
     45//        GraphicsEngine::getInstance().getViewport()->setCamera(this->cam_);
     46    }
    4147
    42   CameraHandler* CameraHandler::singletonRef = NULL;
     48    CameraHandler& CameraHandler::getInstance()
     49    {
     50        static CameraHandler instance;
     51        return instance;
     52    }
    4353
    44   CameraHandler::CameraHandler()
    45   {
    46     this->cam_ = GraphicsEngine::getInstance().getLevelSceneManager()->createCamera("Cam");
    47     GraphicsEngine::getInstance().getViewport()->setCamera(this->cam_);
    48     this->cam_->setNearClipDistance(1);
    49     //GraphicsEngine::getInstance().getRenderWindow()->addViewport(this->cam_, 2, 0.4, 0.4, 0.2, 0.2);
    50     /*this->activeCamera_ = *ObjectList<Camera>::begin();
    51     this->activeCamera_->cam_ = this->cam_;*/
    52   }
     54    Camera* CameraHandler::getActiveCamera() const
     55    {
     56        if (this->cameraList_.size() > 0)
     57            return this->cameraList_.front();
     58        else
     59            return 0;
     60    }
    5361
    54   void CameraHandler::requestFocus(Camera* requestCam)
    55   {
    56     // notify old camera (if it exists)
    57     if(focusList_.size() > 0)
    58       focusList_.back()->removeFocus();
    59     // add to list
    60     focusList_.push_back(requestCam);
    61     // set focus to new camera and update (if necessary)
    62     if(!requestCam->hasFocus()) {
    63       requestCam->setFocus(this->cam_);
    64       requestCam->update();
     62    void CameraHandler::requestFocus(Camera* camera)
     63    {
     64        if (!Core::showsGraphics())
     65            return;
     66
     67        // notify old camera (if it exists)
     68        if (this->cameraList_.size() > 0)
     69            this->cameraList_.front()->removeFocus();
     70
     71        // add to list
     72        this->cameraList_.push_front(camera);
     73        camera->setFocus(GraphicsEngine::getInstance().getViewport());
    6574    }
    66     // delete dublicates
    67     focusList_.unique();
    68   }
    6975
    70   void CameraHandler::releaseFocus(Camera* cam)
    71   {
    72     // notify the cam of releasing the focus
    73     if(cam->hasFocus())
    74       cam->removeFocus();
    75     // delete camera from list
    76     focusList_.remove(cam);
    77     // set new focus if necessary
    78     if(focusList_.size() > 0 && !(focusList_.back()->hasFocus()))
    79       focusList_.back()->setFocus(this->cam_);
    80   }
    81 /*
    82   void CameraHandler::changeActiveCamera(Camera* setCam)
    83   {
    84     cam_->getParentSceneNode()->detachObject(cam_);
    85     //setCam->attachCamera(cam_);
    86     activeCamera_ = setCam;
    87   }
    88 */
    89 /*  bool isInVector(Camera* cam)
    90   {
    91     for(std::vector<Camera*>::iterator it = cams_.begin(); it != cams_.end(); it++)
     76    void CameraHandler::releaseFocus(Camera* camera)
    9277    {
    93       if (*it == cam) return true;
     78        if (!Core::showsGraphics())
     79            return;
     80
     81        // notify the cam of releasing the focus
     82        if (this->cameraList_.front() == camera)
     83        {
     84            camera->removeFocus();
     85            this->cameraList_.pop_front();
     86
     87            // set new focus if necessary
     88            if (cameraList_.size() > 0)
     89                cameraList_.front()->setFocus(GraphicsEngine::getInstance().getViewport());
     90        }
     91        else
     92        {
     93            this->cameraList_.remove(camera);
     94        }
    9495    }
    95     return false;
    96   }*/
    9796}
  • code/branches/objecthierarchy/src/orxonox/objects/CameraHandler.h

    r1993 r2019  
    3636#define _Camera_Handler_H__
    3737
     38#include "OrxonoxPrereqs.h"
     39
    3840#include <list>
    39 
    4041#include <OgreCamera.h>
    41 
    42 #include "OrxonoxPrereqs.h"
    4342
    4443#include "core/BaseObject.h"
     
    4645namespace orxonox
    4746{
    48   class _OrxonoxExport CameraHandler
    49   {
     47    class _OrxonoxExport CameraHandler
     48    {
     49        public:
     50            static CameraHandler& getInstance();
    5051
    51     public:
    52       inline static CameraHandler* getInstance() { if (!CameraHandler::singletonRef) CameraHandler::singletonRef = new CameraHandler(); return CameraHandler::singletonRef; }
    53       inline ~CameraHandler() { CameraHandler::singletonRef = NULL; }
    54       inline Camera* getActiveCamera() { return this->focusList_.back(); }
    55       /*void registerCamera(Camera* newCam);
    56       void changeActiveCamera(Camera* setCam);*/
    57       void requestFocus(Camera* requestCam);
    58       void releaseFocus(Camera* cam);
     52            Camera* getActiveCamera() const;
    5953
    60     private:
    61       CameraHandler();
    62       //bool isInVector(Camera* cam);
     54            void requestFocus(Camera* camera);
     55            void releaseFocus(Camera* camera);
    6356
    64     private:
    65       static CameraHandler* singletonRef;
    66       Ogre::Camera* cam_;
    67       std::list<Camera*> focusList_;
    68       //std::vector<Camera*> cams_;
     57        private:
     58            CameraHandler();
     59            ~CameraHandler() {}
    6960
    70   };
     61            std::list<Camera*> cameraList_;
     62    };
    7163}
    7264
  • code/branches/objecthierarchy/src/orxonox/objects/DistanceTrigger.cc

    r1969 r2019  
    3737  CreateFactory(DistanceTrigger);
    3838
    39   DistanceTrigger::DistanceTrigger()
     39  DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator)
    4040  {
    4141    RegisterObject(DistanceTrigger);
  • code/branches/objecthierarchy/src/orxonox/objects/DistanceTrigger.h

    r1969 r2019  
    4444  {
    4545    public:
    46       DistanceTrigger();
     46      DistanceTrigger(BaseObject* creator);
    4747      ~DistanceTrigger();
    4848      void addTarget(Ogre::Node* targetNode);
  • code/branches/objecthierarchy/src/orxonox/objects/ParticleSpawner.cc

    r1755 r2019  
    8080        this->setPosition(this->getNode()->getParent()->getPosition());
    8181        this->getNode()->getParent()->removeChild(this->getNode());
    82         GraphicsEngine::getInstance().getLevelSceneManager()->getRootSceneNode()->addChild(this->getNode());
     82        this->detachFromParent();
    8383        if (this->particle_)
    8484            this->particle_->setEnabled(false);
  • code/branches/objecthierarchy/src/orxonox/objects/Script.cc

    r1961 r2019  
    3939  CreateFactory(Script);
    4040
    41   Script::Script()
     41  Script::Script(BaseObject* creator) : BaseObject(creator)
    4242  {
    4343    RegisterObject(Script);
  • code/branches/objecthierarchy/src/orxonox/objects/Script.h

    r1959 r2019  
    3939  {
    4040    public:
    41       Script();
     41      Script(BaseObject* creator);
    4242      ~Script();
    4343      void XMLPort(Element& xmlelement, XMLPort::Mode mode);
  • code/branches/objecthierarchy/src/orxonox/objects/Test.cc

    r1990 r2019  
    3636        CreateFactory ( Test );
    3737
    38         Test::Test()
     38        Test::Test(BaseObject* creator) : BaseObject(creator)
    3939        {
    4040                RegisterObject ( Test );
  • code/branches/objecthierarchy/src/orxonox/objects/Test.h

    r1990 r2019  
    3939  {
    4040    public:
    41       Test();
     41      Test(BaseObject* creator);
    4242      virtual ~Test();
    4343
    4444      void setConfigValues();
    4545      void registerVariables();
    46      
     46
    4747      void setV1(unsigned int value){ v1 = value; }
    4848      void setV2(unsigned int value){ v2 = value; }
    4949      void setV3(unsigned int value){ v3 = value; }
    50      
     50
    5151      void checkV1();
    5252      void checkV2();
  • code/branches/objecthierarchy/src/orxonox/objects/Trigger.cc

    r1969 r2019  
    3535#include "core/ConsoleCommand.h"
    3636#include "core/XMLPort.h"
     37#include "objects/Scene.h"
    3738
    3839namespace orxonox
     
    4344  CreateFactory(Trigger);
    4445
    45   Trigger::Trigger()
     46  Trigger::Trigger(BaseObject* creator) : PositionableEntity(creator)
    4647  {
    4748    RegisterObject(Trigger);
     
    5758    latestState_ = 0x0;
    5859
    59     debugBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
    60     debugBillboard_.setVisible(false);
     60    if (this->getScene() && this->getScene()->getSceneManager())
     61    {
     62      debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
     63      debugBillboard_.setVisible(false);
     64    }
    6165
    6266    this->getNode()->attachObject(debugBillboard_.getBillboardSet());
  • code/branches/objecthierarchy/src/orxonox/objects/Trigger.h

    r2013 r2019  
    5050  {
    5151    public:
    52       Trigger();
     52      Trigger(BaseObject* creator);
    5353      ~Trigger();
    5454
  • code/branches/objecthierarchy/src/orxonox/objects/controllers/Controller.cc

    r1993 r2019  
    3636    CreateUnloadableFactory(Controller);
    3737
    38     Controller::Controller()
     38    Controller::Controller(BaseObject* creator) : BaseObject(creator)
    3939    {
    4040        RegisterObject(Controller);
    4141
    4242        this->player_ = 0;
    43         this->pawn_ = 0;
     43        this->controllableEntity_ = 0;
    4444    }
    4545
  • code/branches/objecthierarchy/src/orxonox/objects/controllers/Controller.h

    r1993 r2019  
    3939    {
    4040        public:
    41             Controller();
     41            Controller(BaseObject* creator);
    4242            virtual ~Controller();
    4343
     
    4747                { return this->player_; }
    4848
    49             inline void setPawn(ControllableEntity* pawn)
    50                 { this->pawn_ = pawn; }
    51             inline ControllableEntity* getPawn() const
    52                 { return this->pawn_; }
     49            virtual inline void setControllableEntity(ControllableEntity* entity)
     50                { this->controllableEntity_ = entity; }
     51            virtual inline ControllableEntity* getControllableEntity() const
     52                { return this->controllableEntity_; }
    5353
    5454        protected:
    5555            PlayerInfo* player_;
    56             ControllableEntity* pawn_;
     56            ControllableEntity* controllableEntity_;
    5757    };
    5858}
  • code/branches/objecthierarchy/src/orxonox/objects/controllers/HumanController.cc

    r2001 r2019  
    5252    HumanController* HumanController::localController_s = 0;
    5353
    54     HumanController::HumanController()
     54    HumanController::HumanController(BaseObject* creator) : Controller(creator)
    5555    {
    5656        RegisterObject(HumanController);
     
    6666    void HumanController::moveFrontBack(const Vector2& value)
    6767    {
    68         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    69             HumanController::localController_s->pawn_->moveFrontBack(value.y);
     68        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     69            HumanController::localController_s->controllableEntity_->moveFrontBack(value.y);
    7070    }
    7171
    7272    void HumanController::moveRightLeft(const Vector2& value)
    7373    {
    74         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    75             HumanController::localController_s->pawn_->moveRightLeft(value.y);
     74        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     75            HumanController::localController_s->controllableEntity_->moveRightLeft(value.y);
    7676    }
    7777
    7878    void HumanController::moveUpDown(const Vector2& value)
    7979    {
    80         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    81             HumanController::localController_s->pawn_->moveUpDown(value.y);
     80        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     81            HumanController::localController_s->controllableEntity_->moveUpDown(value.y);
    8282    }
    8383
    8484    void HumanController::rotateYaw(const Vector2& value)
    8585    {
    86         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    87             HumanController::localController_s->pawn_->rotateYaw(value.y);
     86        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     87            HumanController::localController_s->controllableEntity_->rotateYaw(value.y);
    8888    }
    8989
    9090    void HumanController::rotatePitch(const Vector2& value)
    9191    {
    92         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    93             HumanController::localController_s->pawn_->rotatePitch(value.y);
     92        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     93            HumanController::localController_s->controllableEntity_->rotatePitch(value.y);
    9494    }
    9595
    9696    void HumanController::rotateRoll(const Vector2& value)
    9797    {
    98         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    99             HumanController::localController_s->pawn_->rotateRoll(value.y);
     98        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     99            HumanController::localController_s->controllableEntity_->rotateRoll(value.y);
    100100    }
    101101
    102102    void HumanController::fire()
    103103    {
    104         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    105             HumanController::localController_s->pawn_->fire();
     104        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     105            HumanController::localController_s->controllableEntity_->fire();
    106106    }
    107107
    108108    void HumanController::altFire()
    109109    {
    110         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    111             HumanController::localController_s->pawn_->altFire();
     110        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     111            HumanController::localController_s->controllableEntity_->altFire();
    112112    }
    113113
    114114    void HumanController::greet()
    115115    {
    116         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    117             HumanController::localController_s->pawn_->greet();
     116        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     117            HumanController::localController_s->controllableEntity_->greet();
    118118    }
    119119
    120120    void HumanController::use()
    121121    {
    122         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    123             HumanController::localController_s->pawn_->use();
     122        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     123            HumanController::localController_s->controllableEntity_->use();
    124124    }
    125125
    126126    void HumanController::switchCamera()
    127127    {
    128         if (HumanController::localController_s && HumanController::localController_s->pawn_)
    129             HumanController::localController_s->pawn_->switchCamera();
     128        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
     129            HumanController::localController_s->controllableEntity_->switchCamera();
    130130    }
    131131}
  • code/branches/objecthierarchy/src/orxonox/objects/controllers/HumanController.h

    r2001 r2019  
    4040    {
    4141        public:
    42             HumanController();
     42            HumanController(BaseObject* creator);
    4343            virtual ~HumanController();
    4444
  • code/branches/objecthierarchy/src/orxonox/objects/gametypes/Gametype.cc

    r2006 r2019  
    3030#include "Gametype.h"
    3131
     32#include <cstdlib>
     33#include <ctime>
     34
    3235#include "core/CoreIncludes.h"
    3336#include "objects/infos/PlayerInfo.h"
    3437#include "objects/worldentities/pawns/Spectator.h"
     38#include "objects/worldentities/SpawnPoint.h"
    3539
    3640#include "network/Host.h"
     
    4044    CreateUnloadableFactory(Gametype);
    4145
    42     Gametype::Gametype()
     46    Gametype::Gametype(BaseObject* creator) : BaseObject(creator)
    4347    {
    4448        RegisterObject(Gametype);
    4549
    4650        this->defaultPawn_ = Class(Spectator);
     51        this->bStarted_ = false;
     52        this->bEnded_ = false;
     53        this->bAutoStart_ = false;
    4754
    4855        COUT(0) << "created Gametype" << std::endl;
    4956    }
    5057
    51     void Gametype::addPlayer(PlayerInfo* player)
     58    void Gametype::tick(float dt)
     59    {
     60        if (!this->bStarted_)
     61            this->checkStart();
     62
     63        this->assignDefaultPawnsIfNeeded();
     64    }
     65
     66    void Gametype::start()
     67    {
     68        COUT(0) << "game started" << std::endl;
     69        this->bStarted_ = true;
     70
     71        for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     72            this->spawnPlayer(*it);
     73    }
     74
     75    void Gametype::end()
     76    {
     77        COUT(0) << "game ended" << std::endl;
     78        this->bEnded_ = true;
     79    }
     80
     81    void Gametype::playerEntered(PlayerInfo* player)
    5282    {
    5383        this->players_.insert(player);
    54         this->playerJoined(player);
    55 
    56         ControllableEntity* newpawn = this->defaultPawn_.fabricate();
    57         player->startControl(newpawn);
    58     }
    59 
    60     void Gametype::removePlayer(PlayerInfo* player)
    61     {
    62         if (this->players_.find(player) != this->players_.end())
    63         {
    64             player->stopControl();
    65             this->players_.erase(player);
    66             this->playerLeft(player);
    67         }
    68     }
    69 
    70     void Gametype::playerJoined(PlayerInfo* player)
    71     {
     84
    7285        std::string message = player->getName() + " entered the game";
    7386        COUT(0) << message << std::endl;
     
    7790    void Gametype::playerLeft(PlayerInfo* player)
    7891    {
    79         std::string message = player->getName() + " left the game";
    80         COUT(0) << message << std::endl;
    81         network::Host::Broadcast(message);
     92        std::set<PlayerInfo*>::iterator it = this->players_.find(player);
     93        if (it != this->players_.end())
     94        {
     95            this->players_.erase(it);
     96
     97            std::string message = player->getName() + " left the game";
     98            COUT(0) << message << std::endl;
     99            network::Host::Broadcast(message);
     100        }
     101    }
     102
     103    void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype)
     104    {
     105    }
     106
     107    void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype)
     108    {
    82109    }
    83110
     
    94121        }
    95122    }
     123
     124    void Gametype::playerSpawned(PlayerInfo* player)
     125    {
     126    }
     127
     128    void Gametype::playerDied(PlayerInfo* player)
     129    {
     130    }
     131
     132    void Gametype::playerScored(PlayerInfo* player)
     133    {
     134    }
     135
     136    SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const
     137    {
     138        if (this->spawnpoints_.size() > 0)
     139        {
     140            srand(time(0));
     141            rnd();
     142
     143            unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size());
     144            unsigned int index = 0;
     145            for (std::set<SpawnPoint*>::iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it)
     146            {
     147                if (index == randomspawn)
     148                    return (*it);
     149
     150                ++index;
     151            }
     152        }
     153        return 0;
     154    }
     155
     156    void Gametype::assignDefaultPawnsIfNeeded() const
     157    {
     158        for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     159        {
     160            if (!(*it)->getControllableEntity() && (!(*it)->isReadyToSpawn() || !this->bStarted_))
     161            {
     162                SpawnPoint* spawn = this->getBestSpawnPoint(*it);
     163                if (spawn)
     164                {
     165                    // force spawn at spawnpoint with default pawn
     166                    ControllableEntity* newpawn = this->defaultPawn_.fabricate(spawn);
     167                    spawn->spawn(newpawn);
     168                    (*it)->startControl(newpawn);
     169                }
     170                else
     171                {
     172                    COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
     173                    abort();
     174                }
     175            }
     176        }
     177    }
     178
     179    void Gametype::checkStart()
     180    {
     181        if (!this->bStarted_)
     182        {
     183            if (this->players_.size() > 0)
     184            {
     185                if (this->bAutoStart_)
     186                {
     187                    this->start();
     188                }
     189                else
     190                {
     191                    bool allplayersready = true;
     192                    for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     193                    {
     194                        if (!(*it)->isReadyToSpawn())
     195                            allplayersready = false;
     196                    }
     197                    if (allplayersready)
     198                        this->start();
     199                }
     200            }
     201        }
     202    }
     203
     204    void Gametype::spawnPlayer(PlayerInfo* player)
     205    {
     206        if (player->isReadyToSpawn())
     207        {
     208            SpawnPoint* spawnpoint = this->getBestSpawnPoint(player);
     209            if (spawnpoint)
     210            {
     211                player->startControl(spawnpoint->spawn());
     212            }
     213            else
     214            {
     215                COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
     216                abort();
     217            }
     218        }
     219    }
    96220}
  • code/branches/objecthierarchy/src/orxonox/objects/gametypes/Gametype.h

    r2006 r2019  
    3737#include "core/Identifier.h"
    3838#include "objects/worldentities/ControllableEntity.h"
     39#include "objects/Tickable.h"
    3940
    4041namespace orxonox
    4142{
    42     class _OrxonoxExport Gametype : public BaseObject
     43    class _OrxonoxExport Gametype : public BaseObject, public Tickable
    4344    {
    4445        friend class PlayerInfo;
    4546
    4647        public:
    47             Gametype();
     48            Gametype(BaseObject* creator);
    4849            virtual ~Gametype() {}
     50
     51            virtual void tick(float dt);
     52
     53            virtual void start();
     54            virtual void end();
     55            virtual void playerEntered(PlayerInfo* player);
     56            virtual void playerLeft(PlayerInfo* player);
     57            virtual void playerSwitched(PlayerInfo* player, Gametype* newgametype);
     58            virtual void playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype);
     59            virtual void playerChangedName(PlayerInfo* player);
     60            virtual void playerSpawned(PlayerInfo* player);
     61            virtual void playerDied(PlayerInfo* player);
     62            virtual void playerScored(PlayerInfo* player);
    4963
    5064            inline const std::set<PlayerInfo*>& getPlayers() const
    5165                { return this->players_; }
    5266
    53         protected:
    54             virtual void playerJoined(PlayerInfo* player);
    55             virtual void playerLeft(PlayerInfo* player);
    56 
    57             virtual void playerChangedName(PlayerInfo* player);
     67            inline void registerSpawnPoint(SpawnPoint* spawnpoint)
     68                { this->spawnpoints_.insert(spawnpoint); }
    5869
    5970        private:
     71            virtual SpawnPoint* getBestSpawnPoint(PlayerInfo* player) const;
     72
    6073            void addPlayer(PlayerInfo* player);
    6174            void removePlayer(PlayerInfo* player);
    6275
     76            void assignDefaultPawnsIfNeeded() const;
     77            void checkStart();
     78            void spawnPlayer(PlayerInfo* player);
     79
     80            bool bStarted_;
     81            bool bEnded_;
     82            bool bAutoStart_;
    6383            std::set<PlayerInfo*> players_;
     84            std::set<SpawnPoint*> spawnpoints_;
    6485            SubclassIdentifier<ControllableEntity> defaultPawn_;
    6586    };
  • code/branches/objecthierarchy/src/orxonox/objects/infos/Info.cc

    r1947 r2019  
    3434namespace orxonox
    3535{
    36     Info::Info()
     36    Info::Info(BaseObject* creator) : BaseObject(creator)
    3737    {
    3838        RegisterObject(Info);
  • code/branches/objecthierarchy/src/orxonox/objects/infos/Info.h

    r1940 r2019  
    4040    {
    4141        public:
    42             Info();
     42            Info(BaseObject* creator);
    4343            virtual ~Info() {}
    4444    };
  • code/branches/objecthierarchy/src/orxonox/objects/infos/Level.cc

    r2012 r2019  
    3030#include "Level.h"
    3131
    32 #include <OgreSceneManager.h>
    33 #include <OgreLight.h>
    34 
    3532#include "core/CoreIncludes.h"
    3633#include "core/XMLPort.h"
    37 #include "core/Core.h"
    38 #include "core/ConsoleCommand.h"
    3934#include "core/Loader.h"
    4035#include "core/XMLFile.h"
    4136#include "core/Template.h"
    4237
    43 #include "GraphicsEngine.h"
    4438#include "Settings.h"
     39#include "LevelManager.h"
    4540#include "PlayerInfo.h"
     41#include "objects/gametypes/Gametype.h"
    4642
    4743#include "util/Math.h"
     
    4945namespace orxonox
    5046{
    51     SetConsoleCommand(Level, listPlayers, true);
    52 
    5347    CreateFactory(Level);
    5448
    55     Level::Level()
     49    Level::Level(BaseObject* creator) : Info(creator)
    5650    {
    5751        RegisterObject(Level);
    5852
    59         this->rootGametype_ = 0;
    6053        this->registerVariables();
    61 
    62         // test test test
    63         {
    64             Ogre::Light* light;
    65             light = GraphicsEngine::getInstance().getLevelSceneManager()->createLight("Light0");
    66             light->setType(Ogre::Light::LT_DIRECTIONAL);
    67             light->setDiffuseColour(ColourValue(1.0, 0.9, 0.6, 1.0));
    68             light->setSpecularColour(ColourValue(1.0, 0.9, 0.6, 1.0));
    69             light->setDirection(1, -0.2, 0.2);
    70         }
    71         // test test test
     54        this->xmlfilename_ = this->getFilename();
    7255
    7356        COUT(0) << "created Level" << std::endl;
    7457    }
    7558
    76     Level* Level::getActiveLevel()
     59    Level::~Level()
    7760    {
    78         for (ObjectList<Level>::iterator it = ObjectList<Level>::begin(); it != ObjectList<Level>::end(); ++it)
    79             if (it->isActive())
    80                 return (*it);
     61        if (this->isInitialized())
     62        {
     63            LevelManager::getInstance().releaseActivity(this);
    8164
    82         return 0;
    83     }
    84 
    85     PlayerInfo* Level::getClient(unsigned int clientID)
    86     {
    87         Level* level = Level::getActiveLevel();
    88 
    89         if (level)
    90         {
    91             std::map<unsigned int, PlayerInfo*>::const_iterator it = level->clients_.find(clientID);
    92             if (it != level->clients_.end())
    93                 return it->second;
     65            if (this->xmlfile_)
     66                Loader::unload(this->xmlfile_);
    9467        }
    95         else
    96         {
    97             for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it)
    98                 if (it->getClientID() == clientID)
    99                     return (*it);
    100         }
    101         return 0;
    102     }
    103 
    104     void Level::listPlayers()
    105     {
    106         Level* level = Level::getActiveLevel();
    107 
    108         if (level->getGametype())
    109         {
    110             for (std::set<PlayerInfo*>::const_iterator it = level->getGametype()->getPlayers().begin(); it != level->getGametype()->getPlayers().end(); ++it)
    111                 COUT(0) << "ID: " << (*it)->getClientID() << ", Name: " << (*it)->getName() << std::endl;
    112         }
    113         else
    114         {
    115             for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it)
    116                 COUT(0) << "ID: " << (*it)->getClientID() << ", Name: " << (*it)->getName() << std::endl;
    117         }
    118     }
    119 
    120     void Level::clientConnected(unsigned int clientID)
    121     {
    122         COUT(0) << "client connected" << std::endl;
    123 
    124         // create new PlayerInfo instance
    125         PlayerInfo* player = new PlayerInfo();
    126         player->setGametype(this->getGametype());
    127         player->setClientID(clientID);
    128 
    129         // add to clients-map
    130         assert(!this->clients_[clientID]);
    131         this->clients_[clientID] = player;
    132     }
    133 
    134     void Level::clientDisconnected(unsigned int clientID)
    135     {
    136         COUT(0) << "client disconnected" << std::endl;
    137 
    138         // remove from clients-map
    139         PlayerInfo* player = this->clients_[clientID];
    140         this->clients_.erase(clientID);
    141 
    142         // delete PlayerInfo instance
    143         delete player;
    14468    }
    14569
     
    15074        XMLPortParam(Level, "description", setDescription, getDescription, xmlelement, mode);
    15175        XMLPortParam(Level, "gametype", setGametypeString, getGametypeString, xmlelement, mode).defaultValues("Gametype");
    152         XMLPortParam(Level, "skybox", setSkybox, getSkybox, xmlelement, mode);
    153         XMLPortParam(Level, "ambientlight", setAmbientLight, getAmbientLight, xmlelement, mode).defaultValues(ColourValue(0.2, 0.2, 0.2, 1));
    15476
    155         this->xmlfile_ = this->getFilename();
     77        XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
    15678    }
    15779
    15880    void Level::registerVariables()
    15981    {
    160         REGISTERSTRING(this->xmlfile_,    network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile));
     82        REGISTERSTRING(this->xmlfilename_, network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applyXMLFile));
    16183        REGISTERSTRING(this->name_,        network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::changedName));
    16284        REGISTERSTRING(this->description_, network::direction::toclient);
    163         REGISTERSTRING(this->skybox_,      network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applySkybox));
    164         REGISTERDATA(this->ambientLight_,  network::direction::toclient, new network::NetworkCallback<Level>(this, &Level::networkcallback_applyAmbientLight));
    16585    }
    16686
    16787    void Level::networkcallback_applyXMLFile()
    16888    {
    169         COUT(0) << "Loading level \"" << this->xmlfile_ << "\"..." << std::endl;
     89        COUT(0) << "Loading level \"" << this->xmlfilename_ << "\"..." << std::endl;
    17090
    17191        ClassTreeMask mask;
     
    17393        mask.include(Class(Template));
    17494
    175         XMLFile* file = new XMLFile(Settings::getDataPath() + this->xmlfile_, mask);
     95        this->xmlfile_ = new XMLFile(Settings::getDataPath() + this->xmlfilename_, mask);
    17696
    177         Loader::open(file);
    178     }
    179 
    180     void Level::setSkybox(const std::string& skybox)
    181     {
    182         if (Core::showsGraphics())
    183             if (GraphicsEngine::getInstance().getLevelSceneManager())
    184                 GraphicsEngine::getInstance().getLevelSceneManager()->setSkyBox(true, skybox);
    185 
    186         this->skybox_ = skybox;
    187     }
    188 
    189     void Level::setAmbientLight(const ColourValue& colour)
    190     {
    191         if (Core::showsGraphics())
    192             GraphicsEngine::getInstance().getLevelSceneManager()->setAmbientLight(colour);
    193 
    194         this->ambientLight_ = colour;
     97        Loader::open(this->xmlfile_);
    19598    }
    19699
     
    198101    {
    199102        Identifier* identifier = ClassByString(gametype);
    200         if (identifier)
     103        if (identifier && identifier->isA(Class(Gametype)))
    201104        {
    202105            this->gametype_ = gametype;
    203             this->gametypeIdentifier_ = identifier;
    204             this->rootGametype_ = this->gametypeIdentifier_.fabricate();
    205             this->getConnectedClients();
     106
     107            Gametype* rootgametype = dynamic_cast<Gametype*>(identifier->fabricate(this));
     108            this->setGametype(rootgametype);
     109
     110            for (std::list<BaseObject*>::iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
     111                (*it)->setGametype(rootgametype);
     112
     113            LevelManager::getInstance().requestActivity(this);
    206114        }
    207115    }
     116
     117
     118    void Level::addObject(BaseObject* object)
     119    {
     120        this->objects_.push_back(object);
     121        object->setGametype(this->getGametype());
     122    }
     123
     124    BaseObject* Level::getObject(unsigned int index) const
     125    {
     126        unsigned int i = 0;
     127        for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
     128        {
     129            if (i == index)
     130                return (*it);
     131            ++i;
     132        }
     133        return 0;
     134    }
     135
     136    void Level::playerEntered(PlayerInfo* player)
     137    {
     138        COUT(0) << "player entered level" << std::endl;
     139        player->setGametype(this->getGametype());
     140    }
     141
     142    void Level::playerLeft(PlayerInfo* player)
     143    {
     144        player->setGametype(0);
     145    }
    208146}
  • code/branches/objecthierarchy/src/orxonox/objects/infos/Level.h

    r2012 r2019  
    3333
    3434#include "Info.h"
    35 #include "util/Math.h"
    36 #include "core/Identifier.h"
    37 
    38 #include "objects/gametypes/Gametype.h"
    39 #include "network/ClientConnectionListener.h"
    4035
    4136namespace orxonox
    4237{
    43     class _OrxonoxExport Level : public Info, public network::ClientConnectionListener
     38    class _OrxonoxExport Level : public Info
    4439    {
    4540        public:
    46             Level();
    47             virtual ~Level() {}
     41            Level(BaseObject* creator);
     42            virtual ~Level();
    4843
    4944            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    5045            void registerVariables();
    51 
    52             inline const std::map<unsigned int, PlayerInfo*>& getClients() const
    53                 { return this->clients_; }
    5446
    5547            inline void setDescription(const std::string& description)
     
    5850                { return this->description_; }
    5951
    60             void setSkybox(const std::string& skybox);
    61             inline const std::string& getSkybox() const
    62                 { return this->skybox_; }
     52            void playerEntered(PlayerInfo* player);
     53            void playerLeft(PlayerInfo* player);
    6354
    64             void setAmbientLight(const ColourValue& colour);
    65             inline const ColourValue& getAmbientLight() const
    66                 { return this->ambientLight_; }
     55        private:
     56            void addObject(BaseObject* object);
     57            BaseObject* getObject(unsigned int index) const;
    6758
    6859            void setGametypeString(const std::string& gametype);
    6960            inline const std::string& getGametypeString() const
    7061                { return this->gametype_; }
    71             inline Gametype* getGametype() const
    72                 { return this->rootGametype_; }
    73 
    74             static Level* getActiveLevel();
    75             static void listPlayers();
    76             static PlayerInfo* getClient(unsigned int clientID);
    77 
    78         private:
    79             virtual void clientConnected(unsigned int clientID);
    80             virtual void clientDisconnected(unsigned int clientID);
    8162
    8263            void networkcallback_applyXMLFile();
    8364
    84             void networkcallback_applySkybox()
    85                 { this->setSkybox(this->skybox_); }
    86             void networkcallback_applyAmbientLight()
    87                 { this->setAmbientLight(this->ambientLight_); }
    88 
    89             std::map<unsigned int, PlayerInfo*> clients_;
    90             std::string description_;
    91             std::string skybox_;
    92             ColourValue ambientLight_;
    93             std::string gametype_;
    94             SubclassIdentifier<Gametype> gametypeIdentifier_;
    95             Gametype* rootGametype_;
    96             std::string xmlfile_;
     65            std::string            description_;
     66            std::string            gametype_;
     67            std::string            xmlfilename_;
     68            XMLFile*               xmlfile_;
     69            std::list<BaseObject*> objects_;
    9770    };
    9871}
  • code/branches/objecthierarchy/src/orxonox/objects/infos/PlayerInfo.cc

    r2006 r2019  
    2727 */
    2828
     29#include <cassert>
     30
    2931#include "OrxonoxStableHeaders.h"
    3032#include "PlayerInfo.h"
    3133
    32 #include <OgreSceneManager.h>
    33 
    3434#include "core/CoreIncludes.h"
    35 #include "core/ConfigValueIncludes.h"
    36 #include "core/XMLPort.h"
    37 #include "core/Core.h"
    38 
    39 #include "network/Host.h"
    4035#include "network/ClientInformation.h"
    41 
    42 #include "GraphicsEngine.h"
    4336#include "objects/gametypes/Gametype.h"
    44 #include "objects/worldentities/ControllableEntity.h"
    45 #include "objects/controllers/HumanController.h"
    4637
    4738namespace orxonox
    4839{
    49     CreateUnloadableFactory(PlayerInfo);
    50 
    51     PlayerInfo::PlayerInfo()
     40    PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator)
    5241    {
    5342        RegisterObject(PlayerInfo);
    5443
    55         this->ping_ = -1;
    5644        this->clientID_ = network::CLIENTID_UNKNOWN;
     45        this->bHumanPlayer_ = false;
    5746        this->bLocalPlayer_ = false;
    58         this->bHumanPlayer_ = false;
    59         this->bFinishedSetup_ = false;
    60         this->gametype_ = 0;
     47        this->bReadyToSpawn_ = false;
     48        this->controller_ = 0;
     49        this->controllableEntity_ = 0;
     50        this->controllableEntityID_ = network::CLIENTID_UNKNOWN;
    6151
    62         this->pawn_ = 0;
    63         this->pawnID_ = network::OBJECTID_UNKNOWN;
    64         this->controller_ = 0;
    65         this->setDefaultController(Class(HumanController));
    66 
    67         this->setConfigValues();
    6852        this->registerVariables();
    6953    }
     
    7357        if (this->isInitialized())
    7458        {
    75             if (this->gametype_)
    76                 this->gametype_->removePlayer(this);
     59            this->stopControl(this->controllableEntity_);
    7760
    7861            if (this->controller_)
     62            {
    7963                delete this->controller_;
    80 
    81             if (this->pawn_)
    82                 this->pawn_->removePlayer();
     64                this->controller_ = 0;
     65            }
    8366        }
    8467    }
    8568
    86     void PlayerInfo::setConfigValues()
     69    void PlayerInfo::registerVariables()
    8770    {
    88         SetConfigValue(nick_, "Player").callback(this, &PlayerInfo::checkNick);
    89     }
    90 
    91     void PlayerInfo::checkNick()
    92     {
    93         if (this->bLocalPlayer_)
    94         {
    95             this->playerName_ = this->nick_;
    96 
    97             if (Core::isMaster())
    98                 this->setName(this->playerName_);
    99         }
     71        REGISTERSTRING(this->name_,                 network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
     72        REGISTERDATA  (this->controllableEntityID_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID));
    10073    }
    10174
    10275    void PlayerInfo::changedName()
    10376    {
    104         if (this->gametype_)
    105             this->gametype_->playerChangedName(this);
     77        if (this->isReady() && this->getGametype())
     78            this->getGametype()->playerChangedName(this);
    10679    }
    10780
    108     void PlayerInfo::registerVariables()
     81    void PlayerInfo::changedGametype()
    10982    {
    110         REGISTERSTRING(name_,         network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
    111         REGISTERSTRING(playerName_,   network::direction::toserver, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::clientChangedName));
    112         REGISTERDATA(clientID_,       network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::checkClientID));
    113         REGISTERDATA(ping_,           network::direction::toclient);
    114         REGISTERDATA(bHumanPlayer_,   network::direction::toclient);
    115         REGISTERDATA(pawnID_,         network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::updatePawn));
    116         REGISTERDATA(bFinishedSetup_, network::direction::bidirectional, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::finishedSetup));
    117     }
     83        if (this->isReady())
     84        {
     85            if (this->getOldGametype())
     86            {
     87                if (this->getGametype())
     88                    this->getOldGametype()->playerSwitched(this, this->getGametype());
     89                else
     90                    this->getOldGametype()->playerLeft(this);
     91            }
    11892
    119     void PlayerInfo::clientChangedName()
    120     {
    121         this->setName(this->playerName_);
    122     }
    123 
    124     void PlayerInfo::checkClientID()
    125     {
    126         this->bHumanPlayer_ = true;
    127 
    128         if (this->clientID_ == network::Host::getPlayerID())
    129         {
    130             this->takeLocalControl();
    131 
    132             if (Core::isClient())
    133                 this->setObjectMode(network::direction::bidirectional);
    134             else
     93            if (this->getGametype())
    13594            {
    136                 this->clientChangedName();
    137                 this->bFinishedSetup_ = true;
    138                 this->finishedSetup();
     95                if (this->getOldGametype())
     96                    this->getGametype()->playerSwitchedBack(this, this->getOldGametype());
     97                else
     98                    this->getGametype()->playerEntered(this);
    13999            }
    140100        }
    141101    }
    142102
    143     void PlayerInfo::finishedSetup()
     103    void PlayerInfo::createController()
    144104    {
    145         if (Core::isClient())
    146             this->bFinishedSetup_ = true;
    147         else if (this->bFinishedSetup_)
     105        this->controller_ = this->defaultController_.fabricate(this);
     106        assert(this->controller_);
     107        this->controller_->setPlayer(this);
     108        if (this->controllableEntity_)
     109            this->controller_->setControllableEntity(this->controllableEntity_);
     110    }
     111
     112    void PlayerInfo::startControl(ControllableEntity* entity)
     113    {
     114        if (this->controllableEntity_)
     115            this->stopControl(this->controllableEntity_);
     116
     117        this->controllableEntity_ = entity;
     118
     119        if (entity)
    148120        {
    149             if (this->gametype_)
    150                 this->gametype_->addPlayer(this);
     121            this->controllableEntityID_ = entity->getObjectID();
     122            entity->setPlayer(this);
     123        }
     124        else
     125        {
     126            this->controllableEntityID_ = network::OBJECTID_UNKNOWN;
     127        }
     128
     129        if (this->controller_)
     130            this->controller_->setControllableEntity(entity);
     131    }
     132
     133    void PlayerInfo::stopControl(ControllableEntity* entity)
     134    {
     135        if (entity && this->controllableEntity_ == entity)
     136        {
     137            this->controllableEntity_ = 0;
     138            this->controllableEntityID_ = network::OBJECTID_UNKNOWN;
     139
     140            if (this->controller_)
     141                this->controller_->setControllableEntity(0);
     142
     143            entity->removePlayer();
    151144        }
    152145    }
    153146
    154     void PlayerInfo::startControl(ControllableEntity* pawn)
     147    void PlayerInfo::networkcallback_changedcontrollableentityID()
    155148    {
    156         pawn->setPlayer(this);
    157         this->pawn_ = pawn;
    158         this->pawnID_ = pawn->getObjectID();
     149        if (this->controllableEntityID_ != network::OBJECTID_UNKNOWN)
     150        {
     151            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
     152            ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp);
    159153
    160         if (this->controller_)
    161             this->controller_->setPawn(this->pawn_);
    162     }
    163 
    164     void PlayerInfo::stopControl()
    165     {
    166         if (this->pawn_)
    167             this->pawn_->removePlayer();
    168         this->pawn_ = 0;
    169         this->pawnID_ = network::OBJECTID_UNKNOWN;
    170     }
    171 
    172     void PlayerInfo::takeLocalControl()
    173     {
    174         this->bLocalPlayer_ = true;
    175         this->playerName_ = this->nick_;
    176         this->createController();
    177     }
    178 
    179     void PlayerInfo::createController()
    180     {
    181         this->controller_ = this->defaultController_.fabricate();
    182         this->controller_->setPawn(this->pawn_);
    183     }
    184 
    185     void PlayerInfo::updatePawn()
    186     {
    187         this->pawn_ = dynamic_cast<ControllableEntity*>(network::Synchronisable::getSynchronisable(this->pawnID_));
    188         if (this->pawn_ && (this->pawn_->getPlayer() != this))
    189             this->pawn_->setPlayer(this);
     154            this->startControl(entity);
     155        }
     156        else
     157        {
     158            this->stopControl(this->controllableEntity_);
     159        }
    190160    }
    191161}
  • code/branches/objecthierarchy/src/orxonox/objects/infos/PlayerInfo.h

    r2006 r2019  
    4141    {
    4242        public:
    43             PlayerInfo();
     43            PlayerInfo(BaseObject* creator);
    4444            virtual ~PlayerInfo();
    4545
    46             void setConfigValues();
    4746            void registerVariables();
    4847
    4948            virtual void changedName();
    50 
    51             inline void setClientID(unsigned int clientID)
    52                 { this->clientID_ = clientID; this->checkClientID(); }
    53             inline unsigned int getClientID() const
    54                 { return this->clientID_; }
     49            virtual void changedGametype();
    5550
    5651            inline bool isHumanPlayer() const
    5752                { return this->bHumanPlayer_; }
    58 
    5953            inline bool isLocalPlayer() const
    6054                { return this->bLocalPlayer_; }
     55            inline unsigned int getClientID() const
     56                { return this->clientID_; }
     57            inline bool isReadyToSpawn() const
     58                { return this->bReadyToSpawn_; }
    6159
    62             virtual void startControl(ControllableEntity* pawn);
    63             virtual void stopControl();
     60            virtual bool isReady() const = 0;
     61            virtual float getPing() const = 0;
     62            virtual float getPacketLossRatio() const = 0;
    6463
    65             inline ControllableEntity* getPawn() const
    66                 { return this->pawn_; }
    67 /*
    68             inline void setController(Controller* controller)
    69                 { this->controller_ = controller; }
    70             inline Controller* getController() const
    71                 { return this->controller_; }
    72 */
    73             inline void setGametype(Gametype* gametype)
    74                 { this->gametype_ = gametype; }
    75             inline Gametype* getGametype() const
    76                 { return this->gametype_; }
     64            inline void setReadyToSpawn(bool bReady)
     65                { this->bReadyToSpawn_ = bReady; }
     66
     67            void startControl(ControllableEntity* entity);
     68            void stopControl(ControllableEntity* entity);
     69
     70            inline ControllableEntity* getControllableEntity() const
     71                { return this->controllableEntity_; }
    7772
    7873        protected:
    79             inline void setDefaultController(Identifier* identifier)
    80                 { this->defaultController_ = identifier; }
     74            void createController();
     75            void networkcallback_changedcontrollableentityID();
    8176
    82         private:
    83             virtual void createController();
    84             virtual void takeLocalControl();
    85 
    86             void checkClientID();
    87             void finishedSetup();
    88             void checkNick();
    89             void clientChangedName();
    90             void updatePawn();
    91 
     77            bool bHumanPlayer_;
     78            bool bLocalPlayer_;
     79            bool bReadyToSpawn_;
     80            SubclassIdentifier<Controller> defaultController_;
     81            Controller* controller_;
     82            ControllableEntity* controllableEntity_;
     83            unsigned int controllableEntityID_;
    9284            unsigned int clientID_;
    93             float ping_;
    94             bool bLocalPlayer_;
    95             bool bHumanPlayer_;
    96             bool bFinishedSetup_;
    97 
    98             std::string playerName_;
    99             std::string nick_;
    100 
    101             ControllableEntity* pawn_;
    102             unsigned int pawnID_;
    103             Controller* controller_;
    104             SubclassIdentifier<Controller> defaultController_;
    105             Gametype* gametype_;
    10685    };
    10786}
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/ControllableEntity.cc

    r2006 r2019  
    4343    CreateFactory(ControllableEntity);
    4444
    45     ControllableEntity::ControllableEntity()
     45    ControllableEntity::ControllableEntity(BaseObject* creator) : WorldEntity(creator)
    4646    {
    4747        RegisterObject(ControllableEntity);
     
    7171    ControllableEntity::~ControllableEntity()
    7272    {
    73         if (this->isInitialized() && this->bControlled_)
    74             this->stopLocalControl();
     73        if (this->isInitialized())
     74        {
     75            if (this->bControlled_)
     76                this->stopLocalControl();
     77
     78            if (this->hud_)
     79                delete this->hud_;
     80
     81            if (this->camera_)
     82                delete this->camera_;
     83        }
    7584    }
    7685
     
    92101        this->player_ = player;
    93102        this->playerID_ = player->getObjectID();
    94         this->bControlled_ = player->isLocalPlayer();
     103        this->bControlled_ = (player->isLocalPlayer() && player->isHumanPlayer());
    95104
    96105        if (this->bControlled_)
    97106        {
    98107            this->startLocalControl();
    99             this->setObjectMode(network::direction::bidirectional);
     108
     109            if (!Core::isMaster())
     110                this->setObjectMode(network::direction::bidirectional);
    100111        }
    101112    }
     
    115126    }
    116127
    117     void ControllableEntity::updatePlayer()
    118     {
     128    void ControllableEntity::networkcallback_changedplayerID()
     129    {
     130        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
    119131        if (this->playerID_ != network::OBJECTID_UNKNOWN)
    120132        {
    121133            this->player_ = dynamic_cast<PlayerInfo*>(network::Synchronisable::getSynchronisable(this->playerID_));
    122             if (this->player_ && (this->player_->getPawn() != this))
     134            if (this->player_ && (this->player_->getControllableEntity() != this))
    123135                this->player_->startControl(this);
    124136        }
     
    128140    {
    129141        std::cout << this->getObjectID() << " ###### start local control" << std::endl;
    130         this->camera_ = new Camera();
     142        this->camera_ = new Camera(this);
    131143        this->camera_->requestFocus();
    132144        this->attach(this->camera_);
     
    134146        if (this->hudtemplate_ != "")
    135147        {
    136             this->hud_ = new OverlayGroup();
     148            this->hud_ = new OverlayGroup(this);
    137149            this->hud_->addTemplate(this->hudtemplate_);
    138150        }
     
    184196        REGISTERDATA(this->client_overwrite_,   network::direction::toserver);
    185197
    186         REGISTERDATA(this->playerID_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::updatePlayer));
     198        REGISTERDATA(this->playerID_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
    187199    }
    188200
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/ControllableEntity.h

    r2006 r2019  
    4040    {
    4141        public:
    42             ControllableEntity();
     42            ControllableEntity(BaseObject* creator);
    4343            virtual ~ControllableEntity();
    4444
     
    5656            inline bool getDestroyWhenPlayerLeft() const
    5757                { return this->bDestroyWhenPlayerLeft_; }
    58 
    59             virtual void startLocalControl();
    60             virtual void stopLocalControl();
    6158
    6259            virtual void moveFrontBack(float value) {}
     
    8279                { return this->hudtemplate_; }
    8380
    84         protected:
    8581            using WorldEntity::setPosition;
    8682            using WorldEntity::translate;
     
    112108                { this->acceleration_.x = x; this->acceleration_.y = y; this->acceleration_.z = z; }
    113109
     110        protected:
     111            virtual void startLocalControl();
     112            virtual void stopLocalControl();
     113
    114114            inline void setHudTemplate(const std::string& name)
    115115                { this->hudtemplate_ = name; }
     
    130130            void processClientOrientation();
    131131
    132             void updatePlayer();
     132            void networkcallback_changedplayerID();
    133133
    134134            unsigned int server_overwrite_;
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/Model.cc

    r2006 r2019  
    3232#include "core/CoreIncludes.h"
    3333#include "core/XMLPort.h"
     34#include "objects/Scene.h"
    3435
    3536namespace orxonox
     
    3738    CreateFactory(Model);
    3839
    39     Model::Model()
     40    Model::Model(BaseObject* creator) : PositionableEntity(creator)
    4041    {
    4142        RegisterObject(Model);
     
    6970            this->getNode()->detachObject(this->mesh_.getEntity());
    7071
    71         this->mesh_.setMeshSource(this->meshSrc_);
     72        this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
    7273
    7374        if (this->mesh_.getEntity())
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/Model.h

    r2006 r2019  
    3939    {
    4040        public:
    41             Model();
     41            Model(BaseObject* creator);
    4242            virtual ~Model();
    4343
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/MovableEntity.cc

    r1994 r2019  
    4141    CreateFactory(MovableEntity);
    4242
    43     MovableEntity::MovableEntity()
     43    MovableEntity::MovableEntity(BaseObject* creator) : WorldEntity(creator)
    4444    {
    4545        RegisterObject(MovableEntity);
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/MovableEntity.h

    r1993 r2019  
    4141    {
    4242        public:
    43             MovableEntity();
     43            MovableEntity(BaseObject* creator);
    4444            virtual ~MovableEntity();
    4545
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/PositionableEntity.cc

    r2006 r2019  
    3535    CreateFactory(PositionableEntity);
    3636
    37     PositionableEntity::PositionableEntity()
     37    PositionableEntity::PositionableEntity(BaseObject* creator) : WorldEntity(creator)
    3838    {
    3939        RegisterObject(PositionableEntity);
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/PositionableEntity.h

    r2006 r2019  
    3838    {
    3939        public:
    40             PositionableEntity();
     40            PositionableEntity(BaseObject* creator);
    4141            virtual ~PositionableEntity();
    4242
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/WorldEntity.cc

    r2006 r2019  
    3030#include "WorldEntity.h"
    3131
     32#include <cassert>
    3233#include <OgreSceneManager.h>
    3334
     
    3738
    3839#include "GraphicsEngine.h"
     40#include "objects/Scene.h"
    3941
    4042namespace orxonox
     
    4749    const Vector3 WorldEntity::UP    = Vector3::UNIT_Y;
    4850
    49     WorldEntity::WorldEntity()
     51    WorldEntity::WorldEntity(BaseObject* creator) : BaseObject(creator)
    5052    {
    5153        RegisterObject(WorldEntity);
    5254
    53         this->node_ = GraphicsEngine::getInstance().getLevelSceneManager()->getRootSceneNode()->createChildSceneNode();
     55        assert(this->getScene());
     56        assert(this->getScene()->getRootSceneNode());
     57
     58        this->node_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
     59
    5460        this->parent_ = 0;
    5561        this->parentID_ = (unsigned int)-1;
     
    6672        {
    6773            this->node_->detachAllObjects();
    68             GraphicsEngine::getInstance().getLevelSceneManager()->destroySceneNode(this->node_->getName());
     74            if (this->getScene()->getSceneManager())
     75                this->getScene()->getSceneManager()->destroySceneNode(this->node_->getName());
    6976        }
    7077    }
    71 
    7278
    7379    void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     
    131137        object->parentID_ = (unsigned int)-1;
    132138
    133         GraphicsEngine::getInstance().getLevelSceneManager()->getRootSceneNode()->addChild(object->node_);
     139        this->getScene()->getRootSceneNode()->addChild(object->node_);
    134140    }
    135141
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/WorldEntity.h

    r1989 r2019  
    4545    {
    4646        public:
    47             WorldEntity();
     47            WorldEntity(BaseObject* creator);
    4848            virtual ~WorldEntity();
    4949
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/pawns/Spectator.cc

    r2006 r2019  
    3333#include "core/Core.h"
    3434#include "objects/worldentities/Model.h"
     35#include "objects/Scene.h"
     36#include "objects/infos/PlayerInfo.h"
    3537#include "tools/BillboardSet.h"
    3638
     
    3941    CreateFactory(Spectator);
    4042
    41     Spectator::Spectator()
     43    Spectator::Spectator(BaseObject* creator) : ControllableEntity(creator)
    4244    {
    4345        RegisterObject(Spectator);
     
    5456
    5557        // test test test
     58        if (this->getScene()->getSceneManager())
    5659        {
    5760            this->testmesh_ = new Mesh();
    5861            this->testnode_ = this->getNode()->createChildSceneNode();
    59             this->testmesh_->setMeshSource("assff.mesh");
     62            this->testmesh_->setMeshSource(this->getScene()->getSceneManager(), "assff.mesh");
    6063            if (this->testmesh_->getEntity())
    6164                this->testnode_->attachObject(this->testmesh_->getEntity());
     
    6467            this->testnode_->scale(10, 10, 10);
    6568        }
     69        else
     70        {
     71            this->testmesh_ = 0;
     72            this->testnode_ = 0;
     73        }
    6674        // test test test
    6775
    6876        this->greetingFlare_ = new BillboardSet();
    69         this->greetingFlare_->setBillboardSet("Examples/Flare", ColourValue(1.0, 1.0, 0.8), Vector3(0, 20, 0), 1);
     77        this->greetingFlare_->setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 1.0, 0.8), Vector3(0, 20, 0), 1);
    7078        this->getNode()->attachObject(this->greetingFlare_->getBillboardSet());
    7179        this->greetingFlare_->setVisible(false);
     
    8088        if (this->isInitialized())
    8189        {
    82             delete this->greetingFlare_;
     90            if (this->greetingFlare_)
     91            {
     92                this->getNode()->detachObject(this->greetingFlare_->getBillboardSet());
     93                delete this->greetingFlare_;
     94            }
    8395
    8496            // test test test
    8597            {
    86                 delete this->testmesh_;
    87                 delete this->testnode_;
     98                if (this->testmesh_ && this->testnode_)
     99                    this->testnode_->detachObject(this->testmesh_->getEntity());
     100
     101                if (this->testmesh_)
     102                    delete this->testmesh_;
     103
     104                if (this->testnode_)
     105                    this->getNode()->removeAndDestroyChild(this->testnode_->getName());
    88106            }
    89107            // test test test
     
    177195    void Spectator::fire()
    178196    {
     197        if (this->getPlayer())
     198            this->getPlayer()->setReadyToSpawn(true);
    179199    }
    180200
  • code/branches/objecthierarchy/src/orxonox/objects/worldentities/pawns/Spectator.h

    r2006 r2019  
    3939    {
    4040        public:
    41             Spectator();
     41            Spectator(BaseObject* creator);
    4242            virtual ~Spectator();
    4343
Note: See TracChangeset for help on using the changeset viewer.