[1672] | 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 | #include "RootGameState.h" |
---|
| 30 | |
---|
[1755] | 31 | #include "util/Debug.h" |
---|
[1764] | 32 | #include "util/Exception.h" |
---|
[1674] | 33 | #include "Clock.h" |
---|
[1672] | 34 | |
---|
| 35 | namespace orxonox |
---|
| 36 | { |
---|
| 37 | RootGameState::RootGameState(const std::string& name) |
---|
[2817] | 38 | : GameState(name) |
---|
[1672] | 39 | , stateRequest_("") |
---|
| 40 | { |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | RootGameState::~RootGameState() |
---|
| 44 | { |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | @brief |
---|
| 49 | Internal method that actually makes the state transition. Since it is internal, |
---|
| 50 | the method can assume certain things to be granted (like 'this' is always active). |
---|
| 51 | */ |
---|
[2817] | 52 | void RootGameState::makeTransition(GameState* source, GameState* destination) |
---|
[1672] | 53 | { |
---|
| 54 | if (source != 0) |
---|
| 55 | { |
---|
| 56 | // transition was not initiated by root itself |
---|
| 57 | this->activeChild_ = 0; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | if (destination == this) |
---|
| 61 | { |
---|
| 62 | // this marks the end of the game. |
---|
| 63 | return; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | // Check for 'destination' in the children map first |
---|
[2817] | 67 | std::map<GameState*, GameState*>::const_iterator it |
---|
[1672] | 68 | = this->grandchildrenToChildren_.find(destination); |
---|
| 69 | if (it != this->grandchildrenToChildren_.end()) |
---|
| 70 | { |
---|
[2817] | 71 | OrxAssert(static_cast<GameState*>(it->second) != 0, |
---|
[1672] | 72 | "There was a mix with RootGameState and GameState, could not cast."); |
---|
[2817] | 73 | GameState* child = static_cast<GameState*>(it->second); |
---|
[1672] | 74 | // child state. Don't use 'state', might be a grandchild! |
---|
| 75 | this->activeChild_ = child; |
---|
| 76 | child->makeTransition(this, destination); |
---|
| 77 | } |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | // root doesn't have a parent.. |
---|
| 81 | OrxAssert(false, "GameState '" + destination->getName() + "' not found in children list of Root."); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | void RootGameState::gotoState(const std::string& name) |
---|
| 86 | { |
---|
[2817] | 87 | GameState* request = getState(name); |
---|
[1672] | 88 | if (request) |
---|
| 89 | { |
---|
[2817] | 90 | GameState* current = getCurrentState(); |
---|
[1672] | 91 | if (current) |
---|
| 92 | { |
---|
| 93 | current->makeTransition(0, request); |
---|
| 94 | } |
---|
| 95 | else |
---|
| 96 | { |
---|
| 97 | // Root is not yet active. This is a violation. |
---|
| 98 | ThrowException(GameState, "Activate Root before requesting a state."); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | @brief |
---|
| 109 | Makes a state transition according to the state tree. You can choose any state |
---|
| 110 | in the tree to do the call. The function finds the current state on its own. |
---|
| 111 | @param state |
---|
| 112 | The state to be entered, has to exist in the tree. |
---|
| 113 | */ |
---|
| 114 | void RootGameState::requestState(const std::string& name) |
---|
| 115 | { |
---|
| 116 | this->stateRequest_ = name; |
---|
| 117 | } |
---|
| 118 | } |
---|