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