[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 <map> |
---|
[2896] | 42 | #include "CorePrereqs.h" |
---|
[1660] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
| 46 | /** |
---|
| 47 | @brief |
---|
| 48 | An implementation of a tree to manage game states. |
---|
| 49 | This leads to a certain hierarchy that is created at runtime. |
---|
| 50 | To actually use the structure, you will have to derive from it and |
---|
| 51 | implement 'enter', 'leave' and 'tick'. The latter corresponds to an |
---|
| 52 | update function. |
---|
| 53 | |
---|
| 54 | Internally the tree is managed by two maps per node: One stores all its |
---|
| 55 | children, grandchildren, etc. by name of the state. The other maps these |
---|
| 56 | children states to the actual child of the node. |
---|
| 57 | An example: Foo is a grandchildren of Bar and Foofoo is the Foo's parent. |
---|
| 58 | Then Bar stores Foo in map by its name. The other one then maps Foo to Foofoo. |
---|
| 59 | */ |
---|
[2896] | 60 | class _CoreExport GameState |
---|
[1660] | 61 | { |
---|
[2896] | 62 | friend class Game; |
---|
[1672] | 63 | |
---|
[1660] | 64 | public: |
---|
[1672] | 65 | /** |
---|
| 66 | @brief |
---|
| 67 | Gives information about what the GameState is currently doing |
---|
| 68 | */ |
---|
[2896] | 69 | struct State |
---|
[1670] | 70 | { |
---|
[2896] | 71 | unsigned active : 1; |
---|
| 72 | unsigned activating : 1; |
---|
| 73 | unsigned deactivating : 1; |
---|
| 74 | unsigned updating : 1; |
---|
| 75 | unsigned suspended : 1; |
---|
| 76 | unsigned topState : 1; |
---|
[1670] | 77 | }; |
---|
| 78 | |
---|
[1672] | 79 | public: |
---|
[2996] | 80 | GameState(const std::string& name, bool countTicktime = true); |
---|
[2896] | 81 | virtual ~GameState(); |
---|
[1660] | 82 | |
---|
| 83 | const std::string& getName() const { return name_; } |
---|
[2996] | 84 | State getActivity() const { return this->activity_; } |
---|
| 85 | GameState* getParent() const { return this->parent_; } |
---|
[1660] | 86 | |
---|
[2996] | 87 | bool getCountTickTime() const { return this->bCountTickTime_; } |
---|
| 88 | |
---|
[2896] | 89 | void addChild(GameState* state); |
---|
| 90 | void removeChild(GameState* state); |
---|
[1672] | 91 | |
---|
[1660] | 92 | protected: |
---|
[2896] | 93 | virtual void activate() = 0; |
---|
| 94 | virtual void deactivate() = 0; |
---|
| 95 | virtual void update(const Clock& time) = 0; |
---|
[1660] | 96 | |
---|
| 97 | private: |
---|
[2896] | 98 | void setParent(GameState* state) { this->parent_ = state; } |
---|
| 99 | void setActivity(State activity); |
---|
| 100 | void activateInternal(); |
---|
| 101 | void deactivateInternal(); |
---|
| 102 | void updateInternal(const Clock& time); |
---|
[1688] | 103 | |
---|
[1689] | 104 | const std::string name_; |
---|
[2896] | 105 | State activity_; |
---|
[2996] | 106 | const bool bCountTickTime_; |
---|
[2896] | 107 | GameState* parent_; |
---|
| 108 | std::map<std::string, GameState*> children_; |
---|
[1660] | 109 | }; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | #endif /* _GameState_H__ */ |
---|