Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 31, 2008, 5:50:42 PM (16 years ago)
Author:
rgrieder
Message:

Modified the GameState hierarchy so that you can get the parent with the actual type by calling getParent().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui/src/core/GameState.h

    r1686 r1688  
    4141#include <vector>
    4242#include <map>
     43#include <cassert>
    4344#include "util/Integers.h"
    4445#include "Clock.h"
     
    6364    {
    6465        friend class RootGameState;
     66        template <class ParentType>
     67        friend class GameStateTyped;
    6568
    6669    public:
     
    7982
    8083    public:
    81         GameState(const std::string& name);
    8284        virtual ~GameState();
    8385
     
    8890        GameState* getState(const std::string& name);
    8991        GameState* getRoot();
    90         GameState* getParent() const { return this->parent_; }
    9192        //! Returns the currently active game state
    9293        virtual GameState* getCurrentState();
     
    107108        void tickChild(const Clock& time) { if (this->getActiveChild()) this->getActiveChild()->tick(time); }
    108109
     110        virtual GameState* getParent() const = 0;
     111        virtual void setParent(GameState* state) = 0;
     112
    109113    private:
     114        // Making the constructor private ensures that game states
     115        // are always derivates of GameStateTyped<T>. Note the friend declaration above.
     116        GameState(const std::string& name);
     117
    110118        //! Performs a transition to 'destination'
    111119        virtual void makeTransition(GameState* source, GameState* destination);
     
    120128        const std::string                   name_;
    121129        Operations                          operation_;
    122         GameState*                          parent_;
    123130        GameState*                          activeChild_;
    124131        //bool                                bPauseParent_;
     
    126133        std::map<GameState*, GameState*>    grandchildrenToChildren_;
    127134    };
     135
     136    template <class ParentType>
     137    class GameStateTyped : public GameState
     138    {
     139    public:
     140        GameStateTyped(const std::string& name) : GameState(name) { }
     141        virtual ~GameStateTyped() { }
     142
     143        ParentType* getParent() const
     144            { return parent_; }
     145
     146    protected:
     147        void setParent(GameState* state)
     148        {
     149            assert(dynamic_cast<ParentType*>(state) != 0);
     150            this->parent_ = dynamic_cast<ParentType*>(state);
     151        }
     152
     153    private:
     154        ParentType* parent_;
     155    };
    128156}
    129157
Note: See TracChangeset for help on using the changeset viewer.