[1661] | 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 "OrxonoxStableHeaders.h" |
---|
| 30 | #include "GSRoot.h" |
---|
| 31 | |
---|
[1764] | 32 | #include "util/Exception.h" |
---|
| 33 | #include "util/Debug.h" |
---|
[2844] | 34 | #include "core/Clock.h" |
---|
[2848] | 35 | #include "core/Game.h" |
---|
| 36 | #include "core/GameMode.h" |
---|
[2845] | 37 | #include "core/CommandLine.h" |
---|
[1661] | 38 | #include "core/ConsoleCommand.h" |
---|
[2843] | 39 | #include "tools/TimeFactorListener.h" |
---|
[1826] | 40 | #include "tools/Timer.h" |
---|
[2171] | 41 | #include "objects/Tickable.h" |
---|
[1661] | 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
[2844] | 45 | AddGameState(GSRoot, "root"); |
---|
[2850] | 46 | SetCommandLineSwitch(console); |
---|
| 47 | // Shortcuts for easy direct loading |
---|
| 48 | SetCommandLineSwitch(server); |
---|
| 49 | SetCommandLineSwitch(client); |
---|
| 50 | SetCommandLineSwitch(dedicated); |
---|
| 51 | SetCommandLineSwitch(standalone); |
---|
[2844] | 52 | |
---|
| 53 | GSRoot::GSRoot(const std::string& name) |
---|
| 54 | : GameState(name) |
---|
[2662] | 55 | , timeFactor_(1.0f) |
---|
| 56 | , bPaused_(false) |
---|
| 57 | , timeFactorPauseBackup_(1.0f) |
---|
[1661] | 58 | { |
---|
[2662] | 59 | this->ccSetTimeFactor_ = 0; |
---|
| 60 | this->ccPause_ = 0; |
---|
[1661] | 61 | } |
---|
| 62 | |
---|
| 63 | GSRoot::~GSRoot() |
---|
| 64 | { |
---|
| 65 | } |
---|
| 66 | |
---|
[2844] | 67 | void GSRoot::activate() |
---|
[1661] | 68 | { |
---|
[2662] | 69 | // reset game speed to normal |
---|
[2850] | 70 | this->timeFactor_ = 1.0f; |
---|
[2662] | 71 | |
---|
| 72 | { |
---|
| 73 | // time factor console command |
---|
| 74 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor); |
---|
| 75 | functor->setObject(this); |
---|
| 76 | this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor"); |
---|
| 77 | CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | { |
---|
| 81 | // time factor console command |
---|
| 82 | FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause); |
---|
| 83 | functor->setObject(this); |
---|
| 84 | this->ccPause_ = createConsoleCommand(functor, "pause"); |
---|
| 85 | CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline); |
---|
| 86 | } |
---|
[2845] | 87 | |
---|
[2850] | 88 | // Load level directly? |
---|
| 89 | bool loadLevel = false; |
---|
| 90 | if (CommandLine::getValue("standalone").getBool()) |
---|
[2845] | 91 | { |
---|
[2850] | 92 | Game::getInstance().requestStates("graphics, standalone, level"); |
---|
| 93 | loadLevel = true; |
---|
[2845] | 94 | } |
---|
[2850] | 95 | if (CommandLine::getValue("server").getBool()) |
---|
[2845] | 96 | { |
---|
[2850] | 97 | Game::getInstance().requestStates("graphics, server, level"); |
---|
| 98 | loadLevel = true; |
---|
[2845] | 99 | } |
---|
[2850] | 100 | if (CommandLine::getValue("client").getBool()) |
---|
| 101 | { |
---|
| 102 | Game::getInstance().requestStates("graphics, standalone, level"); |
---|
| 103 | loadLevel = true; |
---|
| 104 | } |
---|
| 105 | if (CommandLine::getValue("dedicated").getBool()) |
---|
| 106 | { |
---|
| 107 | Game::getInstance().requestStates("dedicated, level"); |
---|
| 108 | loadLevel = true; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | // Determine where to start |
---|
| 112 | if (!loadLevel) |
---|
| 113 | { |
---|
| 114 | if (CommandLine::getValue("console").getBool()) |
---|
| 115 | { |
---|
| 116 | // Start the game in the console without 3D graphics |
---|
| 117 | Game::getInstance().requestState("ioConsole"); |
---|
| 118 | } |
---|
| 119 | else |
---|
| 120 | { |
---|
| 121 | // Start in GUI mode |
---|
| 122 | Game::getInstance().requestStates("graphics, mainMenu"); |
---|
| 123 | } |
---|
| 124 | } |
---|
[1661] | 125 | } |
---|
| 126 | |
---|
[2844] | 127 | void GSRoot::deactivate() |
---|
[1661] | 128 | { |
---|
[2662] | 129 | if (this->ccSetTimeFactor_) |
---|
| 130 | { |
---|
| 131 | delete this->ccSetTimeFactor_; |
---|
| 132 | this->ccSetTimeFactor_ = 0; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | if (this->ccPause_) |
---|
| 136 | { |
---|
| 137 | delete this->ccPause_; |
---|
| 138 | this->ccPause_ = 0; |
---|
| 139 | } |
---|
[1661] | 140 | } |
---|
| 141 | |
---|
[2844] | 142 | void GSRoot::update(const Clock& time) |
---|
[1661] | 143 | { |
---|
[2850] | 144 | if (this->getActivity().topState) |
---|
| 145 | { |
---|
| 146 | // This state can not 'survive' on its own. |
---|
| 147 | // Load a user interface therefore |
---|
| 148 | Game::getInstance().requestState("ioConsole"); |
---|
| 149 | } |
---|
| 150 | |
---|
[2662] | 151 | uint64_t timeBeforeTick = time.getRealMicroseconds(); |
---|
| 152 | |
---|
[1826] | 153 | for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) |
---|
| 154 | it->tick(time); |
---|
| 155 | |
---|
[2171] | 156 | /*** HACK *** HACK ***/ |
---|
| 157 | // Call the Tickable objects |
---|
[2662] | 158 | float leveldt = time.getDeltaTime(); |
---|
| 159 | if (leveldt > 1.0f) |
---|
| 160 | { |
---|
| 161 | // just loaded |
---|
| 162 | leveldt = 0.0f; |
---|
| 163 | } |
---|
[2171] | 164 | for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
[2662] | 165 | it->tick(leveldt * this->timeFactor_); |
---|
[2171] | 166 | /*** HACK *** HACK ***/ |
---|
| 167 | |
---|
[2662] | 168 | uint64_t timeAfterTick = time.getRealMicroseconds(); |
---|
| 169 | |
---|
[2850] | 170 | // Also add our tick time |
---|
[2817] | 171 | Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); |
---|
[1662] | 172 | } |
---|
[1674] | 173 | |
---|
| 174 | /** |
---|
[2662] | 175 | @brief |
---|
| 176 | Changes the speed of Orxonox |
---|
| 177 | */ |
---|
| 178 | void GSRoot::setTimeFactor(float factor) |
---|
| 179 | { |
---|
[2848] | 180 | if (GameMode::isMaster()) |
---|
[2662] | 181 | { |
---|
| 182 | if (!this->bPaused_) |
---|
| 183 | { |
---|
| 184 | TimeFactorListener::timefactor_s = factor; |
---|
| 185 | |
---|
| 186 | for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it) |
---|
| 187 | it->changedTimeFactor(factor, this->timeFactor_); |
---|
| 188 | |
---|
| 189 | this->timeFactor_ = factor; |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | this->timeFactorPauseBackup_ = factor; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | void GSRoot::pause() |
---|
| 197 | { |
---|
[2848] | 198 | if (GameMode::isMaster()) |
---|
[2662] | 199 | { |
---|
| 200 | if (!this->bPaused_) |
---|
| 201 | { |
---|
| 202 | this->timeFactorPauseBackup_ = this->timeFactor_; |
---|
| 203 | this->setTimeFactor(0.0f); |
---|
| 204 | this->bPaused_ = true; |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | { |
---|
| 208 | this->bPaused_ = false; |
---|
| 209 | this->setTimeFactor(this->timeFactorPauseBackup_); |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|
[1661] | 213 | } |
---|