[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 | |
---|
[5695] | 32 | #include <OgreCompositorManager.h> |
---|
| 33 | |
---|
[1661] | 34 | #include "core/input/InputManager.h" |
---|
[3327] | 35 | #include "core/input/InputState.h" |
---|
[1661] | 36 | #include "core/input/KeyBinder.h" |
---|
[3196] | 37 | #include "core/Clock.h" |
---|
[1887] | 38 | #include "core/ConsoleCommand.h" |
---|
| 39 | #include "core/ConfigValueIncludes.h" |
---|
| 40 | #include "core/CoreIncludes.h" |
---|
[2896] | 41 | #include "core/Game.h" |
---|
| 42 | #include "core/GameMode.h" |
---|
[3370] | 43 | #include "core/GraphicsManager.h" |
---|
| 44 | #include "core/GUIManager.h" |
---|
[3196] | 45 | #include "core/Loader.h" |
---|
| 46 | #include "core/XMLFile.h" |
---|
| 47 | |
---|
[5693] | 48 | #include "tools/interfaces/Tickable.h" |
---|
[2087] | 49 | #include "CameraManager.h" |
---|
| 50 | #include "LevelManager.h" |
---|
[2662] | 51 | #include "PlayerManager.h" |
---|
[5806] | 52 | #include "infos/HumanPlayer.h" |
---|
[1661] | 53 | |
---|
| 54 | namespace orxonox |
---|
| 55 | { |
---|
[3370] | 56 | DeclareGameState(GSLevel, "level", false, false); |
---|
[2896] | 57 | SetConsoleCommand(GSLevel, showIngameGUI, true); |
---|
[1934] | 58 | |
---|
[3008] | 59 | XMLFile* GSLevel::startFile_s = NULL; |
---|
| 60 | |
---|
[3370] | 61 | GSLevel::GSLevel(const GameStateInfo& info) |
---|
| 62 | : GameState(info) |
---|
[2896] | 63 | , keyBinder_(0) |
---|
| 64 | , gameInputState_(0) |
---|
| 65 | , guiMouseOnlyInputState_(0) |
---|
| 66 | , guiKeysOnlyInputState_(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()); |
---|
[2087] | 103 | } |
---|
[1662] | 104 | |
---|
[2662] | 105 | this->playerManager_ = new PlayerManager(); |
---|
| 106 | |
---|
[5693] | 107 | this->scope_GSLevel_ = new Scope<ScopeID::GSLevel>(); |
---|
[2911] | 108 | |
---|
[2896] | 109 | if (GameMode::isMaster()) |
---|
[2087] | 110 | { |
---|
| 111 | this->loadLevel(); |
---|
| 112 | } |
---|
[1755] | 113 | |
---|
[2896] | 114 | if (GameMode::showsGraphics()) |
---|
[2087] | 115 | { |
---|
| 116 | // keybind console command |
---|
[5829] | 117 | ccKeybind_ = createConsoleCommand(createFunctor(&GSLevel::keybind, this), "keybind"); |
---|
[2662] | 118 | CommandExecutor::addConsoleCommandShortcut(ccKeybind_); |
---|
[5829] | 119 | ccTkeybind_ = createConsoleCommand(createFunctor(&GSLevel::tkeybind, this), "tkeybind"); |
---|
[2662] | 120 | CommandExecutor::addConsoleCommandShortcut(ccTkeybind_); |
---|
[2087] | 121 | // set our console command as callback for the key detector |
---|
| 122 | InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_); |
---|
| 123 | |
---|
| 124 | // level is loaded: we can start capturing the input |
---|
[3327] | 125 | InputManager::getInstance().enterState("game"); |
---|
[5820] | 126 | |
---|
| 127 | // connect the HumanPlayer to the game |
---|
| 128 | this->playerManager_->clientConnected(0); |
---|
[2087] | 129 | } |
---|
[2662] | 130 | } |
---|
[2087] | 131 | |
---|
[2896] | 132 | void GSLevel::showIngameGUI(bool show) |
---|
[2662] | 133 | { |
---|
[2896] | 134 | if (show) |
---|
| 135 | { |
---|
[3196] | 136 | GUIManager::getInstance().showGUI("inGameTest"); |
---|
| 137 | GUIManager::getInstance().executeCode("showCursor()"); |
---|
[3327] | 138 | InputManager::getInstance().enterState("guiMouseOnly"); |
---|
[2896] | 139 | } |
---|
| 140 | else |
---|
| 141 | { |
---|
[3196] | 142 | GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")"); |
---|
| 143 | GUIManager::getInstance().executeCode("hideCursor()"); |
---|
[3327] | 144 | InputManager::getInstance().leaveState("guiMouseOnly"); |
---|
[2896] | 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void GSLevel::deactivate() |
---|
| 149 | { |
---|
[2928] | 150 | /* |
---|
[2662] | 151 | // destroy console commands |
---|
| 152 | if (this->ccKeybind_) |
---|
[2087] | 153 | { |
---|
[2662] | 154 | delete this->ccKeybind_; |
---|
| 155 | this->ccKeybind_ = 0; |
---|
[2087] | 156 | } |
---|
[2662] | 157 | if (this->ccTkeybind_) |
---|
| 158 | { |
---|
| 159 | delete this->ccTkeybind_; |
---|
| 160 | this->ccTkeybind_ = 0; |
---|
| 161 | } |
---|
[2928] | 162 | */ |
---|
[1661] | 163 | |
---|
[5695] | 164 | if (GameMode::showsGraphics()) |
---|
| 165 | { |
---|
[5832] | 166 | // disconnect the HumanPlayer |
---|
| 167 | this->playerManager_->clientDisconnected(0); |
---|
| 168 | |
---|
[5695] | 169 | // unload all compositors (this is only necessary because we don't yet destroy all resources!) |
---|
| 170 | Ogre::CompositorManager::getSingleton().removeAll(); |
---|
| 171 | } |
---|
[2896] | 172 | |
---|
[1662] | 173 | // this call will delete every BaseObject! |
---|
| 174 | // But currently this will call methods of objects that exist no more |
---|
| 175 | // The only 'memory leak' is the ParticleSpawer. They would be deleted here |
---|
| 176 | // and call a sceneNode method that has already been destroy by the corresponding space ship. |
---|
| 177 | //Loader::close(); |
---|
| 178 | |
---|
[2896] | 179 | if (GameMode::showsGraphics()) |
---|
[5820] | 180 | { |
---|
[3327] | 181 | InputManager::getInstance().leaveState("game"); |
---|
[5820] | 182 | } |
---|
[1662] | 183 | |
---|
[2896] | 184 | if (GameMode::isMaster()) |
---|
[2087] | 185 | this->unloadLevel(); |
---|
[1662] | 186 | |
---|
[2087] | 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 | { |
---|
[5799] | 195 | this->playerManager_->destroy(); |
---|
[2662] | 196 | this->playerManager_ = 0; |
---|
| 197 | } |
---|
| 198 | |
---|
[5693] | 199 | if (this->scope_GSLevel_) |
---|
[2911] | 200 | { |
---|
[5693] | 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"); |
---|
[5818] | 211 | InputManager::getInstance().destroyState("guiKeysOnly"); |
---|
| 212 | InputManager::getInstance().destroyState("guiMouseOnly"); |
---|
[2087] | 213 | if (this->keyBinder_) |
---|
[2662] | 214 | { |
---|
[5799] | 215 | this->keyBinder_->destroy(); |
---|
[2662] | 216 | this->keyBinder_ = 0; |
---|
| 217 | } |
---|
[2087] | 218 | } |
---|
[1661] | 219 | } |
---|
| 220 | |
---|
[2896] | 221 | void GSLevel::update(const Clock& time) |
---|
[1661] | 222 | { |
---|
[2896] | 223 | // Note: Temporarily moved to GSGraphics. |
---|
[2087] | 224 | //// Call the scene objects |
---|
| 225 | //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
| 226 | // it->tick(time.getDeltaTime() * this->timeFactor_); |
---|
[1661] | 227 | } |
---|
| 228 | |
---|
[1670] | 229 | void GSLevel::loadLevel() |
---|
| 230 | { |
---|
| 231 | // call the loader |
---|
| 232 | COUT(0) << "Loading level..." << std::endl; |
---|
[5695] | 233 | startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel()); |
---|
[3008] | 234 | Loader::open(startFile_s); |
---|
[1670] | 235 | } |
---|
| 236 | |
---|
| 237 | void GSLevel::unloadLevel() |
---|
| 238 | { |
---|
[5805] | 239 | Loader::unload(startFile_s); |
---|
[2087] | 240 | |
---|
[3008] | 241 | delete startFile_s; |
---|
[1670] | 242 | } |
---|
[1887] | 243 | |
---|
| 244 | void GSLevel::keybind(const std::string &command) |
---|
| 245 | { |
---|
| 246 | this->keybindInternal(command, false); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | void GSLevel::tkeybind(const std::string &command) |
---|
| 250 | { |
---|
| 251 | this->keybindInternal(command, true); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | /** |
---|
| 255 | @brief |
---|
| 256 | Assigns a command string to a key/button/axis. The name is determined via KeyDetector. |
---|
| 257 | @param command |
---|
| 258 | Command string that can be executed by the CommandExecutor |
---|
| 259 | OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify |
---|
[2896] | 260 | the key/button/axis that has been activated. This is configured above in activate(). |
---|
[1887] | 261 | */ |
---|
| 262 | void GSLevel::keybindInternal(const std::string& command, bool bTemporary) |
---|
| 263 | { |
---|
[2896] | 264 | if (GameMode::showsGraphics()) |
---|
[1887] | 265 | { |
---|
[2087] | 266 | static std::string bindingString = ""; |
---|
| 267 | static bool bTemporarySaved = false; |
---|
| 268 | static bool bound = true; |
---|
| 269 | // note: We use a long name to make 'sure' that the user doesn't use it accidentally. |
---|
| 270 | // Howerver there will be no real issue if it happens anyway. |
---|
| 271 | if (command.find(keyDetectorCallbackCode_) != 0) |
---|
[1887] | 272 | { |
---|
[2087] | 273 | if (bound) |
---|
| 274 | { |
---|
| 275 | COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; |
---|
[3327] | 276 | InputManager::getInstance().enterState("detector"); |
---|
[2087] | 277 | bindingString = command; |
---|
| 278 | bTemporarySaved = bTemporary; |
---|
| 279 | bound = false; |
---|
| 280 | } |
---|
| 281 | //else: We're still in a keybind command. ignore this call. |
---|
[1887] | 282 | } |
---|
[2087] | 283 | else |
---|
[1887] | 284 | { |
---|
[2087] | 285 | if (!bound) |
---|
| 286 | { |
---|
| 287 | // user has pressed the key |
---|
| 288 | std::string name = command.substr(this->keyDetectorCallbackCode_.size()); |
---|
| 289 | COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl; |
---|
| 290 | this->keyBinder_->setBinding(bindingString, name, bTemporarySaved); |
---|
[3327] | 291 | InputManager::getInstance().leaveState("detector"); |
---|
[2087] | 292 | bound = true; |
---|
| 293 | } |
---|
| 294 | // else: A key was pressed within the same tick, ignore it. |
---|
[1887] | 295 | } |
---|
| 296 | } |
---|
| 297 | } |
---|
[1661] | 298 | } |
---|