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