Changeset 1661 for code/branches/gui/src/core
- Timestamp:
- Aug 18, 2008, 9:17:30 PM (16 years ago)
- Location:
- code/branches/gui/src/core
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gui/src/core/GameState.cc
r1660 r1661 56 56 /** 57 57 @brief 58 Destructor only checks that we don't delete an active state. 59 */ 60 GameState::~GameState() 61 { 62 if (this->bActive_) 63 { 64 if (this->parent_) 65 this->requestState(this->parent_->getName()); 66 else 67 this->requestState(""); 68 } 69 } 70 71 /** 72 @brief 58 73 Adds a child to the current tree. The Child can contain children of its own. 59 74 But you cannot a state tree that already has an active state. … … 104 119 // mark us as parent 105 120 state->parent_ = this; 121 } 122 123 /** 124 @brief 125 Removes a child by instance. This splits the tree in two parts, 126 each of them functional on its own. 127 @param state 128 GameState by instance pointer 129 */ 130 void GameState::removeChild(GameState* state) 131 { 132 std::map<GameState*, GameState*>::iterator it = this->grandchildrenToChildren_.find(state); 133 if (it != this->grandchildrenToChildren_.end()) 134 { 135 if (state->isActive()) 136 { 137 ThrowException(GameState, "Cannot remove active game state child '" 138 + state->getName() + "' from '" + name_ + "'."); 139 //COUT(2) << "Warning: Cannot remove active game state child '" << state->getName() 140 // << "' from '" << name_ << "'." << std::endl; 141 } 142 else 143 { 144 for (std::map<GameState*, GameState*>::const_iterator it = state->grandchildrenToChildren_.begin(); 145 it != state->grandchildrenToChildren_.end(); ++it) 146 { 147 this->grandchildRemoved(it->first); 148 } 149 this->grandchildRemoved(state); 150 } 151 } 152 else 153 { 154 ThrowException(GameState, "Game state '" + name_ + "' doesn't have a child named '" 155 + state->getName() + "'. Removal skipped."); 156 //COUT(2) << "Warning: Game state '" << name_ << "' doesn't have a child named '" 157 // << state->getName() << "'. Removal skipped." << std::endl; 158 } 159 } 160 161 /** 162 @brief 163 Removes a child by name. This splits the tree in two parts, 164 each of them functional on its own. 165 @param state 166 GameState by name 167 */ 168 169 void GameState::removeChild(const std::string& name) 170 { 171 GameState* state = checkState(name); 172 if (state) 173 { 174 removeChild(state); 175 } 176 else 177 { 178 ThrowException(GameState, "GameState '" + name + "' doesn't exist."); 179 //COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl; 180 } 106 181 } 107 182 … … 126 201 /** 127 202 @brief 203 Tells a state that one of its children has removed a child. This is necessary 204 to fill the internal maps correctly. 205 @param child 206 The child who notices this state. 207 @param grandchild 208 The child that has been removed. 209 */ 210 void GameState::grandchildRemoved(GameState* grandchild) 211 { 212 // adjust the two maps correctly. 213 this->allChildren_.erase(grandchild->getName()); 214 this->grandchildrenToChildren_.erase(grandchild); 215 if (this->parent_) 216 this->parent_->grandchildRemoved(grandchild); 217 } 218 219 /** 220 @brief 128 221 Checks whether a specific game states exists in the hierarchy. 129 222 @remarks … … 191 284 void GameState::requestState(const std::string& name) 192 285 { 193 GameState* request = checkState(name);194 GameState* current = getCurrentState();195 if (request)196 {286 if (name == "") 287 { 288 // user would like to leave every state. 289 GameState* current = getCurrentState(); 197 290 if (current) 198 291 { 199 // There is already an active state 200 current->makeTransition(request); 201 } 202 else 203 { 204 // no active state --> we have to activate the root node first. 292 // Deactivate all states but root 205 293 GameState* root = getRootNode(); 206 root->activate(); 207 root->makeTransition(request); 208 } 209 } 210 else 211 { 212 COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl; 294 current->makeTransition(root); 295 // Kick root too 296 root->deactivate(); 297 } 298 } 299 else 300 { 301 GameState* request = checkState(name); 302 GameState* current = getCurrentState(); 303 if (request) 304 { 305 if (current) 306 { 307 // There is already an active state 308 current->makeTransition(request); 309 } 310 else 311 { 312 // no active state --> we have to activate the root node first. 313 GameState* root = getRootNode(); 314 root->activate(); 315 root->makeTransition(request); 316 } 317 } 318 else 319 { 320 COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl; 321 } 213 322 } 214 323 } -
code/branches/gui/src/core/GameState.h
r1660 r1661 62 62 public: 63 63 GameState(const std::string& name); 64 virtual ~GameState() { }64 virtual ~GameState(); 65 65 66 66 const std::string& getName() const { return name_; } 67 67 68 68 void addChild(GameState* state); 69 void removeChild(GameState* state); 70 void removeChild(const std::string& name); 69 71 void requestState(const std::string& name); 70 72 … … 76 78 bool isCurrentState() { return this->bActive_ && !this->activeChild_; } 77 79 80 virtual bool tick(float dt) { return true; } 81 78 82 protected: 83 //virtual void enter() = 0; 84 //virtual void leave() = 0; 85 //virtual void tick(float dt) = 0; 79 86 virtual void enter() { } 80 87 virtual void leave() { } 81 virtual void tick(float dt) { } 88 89 GameState* getActiveChild() { return this->activeChild_; } 82 90 83 91 private: … … 86 94 GameState* getRootNode(); 87 95 void grandchildAdded(GameState* child, GameState* grandchild); 96 void grandchildRemoved(GameState* grandchild); 88 97 void makeTransition(GameState* state); 89 98 void activate();
Note: See TracChangeset
for help on using the changeset viewer.