Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 8, 2009, 12:58:47 AM (16 years ago)
Author:
dafrick
Message:

Reverted to revision 2906 (because I'm too stupid to merge correctly, 2nd try will follow shortly. ;))

Location:
code/branches/questsystem5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/questsystem5

  • code/branches/questsystem5/src/core/GameState.h

    r2907 r2908  
    3939
    4040#include <string>
     41#include <vector>
    4142#include <map>
    42 #include "CorePrereqs.h"
     43#include <cassert>
     44#include "Clock.h"
    4345
    4446namespace orxonox
     
    5860        Then Bar stores Foo in map by its name. The other one then maps Foo to Foofoo.
    5961    */
    60     class _CoreExport GameState
     62    class _CoreExport GameStateBase
    6163    {
    62         friend class Game;
     64        friend class RootGameState;
     65        template <class ParentType>
     66        friend class GameState;
    6367
    6468    public:
     
    6771            Gives information about what the GameState is currently doing
    6872        */
    69         struct State
     73        struct Operations
    7074        {
    71             unsigned active       : 1;
    72             unsigned activating   : 1;
    73             unsigned deactivating : 1;
    74             unsigned updating     : 1;
    75             unsigned suspended    : 1;
    76             unsigned topState     : 1;
     75            unsigned active    : 1;
     76            unsigned entering  : 1;
     77            unsigned leaving   : 1;
     78            unsigned running   : 1;
     79            unsigned suspended : 1;
    7780        };
    7881
    7982    public:
    80         GameState(const std::string& name);
    81         virtual ~GameState();
     83        virtual ~GameStateBase();
    8284
    8385        const std::string& getName() const { return name_; }
    84         State getActivity() const    { return this->activity_; }
    85         GameState* getParent() const       { return this->parent_; }
     86        const Operations getOperation() const { return this->operation_; }
     87        bool isInSubtree(GameStateBase* state) const;
    8688
    87         void addChild(GameState* state);
    88         void removeChild(GameState* state);
     89        GameStateBase* getState(const std::string& name);
     90        GameStateBase* getRoot();
     91        //! Returns the currently active game state
     92        virtual GameStateBase* getCurrentState();
     93
     94        virtual void requestState(const std::string& name);
     95
     96        void addChild(GameStateBase* state);
     97        void removeChild(GameStateBase* state);
     98        void removeChild(const std::string& name);
    8999
    90100    protected:
    91         virtual void activate() = 0;
    92         virtual void deactivate() = 0;
    93         virtual void update(const Clock& time) = 0;
     101        virtual void enter() = 0;
     102        virtual void leave() = 0;
     103        virtual void ticked(const Clock& time) = 0;
     104
     105        GameStateBase* getActiveChild() { return this->activeChild_; }
     106
     107        void tickChild(const Clock& time) { if (this->getActiveChild()) this->getActiveChild()->tick(time); }
     108
     109        virtual GameStateBase* getParent() const = 0;
     110        virtual void setParent(GameStateBase* state) = 0;
    94111
    95112    private:
    96         void setParent(GameState* state) { this->parent_ = state; }
    97         void setActivity(State activity);
    98         void activateInternal();
    99         void deactivateInternal();
    100         void updateInternal(const Clock& time);
     113        // Making the constructor private ensures that game states
     114        // are always derivates of GameState<T>. Note the friend declaration above.
     115        GameStateBase(const std::string& name);
     116
     117        //! Performs a transition to 'destination'
     118        virtual void makeTransition(GameStateBase* source, GameStateBase* destination);
     119
     120        void grandchildAdded(GameStateBase* child, GameStateBase* grandchild);
     121        void grandchildRemoved(GameStateBase* grandchild);
     122
     123        void tick(const Clock& time);
     124        void activate();
     125        void deactivate();
    101126
    102127        const std::string                        name_;
    103         State                                    activity_;
    104         GameState*                               parent_;
    105         std::map<std::string, GameState*>        children_;
     128        Operations                               operation_;
     129        GameStateBase*                           activeChild_;
     130        //bool                                     bPauseParent_;
     131        std::map<std::string, GameStateBase*>    allChildren_;
     132        std::map<GameStateBase*, GameStateBase*> grandchildrenToChildren_;
     133    };
     134
     135
     136    template <class ParentType>
     137    class GameState : public GameStateBase
     138    {
     139    public:
     140        GameState(const std::string& name)
     141            : GameStateBase(name)
     142            , parent_(0)
     143        { }
     144        virtual ~GameState() { }
     145
     146        ParentType* getParent() const
     147            { return parent_; }
     148
     149    protected:
     150        void setParent(GameStateBase* state)
     151        {
     152            assert(dynamic_cast<ParentType*>(state) != 0);
     153            this->parent_ = dynamic_cast<ParentType*>(state);
     154        }
     155
     156    private:
     157        ParentType* parent_;
    106158    };
    107159}
Note: See TracChangeset for help on using the changeset viewer.