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