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