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