[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 | |
---|
| 31 | #include <OgreTimer.h> |
---|
| 32 | #include "util/Integers.h" |
---|
| 33 | #include "Debug.h" |
---|
| 34 | #include "Exception.h" |
---|
| 35 | #include "CommandLine.h" |
---|
| 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
| 39 | SetCommandLineArgument(state, "standalone").setShortcut("s"); |
---|
| 40 | |
---|
| 41 | RootGameState::RootGameState(const std::string& name) |
---|
| 42 | : GameState(name) |
---|
| 43 | , stateRequest_("") |
---|
| 44 | { |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | RootGameState::~RootGameState() |
---|
| 48 | { |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | @brief |
---|
| 53 | Internal method that actually makes the state transition. Since it is internal, |
---|
| 54 | the method can assume certain things to be granted (like 'this' is always active). |
---|
| 55 | */ |
---|
| 56 | void RootGameState::makeTransition(GameState* source, GameState* destination) |
---|
| 57 | { |
---|
| 58 | if (source != 0) |
---|
| 59 | { |
---|
| 60 | // transition was not initiated by root itself |
---|
| 61 | this->activeChild_ = 0; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | if (destination == this) |
---|
| 65 | { |
---|
| 66 | // this marks the end of the game. |
---|
| 67 | return; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | // Check for 'destination' in the children map first |
---|
| 71 | std::map<GameState*, GameState*>::const_iterator it |
---|
| 72 | = this->grandchildrenToChildren_.find(destination); |
---|
| 73 | if (it != this->grandchildrenToChildren_.end()) |
---|
| 74 | { |
---|
| 75 | OrxAssert(dynamic_cast<GameState*>(it->second) != 0, |
---|
| 76 | "There was a mix with RootGameState and GameState, could not cast."); |
---|
| 77 | GameState* child = static_cast<GameState*>(it->second); |
---|
| 78 | // child state. Don't use 'state', might be a grandchild! |
---|
| 79 | this->activeChild_ = child; |
---|
| 80 | child->makeTransition(this, destination); |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
| 84 | // root doesn't have a parent.. |
---|
| 85 | OrxAssert(false, "GameState '" + destination->getName() + "' not found in children list of Root."); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void RootGameState::gotoState(const std::string& name) |
---|
| 90 | { |
---|
| 91 | GameState* request = getState(name); |
---|
| 92 | if (request) |
---|
| 93 | { |
---|
| 94 | GameState* current = getCurrentState(); |
---|
| 95 | if (current) |
---|
| 96 | { |
---|
| 97 | //OrxAssert(dynamic_cast<GameState*>(current), |
---|
| 98 | // "RootGameState: There was a RootGameState in the subtree of Root"); |
---|
| 99 | //GameState* currentGS = static_cast<GameState*>(current); |
---|
| 100 | //currentGS->makeTransition(this, request); |
---|
| 101 | current->makeTransition(0, request); |
---|
| 102 | } |
---|
| 103 | else |
---|
| 104 | { |
---|
| 105 | // Root is not yet active. This is a violation. |
---|
| 106 | ThrowException(GameState, "Activate Root before requesting a state."); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | else |
---|
| 110 | { |
---|
| 111 | COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | @brief |
---|
| 117 | Makes a state transition according to the state tree. You can choose any state |
---|
| 118 | in the tree to do the call. The function finds the current state on its own. |
---|
| 119 | @param state |
---|
| 120 | The state to be entered, has to exist in the tree. |
---|
| 121 | */ |
---|
| 122 | void RootGameState::requestState(const std::string& name) |
---|
| 123 | { |
---|
| 124 | this->stateRequest_ = name; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | @brief |
---|
| 129 | Starts the game. The little 'while' denotes the main loop. |
---|
| 130 | Whenever the root state is selected, the game ends. |
---|
| 131 | @param name |
---|
| 132 | State to start with (usually main menu or specified by command line) |
---|
| 133 | */ |
---|
| 134 | void RootGameState::start() |
---|
| 135 | { |
---|
| 136 | this->activate(); |
---|
| 137 | |
---|
| 138 | // get initial state from command line |
---|
| 139 | std::string initialState; |
---|
| 140 | CommandLine::getValue<std::string>("state", &initialState); |
---|
| 141 | gotoState(initialState); |
---|
| 142 | |
---|
| 143 | Ogre::Timer timer; |
---|
| 144 | uint64_t storedTime = 0; |
---|
| 145 | unsigned long lastTimersTime = 1; |
---|
| 146 | timer.reset(); |
---|
| 147 | while (this->activeChild_) |
---|
| 148 | { |
---|
| 149 | // get current time |
---|
| 150 | unsigned long timersTime = timer.getMicroseconds(); |
---|
| 151 | uint64_t realTime = storedTime + timersTime; |
---|
| 152 | float dt = (float)(timersTime - lastTimersTime)/1000000.0f; |
---|
| 153 | if (timersTime > 7000000) |
---|
| 154 | { |
---|
| 155 | // Under worst condition, the ogre timer will overflow right after 7 seconds |
---|
| 156 | storedTime += timersTime; |
---|
| 157 | lastTimersTime = 0; |
---|
| 158 | timer.reset(); |
---|
| 159 | } |
---|
| 160 | else |
---|
| 161 | { |
---|
| 162 | lastTimersTime = timersTime; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | this->tick(dt, realTime); |
---|
| 166 | |
---|
| 167 | if (this->stateRequest_ != "") |
---|
| 168 | gotoState(stateRequest_); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | this->deactivate(); |
---|
| 172 | } |
---|
| 173 | } |
---|