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().

Location:
code/branches/gui/src/core
Files:
5 edited

Legend:

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

    r1674 r1688  
    163163  // game states
    164164  class GameState;
     165  template <class ParentType>
     166  class GameStateTyped;
    165167  class RootGameState;
    166168
  • code/branches/gui/src/core/GameState.cc

    r1674 r1688  
    4545    GameState::GameState(const std::string& name)
    4646        : name_(name)
    47         , parent_(0)
     47        //, parent_(0)
    4848        , activeChild_(0)
    49         //, bPauseParent_(false)
     49        //, bPausegetParent()(false)
    5050    {
    5151        Operations temp = {false, false, false, false, false};
     
    103103
    104104        // mark us as parent
    105         state->parent_ = this;
     105        state->setParent(this);
    106106    }
    107107
     
    178178        this->allChildren_[grandchild->getName()] = grandchild;
    179179        this->grandchildrenToChildren_[grandchild] = child;
    180         if (this->parent_)
    181             this->parent_->grandchildAdded(this, grandchild);
     180        if (this->getParent())
     181            this->getParent()->grandchildAdded(this, grandchild);
    182182    }
    183183
     
    196196        this->allChildren_.erase(grandchild->getName());
    197197        this->grandchildrenToChildren_.erase(grandchild);
    198         if (this->parent_)
    199             this->parent_->grandchildRemoved(grandchild);
     198        if (this->getParent())
     199            this->getParent()->grandchildRemoved(grandchild);
    200200    }
    201201
     
    208208    GameState* GameState::getState(const std::string& name)
    209209    {
    210         if (this->parent_)
    211             return this->parent_->getState(name);
     210        if (this->getParent())
     211            return this->getParent()->getState(name);
    212212        else
    213213        {
     
    227227    GameState* GameState::getRoot()
    228228    {
    229         if (this->parent_)
    230             return this->parent_->getRoot();
     229        if (this->getParent())
     230            return this->getParent()->getRoot();
    231231        else
    232232            return this;
  • 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
  • code/branches/gui/src/core/RootGameState.cc

    r1674 r1688  
    3939
    4040    RootGameState::RootGameState(const std::string& name)
    41         : GameState(name)
     41        : GameStateTyped<GameState>(name)
    4242        , stateRequest_("")
    4343    {
  • code/branches/gui/src/core/RootGameState.h

    r1674 r1688  
    3535namespace orxonox
    3636{
    37     class _CoreExport RootGameState : public GameState
     37    class _CoreExport RootGameState : public GameStateTyped<GameState>
    3838    {
    3939    public:
Note: See TracChangeset for help on using the changeset viewer.