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