[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> |
---|
| 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 | */ |
---|
| 60 | class _CoreExport GameState |
---|
| 61 | { |
---|
| 62 | public: |
---|
[1670] | 63 | struct Operations |
---|
| 64 | { |
---|
| 65 | unsigned active : 1; |
---|
| 66 | unsigned entering : 1; |
---|
| 67 | unsigned leaving : 1; |
---|
| 68 | unsigned running : 1; |
---|
| 69 | unsigned suspended : 1; |
---|
| 70 | }; |
---|
| 71 | |
---|
[1660] | 72 | GameState(const std::string& name); |
---|
[1661] | 73 | virtual ~GameState(); |
---|
[1660] | 74 | |
---|
| 75 | const std::string& getName() const { return name_; } |
---|
| 76 | |
---|
| 77 | void addChild(GameState* state); |
---|
[1661] | 78 | void removeChild(GameState* state); |
---|
| 79 | void removeChild(const std::string& name); |
---|
[1660] | 80 | void requestState(const std::string& name); |
---|
| 81 | |
---|
[1670] | 82 | ////! Determines whether the state is active. |
---|
| 83 | //bool isActive() { return this->bActive_; } |
---|
| 84 | ////! Determines whether the state is suspended. |
---|
| 85 | //bool isSuspended() { return this->bSuspended_; } |
---|
| 86 | ////! Determines whether the state is the current |
---|
| 87 | //bool isCurrentState() { return this->bActive_ && !this->activeChild_; } |
---|
| 88 | const Operations getOperation() { return this->operation_; } |
---|
[1660] | 89 | |
---|
[1670] | 90 | void tick(float dt); |
---|
| 91 | void tickChild(float dt) { if (this->activeChild_) this->activeChild_->tick(dt); } |
---|
[1661] | 92 | |
---|
[1660] | 93 | protected: |
---|
[1670] | 94 | virtual void enter() = 0; |
---|
| 95 | virtual void leave() = 0; |
---|
| 96 | virtual void ticked(float dt) = 0; |
---|
| 97 | //virtual void enter() { } |
---|
| 98 | //virtual void leave() { } |
---|
| 99 | //virtual void ticked(float dt) { } |
---|
[1660] | 100 | |
---|
[1661] | 101 | GameState* getActiveChild() { return this->activeChild_; } |
---|
[1670] | 102 | bool hasScheduledTransition() { return this->scheduledTransition_; } |
---|
[1661] | 103 | |
---|
[1660] | 104 | private: |
---|
| 105 | GameState* checkState(const std::string& name); |
---|
| 106 | GameState* getCurrentState(); |
---|
| 107 | GameState* getRootNode(); |
---|
| 108 | void grandchildAdded(GameState* child, GameState* grandchild); |
---|
[1661] | 109 | void grandchildRemoved(GameState* grandchild); |
---|
[1660] | 110 | void makeTransition(GameState* state); |
---|
| 111 | void activate(); |
---|
| 112 | void deactivate(); |
---|
| 113 | |
---|
| 114 | const std::string name_; |
---|
| 115 | bool bPauseParent_; |
---|
| 116 | |
---|
[1670] | 117 | Operations operation_; |
---|
[1660] | 118 | |
---|
| 119 | GameState* parent_; |
---|
| 120 | GameState* activeChild_; |
---|
[1670] | 121 | GameState* scheduledTransition_; |
---|
[1660] | 122 | std::map<std::string, GameState*> allChildren_; |
---|
| 123 | std::map<GameState*, GameState*> grandchildrenToChildren_; |
---|
| 124 | }; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | #endif /* _GameState_H__ */ |
---|