Changeset 2817 for code/branches/gui/src/core
- Timestamp:
- Mar 21, 2009, 10:17:59 PM (16 years ago)
- Location:
- code/branches/gui/src/core
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gui/src/core/CorePrereqs.h
r2710 r2817 166 166 167 167 // game states 168 class GameStateBase;169 template <class ParentType>170 168 class GameState; 171 169 class RootGameState; -
code/branches/gui/src/core/GameState.cc
r2815 r2817 30 30 @file 31 31 @brief 32 Implementation of GameState Baseclass.32 Implementation of GameState class. 33 33 */ 34 34 … … 43 43 Constructor only initialises variables and sets the name permanently. 44 44 */ 45 GameState Base::GameStateBase(const std::string& name)45 GameState::GameState(const std::string& name) 46 46 : name_(name) 47 //, parent_(0)47 , parent_(0) 48 48 , activeChild_(0) 49 49 //, bPausegetParent()(false) … … 57 57 Destructor only checks that we don't delete an active state. 58 58 */ 59 GameState Base::~GameStateBase()59 GameState::~GameState() 60 60 { 61 61 OrxAssert(this->operation_.active == false, "Deleting an active GameState is a very bad idea.."); … … 69 69 The state to be added. 70 70 */ 71 void GameState Base::addChild(GameStateBase* state)71 void GameState::addChild(GameState* state) 72 72 { 73 73 if (!state) 74 74 return; 75 75 // check if the state/tree to be added has states in it that already exist in this tree. 76 for (std::map<std::string, GameState Base*>::const_iterator it = state->allChildren_.begin();76 for (std::map<std::string, GameState*>::const_iterator it = state->allChildren_.begin(); 77 77 it != state->allChildren_.end(); ++it) 78 78 { … … 96 96 97 97 // merge the child's children into this tree 98 for (std::map<std::string, GameState Base*>::const_iterator it = state->allChildren_.begin();98 for (std::map<std::string, GameState*>::const_iterator it = state->allChildren_.begin(); 99 99 it != state->allChildren_.end(); ++it) 100 100 this->grandchildAdded(state, it->second); … … 113 113 GameState by instance pointer 114 114 */ 115 void GameState Base::removeChild(GameStateBase* state)116 { 117 std::map<GameState Base*, GameStateBase*>::iterator it = this->grandchildrenToChildren_.find(state);115 void GameState::removeChild(GameState* state) 116 { 117 std::map<GameState*, GameState*>::iterator it = this->grandchildrenToChildren_.find(state); 118 118 if (it != this->grandchildrenToChildren_.end()) 119 119 { … … 127 127 else 128 128 { 129 for (std::map<GameState Base*, GameStateBase*>::const_iterator it = state->grandchildrenToChildren_.begin();129 for (std::map<GameState*, GameState*>::const_iterator it = state->grandchildrenToChildren_.begin(); 130 130 it != state->grandchildrenToChildren_.end(); ++it) 131 131 this->grandchildRemoved(it->first); … … 150 150 */ 151 151 152 void GameState Base::removeChild(const std::string& name)153 { 154 GameState Base* state = getState(name);152 void GameState::removeChild(const std::string& name) 153 { 154 GameState* state = getState(name); 155 155 if (state) 156 156 { … … 173 173 The child that has been added. 174 174 */ 175 inline void GameState Base::grandchildAdded(GameStateBase* child, GameStateBase* grandchild)175 inline void GameState::grandchildAdded(GameState* child, GameState* grandchild) 176 176 { 177 177 // fill the two maps correctly. 178 178 this->allChildren_[grandchild->getName()] = grandchild; 179 179 this->grandchildrenToChildren_[grandchild] = child; 180 if (this->getParent AsBase())181 this->getParent AsBase()->grandchildAdded(this, grandchild);180 if (this->getParent()) 181 this->getParent()->grandchildAdded(this, grandchild); 182 182 } 183 183 … … 191 191 The child that has been removed. 192 192 */ 193 inline void GameState Base::grandchildRemoved(GameStateBase* grandchild)193 inline void GameState::grandchildRemoved(GameState* grandchild) 194 194 { 195 195 // adjust the two maps correctly. 196 196 this->allChildren_.erase(grandchild->getName()); 197 197 this->grandchildrenToChildren_.erase(grandchild); 198 if (this->getParent AsBase())199 this->getParent AsBase()->grandchildRemoved(grandchild);198 if (this->getParent()) 199 this->getParent()->grandchildRemoved(grandchild); 200 200 } 201 201 … … 206 206 Remember that the every node has a map with all its child nodes. 207 207 */ 208 GameState Base* GameStateBase::getState(const std::string& name)209 { 210 if (this->getParent AsBase())211 return this->getParent AsBase()->getState(name);208 GameState* GameState::getState(const std::string& name) 209 { 210 if (this->getParent()) 211 return this->getParent()->getState(name); 212 212 else 213 213 { … … 216 216 return this; 217 217 // Search in the map. If there is no entry, we can be sure the state doesn't exist. 218 std::map<std::string, GameState Base*>::const_iterator it = this->allChildren_.find(name);218 std::map<std::string, GameState*>::const_iterator it = this->allChildren_.find(name); 219 219 return (it!= this->allChildren_.end() ? it->second : 0); 220 220 } … … 225 225 Returns the root node of the tree. 226 226 */ 227 GameState Base* GameStateBase::getRoot()228 { 229 if (this->getParent AsBase())230 return this->getParent AsBase()->getRoot();227 GameState* GameState::getRoot() 228 { 229 if (this->getParent()) 230 return this->getParent()->getRoot(); 231 231 else 232 232 return this; … … 240 240 have active children itself. Many states can be active at once. 241 241 */ 242 GameState Base* GameStateBase::getCurrentState()242 GameState* GameState::getCurrentState() 243 243 { 244 244 if (this->operation_.active) … … 251 251 else 252 252 { 253 if (this->getParent AsBase())254 return this->getParent AsBase()->getCurrentState();253 if (this->getParent()) 254 return this->getParent()->getCurrentState(); 255 255 else 256 256 return 0; … … 262 262 Determines whether 'state' is in this subtree, including this node. 263 263 */ 264 bool GameState Base::isInSubtree(GameStateBase* state) const264 bool GameState::isInSubtree(GameState* state) const 265 265 { 266 266 return (grandchildrenToChildren_.find(state) != grandchildrenToChildren_.end() … … 275 275 The state to be entered, has to exist in the tree. 276 276 */ 277 void GameState Base::requestState(const std::string& name)277 void GameState::requestState(const std::string& name) 278 278 { 279 279 assert(getRoot()); … … 286 286 the method can assume certain things to be granted (like 'this' is always active). 287 287 */ 288 void GameState Base::makeTransition(GameStateBase* source, GameStateBase* destination)289 { 290 if (source == this->getParent AsBase())288 void GameState::makeTransition(GameState* source, GameState* destination) 289 { 290 if (source == this->getParent()) 291 291 { 292 292 // call is from the parent … … 308 308 309 309 // Check for 'destination' in the children map first 310 std::map<GameState Base*, GameStateBase*>::const_iterator it310 std::map<GameState*, GameState*>::const_iterator it 311 311 = this->grandchildrenToChildren_.find(destination); 312 312 if (it != this->grandchildrenToChildren_.end()) … … 319 319 { 320 320 // parent. We can be sure of this. 321 assert(this->getParent AsBase() != 0);321 assert(this->getParent() != 0); 322 322 323 323 this->deactivate(); 324 this->getParent AsBase()->makeTransition(this, destination);324 this->getParent()->makeTransition(this, destination); 325 325 } 326 326 } … … 330 330 Activates the state. Only sets bActive_ to true and notifies the parent. 331 331 */ 332 void GameState Base::activate()332 void GameState::activate() 333 333 { 334 334 this->operation_.active = true; … … 341 341 Activates the state. Only sets bActive_ to false and notifies the parent. 342 342 */ 343 void GameState Base::deactivate()343 void GameState::deactivate() 344 344 { 345 345 this->operation_.leaving = true; … … 358 358 This method is not virtual! You cannot override it therefore. 359 359 */ 360 void GameState Base::tick(const Clock& time)360 void GameState::tick(const Clock& time) 361 361 { 362 362 this->operation_.running = true; -
code/branches/gui/src/core/GameState.h
r2815 r2817 60 60 Then Bar stores Foo in map by its name. The other one then maps Foo to Foofoo. 61 61 */ 62 class _CoreExport GameState Base62 class _CoreExport GameState 63 63 { 64 64 friend class RootGameState; 65 template <class ParentType>66 friend class GameState;67 65 // Hack 68 66 friend class Game; … … 83 81 84 82 public: 85 virtual ~GameStateBase(); 83 GameState(const std::string& name); 84 virtual ~GameState(); 86 85 87 86 const std::string& getName() const { return name_; } 88 87 const Operations getOperation() const { return this->operation_; } 89 bool isInSubtree(GameState Base* state) const;88 bool isInSubtree(GameState* state) const; 90 89 91 GameState Base* getState(const std::string& name);92 GameState Base* getRoot();90 GameState* getState(const std::string& name); 91 GameState* getRoot(); 93 92 //! Returns the currently active game state 94 virtual GameState Base* getCurrentState();93 virtual GameState* getCurrentState(); 95 94 96 95 virtual void requestState(const std::string& name); 97 96 98 void addChild(GameState Base* state);99 void removeChild(GameState Base* state);97 void addChild(GameState* state); 98 void removeChild(GameState* state); 100 99 void removeChild(const std::string& name); 101 100 … … 105 104 virtual void ticked(const Clock& time) = 0; 106 105 107 GameState Base* getActiveChild() { return this->activeChild_; }106 GameState* getActiveChild() { return this->activeChild_; } 108 107 109 108 void tickChild(const Clock& time) { if (this->getActiveChild()) this->getActiveChild()->tick(time); } 110 109 111 virtual GameStateBase* getParentAsBase() const = 0;112 v irtual void setParent(GameStateBase* state) = 0;110 GameState* getParent() const { return this->parent_; } 111 void setParent(GameState* state) { this->parent_ = state; } 113 112 114 113 private: 115 // Making the constructor private ensures that game states 116 // are always derivates of GameState<T>. Note the friend declaration above. 117 GameStateBase(const std::string& name); 114 //! Performs a transition to 'destination' 115 virtual void makeTransition(GameState* source, GameState* destination); 118 116 119 //! Performs a transition to 'destination' 120 virtual void makeTransition(GameStateBase* source, GameStateBase* destination); 121 122 void grandchildAdded(GameStateBase* child, GameStateBase* grandchild); 123 void grandchildRemoved(GameStateBase* grandchild); 117 void grandchildAdded(GameState* child, GameState* grandchild); 118 void grandchildRemoved(GameState* grandchild); 124 119 125 120 void tick(const Clock& time); … … 129 124 const std::string name_; 130 125 Operations operation_; 131 GameStateBase* activeChild_; 126 GameState* parent_; 127 GameState* activeChild_; 132 128 //bool bPauseParent_; 133 std::map<std::string, GameStateBase*> allChildren_; 134 std::map<GameStateBase*, GameStateBase*> grandchildrenToChildren_; 135 }; 136 137 138 template <class ParentType> 139 class GameState : public GameStateBase 140 { 141 public: 142 GameState(const std::string& name) 143 : GameStateBase(name) 144 , parent_(0) 145 { } 146 virtual ~GameState() { } 147 148 GameStateBase* getParentAsBase() const 149 { return parent_; } 150 151 ParentType* getParent() const 152 { return parent_; } 153 154 protected: 155 void setParent(GameStateBase* state) 156 { 157 assert(dynamic_cast<ParentType*>(state) != 0); 158 this->parent_ = dynamic_cast<ParentType*>(state); 159 } 160 161 private: 162 ParentType* parent_; 129 std::map<std::string, GameState*> allChildren_; 130 std::map<GameState*, GameState*> grandchildrenToChildren_; 163 131 }; 164 132 } -
code/branches/gui/src/core/RootGameState.cc
r2815 r2817 36 36 { 37 37 RootGameState::RootGameState(const std::string& name) 38 : GameState <GameStateBase>(name)38 : GameState(name) 39 39 , stateRequest_("") 40 40 { … … 50 50 the method can assume certain things to be granted (like 'this' is always active). 51 51 */ 52 void RootGameState::makeTransition(GameState Base* source, GameStateBase* destination)52 void RootGameState::makeTransition(GameState* source, GameState* destination) 53 53 { 54 54 if (source != 0) … … 65 65 66 66 // Check for 'destination' in the children map first 67 std::map<GameState Base*, GameStateBase*>::const_iterator it67 std::map<GameState*, GameState*>::const_iterator it 68 68 = this->grandchildrenToChildren_.find(destination); 69 69 if (it != this->grandchildrenToChildren_.end()) 70 70 { 71 OrxAssert(static_cast<GameState Base*>(it->second) != 0,71 OrxAssert(static_cast<GameState*>(it->second) != 0, 72 72 "There was a mix with RootGameState and GameState, could not cast."); 73 GameState Base* child = static_cast<GameStateBase*>(it->second);73 GameState* child = static_cast<GameState*>(it->second); 74 74 // child state. Don't use 'state', might be a grandchild! 75 75 this->activeChild_ = child; … … 85 85 void RootGameState::gotoState(const std::string& name) 86 86 { 87 GameState Base* request = getState(name);87 GameState* request = getState(name); 88 88 if (request) 89 89 { 90 GameState Base* current = getCurrentState();90 GameState* current = getCurrentState(); 91 91 if (current) 92 92 { -
code/branches/gui/src/core/RootGameState.h
r2805 r2817 35 35 namespace orxonox 36 36 { 37 class _CoreExport RootGameState : public GameState <GameStateBase>37 class _CoreExport RootGameState : public GameState 38 38 { 39 39 // Hack! … … 47 47 48 48 private: 49 void makeTransition(GameState Base* source, GameStateBase* destination);49 void makeTransition(GameState* source, GameState* destination); 50 50 void gotoState(const std::string& name); 51 51
Note: See TracChangeset
for help on using the changeset viewer.