| 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 "util/Clock.h" | 
|---|
| 32 | #include "core/BaseObject.h" | 
|---|
| 33 | #include "core/ConsoleCommand.h" | 
|---|
| 34 | #include "core/Game.h" | 
|---|
| 35 | #include "core/GameMode.h" | 
|---|
| 36 | #include "network/NetworkFunction.h" | 
|---|
| 37 | #include "tools/Timer.h" | 
|---|
| 38 | #include "tools/interfaces/TimeFactorListener.h" | 
|---|
| 39 | #include "tools/interfaces/Tickable.h" | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 |     DeclareGameState(GSRoot, "root", false, false); | 
|---|
| 44 |     SetConsoleCommandShortcut(GSRoot, printObjects); | 
|---|
| 45 |  | 
|---|
| 46 |     GSRoot::GSRoot(const GameStateInfo& info) | 
|---|
| 47 |         : GameState(info) | 
|---|
| 48 |         , bPaused_(false) | 
|---|
| 49 |         , timeFactorPauseBackup_(1.0f) | 
|---|
| 50 |     { | 
|---|
| 51 |     } | 
|---|
| 52 |  | 
|---|
| 53 |     GSRoot::~GSRoot() | 
|---|
| 54 |     { | 
|---|
| 55 |         NetworkFunctionBase::destroyAllNetworkFunctions(); | 
|---|
| 56 |     } | 
|---|
| 57 |  | 
|---|
| 58 |     void GSRoot::printObjects() | 
|---|
| 59 |     { | 
|---|
| 60 |         unsigned int nr=0; | 
|---|
| 61 |         for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ++it) | 
|---|
| 62 |         { | 
|---|
| 63 |             if (dynamic_cast<Synchronisable*>(*it)) | 
|---|
| 64 |                 COUT(0) << "object: " << it->getIdentifier()->getName() << " id: " << dynamic_cast<Synchronisable*>(*it)->getObjectID() << std::endl; | 
|---|
| 65 |             else | 
|---|
| 66 |                 COUT(0) << "object: " << it->getIdentifier()->getName() << std::endl; | 
|---|
| 67 |             nr++; | 
|---|
| 68 |         } | 
|---|
| 69 |         COUT(0) << "currently got " << nr << " objects" << std::endl; | 
|---|
| 70 |     } | 
|---|
| 71 |  | 
|---|
| 72 |     void GSRoot::activate() | 
|---|
| 73 |     { | 
|---|
| 74 |         // reset game speed to normal | 
|---|
| 75 |         TimeFactorListener::setTimeFactor(1.0f); | 
|---|
| 76 |  | 
|---|
| 77 |         // time factor console command | 
|---|
| 78 |         ConsoleCommand* command = createConsoleCommand(createFunctor(&GSRoot::setTimeFactor, this), "setTimeFactor"); | 
|---|
| 79 |         CommandExecutor::addConsoleCommandShortcut(command).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0); | 
|---|
| 80 |  | 
|---|
| 81 |         // time factor console command | 
|---|
| 82 |         command = createConsoleCommand(createFunctor(&GSRoot::pause, this), "pause"); | 
|---|
| 83 |         CommandExecutor::addConsoleCommandShortcut(command).accessLevel(AccessLevel::Offline); | 
|---|
| 84 |     } | 
|---|
| 85 |  | 
|---|
| 86 |     void GSRoot::deactivate() | 
|---|
| 87 |     { | 
|---|
| 88 |     } | 
|---|
| 89 |  | 
|---|
| 90 |     void GSRoot::update(const Clock& time) | 
|---|
| 91 |     { | 
|---|
| 92 |         for (ObjectList<Timer>::iterator it = ObjectList<Timer>::begin(); it; ) | 
|---|
| 93 |             (it++)->tick(time); | 
|---|
| 94 |  | 
|---|
| 95 |         /*** HACK *** HACK ***/ | 
|---|
| 96 |         // Call the Tickable objects | 
|---|
| 97 |         float leveldt = time.getDeltaTime(); | 
|---|
| 98 |         if (leveldt > 1.0f) | 
|---|
| 99 |         { | 
|---|
| 100 |             // just loaded | 
|---|
| 101 |             leveldt = 0.0f; | 
|---|
| 102 |         } | 
|---|
| 103 |         for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ) | 
|---|
| 104 |             (it++)->tick(leveldt * TimeFactorListener::getTimeFactor()); | 
|---|
| 105 |         /*** HACK *** HACK ***/ | 
|---|
| 106 |     } | 
|---|
| 107 |  | 
|---|
| 108 |     /** | 
|---|
| 109 |     @brief | 
|---|
| 110 |         Changes the speed of Orxonox | 
|---|
| 111 |     @remark | 
|---|
| 112 |         This function is a hack when placed here! | 
|---|
| 113 |         Timefactor should be related to the scene (level or so), not the game | 
|---|
| 114 |     */ | 
|---|
| 115 |     void GSRoot::setTimeFactor(float factor) | 
|---|
| 116 |     { | 
|---|
| 117 |         if (GameMode::isMaster()) | 
|---|
| 118 |         { | 
|---|
| 119 |             if (!this->bPaused_) | 
|---|
| 120 |             { | 
|---|
| 121 |                 TimeFactorListener::setTimeFactor(factor); | 
|---|
| 122 |             } | 
|---|
| 123 |             else | 
|---|
| 124 |                 this->timeFactorPauseBackup_ = factor; | 
|---|
| 125 |         } | 
|---|
| 126 |     } | 
|---|
| 127 |  | 
|---|
| 128 |     void GSRoot::pause() | 
|---|
| 129 |     { | 
|---|
| 130 |         if (GameMode::isMaster()) | 
|---|
| 131 |         { | 
|---|
| 132 |             if (!this->bPaused_) | 
|---|
| 133 |             { | 
|---|
| 134 |                 this->timeFactorPauseBackup_ = TimeFactorListener::getTimeFactor(); | 
|---|
| 135 |                 this->setTimeFactor(0.0f); | 
|---|
| 136 |                 this->bPaused_ = true; | 
|---|
| 137 |             } | 
|---|
| 138 |             else | 
|---|
| 139 |             { | 
|---|
| 140 |                 this->bPaused_ = false; | 
|---|
| 141 |                 this->setTimeFactor(this->timeFactorPauseBackup_); | 
|---|
| 142 |             } | 
|---|
| 143 |         } | 
|---|
| 144 |     } | 
|---|
| 145 |  | 
|---|
| 146 |     float GSRoot::getTimeFactor() | 
|---|
| 147 |     { | 
|---|
| 148 |         return TimeFactorListener::getTimeFactor(); | 
|---|
| 149 |     } | 
|---|
| 150 | } | 
|---|