[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 "GSRoot.h" |
---|
| 30 | |
---|
[2896] | 31 | #include "core/Clock.h" |
---|
[3196] | 32 | #include "core/ConsoleCommand.h" |
---|
[2896] | 33 | #include "core/Game.h" |
---|
| 34 | #include "core/GameMode.h" |
---|
[3340] | 35 | #include "core/LuaBind.h" |
---|
[3304] | 36 | #include "network/NetworkFunction.h" |
---|
[3340] | 37 | #include "ToluaBindCore.h" |
---|
| 38 | #include "ToluaBindOrxonox.h" |
---|
[1826] | 39 | #include "tools/Timer.h" |
---|
[3196] | 40 | #include "interfaces/TimeFactorListener.h" |
---|
| 41 | #include "interfaces/Tickable.h" |
---|
[3280] | 42 | #include "LevelManager.h" |
---|
[1661] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
[3349] | 46 | DeclareGameState(GSRoot, "root", false, false); |
---|
[1663] | 47 | |
---|
[3355] | 48 | GSRoot::GSRoot(const GameStateInfo& info) |
---|
| 49 | : GameState(info) |
---|
[2662] | 50 | , timeFactor_(1.0f) |
---|
| 51 | , bPaused_(false) |
---|
| 52 | , timeFactorPauseBackup_(1.0f) |
---|
[1661] | 53 | { |
---|
[2662] | 54 | this->ccSetTimeFactor_ = 0; |
---|
| 55 | this->ccPause_ = 0; |
---|
[3340] | 56 | |
---|
| 57 | // Tell LuaBind about all tolua interfaces |
---|
| 58 | LuaBind::getInstance().addToluaInterface(&tolua_Core_open, "Core"); |
---|
| 59 | LuaBind::getInstance().addToluaInterface(&tolua_Orxonox_open, "Orxonox"); |
---|
[1661] | 60 | } |
---|
| 61 | |
---|
| 62 | GSRoot::~GSRoot() |
---|
| 63 | { |
---|
[3304] | 64 | NetworkFunctionBase::destroyAllNetworkFunctions(); |
---|
[1661] | 65 | } |
---|
| 66 | |
---|
[2896] | 67 | void GSRoot::activate() |
---|
[1686] | 68 | { |
---|
[2662] | 69 | // reset game speed to normal |
---|
[2896] | 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 | } |
---|
[2896] | 87 | |
---|
[3280] | 88 | // create the global LevelManager |
---|
| 89 | this->levelManager_ = new LevelManager(); |
---|
[1661] | 90 | } |
---|
| 91 | |
---|
[2896] | 92 | void GSRoot::deactivate() |
---|
[1661] | 93 | { |
---|
[2928] | 94 | /* |
---|
[2662] | 95 | if (this->ccSetTimeFactor_) |
---|
| 96 | { |
---|
| 97 | delete this->ccSetTimeFactor_; |
---|
| 98 | this->ccSetTimeFactor_ = 0; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | if (this->ccPause_) |
---|
| 102 | { |
---|
| 103 | delete this->ccPause_; |
---|
| 104 | this->ccPause_ = 0; |
---|
| 105 | } |
---|
[2928] | 106 | */ |
---|
[3280] | 107 | |
---|
| 108 | delete this->levelManager_; |
---|
[1661] | 109 | } |
---|
| 110 | |
---|
[2896] | 111 | void GSRoot::update(const Clock& time) |
---|
[1661] | 112 | { |
---|
[2896] | 113 | if (this->getActivity().topState) |
---|
| 114 | { |
---|
| 115 | // This state can not 'survive' on its own. |
---|
| 116 | // Load a user interface therefore |
---|
| 117 | Game::getInstance().requestState("ioConsole"); |
---|
| 118 | } |
---|
| 119 | |
---|
[1826] | 120 | for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) |
---|
| 121 | it->tick(time); |
---|
| 122 | |
---|
[2171] | 123 | /*** HACK *** HACK ***/ |
---|
| 124 | // Call the Tickable objects |
---|
[2662] | 125 | float leveldt = time.getDeltaTime(); |
---|
| 126 | if (leveldt > 1.0f) |
---|
| 127 | { |
---|
| 128 | // just loaded |
---|
| 129 | leveldt = 0.0f; |
---|
| 130 | } |
---|
[2171] | 131 | for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
[2662] | 132 | it->tick(leveldt * this->timeFactor_); |
---|
[2171] | 133 | /*** HACK *** HACK ***/ |
---|
[1662] | 134 | } |
---|
[1674] | 135 | |
---|
| 136 | /** |
---|
[2662] | 137 | @brief |
---|
| 138 | Changes the speed of Orxonox |
---|
[3355] | 139 | @remark |
---|
| 140 | This function is a hack when placed here! |
---|
| 141 | Timefactor should be related to the scene (level or so), not the game |
---|
[2662] | 142 | */ |
---|
| 143 | void GSRoot::setTimeFactor(float factor) |
---|
| 144 | { |
---|
[2896] | 145 | if (GameMode::isMaster()) |
---|
[2662] | 146 | { |
---|
| 147 | if (!this->bPaused_) |
---|
| 148 | { |
---|
| 149 | TimeFactorListener::timefactor_s = factor; |
---|
| 150 | |
---|
| 151 | for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it) |
---|
| 152 | it->changedTimeFactor(factor, this->timeFactor_); |
---|
| 153 | |
---|
| 154 | this->timeFactor_ = factor; |
---|
| 155 | } |
---|
| 156 | else |
---|
| 157 | this->timeFactorPauseBackup_ = factor; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | void GSRoot::pause() |
---|
| 162 | { |
---|
[2896] | 163 | if (GameMode::isMaster()) |
---|
[2662] | 164 | { |
---|
| 165 | if (!this->bPaused_) |
---|
| 166 | { |
---|
| 167 | this->timeFactorPauseBackup_ = this->timeFactor_; |
---|
| 168 | this->setTimeFactor(0.0f); |
---|
| 169 | this->bPaused_ = true; |
---|
| 170 | } |
---|
| 171 | else |
---|
| 172 | { |
---|
| 173 | this->bPaused_ = false; |
---|
| 174 | this->setTimeFactor(this->timeFactorPauseBackup_); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
[1661] | 178 | } |
---|