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