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