[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 |
---|
[2896] | 26 | * Benjamin Knecht |
---|
[1661] | 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "GSLevel.h" |
---|
| 31 | |
---|
| 32 | #include "core/input/InputManager.h" |
---|
| 33 | #include "core/input/SimpleInputState.h" |
---|
| 34 | #include "core/input/KeyBinder.h" |
---|
[3196] | 35 | #include "core/Clock.h" |
---|
| 36 | #include "core/CommandLine.h" |
---|
[1887] | 37 | #include "core/ConsoleCommand.h" |
---|
| 38 | #include "core/ConfigValueIncludes.h" |
---|
| 39 | #include "core/CoreIncludes.h" |
---|
[2896] | 40 | #include "core/Game.h" |
---|
| 41 | #include "core/GameMode.h" |
---|
[3196] | 42 | #include "core/Core.h" |
---|
| 43 | #include "core/Loader.h" |
---|
| 44 | #include "core/XMLFile.h" |
---|
| 45 | |
---|
| 46 | #include "interfaces/Tickable.h" |
---|
[1819] | 47 | #include "objects/Radar.h" |
---|
[3196] | 48 | #include "objects/quest/QuestManager.h" |
---|
| 49 | #include "overlays/notifications/NotificationManager.h" |
---|
| 50 | #include "gui/GUIManager.h" |
---|
[2087] | 51 | #include "CameraManager.h" |
---|
[2896] | 52 | #include "GraphicsManager.h" |
---|
[2087] | 53 | #include "LevelManager.h" |
---|
[2662] | 54 | #include "PlayerManager.h" |
---|
[1661] | 55 | |
---|
| 56 | namespace orxonox |
---|
| 57 | { |
---|
[2896] | 58 | AddGameState(GSLevel, "level"); |
---|
| 59 | |
---|
[3036] | 60 | SetCommandLineArgument(level, "").shortcut("l"); |
---|
[2896] | 61 | SetConsoleCommand(GSLevel, showIngameGUI, true); |
---|
[1934] | 62 | |
---|
[3008] | 63 | XMLFile* GSLevel::startFile_s = NULL; |
---|
| 64 | |
---|
[2896] | 65 | GSLevel::GSLevel(const std::string& name) |
---|
| 66 | : GameState(name) |
---|
| 67 | , keyBinder_(0) |
---|
| 68 | , gameInputState_(0) |
---|
| 69 | , guiMouseOnlyInputState_(0) |
---|
| 70 | , guiKeysOnlyInputState_(0) |
---|
[1662] | 71 | , radar_(0) |
---|
[2087] | 72 | , cameraManager_(0) |
---|
| 73 | , levelManager_(0) |
---|
[1661] | 74 | { |
---|
[1887] | 75 | RegisterObject(GSLevel); |
---|
[2662] | 76 | |
---|
| 77 | this->ccKeybind_ = 0; |
---|
| 78 | this->ccTkeybind_ = 0; |
---|
[1661] | 79 | } |
---|
| 80 | |
---|
| 81 | GSLevel::~GSLevel() |
---|
| 82 | { |
---|
| 83 | } |
---|
| 84 | |
---|
[1887] | 85 | void GSLevel::setConfigValues() |
---|
| 86 | { |
---|
| 87 | SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName="); |
---|
| 88 | } |
---|
| 89 | |
---|
[2896] | 90 | void GSLevel::activate() |
---|
[1661] | 91 | { |
---|
[2896] | 92 | setConfigValues(); |
---|
| 93 | |
---|
| 94 | if (GameMode::showsGraphics()) |
---|
[2087] | 95 | { |
---|
[2896] | 96 | gameInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game"); |
---|
[2087] | 97 | keyBinder_ = new KeyBinder(); |
---|
[2710] | 98 | keyBinder_->loadBindings("keybindings.ini"); |
---|
[2896] | 99 | gameInputState_->setHandler(keyBinder_); |
---|
[1661] | 100 | |
---|
[2896] | 101 | guiMouseOnlyInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("guiMouseOnly"); |
---|
| 102 | guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr()); |
---|
| 103 | |
---|
| 104 | guiKeysOnlyInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("guiKeysOnly"); |
---|
| 105 | guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr()); |
---|
| 106 | |
---|
[2087] | 107 | // create the global CameraManager |
---|
[2896] | 108 | this->cameraManager_ = new CameraManager(GraphicsManager::getInstance().getViewport()); |
---|
[1688] | 109 | |
---|
[2087] | 110 | // Start the Radar |
---|
| 111 | this->radar_ = new Radar(); |
---|
| 112 | } |
---|
[1662] | 113 | |
---|
[2662] | 114 | this->playerManager_ = new PlayerManager(); |
---|
| 115 | |
---|
[2911] | 116 | this->questManager_ = new QuestManager(); |
---|
| 117 | |
---|
| 118 | this->notificationManager_ = new NotificationManager(); |
---|
| 119 | |
---|
[2896] | 120 | if (GameMode::isMaster()) |
---|
[2087] | 121 | { |
---|
| 122 | // create the global LevelManager |
---|
| 123 | this->levelManager_ = new LevelManager(); |
---|
[1662] | 124 | |
---|
[2087] | 125 | this->loadLevel(); |
---|
| 126 | } |
---|
[1755] | 127 | |
---|
[2896] | 128 | if (GameMode::showsGraphics()) |
---|
[2087] | 129 | { |
---|
| 130 | // keybind console command |
---|
| 131 | FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind); |
---|
| 132 | functor1->setObject(this); |
---|
[2662] | 133 | ccKeybind_ = createConsoleCommand(functor1, "keybind"); |
---|
| 134 | CommandExecutor::addConsoleCommandShortcut(ccKeybind_); |
---|
[2087] | 135 | FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind); |
---|
| 136 | functor2->setObject(this); |
---|
[2662] | 137 | ccTkeybind_ = createConsoleCommand(functor2, "tkeybind"); |
---|
| 138 | CommandExecutor::addConsoleCommandShortcut(ccTkeybind_); |
---|
[2087] | 139 | // set our console command as callback for the key detector |
---|
| 140 | InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_); |
---|
| 141 | |
---|
| 142 | // level is loaded: we can start capturing the input |
---|
| 143 | InputManager::getInstance().requestEnterState("game"); |
---|
| 144 | } |
---|
[2662] | 145 | } |
---|
[2087] | 146 | |
---|
[2896] | 147 | void GSLevel::showIngameGUI(bool show) |
---|
[2662] | 148 | { |
---|
[2896] | 149 | if (show) |
---|
| 150 | { |
---|
[3196] | 151 | GUIManager::getInstance().showGUI("inGameTest"); |
---|
| 152 | GUIManager::getInstance().executeCode("showCursor()"); |
---|
[2896] | 153 | InputManager::getInstance().requestEnterState("guiMouseOnly"); |
---|
| 154 | } |
---|
| 155 | else |
---|
| 156 | { |
---|
[3196] | 157 | GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")"); |
---|
| 158 | GUIManager::getInstance().executeCode("hideCursor()"); |
---|
[2896] | 159 | InputManager::getInstance().requestLeaveState("guiMouseOnly"); |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void GSLevel::deactivate() |
---|
| 164 | { |
---|
[2928] | 165 | /* |
---|
[2662] | 166 | // destroy console commands |
---|
| 167 | if (this->ccKeybind_) |
---|
[2087] | 168 | { |
---|
[2662] | 169 | delete this->ccKeybind_; |
---|
| 170 | this->ccKeybind_ = 0; |
---|
[2087] | 171 | } |
---|
[2662] | 172 | if (this->ccTkeybind_) |
---|
| 173 | { |
---|
| 174 | delete this->ccTkeybind_; |
---|
| 175 | this->ccTkeybind_ = 0; |
---|
| 176 | } |
---|
[2928] | 177 | */ |
---|
[1661] | 178 | |
---|
[2896] | 179 | |
---|
[1662] | 180 | // this call will delete every BaseObject! |
---|
| 181 | // But currently this will call methods of objects that exist no more |
---|
| 182 | // The only 'memory leak' is the ParticleSpawer. They would be deleted here |
---|
| 183 | // and call a sceneNode method that has already been destroy by the corresponding space ship. |
---|
| 184 | //Loader::close(); |
---|
| 185 | |
---|
[2896] | 186 | if (GameMode::showsGraphics()) |
---|
[2087] | 187 | InputManager::getInstance().requestLeaveState("game"); |
---|
[1662] | 188 | |
---|
[2896] | 189 | if (GameMode::isMaster()) |
---|
[2087] | 190 | this->unloadLevel(); |
---|
[1662] | 191 | |
---|
[2087] | 192 | if (this->radar_) |
---|
[2662] | 193 | { |
---|
[2087] | 194 | delete this->radar_; |
---|
[2662] | 195 | this->radar_ = 0; |
---|
| 196 | } |
---|
[2087] | 197 | |
---|
| 198 | if (this->cameraManager_) |
---|
[2662] | 199 | { |
---|
[2087] | 200 | delete this->cameraManager_; |
---|
[2662] | 201 | this->cameraManager_ = 0; |
---|
| 202 | } |
---|
[2087] | 203 | |
---|
| 204 | if (this->levelManager_) |
---|
[2662] | 205 | { |
---|
[2087] | 206 | delete this->levelManager_; |
---|
[2662] | 207 | this->levelManager_ = 0; |
---|
| 208 | } |
---|
[2087] | 209 | |
---|
[2662] | 210 | if (this->playerManager_) |
---|
| 211 | { |
---|
| 212 | delete this->playerManager_; |
---|
| 213 | this->playerManager_ = 0; |
---|
| 214 | } |
---|
| 215 | |
---|
[2911] | 216 | if (this->questManager_) |
---|
| 217 | { |
---|
| 218 | delete this->questManager_; |
---|
| 219 | this->questManager_ = NULL; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | if (this->notificationManager_) |
---|
| 223 | { |
---|
| 224 | delete this->notificationManager_; |
---|
| 225 | this->notificationManager_ = NULL; |
---|
| 226 | } |
---|
| 227 | |
---|
[2896] | 228 | if (GameMode::showsGraphics()) |
---|
[2087] | 229 | { |
---|
[2896] | 230 | gameInputState_->setHandler(0); |
---|
| 231 | guiMouseOnlyInputState_->setHandler(0); |
---|
| 232 | guiKeysOnlyInputState_->setHandler(0); |
---|
[2087] | 233 | InputManager::getInstance().requestDestroyState("game"); |
---|
| 234 | if (this->keyBinder_) |
---|
[2662] | 235 | { |
---|
[2087] | 236 | delete this->keyBinder_; |
---|
[2662] | 237 | this->keyBinder_ = 0; |
---|
| 238 | } |
---|
[2087] | 239 | } |
---|
[1661] | 240 | } |
---|
| 241 | |
---|
[2896] | 242 | void GSLevel::update(const Clock& time) |
---|
[1661] | 243 | { |
---|
[2896] | 244 | // Note: Temporarily moved to GSGraphics. |
---|
[2087] | 245 | //// Call the scene objects |
---|
| 246 | //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
| 247 | // it->tick(time.getDeltaTime() * this->timeFactor_); |
---|
[1661] | 248 | } |
---|
| 249 | |
---|
[1670] | 250 | void GSLevel::loadLevel() |
---|
| 251 | { |
---|
| 252 | // call the loader |
---|
| 253 | COUT(0) << "Loading level..." << std::endl; |
---|
[1934] | 254 | std::string levelName; |
---|
[3036] | 255 | CommandLine::getValue("level", &levelName); |
---|
| 256 | if (levelName == "") |
---|
| 257 | startFile_s = new XMLFile(Core::getMediaPathString() + "levels" + '/' + Game::getInstance().getLevel()); |
---|
| 258 | else |
---|
[3008] | 259 | startFile_s = new XMLFile(Core::getMediaPathString() + "levels" + '/' + levelName); |
---|
| 260 | Loader::open(startFile_s); |
---|
[1670] | 261 | } |
---|
| 262 | |
---|
| 263 | void GSLevel::unloadLevel() |
---|
| 264 | { |
---|
[2087] | 265 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 266 | // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // |
---|
| 267 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 268 | // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY / |
---|
| 269 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 270 | |
---|
[3008] | 271 | delete startFile_s; |
---|
[1670] | 272 | } |
---|
[1887] | 273 | |
---|
| 274 | void GSLevel::keybind(const std::string &command) |
---|
| 275 | { |
---|
| 276 | this->keybindInternal(command, false); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | void GSLevel::tkeybind(const std::string &command) |
---|
| 280 | { |
---|
| 281 | this->keybindInternal(command, true); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | /** |
---|
| 285 | @brief |
---|
| 286 | Assigns a command string to a key/button/axis. The name is determined via KeyDetector. |
---|
| 287 | @param command |
---|
| 288 | Command string that can be executed by the CommandExecutor |
---|
| 289 | OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify |
---|
[2896] | 290 | the key/button/axis that has been activated. This is configured above in activate(). |
---|
[1887] | 291 | */ |
---|
| 292 | void GSLevel::keybindInternal(const std::string& command, bool bTemporary) |
---|
| 293 | { |
---|
[2896] | 294 | if (GameMode::showsGraphics()) |
---|
[1887] | 295 | { |
---|
[2087] | 296 | static std::string bindingString = ""; |
---|
| 297 | static bool bTemporarySaved = false; |
---|
| 298 | static bool bound = true; |
---|
| 299 | // note: We use a long name to make 'sure' that the user doesn't use it accidentally. |
---|
| 300 | // Howerver there will be no real issue if it happens anyway. |
---|
| 301 | if (command.find(keyDetectorCallbackCode_) != 0) |
---|
[1887] | 302 | { |
---|
[2087] | 303 | if (bound) |
---|
| 304 | { |
---|
| 305 | COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; |
---|
| 306 | InputManager::getInstance().requestEnterState("detector"); |
---|
| 307 | bindingString = command; |
---|
| 308 | bTemporarySaved = bTemporary; |
---|
| 309 | bound = false; |
---|
| 310 | } |
---|
| 311 | //else: We're still in a keybind command. ignore this call. |
---|
[1887] | 312 | } |
---|
[2087] | 313 | else |
---|
[1887] | 314 | { |
---|
[2087] | 315 | if (!bound) |
---|
| 316 | { |
---|
| 317 | // user has pressed the key |
---|
| 318 | std::string name = command.substr(this->keyDetectorCallbackCode_.size()); |
---|
| 319 | COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl; |
---|
| 320 | this->keyBinder_->setBinding(bindingString, name, bTemporarySaved); |
---|
| 321 | InputManager::getInstance().requestLeaveState("detector"); |
---|
| 322 | bound = true; |
---|
| 323 | } |
---|
| 324 | // else: A key was pressed within the same tick, ignore it. |
---|
[1887] | 325 | } |
---|
| 326 | } |
---|
| 327 | } |
---|
[1661] | 328 | } |
---|