[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> |
---|
[1672] | 43 | #include "util/Integers.h" |
---|
[1660] | 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | /** |
---|
| 48 | @brief |
---|
| 49 | An implementation of a tree to manage game states. |
---|
| 50 | This leads to a certain hierarchy that is created at runtime. |
---|
| 51 | To actually use the structure, you will have to derive from it and |
---|
| 52 | implement 'enter', 'leave' and 'tick'. The latter corresponds to an |
---|
| 53 | update function. |
---|
| 54 | |
---|
| 55 | Internally the tree is managed by two maps per node: One stores all its |
---|
| 56 | children, grandchildren, etc. by name of the state. The other maps these |
---|
| 57 | children states to the actual child of the node. |
---|
| 58 | An example: Foo is a grandchildren of Bar and Foofoo is the Foo's parent. |
---|
| 59 | Then Bar stores Foo in map by its name. The other one then maps Foo to Foofoo. |
---|
| 60 | */ |
---|
| 61 | class _CoreExport GameState |
---|
| 62 | { |
---|
[1672] | 63 | friend class RootGameState; |
---|
| 64 | |
---|
[1660] | 65 | public: |
---|
[1672] | 66 | /** |
---|
| 67 | @brief |
---|
| 68 | Gives information about what the GameState is currently doing |
---|
| 69 | */ |
---|
[1670] | 70 | struct Operations |
---|
| 71 | { |
---|
| 72 | unsigned active : 1; |
---|
| 73 | unsigned entering : 1; |
---|
| 74 | unsigned leaving : 1; |
---|
| 75 | unsigned running : 1; |
---|
| 76 | unsigned suspended : 1; |
---|
| 77 | }; |
---|
| 78 | |
---|
[1672] | 79 | public: |
---|
[1660] | 80 | GameState(const std::string& name); |
---|
[1661] | 81 | virtual ~GameState(); |
---|
[1660] | 82 | |
---|
| 83 | const std::string& getName() const { return name_; } |
---|
[1672] | 84 | const Operations getOperation() const { return this->operation_; } |
---|
| 85 | bool isInSubtree(GameState* state) const; |
---|
[1660] | 86 | |
---|
[1672] | 87 | GameState* getState(const std::string& name); |
---|
| 88 | GameState* getRoot(); |
---|
| 89 | GameState* getParent() const { return this->parent_; } |
---|
| 90 | //! Returns the currently active game state |
---|
| 91 | virtual GameState* getCurrentState(); |
---|
| 92 | |
---|
| 93 | virtual void requestState(const std::string& name); |
---|
| 94 | |
---|
[1660] | 95 | void addChild(GameState* state); |
---|
[1661] | 96 | void removeChild(GameState* state); |
---|
| 97 | void removeChild(const std::string& name); |
---|
[1660] | 98 | |
---|
| 99 | protected: |
---|
[1670] | 100 | virtual void enter() = 0; |
---|
| 101 | virtual void leave() = 0; |
---|
[1672] | 102 | virtual void ticked(float dt, uint64_t time) = 0; |
---|
[1660] | 103 | |
---|
[1661] | 104 | GameState* getActiveChild() { return this->activeChild_; } |
---|
| 105 | |
---|
[1672] | 106 | void tickChild(float dt, uint64_t time) { if (this->getActiveChild()) this->getActiveChild()->tick(dt, time); } |
---|
| 107 | |
---|
[1660] | 108 | private: |
---|
[1672] | 109 | //! Performs a transition to 'destination' |
---|
| 110 | virtual void makeTransition(GameState* source, GameState* destination); |
---|
| 111 | |
---|
[1660] | 112 | void grandchildAdded(GameState* child, GameState* grandchild); |
---|
[1661] | 113 | void grandchildRemoved(GameState* grandchild); |
---|
[1672] | 114 | |
---|
| 115 | void tick(float dt, uint64_t time); |
---|
[1660] | 116 | void activate(); |
---|
| 117 | void deactivate(); |
---|
| 118 | |
---|
[1672] | 119 | const std::string name_; |
---|
| 120 | Operations operation_; |
---|
| 121 | GameState* parent_; |
---|
| 122 | GameState* activeChild_; |
---|
| 123 | //bool bPauseParent_; |
---|
| 124 | std::map<std::string, GameState*> allChildren_; |
---|
| 125 | std::map<GameState*, GameState*> grandchildrenToChildren_; |
---|
[1660] | 126 | }; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | #endif /* _GameState_H__ */ |
---|