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