[1660] | 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> |
---|
[1688] | 43 | #include <cassert> |
---|
[1674] | 44 | #include "Clock.h" |
---|
[1660] | 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 | */ |
---|
[1689] | 62 | class _CoreExport GameStateBase |
---|
[1660] | 63 | { |
---|
[1672] | 64 | friend class RootGameState; |
---|
[1688] | 65 | template <class ParentType> |
---|
[1689] | 66 | friend class GameState; |
---|
[1672] | 67 | |
---|
[1660] | 68 | public: |
---|
[1672] | 69 | /** |
---|
| 70 | @brief |
---|
| 71 | Gives information about what the GameState is currently doing |
---|
| 72 | */ |
---|
[1670] | 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 | |
---|
[1672] | 82 | public: |
---|
[1689] | 83 | virtual ~GameStateBase(); |
---|
[1660] | 84 | |
---|
| 85 | const std::string& getName() const { return name_; } |
---|
[1672] | 86 | const Operations getOperation() const { return this->operation_; } |
---|
[1689] | 87 | bool isInSubtree(GameStateBase* state) const; |
---|
[1660] | 88 | |
---|
[1689] | 89 | GameStateBase* getState(const std::string& name); |
---|
| 90 | GameStateBase* getRoot(); |
---|
[1672] | 91 | //! Returns the currently active game state |
---|
[1689] | 92 | virtual GameStateBase* getCurrentState(); |
---|
[1672] | 93 | |
---|
| 94 | virtual void requestState(const std::string& name); |
---|
| 95 | |
---|
[1689] | 96 | void addChild(GameStateBase* state); |
---|
| 97 | void removeChild(GameStateBase* state); |
---|
[1661] | 98 | void removeChild(const std::string& name); |
---|
[1660] | 99 | |
---|
| 100 | protected: |
---|
[1670] | 101 | virtual void enter() = 0; |
---|
| 102 | virtual void leave() = 0; |
---|
[1674] | 103 | virtual void ticked(const Clock& time) = 0; |
---|
[1660] | 104 | |
---|
[1689] | 105 | GameStateBase* getActiveChild() { return this->activeChild_; } |
---|
[1661] | 106 | |
---|
[1674] | 107 | void tickChild(const Clock& time) { if (this->getActiveChild()) this->getActiveChild()->tick(time); } |
---|
[1672] | 108 | |
---|
[1689] | 109 | virtual GameStateBase* getParent() const = 0; |
---|
| 110 | virtual void setParent(GameStateBase* state) = 0; |
---|
[1688] | 111 | |
---|
[1660] | 112 | private: |
---|
[1688] | 113 | // Making the constructor private ensures that game states |
---|
[1689] | 114 | // are always derivates of GameState<T>. Note the friend declaration above. |
---|
| 115 | GameStateBase(const std::string& name); |
---|
[1688] | 116 | |
---|
[1672] | 117 | //! Performs a transition to 'destination' |
---|
[1689] | 118 | virtual void makeTransition(GameStateBase* source, GameStateBase* destination); |
---|
[1672] | 119 | |
---|
[1689] | 120 | void grandchildAdded(GameStateBase* child, GameStateBase* grandchild); |
---|
| 121 | void grandchildRemoved(GameStateBase* grandchild); |
---|
[1672] | 122 | |
---|
[1674] | 123 | void tick(const Clock& time); |
---|
[1660] | 124 | void activate(); |
---|
| 125 | void deactivate(); |
---|
| 126 | |
---|
[1689] | 127 | const std::string name_; |
---|
| 128 | Operations operation_; |
---|
| 129 | GameStateBase* activeChild_; |
---|
| 130 | //bool bPauseParent_; |
---|
| 131 | std::map<std::string, GameStateBase*> allChildren_; |
---|
| 132 | std::map<GameStateBase*, GameStateBase*> grandchildrenToChildren_; |
---|
[1660] | 133 | }; |
---|
[1688] | 134 | |
---|
[1689] | 135 | |
---|
[1688] | 136 | template <class ParentType> |
---|
[1689] | 137 | class GameState : public GameStateBase |
---|
[1688] | 138 | { |
---|
| 139 | public: |
---|
[1690] | 140 | GameState(const std::string& name) |
---|
| 141 | : GameStateBase(name) |
---|
| 142 | , parent_(0) |
---|
| 143 | { } |
---|
[1689] | 144 | virtual ~GameState() { } |
---|
[1688] | 145 | |
---|
| 146 | ParentType* getParent() const |
---|
| 147 | { return parent_; } |
---|
| 148 | |
---|
| 149 | protected: |
---|
[1689] | 150 | void setParent(GameStateBase* state) |
---|
[1688] | 151 | { |
---|
| 152 | assert(dynamic_cast<ParentType*>(state) != 0); |
---|
| 153 | this->parent_ = dynamic_cast<ParentType*>(state); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | private: |
---|
| 157 | ParentType* parent_; |
---|
| 158 | }; |
---|
[1660] | 159 | } |
---|
| 160 | |
---|
| 161 | #endif /* _GameState_H__ */ |
---|