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