Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 25, 2009, 5:23:00 PM (16 years ago)
Author:
rgrieder
Message:

Implemented new GameState concept. It doesn't differ that much from the old one, but there's still lots of changes.
The most important change is that one GameState can occur multiple times in the hierarchy.

Short log:

  • No RootGameState anymore. Simply the highest is root.
  • Game::requestGameState(name) can refer to the parent, grandparent, great-grandparent, etc. or one of the children.
  • Requested states are saved. So if you select "level", the next request (even right after the call) will be relative to "level"
  • Game::popState() will simply unload the current one
  • Re added Main.cc again because Game as well as GameState have been moved to the core
  • Adapted all GameStates to the new system

Things should already work, except for network support because standalone only works with a little hack.
We can now start creating a better hierarchy.

Location:
code/branches/gui/src/orxonox/gamestates
Files:
18 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui/src/orxonox/gamestates/GSClient.cc

    r2817 r2844  
    3535#include "core/Core.h"
    3636#include "network/Client.h"
     37#include "core/Game.h"
    3738
    3839namespace orxonox
    3940{
     41    AddGameState(GSClient, "client");
     42
    4043    SetCommandLineArgument(ip, "127.0.0.1").information("#.#.#.#");
    4144
    42     GSClient::GSClient()
    43         : GameState("client")
     45    GSClient::GSClient(const std::string& name)
     46        : GameState(name)
    4447        , client_(0)
    4548    {
     
    5053    }
    5154
    52     void GSClient::enter()
     55    void GSClient::activate()
    5356    {
    5457        Core::setIsClient(true);
     
    5962            ThrowException(InitialisationFailed, "Could not establish connection with server.");
    6063
    61         GSLevel::enter();
    62 
    6364        client_->update(Core::getGameClock());
    6465    }
    6566
    66     void GSClient::leave()
     67    void GSClient::deactivate()
    6768    {
    68         GSLevel::leave();
    69 
    7069        client_->closeConnection();
    7170
     
    7675    }
    7776
    78     void GSClient::ticked(const Clock& time)
     77    void GSClient::update(const Clock& time)
    7978    {
    80         GSLevel::ticked(time);
    8179        client_->update(time);
    82 
    83         this->tickChild(time);
    8480    }
    8581}
  • code/branches/gui/src/orxonox/gamestates/GSClient.h

    r2817 r2844  
    3333#include "core/GameState.h"
    3434#include "network/NetworkPrereqs.h"
    35 #include "GSLevel.h"
    3635
    3736namespace orxonox
    3837{
    39     class _OrxonoxExport GSClient : public GameState, public GSLevel
     38    class _OrxonoxExport GSClient : public GameState
    4039    {
    4140    public:
    42         GSClient();
     41        GSClient(const std::string& name);
    4342        ~GSClient();
    4443
     44        void activate();
     45        void deactivate();
     46        void update(const Clock& time);
    4547
    4648    private:
    47         void enter();
    48         void leave();
    49         void ticked(const Clock& time);
    50 
    5149        Client* client_;
    5250    };
  • code/branches/gui/src/orxonox/gamestates/GSDedicated.cc

    r2817 r2844  
    3737#include "objects/Tickable.h"
    3838#include "util/Sleep.h"
     39#include "core/Game.h"
    3940
    4041namespace orxonox
    4142{
    42     GSDedicated::GSDedicated()
    43         : GameState("dedicated")
     43    AddGameState(GSDedicated, "dedicated");
     44
     45    GSDedicated::GSDedicated(const std::string& name)
     46        : GameState(name)
    4447        , server_(0)
    4548        , timeSinceLastUpdate_(0)
     
    5154    }
    5255
    53     void GSDedicated::enter()
     56    void GSDedicated::activate()
    5457    {
    5558        Core::setHasServer(true);
     
    5861        COUT(0) << "Loading scene in server mode" << std::endl;
    5962
    60         GSLevel::enter();
    61 
    6263        server_->open();
    6364    }
    6465
    65     void GSDedicated::leave()
     66    void GSDedicated::deactivate()
    6667    {
    67         GSLevel::leave();
    68 
    6968        this->server_->close();
    7069        delete this->server_;
     
    7372    }
    7473
    75     void GSDedicated::ticked(const Clock& time)
     74    void GSDedicated::update(const Clock& time)
    7675    {
    7776//        static float startTime = time.getSecondsPrecise();
     
    8382//            COUT(0) << "estimated ticks/sec: " << nrOfTicks/(time.getSecondsPrecise()-startTime) << endl;
    8483            timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;
    85             GSLevel::ticked(time);
    8684            server_->update(time);
    87             this->tickChild(time);
    8885        }
    8986        else
  • code/branches/gui/src/orxonox/gamestates/GSDedicated.h

    r2817 r2844  
    3333#include "core/GameState.h"
    3434#include "network/NetworkPrereqs.h"
    35 #include "GSLevel.h"
    3635
    3736namespace orxonox
    3837{
    39     class _OrxonoxExport GSDedicated : public GameState, public GSLevel
     38    class _OrxonoxExport GSDedicated : public GameState
    4039    {
    4140    public:
    42         GSDedicated();
     41        GSDedicated(const std::string& name);
    4342        ~GSDedicated();
    4443
     44        void activate();
     45        void deactivate();
     46        void update(const Clock& time);
     47
    4548    private:
    46         void enter();
    47         void leave();
    48         void ticked(const Clock& time);
    49 
    50         Server*      server_;
    51         float        timeSinceLastUpdate_;
     49        Server* server_;
     50        float   timeSinceLastUpdate_;
    5251    };
    5352}
  • code/branches/gui/src/orxonox/gamestates/GSGUI.cc

    r2817 r2844  
    3232#include <OgreViewport.h>
    3333#include "core/Clock.h"
     34#include "core/ConsoleCommand.h"
    3435#include "core/input/InputManager.h"
    3536#include "core/input/SimpleInputState.h"
    3637#include "gui/GUIManager.h"
    3738#include "GraphicsManager.h"
     39#include "core/Game.h"
    3840
    3941namespace orxonox
    4042{
    41     GSGUI::GSGUI()
    42         : GameState("gui")
     43    AddGameState(GSGUI, "gui");
     44
     45    GSGUI::GSGUI(const std::string& name)
     46        : GameState(name)
    4347    {
    4448    }
     
    4852    }
    4953
    50     void GSGUI::enter()
     54    void GSGUI::activate()
    5155    {
    5256        guiManager_ = GUIManager::getInstancePtr();
     
    5660        guiManager_->showGUI("MainMenu", 0);
    5761        GraphicsManager::getInstance().getViewport()->setCamera(guiManager_->getCamera());
     62
     63        {
     64            // time factor console command
     65            FunctorMember<GSGUI>* functor = createFunctor(&GSGUI::startGame);
     66            functor->setObject(this);
     67            this->ccStartGame_ = createConsoleCommand(functor, "startGame");
     68            CommandExecutor::addConsoleCommandShortcut(this->ccStartGame_);
     69        }
    5870    }
    5971
    60     void GSGUI::leave()
     72    void GSGUI::deactivate()
    6173    {
     74        if (this->ccStartGame_)
     75        {
     76            delete this->ccStartGame_;
     77            this->ccStartGame_ = 0;
     78        }
     79
    6280        guiManager_->hideGUI();
    6381    }
    6482
    65     void GSGUI::ticked(const Clock& time)
     83    void GSGUI::update(const Clock& time)
    6684    {
    6785        // tick CEGUI
    6886        guiManager_->update(time);
     87    }
    6988
    70         this->tickChild(time);
     89    void GSGUI::startGame()
     90    {
     91        // HACK - HACK
     92        Game::getInstance().popState();
     93        Game::getInstance().requestState("standalone");
     94        Game::getInstance().requestState("level");
    7195    }
    7296}
  • code/branches/gui/src/orxonox/gamestates/GSGUI.h

    r2817 r2844  
    3838    {
    3939    public:
    40         GSGUI();
     40        GSGUI(const std::string& name);
    4141        ~GSGUI();
    4242
     43        void activate();
     44        void deactivate();
     45        void update(const Clock& time);
     46
     47        void startGame();
     48
    4349    private:
    44         void enter();
    45         void leave();
    46         void ticked(const Clock& time);
     50        GUIManager* guiManager_;
    4751
    48         GUIManager* guiManager_;
     52        // console commands
     53        ConsoleCommand* ccStartGame_;
    4954    };
    5055}
  • code/branches/gui/src/orxonox/gamestates/GSGraphics.cc

    r2817 r2844  
    4545#include "gui/GUIManager.h"
    4646#include "GraphicsManager.h"
    47 #include "Game.h"
     47#include "core/Game.h"
    4848
    4949namespace orxonox
    5050{
    51     GSGraphics::GSGraphics()
    52         : GameState("graphics")
     51    AddGameState(GSGraphics, "graphics");
     52
     53    GSGraphics::GSGraphics(const std::string& name)
     54        : GameState(name)
    5355        , inputManager_(0)
    5456        , console_(0)
     
    6062    {
    6163        RegisterRootObject(GSGraphics);
    62         setConfigValues();
    6364    }
    6465
     
    7172    }
    7273
    73     void GSGraphics::enter()
     74    void GSGraphics::activate()
    7475    {
     76        setConfigValues();
     77
    7578        Core::setShowsGraphics(true);
    7679
     
    108111    }
    109112
    110     void GSGraphics::leave()
     113    void GSGraphics::deactivate()
    111114    {
    112115        if (Core::showsGraphics())
     
    146149        need the time. So we shouldn't run into problems.
    147150    */
    148     void GSGraphics::ticked(const Clock& time)
     151    void GSGraphics::update(const Clock& time)
    149152    {
    150153        uint64_t timeBeforeTick = time.getRealMicroseconds();
     
    152155        this->inputManager_->update(time);        // tick console
    153156        this->console_->update(time);
    154         this->tickChild(time);
    155157
    156158        uint64_t timeAfterTick = time.getRealMicroseconds();
  • code/branches/gui/src/orxonox/gamestates/GSGraphics.h

    r2817 r2844  
    3232#include "OrxonoxPrereqs.h"
    3333#include "core/GameState.h"
    34 #include "core/OrxonoxClass.h"
    3534#include "tools/WindowEventListener.h"
    3635
     
    3938    class _OrxonoxExport GSGraphics : public GameState, public WindowEventListener
    4039    {
    41         friend class ClassIdentifier<GSGraphics>;
    42 
    4340    public:
    44         GSGraphics();
     41        GSGraphics(const std::string& name);
    4542        ~GSGraphics();
    46 
    47     private: // functions
    48         void enter();
    49         void leave();
    50         void ticked(const Clock& time);
    51 
    5243        void setConfigValues();
    5344
     45        void activate();
     46        void deactivate();
     47        void update(const Clock& time);
     48
     49    private:
    5450        // Window events from WindowEventListener
    5551        void windowResized(unsigned int newWidth, unsigned int newHeight);
    5652        void windowFocusChanged();
    5753
    58     private:
    5954        // managed singletons
    6055        InputManager*         inputManager_;
  • code/branches/gui/src/orxonox/gamestates/GSIOConsole.cc

    r2817 r2844  
    3636
    3737#include "core/ConsoleCommand.h"
     38#include "core/Game.h"
    3839
    3940namespace orxonox
    4041{
    41     GSIOConsole::GSIOConsole()
    42         : GameState("ioConsole")
     42    AddGameState(GSIOConsole, "ioConsole");
     43
     44    GSIOConsole::GSIOConsole(const std::string& name)
     45        : GameState(name)
    4346    {
    4447    }
     
    4851    }
    4952
    50     void GSIOConsole::enter()
     53    void GSIOConsole::activate()
    5154    {
    5255    }
    5356
    54     void GSIOConsole::leave()
     57    void GSIOConsole::deactivate()
    5558    {
    5659    }
    5760
    58     void GSIOConsole::ticked(const Clock& time)
     61    void GSIOConsole::update(const Clock& time)
    5962    {
    6063        std::string command;
    6164        std::getline(std::cin, command);
    6265        CommandExecutor::execute(command, true);
    63        
    64         tickChild(time);
    6566    }
    6667}
  • code/branches/gui/src/orxonox/gamestates/GSIOConsole.h

    r2817 r2844  
    3131
    3232#include "OrxonoxPrereqs.h"
    33 #include <OgrePrerequisites.h>
    3433#include "core/GameState.h"
    3534
     
    3938    {
    4039    public:
    41         GSIOConsole();
     40        GSIOConsole(const std::string& name);
    4241        ~GSIOConsole();
    4342
     43        void activate();
     44        void deactivate();
     45        void update(const Clock& time);
     46
    4447    private:
    45         void enter();
    46         void leave();
    47         void ticked(const Clock& time);
     48
    4849    };
    4950}
  • code/branches/gui/src/orxonox/gamestates/GSLevel.cc

    r2817 r2844  
    4747#include "LevelManager.h"
    4848#include "PlayerManager.h"
     49#include "core/Game.h"
    4950
    5051namespace orxonox
    5152{
     53    AddGameState(GSLevel, "level");
     54
    5255    SetCommandLineArgument(level, "presentation.oxw").shortcut("l");
    5356
    54     GSLevel::GSLevel()
    55 //        : GameState(name)
    56         : keyBinder_(0)
     57    GSLevel::GSLevel(const std::string& name)
     58        : GameState(name)
     59        , keyBinder_(0)
    5760        , inputState_(0)
    5861        , radar_(0)
     
    6568        this->ccKeybind_ = 0;
    6669        this->ccTkeybind_ = 0;
    67 
     70    }
     71
     72    GSLevel::~GSLevel()
     73    {
     74    }
     75
     76    void GSLevel::setConfigValues()
     77    {
     78        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
     79    }
     80
     81    void GSLevel::activate()
     82    {
    6883        setConfigValues();
    69     }
    70 
    71     GSLevel::~GSLevel()
    72     {
    73     }
    74 
    75     void GSLevel::setConfigValues()
    76     {
    77         SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
    78     }
    79 
    80     void GSLevel::enter()
    81     {
     84
    8285        if (Core::showsGraphics())
    8386        {
     
    126129    }
    127130
    128     void GSLevel::leave()
     131    void GSLevel::deactivate()
    129132    {
    130133        // destroy console commands
     
    188191    }
    189192
    190     void GSLevel::ticked(const Clock& time)
    191     {
    192         // Commented by 1337: Temporarily moved to GSGraphics.
     193    void GSLevel::update(const Clock& time)
     194    {
     195        // Note: Temporarily moved to GSGraphics.
    193196        //// Call the scene objects
    194197        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
     
    233236        Command string that can be executed by the CommandExecutor
    234237        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
    235         the key/button/axis that has been activated. This is configured above in enter().
     238        the key/button/axis that has been activated. This is configured above in activate().
    236239    */
    237240    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
  • code/branches/gui/src/orxonox/gamestates/GSLevel.h

    r2817 r2844  
    3131
    3232#include "OrxonoxPrereqs.h"
     33#include "core/OrxonoxClass.h"
    3334#include "core/GameState.h"
    34 #include "core/OrxonoxClass.h"
    3535
    3636namespace orxonox
    3737{
    38     class _OrxonoxExport GSLevel : public OrxonoxClass
     38    class _OrxonoxExport GSLevel : public GameState, public OrxonoxClass
    3939    {
    40         friend class ClassIdentifier<GSLevel>;
    4140    public:
    42         GSLevel();
     41        GSLevel(const std::string& name);
    4342        ~GSLevel();
    44 
    45         // was private before (is public now because of console command in GSStandalone)
    4643        void setConfigValues();
    4744
     45        void activate();
     46        void deactivate();
     47        void update(const Clock& time);
     48
    4849    protected:
    49         void enter();
    50         void leave();
    51         void ticked(const Clock& time);
    52 
    5350        void loadLevel();
    5451        void unloadLevel();
  • code/branches/gui/src/orxonox/gamestates/GSRoot.cc

    r2843 r2844  
    3232#include "util/Exception.h"
    3333#include "util/Debug.h"
     34#include "core/Clock.h"
    3435#include "core/Core.h"
    35 #include "core/CoreIncludes.h"
    3636#include "core/ConsoleCommand.h"
    3737#include "tools/TimeFactorListener.h"
    3838#include "tools/Timer.h"
    3939#include "objects/Tickable.h"
    40 #include "Game.h"
     40#include "core/Game.h"
    4141
    4242namespace orxonox
    4343{
    44     GSRoot::GSRoot()
    45         : RootGameState("root")
     44    AddGameState(GSRoot, "root");
     45
     46    GSRoot::GSRoot(const std::string& name)
     47        : GameState(name)
    4648        , timeFactor_(1.0f)
    4749        , bPaused_(false)
     
    5658    }
    5759
    58     void GSRoot::enter()
     60    void GSRoot::activate()
    5961    {
    6062        // reset game speed to normal
    6163        timeFactor_ = 1.0f;
    62 
    63         {
    64             // add console commands
    65             FunctorMember01<GameState, const std::string&>* functor = createFunctor(&GameState::requestState);
    66             functor->setObject(this);
    67             this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState");
    68             CommandExecutor::addConsoleCommandShortcut(this->ccSelectGameState_);
    69         }
    7064
    7165        {
     
    8680    }
    8781
    88     void GSRoot::leave()
     82    void GSRoot::deactivate()
    8983    {
    90         // destroy console commands
    91         delete this->ccSelectGameState_;
    92 
    9384        if (this->ccSetTimeFactor_)
    9485        {
     
    10495    }
    10596
    106     void GSRoot::ticked(const Clock& time)
     97    void GSRoot::update(const Clock& time)
    10798    {
    10899        uint64_t timeBeforeTick = time.getRealMicroseconds();
     
    129120        // Also add our tick time to the list in GSRoot
    130121        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
    131 
    132         this->tickChild(time);
    133122    }
    134123
  • code/branches/gui/src/orxonox/gamestates/GSRoot.h

    r2843 r2844  
    3131
    3232#include "OrxonoxPrereqs.h"
    33 
    34 #include <list>
    35 #include "core/RootGameState.h"
     33#include "core/GameState.h"
    3634#include "core/OrxonoxClass.h"
    3735
    3836namespace orxonox
    3937{
    40     class _OrxonoxExport GSRoot : public RootGameState
     38    class _OrxonoxExport GSRoot : public GameState
    4139    {
    42         friend class ClassIdentifier<GSRoot>;
     40    public:
     41        GSRoot(const std::string& name);
     42        ~GSRoot();
    4343
    44     public:
    45    
    46     public:
    47         GSRoot();
    48         ~GSRoot();
     44        void activate();
     45        void deactivate();
     46        void update(const Clock& time);
    4947
    5048        // this has to be public because proteced triggers a bug in msvc
     
    5553
    5654    private:
    57         void enter();
    58         void leave();
    59         void ticked(const Clock& time);
    60 
    6155        float                 timeFactor_;       //!< A factor that sets the gamespeed. 1 is normal.
    6256        bool                  bPaused_;
     
    6458
    6559        // console commands
    66         ConsoleCommand*       ccSelectGameState_;
    6760        ConsoleCommand*       ccSetTimeFactor_;
    6861        ConsoleCommand*       ccPause_;
  • code/branches/gui/src/orxonox/gamestates/GSServer.cc

    r2817 r2844  
    3333#include "core/Core.h"
    3434#include "network/Server.h"
     35#include "core/Game.h"
    3536
    3637namespace orxonox
    3738{
     39    AddGameState(GSServer, "server");
     40
    3841    SetCommandLineArgument(port, 55556).shortcut("p").information("0-65535");
    3942
    40     GSServer::GSServer()
    41         : GameState("server")
     43    GSServer::GSServer(const std::string& name)
     44        : GameState(name)
    4245        , server_(0)
    4346    {
     
    4851    }
    4952
    50     void GSServer::enter()
     53    void GSServer::activate()
    5154    {
    5255        Core::setHasServer(true);
     
    5558        COUT(0) << "Loading scene in server mode" << std::endl;
    5659
    57         GSLevel::enter();
    58 
    5960        server_->open();
    6061    }
    6162
    62     void GSServer::leave()
     63    void GSServer::deactivate()
    6364    {
    64         GSLevel::leave();
    65 
    6665        this->server_->close();
    6766        delete this->server_;
     
    7069    }
    7170
    72     void GSServer::ticked(const Clock& time)
     71    void GSServer::update(const Clock& time)
    7372    {
    74         GSLevel::ticked(time);
    7573        server_->update(time);
    76         this->tickChild(time);
    7774    }
    7875}
  • code/branches/gui/src/orxonox/gamestates/GSServer.h

    r2817 r2844  
    3333#include "core/GameState.h"
    3434#include "network/NetworkPrereqs.h"
    35 #include "GSLevel.h"
    3635
    3736namespace orxonox
    3837{
    39     class _OrxonoxExport GSServer : public GameState, public GSLevel
     38    class _OrxonoxExport GSServer : public GameState
    4039    {
    4140    public:
    42         GSServer();
     41        GSServer(const std::string& name);
    4342        ~GSServer();
    4443
     44        void activate();
     45        void deactivate();
     46        void update(const Clock& time);
     47
    4548    private:
    46         void enter();
    47         void leave();
    48         void ticked(const Clock& time);
    49 
    50         Server*      server_;
     49        Server* server_;
    5150    };
    5251}
  • code/branches/gui/src/orxonox/gamestates/GSStandalone.cc

    r2834 r2844  
    3636#include "gui/GUIManager.h"
    3737#include "GraphicsManager.h"
     38#include "core/Game.h"
    3839
    3940namespace orxonox
    4041{
    41     GSStandalone::GSStandalone()
    42         : GameState("standalone")
     42    AddGameState(GSStandalone, "standalone");
     43
     44    GSStandalone::GSStandalone(const std::string& name)
     45        : GameState(name)
    4346    {
    4447    }
     
    4952
    5053
    51     void GSStandalone::enter()
     54    void GSStandalone::activate()
    5255    {
    5356        Core::setIsStandalone(true);
    54 
    55         GSLevel::enter();
    5657
    5758        guiManager_ = GUIManager::getInstancePtr();
     
    6061    }
    6162
    62     void GSStandalone::leave()
     63    void GSStandalone::deactivate()
    6364    {
    64         GSLevel::leave();
    65 
    6665        Core::setIsStandalone(false);
    6766    }
    6867
    69     void GSStandalone::ticked(const Clock& time)
     68    void GSStandalone::update(const Clock& time)
    7069    {
    7170        //Ogre::Viewport* viewport = GraphicsManager::getInstance().getViewport();
     
    7776        // tick CEGUI
    7877        guiManager_->update(time);
    79 
    80         GSLevel::ticked(time);
    81         this->tickChild(time);
    8278    }
    8379}
  • code/branches/gui/src/orxonox/gamestates/GSStandalone.h

    r2817 r2844  
    3232#include "OrxonoxPrereqs.h"
    3333#include "core/GameState.h"
    34 #include "GSLevel.h"
    3534
    3635namespace orxonox
    3736{
    38     class _OrxonoxExport GSStandalone : public GameState, public GSLevel
     37    class _OrxonoxExport GSStandalone : public GameState
    3938    {
    4039    public:
    41         GSStandalone();
     40        GSStandalone(const std::string& name);
    4241        ~GSStandalone();
    4342
     43        void activate();
     44        void deactivate();
     45        void update(const Clock& time);
     46
    4447    private:
    45         void enter();
    46         void leave();
    47         void ticked(const Clock& time);
    48 
    49         GUIManager*     guiManager_;
     48        GUIManager* guiManager_;
    5049    };
    5150}
Note: See TracChangeset for help on using the changeset viewer.