| 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 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "OrxonoxStableHeaders.h" | 
|---|
| 30 | #include "GSLevel.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include "core/input/InputManager.h" | 
|---|
| 33 | #include "core/input/SimpleInputState.h" | 
|---|
| 34 | #include "core/input/KeyBinder.h" | 
|---|
| 35 | #include "core/Loader.h" | 
|---|
| 36 | #include "core/XMLFile.h" | 
|---|
| 37 | #include "core/CommandExecutor.h" | 
|---|
| 38 | #include "core/ConsoleCommand.h" | 
|---|
| 39 | #include "core/CommandLine.h" | 
|---|
| 40 | #include "core/ConfigValueIncludes.h" | 
|---|
| 41 | #include "core/CoreIncludes.h" | 
|---|
| 42 | #include "core/Core.h" | 
|---|
| 43 | #include "objects/Tickable.h" | 
|---|
| 44 | #include "objects/Radar.h" | 
|---|
| 45 | #include "CameraManager.h" | 
|---|
| 46 | #include "LevelManager.h" | 
|---|
| 47 | #include "PlayerManager.h" | 
|---|
| 48 | #include "objects/quest/QuestManager.h" | 
|---|
| 49 | #include "overlays/notifications/NotificationManager.h" | 
|---|
| 50 |  | 
|---|
| 51 | namespace orxonox | 
|---|
| 52 | { | 
|---|
| 53 |     SetCommandLineArgument(level, "presentation.oxw").shortcut("l"); | 
|---|
| 54 |  | 
|---|
| 55 |     GSLevel::GSLevel() | 
|---|
| 56 | //        : GameState<GSGraphics>(name) | 
|---|
| 57 |         : keyBinder_(0) | 
|---|
| 58 |         , inputState_(0) | 
|---|
| 59 |         , radar_(0) | 
|---|
| 60 |         , startFile_(0) | 
|---|
| 61 |         , cameraManager_(0) | 
|---|
| 62 |         , levelManager_(0) | 
|---|
| 63 |     { | 
|---|
| 64 |         RegisterObject(GSLevel); | 
|---|
| 65 |  | 
|---|
| 66 |         this->ccKeybind_ = 0; | 
|---|
| 67 |         this->ccTkeybind_ = 0; | 
|---|
| 68 |  | 
|---|
| 69 |         setConfigValues(); | 
|---|
| 70 |     } | 
|---|
| 71 |  | 
|---|
| 72 |     GSLevel::~GSLevel() | 
|---|
| 73 |     { | 
|---|
| 74 |     } | 
|---|
| 75 |  | 
|---|
| 76 |     void GSLevel::setConfigValues() | 
|---|
| 77 |     { | 
|---|
| 78 |         SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName="); | 
|---|
| 79 |     } | 
|---|
| 80 |  | 
|---|
| 81 |     void GSLevel::enter(Ogre::Viewport* viewport) | 
|---|
| 82 |     { | 
|---|
| 83 |         if (Core::showsGraphics()) | 
|---|
| 84 |         { | 
|---|
| 85 |             inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20); | 
|---|
| 86 |             keyBinder_ = new KeyBinder(); | 
|---|
| 87 |             keyBinder_->loadBindings("keybindings.ini"); | 
|---|
| 88 |             inputState_->setHandler(keyBinder_); | 
|---|
| 89 |  | 
|---|
| 90 |             // create the global CameraManager | 
|---|
| 91 |             assert(viewport); | 
|---|
| 92 |             this->cameraManager_ = new CameraManager(viewport); | 
|---|
| 93 |  | 
|---|
| 94 |             // Start the Radar | 
|---|
| 95 |             this->radar_ = new Radar(); | 
|---|
| 96 |         } | 
|---|
| 97 |  | 
|---|
| 98 |         this->playerManager_ = new PlayerManager(); | 
|---|
| 99 |  | 
|---|
| 100 |         this->questManager_ = new QuestManager(); | 
|---|
| 101 |  | 
|---|
| 102 |         this->notificationManager_ = new NotificationManager(); | 
|---|
| 103 |  | 
|---|
| 104 |         if (Core::isMaster()) | 
|---|
| 105 |         { | 
|---|
| 106 |             // create the global LevelManager | 
|---|
| 107 |             this->levelManager_ = new LevelManager(); | 
|---|
| 108 |  | 
|---|
| 109 |             this->loadLevel(); | 
|---|
| 110 |         } | 
|---|
| 111 |  | 
|---|
| 112 |         if (Core::showsGraphics()) | 
|---|
| 113 |         { | 
|---|
| 114 |             // TODO: insert slomo console command with | 
|---|
| 115 |             // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false); | 
|---|
| 116 |  | 
|---|
| 117 |             // keybind console command | 
|---|
| 118 |             FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind); | 
|---|
| 119 |             functor1->setObject(this); | 
|---|
| 120 |             ccKeybind_ = createConsoleCommand(functor1, "keybind"); | 
|---|
| 121 |             CommandExecutor::addConsoleCommandShortcut(ccKeybind_); | 
|---|
| 122 |             FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind); | 
|---|
| 123 |             functor2->setObject(this); | 
|---|
| 124 |             ccTkeybind_ = createConsoleCommand(functor2, "tkeybind"); | 
|---|
| 125 |             CommandExecutor::addConsoleCommandShortcut(ccTkeybind_); | 
|---|
| 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 | 
|---|
| 130 |             InputManager::getInstance().requestEnterState("game"); | 
|---|
| 131 |         } | 
|---|
| 132 |     } | 
|---|
| 133 |  | 
|---|
| 134 |     void GSLevel::leave() | 
|---|
| 135 |     { | 
|---|
| 136 |         // destroy console commands | 
|---|
| 137 |         if (this->ccKeybind_) | 
|---|
| 138 |         { | 
|---|
| 139 |             delete this->ccKeybind_; | 
|---|
| 140 |             this->ccKeybind_ = 0; | 
|---|
| 141 |         } | 
|---|
| 142 |         if (this->ccTkeybind_) | 
|---|
| 143 |         { | 
|---|
| 144 |             delete this->ccTkeybind_; | 
|---|
| 145 |             this->ccTkeybind_ = 0; | 
|---|
| 146 |         } | 
|---|
| 147 |  | 
|---|
| 148 |         // this call will delete every BaseObject! | 
|---|
| 149 |         // But currently this will call methods of objects that exist no more | 
|---|
| 150 |         // The only 'memory leak' is the ParticleSpawer. They would be deleted here | 
|---|
| 151 |         // and call a sceneNode method that has already been destroy by the corresponding space ship. | 
|---|
| 152 |         //Loader::close(); | 
|---|
| 153 |  | 
|---|
| 154 |         if (Core::showsGraphics()) | 
|---|
| 155 |             InputManager::getInstance().requestLeaveState("game"); | 
|---|
| 156 |  | 
|---|
| 157 |         if (Core::isMaster()) | 
|---|
| 158 |             this->unloadLevel(); | 
|---|
| 159 |  | 
|---|
| 160 |         if (this->radar_) | 
|---|
| 161 |         { | 
|---|
| 162 |             delete this->radar_; | 
|---|
| 163 |             this->radar_ = 0; | 
|---|
| 164 |         } | 
|---|
| 165 |  | 
|---|
| 166 |         if (this->cameraManager_) | 
|---|
| 167 |         { | 
|---|
| 168 |             delete this->cameraManager_; | 
|---|
| 169 |             this->cameraManager_ = 0; | 
|---|
| 170 |         } | 
|---|
| 171 |  | 
|---|
| 172 |         if (this->levelManager_) | 
|---|
| 173 |         { | 
|---|
| 174 |             delete this->levelManager_; | 
|---|
| 175 |             this->levelManager_ = 0; | 
|---|
| 176 |         } | 
|---|
| 177 |  | 
|---|
| 178 |         if (this->playerManager_) | 
|---|
| 179 |         { | 
|---|
| 180 |             delete this->playerManager_; | 
|---|
| 181 |             this->playerManager_ = 0; | 
|---|
| 182 |         } | 
|---|
| 183 |  | 
|---|
| 184 |         if (this->questManager_) | 
|---|
| 185 |         { | 
|---|
| 186 |             delete this->questManager_; | 
|---|
| 187 |             this->questManager_ = NULL; | 
|---|
| 188 |         } | 
|---|
| 189 |  | 
|---|
| 190 |         if (this->notificationManager_) | 
|---|
| 191 |         { | 
|---|
| 192 |             delete this->notificationManager_; | 
|---|
| 193 |             this->notificationManager_ = NULL; | 
|---|
| 194 |         } | 
|---|
| 195 |  | 
|---|
| 196 |         if (Core::showsGraphics()) | 
|---|
| 197 |         { | 
|---|
| 198 |             inputState_->setHandler(0); | 
|---|
| 199 |             InputManager::getInstance().requestDestroyState("game"); | 
|---|
| 200 |             if (this->keyBinder_) | 
|---|
| 201 |             { | 
|---|
| 202 |                 delete this->keyBinder_; | 
|---|
| 203 |                 this->keyBinder_ = 0; | 
|---|
| 204 |             } | 
|---|
| 205 |         } | 
|---|
| 206 |     } | 
|---|
| 207 |  | 
|---|
| 208 |     void GSLevel::ticked(const Clock& time) | 
|---|
| 209 |     { | 
|---|
| 210 |         // Commented by 1337: Temporarily moved to GSGraphics. | 
|---|
| 211 |         //// Call the scene objects | 
|---|
| 212 |         //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) | 
|---|
| 213 |         //    it->tick(time.getDeltaTime() * this->timeFactor_); | 
|---|
| 214 |     } | 
|---|
| 215 |  | 
|---|
| 216 |     void GSLevel::loadLevel() | 
|---|
| 217 |     { | 
|---|
| 218 |         // call the loader | 
|---|
| 219 |         COUT(0) << "Loading level..." << std::endl; | 
|---|
| 220 |         std::string levelName; | 
|---|
| 221 |         CommandLine::getValue("level", &levelName); | 
|---|
| 222 |         startFile_ = new XMLFile(Core::getMediaPathString() + "levels" + '/' + levelName); | 
|---|
| 223 |         Loader::open(startFile_); | 
|---|
| 224 |     } | 
|---|
| 225 |  | 
|---|
| 226 |     void GSLevel::unloadLevel() | 
|---|
| 227 |     { | 
|---|
| 228 |         ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 229 |         // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // | 
|---|
| 230 |         ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 231 |         // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY / | 
|---|
| 232 |         ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 233 |  | 
|---|
| 234 |         delete this->startFile_; | 
|---|
| 235 |     } | 
|---|
| 236 |  | 
|---|
| 237 |     void GSLevel::keybind(const std::string &command) | 
|---|
| 238 |     { | 
|---|
| 239 |         this->keybindInternal(command, false); | 
|---|
| 240 |     } | 
|---|
| 241 |  | 
|---|
| 242 |     void GSLevel::tkeybind(const std::string &command) | 
|---|
| 243 |     { | 
|---|
| 244 |         this->keybindInternal(command, true); | 
|---|
| 245 |     } | 
|---|
| 246 |  | 
|---|
| 247 |     /** | 
|---|
| 248 |     @brief | 
|---|
| 249 |         Assigns a command string to a key/button/axis. The name is determined via KeyDetector. | 
|---|
| 250 |     @param command | 
|---|
| 251 |         Command string that can be executed by the CommandExecutor | 
|---|
| 252 |         OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify | 
|---|
| 253 |         the key/button/axis that has been activated. This is configured above in enter(). | 
|---|
| 254 |     */ | 
|---|
| 255 |     void GSLevel::keybindInternal(const std::string& command, bool bTemporary) | 
|---|
| 256 |     { | 
|---|
| 257 |         if (Core::showsGraphics()) | 
|---|
| 258 |         { | 
|---|
| 259 |             static std::string bindingString = ""; | 
|---|
| 260 |             static bool bTemporarySaved = false; | 
|---|
| 261 |             static bool bound = true; | 
|---|
| 262 |             // note: We use a long name to make 'sure' that the user doesn't use it accidentally. | 
|---|
| 263 |             // Howerver there will be no real issue if it happens anyway. | 
|---|
| 264 |             if (command.find(keyDetectorCallbackCode_) != 0) | 
|---|
| 265 |             { | 
|---|
| 266 |                 if (bound) | 
|---|
| 267 |                 { | 
|---|
| 268 |                     COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; | 
|---|
| 269 |                     InputManager::getInstance().requestEnterState("detector"); | 
|---|
| 270 |                     bindingString = command; | 
|---|
| 271 |                     bTemporarySaved = bTemporary; | 
|---|
| 272 |                     bound = false; | 
|---|
| 273 |                 } | 
|---|
| 274 |                 //else:  We're still in a keybind command. ignore this call. | 
|---|
| 275 |             } | 
|---|
| 276 |             else | 
|---|
| 277 |             { | 
|---|
| 278 |                 if (!bound) | 
|---|
| 279 |                 { | 
|---|
| 280 |                     // user has pressed the key | 
|---|
| 281 |                     std::string name = command.substr(this->keyDetectorCallbackCode_.size()); | 
|---|
| 282 |                     COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl; | 
|---|
| 283 |                     this->keyBinder_->setBinding(bindingString, name, bTemporarySaved); | 
|---|
| 284 |                     InputManager::getInstance().requestLeaveState("detector"); | 
|---|
| 285 |                     bound = true; | 
|---|
| 286 |                 } | 
|---|
| 287 |                 // else: A key was pressed within the same tick, ignore it. | 
|---|
| 288 |             } | 
|---|
| 289 |         } | 
|---|
| 290 |     } | 
|---|
| 291 | } | 
|---|