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 | |
---|
46 | namespace 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 GameState |
---|
63 | { |
---|
64 | friend class RootGameState; |
---|
65 | // Hack |
---|
66 | friend class Game; |
---|
67 | |
---|
68 | public: |
---|
69 | /** |
---|
70 | @brief |
---|
71 | Gives information about what the GameState is currently doing |
---|
72 | */ |
---|
73 | struct Operations |
---|
74 | { |
---|
75 | unsigned active : 1; |
---|
76 | unsigned entering : 1; |
---|
77 | unsigned leaving : 1; |
---|
78 | unsigned running : 1; |
---|
79 | unsigned suspended : 1; |
---|
80 | }; |
---|
81 | |
---|
82 | public: |
---|
83 | GameState(const std::string& name); |
---|
84 | virtual ~GameState(); |
---|
85 | |
---|
86 | const std::string& getName() const { return name_; } |
---|
87 | const Operations getOperation() const { return this->operation_; } |
---|
88 | bool isInSubtree(GameState* state) const; |
---|
89 | |
---|
90 | GameState* getState(const std::string& name); |
---|
91 | GameState* getRoot(); |
---|
92 | //! Returns the currently active game state |
---|
93 | virtual GameState* getCurrentState(); |
---|
94 | |
---|
95 | virtual void requestState(const std::string& name); |
---|
96 | |
---|
97 | void addChild(GameState* state); |
---|
98 | void removeChild(GameState* 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 | GameState* getActiveChild() { return this->activeChild_; } |
---|
107 | |
---|
108 | void tickChild(const Clock& time) { if (this->getActiveChild()) this->getActiveChild()->tick(time); } |
---|
109 | |
---|
110 | GameState* getParent() const { return this->parent_; } |
---|
111 | void setParent(GameState* state) { this->parent_ = state; } |
---|
112 | |
---|
113 | private: |
---|
114 | //! Performs a transition to 'destination' |
---|
115 | virtual void makeTransition(GameState* source, GameState* destination); |
---|
116 | |
---|
117 | void grandchildAdded(GameState* child, GameState* grandchild); |
---|
118 | void grandchildRemoved(GameState* grandchild); |
---|
119 | |
---|
120 | void tick(const Clock& time); |
---|
121 | void activate(); |
---|
122 | void deactivate(); |
---|
123 | |
---|
124 | const std::string name_; |
---|
125 | Operations operation_; |
---|
126 | GameState* parent_; |
---|
127 | GameState* activeChild_; |
---|
128 | //bool bPauseParent_; |
---|
129 | std::map<std::string, GameState*> allChildren_; |
---|
130 | std::map<GameState*, GameState*> grandchildrenToChildren_; |
---|
131 | }; |
---|
132 | } |
---|
133 | |
---|
134 | #endif /* _GameState_H__ */ |
---|