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