[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: |
---|
[1662] | 25 | * Fabian 'x3n' Landau |
---|
[1661] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | #include "GSLevel.h" |
---|
| 31 | |
---|
| 32 | #include "core/input/InputManager.h" |
---|
| 33 | #include "core/input/SimpleInputState.h" |
---|
| 34 | #include "core/input/KeyBinder.h" |
---|
[1662] | 35 | #include "core/Loader.h" |
---|
[2087] | 36 | #include "core/XMLFile.h" |
---|
[1887] | 37 | #include "core/CommandExecutor.h" |
---|
| 38 | #include "core/ConsoleCommand.h" |
---|
[1934] | 39 | #include "core/CommandLine.h" |
---|
[1887] | 40 | #include "core/ConfigValueIncludes.h" |
---|
| 41 | #include "core/CoreIncludes.h" |
---|
[2087] | 42 | #include "core/Core.h" |
---|
| 43 | //#include "objects/Backlight.h" |
---|
[1694] | 44 | #include "objects/Tickable.h" |
---|
[1819] | 45 | #include "objects/Radar.h" |
---|
[2087] | 46 | //#include "tools/ParticleInterface.h" |
---|
| 47 | #include "CameraManager.h" |
---|
| 48 | #include "LevelManager.h" |
---|
[2344] | 49 | #include "PlayerManager.h" |
---|
[1670] | 50 | #include "Settings.h" |
---|
[1661] | 51 | |
---|
| 52 | namespace orxonox |
---|
| 53 | { |
---|
[2173] | 54 | SetCommandLineArgument(level, "sample3.oxw").shortcut("l"); |
---|
[1934] | 55 | |
---|
[2087] | 56 | GSLevel::GSLevel() |
---|
| 57 | // : GameState<GSGraphics>(name) |
---|
| 58 | : timeFactor_(1.0f) |
---|
[1662] | 59 | , keyBinder_(0) |
---|
[1670] | 60 | , inputState_(0) |
---|
[1662] | 61 | , radar_(0) |
---|
[2087] | 62 | , startFile_(0) |
---|
| 63 | , cameraManager_(0) |
---|
| 64 | , levelManager_(0) |
---|
[1661] | 65 | { |
---|
[1887] | 66 | RegisterObject(GSLevel); |
---|
| 67 | setConfigValues(); |
---|
[1661] | 68 | } |
---|
| 69 | |
---|
| 70 | GSLevel::~GSLevel() |
---|
| 71 | { |
---|
| 72 | } |
---|
| 73 | |
---|
[1887] | 74 | void GSLevel::setConfigValues() |
---|
| 75 | { |
---|
| 76 | SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName="); |
---|
[2103] | 77 | SetConfigValue(defaultKeybindings_, "def_keybindings.ini") |
---|
| 78 | .description("Filename of default keybindings."); |
---|
[1887] | 79 | } |
---|
| 80 | |
---|
[2087] | 81 | void GSLevel::enter(Ogre::Viewport* viewport) |
---|
[1661] | 82 | { |
---|
[2087] | 83 | if (Core::showsGraphics()) |
---|
| 84 | { |
---|
| 85 | inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20); |
---|
| 86 | keyBinder_ = new KeyBinder(); |
---|
[2103] | 87 | keyBinder_->loadBindings("keybindings.ini", defaultKeybindings_); |
---|
[2087] | 88 | inputState_->setHandler(keyBinder_); |
---|
[1661] | 89 | |
---|
[2087] | 90 | // create the global CameraManager |
---|
| 91 | assert(viewport); |
---|
| 92 | this->cameraManager_ = new CameraManager(viewport); |
---|
[1688] | 93 | |
---|
[2087] | 94 | // Start the Radar |
---|
| 95 | this->radar_ = new Radar(); |
---|
| 96 | } |
---|
[1662] | 97 | |
---|
[2087] | 98 | if (Core::isMaster()) |
---|
| 99 | { |
---|
| 100 | // create the global LevelManager |
---|
| 101 | this->levelManager_ = new LevelManager(); |
---|
[2344] | 102 | this->playerManager_ = new PlayerManager(); |
---|
[1662] | 103 | |
---|
[2087] | 104 | // reset game speed to normal |
---|
| 105 | timeFactor_ = 1.0f; |
---|
[1674] | 106 | |
---|
[2087] | 107 | this->loadLevel(); |
---|
| 108 | } |
---|
[1755] | 109 | |
---|
[2087] | 110 | if (Core::showsGraphics()) |
---|
| 111 | { |
---|
| 112 | // TODO: insert slomo console command with |
---|
| 113 | // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false); |
---|
[1887] | 114 | |
---|
[2087] | 115 | // keybind console command |
---|
| 116 | FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind); |
---|
| 117 | functor1->setObject(this); |
---|
[2344] | 118 | ccKeybind_ = createConsoleCommand(functor1, "keybind"); |
---|
| 119 | CommandExecutor::addConsoleCommandShortcut(ccKeybind_); |
---|
[2087] | 120 | FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind); |
---|
| 121 | functor2->setObject(this); |
---|
[2344] | 122 | ccTkeybind_ = createConsoleCommand(functor2, "tkeybind"); |
---|
| 123 | CommandExecutor::addConsoleCommandShortcut(ccTkeybind_); |
---|
[2087] | 124 | // set our console command as callback for the key detector |
---|
| 125 | InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_); |
---|
| 126 | |
---|
| 127 | // level is loaded: we can start capturing the input |
---|
| 128 | InputManager::getInstance().requestEnterState("game"); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if (Core::isMaster()) |
---|
| 132 | { |
---|
| 133 | // time factor console command |
---|
| 134 | FunctorMember<GSLevel>* functor = createFunctor(&GSLevel::setTimeFactor); |
---|
| 135 | functor->setObject(this); |
---|
[2344] | 136 | ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor"); |
---|
| 137 | CommandExecutor::addConsoleCommandShortcut(ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);; |
---|
[2087] | 138 | } |
---|
[1661] | 139 | } |
---|
| 140 | |
---|
| 141 | void GSLevel::leave() |
---|
| 142 | { |
---|
[2344] | 143 | // destroy console commands |
---|
| 144 | delete this->ccKeybind_; |
---|
| 145 | delete this->ccSetTimeFactor_; |
---|
| 146 | delete this->ccTkeybind_; |
---|
| 147 | |
---|
[1662] | 148 | // this call will delete every BaseObject! |
---|
| 149 | // But currently this will call methods of objects that exist no more |
---|
| 150 | // The only 'memory leak' is the ParticleSpawer. They would be deleted here |
---|
| 151 | // and call a sceneNode method that has already been destroy by the corresponding space ship. |
---|
| 152 | //Loader::close(); |
---|
| 153 | |
---|
[2087] | 154 | if (Core::showsGraphics()) |
---|
| 155 | InputManager::getInstance().requestLeaveState("game"); |
---|
[1662] | 156 | |
---|
[2087] | 157 | if (Core::isMaster()) |
---|
| 158 | this->unloadLevel(); |
---|
[1662] | 159 | |
---|
[2087] | 160 | if (this->radar_) |
---|
| 161 | delete this->radar_; |
---|
| 162 | |
---|
| 163 | if (this->cameraManager_) |
---|
| 164 | delete this->cameraManager_; |
---|
| 165 | |
---|
| 166 | if (this->levelManager_) |
---|
| 167 | delete this->levelManager_; |
---|
| 168 | |
---|
[2344] | 169 | if (this->playerManager_) |
---|
| 170 | delete this->playerManager_; |
---|
| 171 | |
---|
[2087] | 172 | if (Core::showsGraphics()) |
---|
| 173 | { |
---|
| 174 | inputState_->setHandler(0); |
---|
| 175 | InputManager::getInstance().requestDestroyState("game"); |
---|
| 176 | if (this->keyBinder_) |
---|
| 177 | delete this->keyBinder_; |
---|
| 178 | } |
---|
[1661] | 179 | } |
---|
| 180 | |
---|
[1674] | 181 | void GSLevel::ticked(const Clock& time) |
---|
[1661] | 182 | { |
---|
[2087] | 183 | // Commented by 1337: Temporarily moved to GSGraphics. |
---|
| 184 | //// Call the scene objects |
---|
| 185 | //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
| 186 | // it->tick(time.getDeltaTime() * this->timeFactor_); |
---|
[1661] | 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | @brief |
---|
| 191 | Changes the speed of Orxonox |
---|
| 192 | */ |
---|
[1662] | 193 | void GSLevel::setTimeFactor(float factor) |
---|
| 194 | { |
---|
[2087] | 195 | /* |
---|
[1674] | 196 | float change = factor / this->timeFactor_; |
---|
[2087] | 197 | */ |
---|
[1674] | 198 | this->timeFactor_ = factor; |
---|
[2087] | 199 | /* |
---|
[1755] | 200 | for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it) |
---|
[1662] | 201 | it->setSpeedFactor(it->getSpeedFactor() * change); |
---|
[1661] | 202 | |
---|
[1755] | 203 | for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it) |
---|
[1674] | 204 | it->setTimeFactor(timeFactor_); |
---|
[2087] | 205 | */ |
---|
[1662] | 206 | } |
---|
[1670] | 207 | |
---|
| 208 | void GSLevel::loadLevel() |
---|
| 209 | { |
---|
| 210 | // call the loader |
---|
| 211 | COUT(0) << "Loading level..." << std::endl; |
---|
[1934] | 212 | std::string levelName; |
---|
| 213 | CommandLine::getValue("level", &levelName); |
---|
[2087] | 214 | startFile_ = new XMLFile(Settings::getDataPath() + std::string("levels/") + levelName); |
---|
| 215 | Loader::open(startFile_); |
---|
[1670] | 216 | } |
---|
| 217 | |
---|
| 218 | void GSLevel::unloadLevel() |
---|
| 219 | { |
---|
[2087] | 220 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 221 | // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // |
---|
| 222 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 223 | // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY / |
---|
| 224 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 225 | |
---|
| 226 | delete this->startFile_; |
---|
[1670] | 227 | } |
---|
[1887] | 228 | |
---|
| 229 | void GSLevel::keybind(const std::string &command) |
---|
| 230 | { |
---|
| 231 | this->keybindInternal(command, false); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | void GSLevel::tkeybind(const std::string &command) |
---|
| 235 | { |
---|
| 236 | this->keybindInternal(command, true); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | @brief |
---|
| 241 | Assigns a command string to a key/button/axis. The name is determined via KeyDetector. |
---|
| 242 | @param command |
---|
| 243 | Command string that can be executed by the CommandExecutor |
---|
| 244 | OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify |
---|
| 245 | the key/button/axis that has been activated. This is configured above in enter(). |
---|
| 246 | */ |
---|
| 247 | void GSLevel::keybindInternal(const std::string& command, bool bTemporary) |
---|
| 248 | { |
---|
[2087] | 249 | if (Core::showsGraphics()) |
---|
[1887] | 250 | { |
---|
[2087] | 251 | static std::string bindingString = ""; |
---|
| 252 | static bool bTemporarySaved = false; |
---|
| 253 | static bool bound = true; |
---|
| 254 | // note: We use a long name to make 'sure' that the user doesn't use it accidentally. |
---|
| 255 | // Howerver there will be no real issue if it happens anyway. |
---|
| 256 | if (command.find(keyDetectorCallbackCode_) != 0) |
---|
[1887] | 257 | { |
---|
[2087] | 258 | if (bound) |
---|
| 259 | { |
---|
| 260 | COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; |
---|
| 261 | InputManager::getInstance().requestEnterState("detector"); |
---|
| 262 | bindingString = command; |
---|
| 263 | bTemporarySaved = bTemporary; |
---|
| 264 | bound = false; |
---|
| 265 | } |
---|
| 266 | //else: We're still in a keybind command. ignore this call. |
---|
[1887] | 267 | } |
---|
[2087] | 268 | else |
---|
[1887] | 269 | { |
---|
[2087] | 270 | if (!bound) |
---|
| 271 | { |
---|
| 272 | // user has pressed the key |
---|
| 273 | std::string name = command.substr(this->keyDetectorCallbackCode_.size()); |
---|
| 274 | COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl; |
---|
| 275 | this->keyBinder_->setBinding(bindingString, name, bTemporarySaved); |
---|
| 276 | InputManager::getInstance().requestLeaveState("detector"); |
---|
| 277 | bound = true; |
---|
| 278 | } |
---|
| 279 | // else: A key was pressed within the same tick, ignore it. |
---|
[1887] | 280 | } |
---|
| 281 | } |
---|
| 282 | } |
---|
[1661] | 283 | } |
---|