Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/GameState.h @ 1697

Last change on this file since 1697 was 1690, checked in by rgrieder, 16 years ago

Added support to specify the command line arguments in start.ini (uses the same formatting). '#' is the comment character.

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30@file
31@brief
32    Definition of GameState class.
33*/
34
35#ifndef _GameState_H__
36#define _GameState_H__
37
38#include "CorePrereqs.h"
39
40#include <string>
41#include <vector>
42#include <map>
43#include <cassert>
44#include "util/Integers.h"
45#include "Clock.h"
46
47namespace orxonox
48{
49    /**
50    @brief
51        An implementation of a tree to manage game states.
52        This leads to a certain hierarchy that is created at runtime.
53        To actually use the structure, you will have to derive from it and
54        implement 'enter', 'leave' and 'tick'. The latter corresponds to an
55        update function.
56
57        Internally the tree is managed by two maps per node: One stores all its
58        children, grandchildren, etc. by name of the state. The other maps these
59        children states to the actual child of the node.
60        An example: Foo is a grandchildren of Bar and Foofoo is the Foo's parent.
61        Then Bar stores Foo in map by its name. The other one then maps Foo to Foofoo.
62    */
63    class _CoreExport GameStateBase
64    {
65        friend class RootGameState;
66        template <class ParentType>
67        friend class GameState;
68
69    public:
70        /**
71        @brief
72            Gives information about what the GameState is currently doing
73        */
74        struct Operations
75        {
76            unsigned active    : 1;
77            unsigned entering  : 1;
78            unsigned leaving   : 1;
79            unsigned running   : 1;
80            unsigned suspended : 1;
81        };
82
83    public:
84        virtual ~GameStateBase();
85
86        const std::string& getName() const { return name_; }
87        const Operations getOperation() const { return this->operation_; }
88        bool isInSubtree(GameStateBase* state) const;
89
90        GameStateBase* getState(const std::string& name);
91        GameStateBase* getRoot();
92        //! Returns the currently active game state
93        virtual GameStateBase* getCurrentState();
94
95        virtual void requestState(const std::string& name);
96
97        void addChild(GameStateBase* state);
98        void removeChild(GameStateBase* state);
99        void removeChild(const std::string& name);
100
101    protected:
102        virtual void enter() = 0;
103        virtual void leave() = 0;
104        virtual void ticked(const Clock& time) = 0;
105
106        GameStateBase* getActiveChild() { return this->activeChild_; }
107
108        void tickChild(const Clock& time) { if (this->getActiveChild()) this->getActiveChild()->tick(time); }
109
110        virtual GameStateBase* getParent() const = 0;
111        virtual void setParent(GameStateBase* state) = 0;
112
113    private:
114        // Making the constructor private ensures that game states
115        // are always derivates of GameState<T>. Note the friend declaration above.
116        GameStateBase(const std::string& name);
117
118        //! Performs a transition to 'destination'
119        virtual void makeTransition(GameStateBase* source, GameStateBase* destination);
120
121        void grandchildAdded(GameStateBase* child, GameStateBase* grandchild);
122        void grandchildRemoved(GameStateBase* grandchild);
123
124        void tick(const Clock& time);
125        void activate();
126        void deactivate();
127
128        const std::string                        name_;
129        Operations                               operation_;
130        GameStateBase*                           activeChild_;
131        //bool                                     bPauseParent_;
132        std::map<std::string, GameStateBase*>    allChildren_;
133        std::map<GameStateBase*, GameStateBase*> grandchildrenToChildren_;
134    };
135
136
137    template <class ParentType>
138    class GameState : public GameStateBase
139    {
140    public:
141        GameState(const std::string& name)
142            : GameStateBase(name)
143            , parent_(0)
144        { }
145        virtual ~GameState() { }
146
147        ParentType* getParent() const
148            { return parent_; }
149
150    protected:
151        void setParent(GameStateBase* state)
152        {
153            assert(dynamic_cast<ParentType*>(state) != 0);
154            this->parent_ = dynamic_cast<ParentType*>(state);
155        }
156
157    private:
158        ParentType* parent_;
159    };
160}
161
162#endif /* _GameState_H__ */
Note: See TracBrowser for help on using the repository browser.