Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2805 was 2805, checked in by rgrieder, 15 years ago

Added new class: Game
It represents basic operation related to the running game like start and stop or managing the new GameStates.
And since only three lines were left in Main.cc I thought I could move that to the very beginning of 'Game'.

  • 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 "Clock.h"
45
46namespace orxonox
47{
48    /**
49    @brief
50        An implementation of a tree to manage game states.
51        This leads to a certain hierarchy that is created at runtime.
52        To actually use the structure, you will have to derive from it and
53        implement 'enter', 'leave' and 'tick'. The latter corresponds to an
54        update function.
55
56        Internally the tree is managed by two maps per node: One stores all its
57        children, grandchildren, etc. by name of the state. The other maps these
58        children states to the actual child of the node.
59        An example: Foo is a grandchildren of Bar and Foofoo is the Foo's parent.
60        Then Bar stores Foo in map by its name. The other one then maps Foo to Foofoo.
61    */
62    class _CoreExport GameStateBase
63    {
64        friend class RootGameState;
65        template <class ParentType>
66        friend class GameState;
67        // Hack
68        friend class Game;
69
70    public:
71        /**
72        @brief
73            Gives information about what the GameState is currently doing
74        */
75        struct Operations
76        {
77            unsigned active    : 1;
78            unsigned entering  : 1;
79            unsigned leaving   : 1;
80            unsigned running   : 1;
81            unsigned suspended : 1;
82        };
83
84    public:
85        virtual ~GameStateBase();
86
87        const std::string& getName() const { return name_; }
88        const Operations getOperation() const { return this->operation_; }
89        bool isInSubtree(GameStateBase* state) const;
90
91        GameStateBase* getState(const std::string& name);
92        GameStateBase* getRoot();
93        //! Returns the currently active game state
94        virtual GameStateBase* getCurrentState();
95
96        virtual void requestState(const std::string& name);
97
98        void addChild(GameStateBase* state);
99        void removeChild(GameStateBase* state);
100        void removeChild(const std::string& name);
101
102    protected:
103        virtual void enter() = 0;
104        virtual void leave() = 0;
105        virtual void ticked(const Clock& time) = 0;
106
107        GameStateBase* getActiveChild() { return this->activeChild_; }
108
109        void tickChild(const Clock& time) { if (this->getActiveChild()) this->getActiveChild()->tick(time); }
110
111        virtual GameStateBase* getParent() const = 0;
112        virtual void setParent(GameStateBase* state) = 0;
113
114    private:
115        // Making the constructor private ensures that game states
116        // are always derivates of GameState<T>. Note the friend declaration above.
117        GameStateBase(const std::string& name);
118
119        //! Performs a transition to 'destination'
120        virtual void makeTransition(GameStateBase* source, GameStateBase* destination);
121
122        void grandchildAdded(GameStateBase* child, GameStateBase* grandchild);
123        void grandchildRemoved(GameStateBase* grandchild);
124
125        void tick(const Clock& time);
126        void activate();
127        void deactivate();
128
129        const std::string                        name_;
130        Operations                               operation_;
131        GameStateBase*                           activeChild_;
132        //bool                                     bPauseParent_;
133        std::map<std::string, GameStateBase*>    allChildren_;
134        std::map<GameStateBase*, GameStateBase*> grandchildrenToChildren_;
135    };
136
137
138    template <class ParentType>
139    class GameState : public GameStateBase
140    {
141    public:
142        GameState(const std::string& name)
143            : GameStateBase(name)
144            , parent_(0)
145        { }
146        virtual ~GameState() { }
147
148        ParentType* getParent() const
149            { return parent_; }
150
151    protected:
152        void setParent(GameStateBase* state)
153        {
154            assert(dynamic_cast<ParentType*>(state) != 0);
155            this->parent_ = dynamic_cast<ParentType*>(state);
156        }
157
158    private:
159        ParentType* parent_;
160    };
161}
162
163#endif /* _GameState_H__ */
Note: See TracBrowser for help on using the repository browser.