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