| 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 |  | 
|---|
| 31 | #include "core/Clock.h" | 
|---|
| 32 | #include "core/ConsoleCommand.h" | 
|---|
| 33 | #include "core/Game.h" | 
|---|
| 34 | #include "core/GameMode.h" | 
|---|
| 35 | #include "network/NetworkFunction.h" | 
|---|
| 36 | #include "tools/Timer.h" | 
|---|
| 37 | #include "interfaces/TimeFactorListener.h" | 
|---|
| 38 | #include "interfaces/Tickable.h" | 
|---|
| 39 | #include "LevelManager.h" | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 |     DeclareGameState(GSRoot, "root", false, false); | 
|---|
| 44 |  | 
|---|
| 45 |     GSRoot::GSRoot(const GameStateInfo& info) | 
|---|
| 46 |         : GameState(info) | 
|---|
| 47 |         , timeFactor_(1.0f) | 
|---|
| 48 |         , bPaused_(false) | 
|---|
| 49 |         , timeFactorPauseBackup_(1.0f) | 
|---|
| 50 |     { | 
|---|
| 51 |         this->ccSetTimeFactor_ = 0; | 
|---|
| 52 |         this->ccPause_ = 0; | 
|---|
| 53 |     } | 
|---|
| 54 |  | 
|---|
| 55 |     GSRoot::~GSRoot() | 
|---|
| 56 |     { | 
|---|
| 57 |         NetworkFunctionBase::destroyAllNetworkFunctions(); | 
|---|
| 58 |     } | 
|---|
| 59 |  | 
|---|
| 60 |     void GSRoot::activate() | 
|---|
| 61 |     { | 
|---|
| 62 |         // reset game speed to normal | 
|---|
| 63 |         this->timeFactor_ = 1.0f; | 
|---|
| 64 |  | 
|---|
| 65 |         { | 
|---|
| 66 |             // time factor console command | 
|---|
| 67 |             FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor); | 
|---|
| 68 |             functor->setObject(this); | 
|---|
| 69 |             this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor"); | 
|---|
| 70 |             CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0); | 
|---|
| 71 |         } | 
|---|
| 72 |  | 
|---|
| 73 |         { | 
|---|
| 74 |             // time factor console command | 
|---|
| 75 |             FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause); | 
|---|
| 76 |             functor->setObject(this); | 
|---|
| 77 |             this->ccPause_ = createConsoleCommand(functor, "pause"); | 
|---|
| 78 |             CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline); | 
|---|
| 79 |         } | 
|---|
| 80 |  | 
|---|
| 81 |         // create the LevelManager | 
|---|
| 82 |         this->levelManager_ = new LevelManager(); | 
|---|
| 83 |     } | 
|---|
| 84 |  | 
|---|
| 85 |     void GSRoot::deactivate() | 
|---|
| 86 |     { | 
|---|
| 87 | /* | 
|---|
| 88 |         if (this->ccSetTimeFactor_) | 
|---|
| 89 |         { | 
|---|
| 90 |             delete this->ccSetTimeFactor_; | 
|---|
| 91 |             this->ccSetTimeFactor_ = 0; | 
|---|
| 92 |         } | 
|---|
| 93 |  | 
|---|
| 94 |         if (this->ccPause_) | 
|---|
| 95 |         { | 
|---|
| 96 |             delete this->ccPause_; | 
|---|
| 97 |             this->ccPause_ = 0; | 
|---|
| 98 |         } | 
|---|
| 99 | */ | 
|---|
| 100 |  | 
|---|
| 101 |         delete this->levelManager_; | 
|---|
| 102 |     } | 
|---|
| 103 |  | 
|---|
| 104 |     void GSRoot::update(const Clock& time) | 
|---|
| 105 |     { | 
|---|
| 106 |         if (this->getActivity().topState) | 
|---|
| 107 |         { | 
|---|
| 108 |             // This state can not 'survive' on its own. | 
|---|
| 109 |             // Load a user interface therefore | 
|---|
| 110 |             Game::getInstance().requestState("ioConsole"); | 
|---|
| 111 |         } | 
|---|
| 112 |  | 
|---|
| 113 |         for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) | 
|---|
| 114 |             it->tick(time); | 
|---|
| 115 |  | 
|---|
| 116 |         /*** HACK *** HACK ***/ | 
|---|
| 117 |         // Call the Tickable objects | 
|---|
| 118 |         float leveldt = time.getDeltaTime(); | 
|---|
| 119 |         if (leveldt > 1.0f) | 
|---|
| 120 |         { | 
|---|
| 121 |             // just loaded | 
|---|
| 122 |             leveldt = 0.0f; | 
|---|
| 123 |         } | 
|---|
| 124 |         for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) | 
|---|
| 125 |             it->tick(leveldt * this->timeFactor_); | 
|---|
| 126 |         /*** HACK *** HACK ***/ | 
|---|
| 127 |     } | 
|---|
| 128 |  | 
|---|
| 129 |     /** | 
|---|
| 130 |     @brief | 
|---|
| 131 |         Changes the speed of Orxonox | 
|---|
| 132 |     @remark | 
|---|
| 133 |         This function is a hack when placed here! | 
|---|
| 134 |         Timefactor should be related to the scene (level or so), not the game | 
|---|
| 135 |     */ | 
|---|
| 136 |     void GSRoot::setTimeFactor(float factor) | 
|---|
| 137 |     { | 
|---|
| 138 |         if (GameMode::isMaster()) | 
|---|
| 139 |         { | 
|---|
| 140 |             if (!this->bPaused_) | 
|---|
| 141 |             { | 
|---|
| 142 |                 TimeFactorListener::timefactor_s = factor; | 
|---|
| 143 |  | 
|---|
| 144 |                 for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it) | 
|---|
| 145 |                     it->changedTimeFactor(factor, this->timeFactor_); | 
|---|
| 146 |  | 
|---|
| 147 |                 this->timeFactor_ = factor; | 
|---|
| 148 |             } | 
|---|
| 149 |             else | 
|---|
| 150 |                 this->timeFactorPauseBackup_ = factor; | 
|---|
| 151 |         } | 
|---|
| 152 |     } | 
|---|
| 153 |  | 
|---|
| 154 |     void GSRoot::pause() | 
|---|
| 155 |     { | 
|---|
| 156 |         if (GameMode::isMaster()) | 
|---|
| 157 |         { | 
|---|
| 158 |             if (!this->bPaused_) | 
|---|
| 159 |             { | 
|---|
| 160 |                 this->timeFactorPauseBackup_ = this->timeFactor_; | 
|---|
| 161 |                 this->setTimeFactor(0.0f); | 
|---|
| 162 |                 this->bPaused_ = true; | 
|---|
| 163 |             } | 
|---|
| 164 |             else | 
|---|
| 165 |             { | 
|---|
| 166 |                 this->bPaused_ = false; | 
|---|
| 167 |                 this->setTimeFactor(this->timeFactorPauseBackup_); | 
|---|
| 168 |             } | 
|---|
| 169 |         } | 
|---|
| 170 |     } | 
|---|
| 171 | } | 
|---|