| [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 | 
|---|
| [1661] | 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" | 
|---|
| [1662] | 35 | #include "core/Loader.h" | 
|---|
| [2087] | 36 | #include "core/XMLFile.h" | 
|---|
| [1887] | 37 | #include "core/CommandExecutor.h" | 
|---|
 | 38 | #include "core/ConsoleCommand.h" | 
|---|
| [1934] | 39 | #include "core/CommandLine.h" | 
|---|
| [1887] | 40 | #include "core/ConfigValueIncludes.h" | 
|---|
| [2908] | 41 | #include "core/CoreIncludes.h" | 
|---|
| [2907] | 42 | #include "core/Core.h" | 
|---|
| [1694] | 43 | #include "objects/Tickable.h" | 
|---|
| [1819] | 44 | #include "objects/Radar.h" | 
|---|
| [2087] | 45 | #include "CameraManager.h" | 
|---|
 | 46 | #include "LevelManager.h" | 
|---|
| [2662] | 47 | #include "PlayerManager.h" | 
|---|
| [2908] | 48 | #include "objects/quest/QuestManager.h" | 
|---|
 | 49 | #include "overlays/notifications/NotificationManager.h" | 
|---|
| [1661] | 50 |  | 
|---|
 | 51 | namespace orxonox | 
|---|
 | 52 | { | 
|---|
| [2908] | 53 |     SetCommandLineArgument(level, "presentation.oxw").shortcut("l"); | 
|---|
| [1934] | 54 |  | 
|---|
| [2908] | 55 |     GSLevel::GSLevel() | 
|---|
 | 56 | //        : GameState<GSGraphics>(name) | 
|---|
 | 57 |         : keyBinder_(0) | 
|---|
 | 58 |         , inputState_(0) | 
|---|
| [1662] | 59 |         , radar_(0) | 
|---|
| [2087] | 60 |         , startFile_(0) | 
|---|
 | 61 |         , cameraManager_(0) | 
|---|
 | 62 |         , levelManager_(0) | 
|---|
| [1661] | 63 |     { | 
|---|
| [1887] | 64 |         RegisterObject(GSLevel); | 
|---|
| [2662] | 65 |  | 
|---|
 | 66 |         this->ccKeybind_ = 0; | 
|---|
 | 67 |         this->ccTkeybind_ = 0; | 
|---|
| [2908] | 68 |  | 
|---|
 | 69 |         setConfigValues(); | 
|---|
| [1661] | 70 |     } | 
|---|
 | 71 |  | 
|---|
 | 72 |     GSLevel::~GSLevel() | 
|---|
 | 73 |     { | 
|---|
 | 74 |     } | 
|---|
 | 75 |  | 
|---|
| [1887] | 76 |     void GSLevel::setConfigValues() | 
|---|
 | 77 |     { | 
|---|
 | 78 |         SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName="); | 
|---|
 | 79 |     } | 
|---|
 | 80 |  | 
|---|
| [2908] | 81 |     void GSLevel::enter(Ogre::Viewport* viewport) | 
|---|
| [1661] | 82 |     { | 
|---|
| [2908] | 83 |         if (Core::showsGraphics()) | 
|---|
| [2087] | 84 |         { | 
|---|
| [2908] | 85 |             inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20); | 
|---|
| [2087] | 86 |             keyBinder_ = new KeyBinder(); | 
|---|
| [2710] | 87 |             keyBinder_->loadBindings("keybindings.ini"); | 
|---|
| [2908] | 88 |             inputState_->setHandler(keyBinder_); | 
|---|
| [1661] | 89 |  | 
|---|
| [2087] | 90 |             // create the global CameraManager | 
|---|
| [2908] | 91 |             assert(viewport); | 
|---|
 | 92 |             this->cameraManager_ = new CameraManager(viewport); | 
|---|
| [1688] | 93 |  | 
|---|
| [2087] | 94 |             // Start the Radar | 
|---|
 | 95 |             this->radar_ = new Radar(); | 
|---|
 | 96 |         } | 
|---|
| [1662] | 97 |  | 
|---|
| [2662] | 98 |         this->playerManager_ = new PlayerManager(); | 
|---|
 | 99 |  | 
|---|
| [2786] | 100 |         this->questManager_ = new QuestManager(); | 
|---|
 | 101 |  | 
|---|
 | 102 |         this->notificationManager_ = new NotificationManager(); | 
|---|
 | 103 |  | 
|---|
| [2908] | 104 |         if (Core::isMaster()) | 
|---|
| [2087] | 105 |         { | 
|---|
 | 106 |             // create the global LevelManager | 
|---|
 | 107 |             this->levelManager_ = new LevelManager(); | 
|---|
| [1662] | 108 |  | 
|---|
| [2087] | 109 |             this->loadLevel(); | 
|---|
 | 110 |         } | 
|---|
| [1755] | 111 |  | 
|---|
| [2908] | 112 |         if (Core::showsGraphics()) | 
|---|
| [2087] | 113 |         { | 
|---|
| [2908] | 114 |             // TODO: insert slomo console command with | 
|---|
 | 115 |             // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false); | 
|---|
 | 116 |  | 
|---|
| [2087] | 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 | 
|---|
 | 130 |             InputManager::getInstance().requestEnterState("game"); | 
|---|
 | 131 |         } | 
|---|
| [2662] | 132 |     } | 
|---|
| [2087] | 133 |  | 
|---|
| [2908] | 134 |     void GSLevel::leave() | 
|---|
| [2662] | 135 |     { | 
|---|
 | 136 |         // destroy console commands | 
|---|
 | 137 |         if (this->ccKeybind_) | 
|---|
| [2087] | 138 |         { | 
|---|
| [2662] | 139 |             delete this->ccKeybind_; | 
|---|
 | 140 |             this->ccKeybind_ = 0; | 
|---|
| [2087] | 141 |         } | 
|---|
| [2662] | 142 |         if (this->ccTkeybind_) | 
|---|
 | 143 |         { | 
|---|
 | 144 |             delete this->ccTkeybind_; | 
|---|
 | 145 |             this->ccTkeybind_ = 0; | 
|---|
 | 146 |         } | 
|---|
| [1661] | 147 |  | 
|---|
| [1662] | 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 |  | 
|---|
| [2908] | 154 |         if (Core::showsGraphics()) | 
|---|
| [2087] | 155 |             InputManager::getInstance().requestLeaveState("game"); | 
|---|
| [1662] | 156 |  | 
|---|
| [2908] | 157 |         if (Core::isMaster()) | 
|---|
| [2087] | 158 |             this->unloadLevel(); | 
|---|
| [1662] | 159 |  | 
|---|
| [2087] | 160 |         if (this->radar_) | 
|---|
| [2662] | 161 |         { | 
|---|
| [2087] | 162 |             delete this->radar_; | 
|---|
| [2662] | 163 |             this->radar_ = 0; | 
|---|
 | 164 |         } | 
|---|
| [2087] | 165 |  | 
|---|
 | 166 |         if (this->cameraManager_) | 
|---|
| [2662] | 167 |         { | 
|---|
| [2087] | 168 |             delete this->cameraManager_; | 
|---|
| [2662] | 169 |             this->cameraManager_ = 0; | 
|---|
 | 170 |         } | 
|---|
| [2087] | 171 |  | 
|---|
 | 172 |         if (this->levelManager_) | 
|---|
| [2662] | 173 |         { | 
|---|
| [2087] | 174 |             delete this->levelManager_; | 
|---|
| [2662] | 175 |             this->levelManager_ = 0; | 
|---|
 | 176 |         } | 
|---|
| [2087] | 177 |  | 
|---|
| [2662] | 178 |         if (this->playerManager_) | 
|---|
 | 179 |         { | 
|---|
 | 180 |             delete this->playerManager_; | 
|---|
 | 181 |             this->playerManager_ = 0; | 
|---|
 | 182 |         } | 
|---|
 | 183 |  | 
|---|
| [2788] | 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 |  | 
|---|
| [2908] | 196 |         if (Core::showsGraphics()) | 
|---|
| [2087] | 197 |         { | 
|---|
| [2908] | 198 |             inputState_->setHandler(0); | 
|---|
| [2087] | 199 |             InputManager::getInstance().requestDestroyState("game"); | 
|---|
 | 200 |             if (this->keyBinder_) | 
|---|
| [2662] | 201 |             { | 
|---|
| [2087] | 202 |                 delete this->keyBinder_; | 
|---|
| [2662] | 203 |                 this->keyBinder_ = 0; | 
|---|
 | 204 |             } | 
|---|
| [2087] | 205 |         } | 
|---|
| [1661] | 206 |     } | 
|---|
 | 207 |  | 
|---|
| [2908] | 208 |     void GSLevel::ticked(const Clock& time) | 
|---|
| [1661] | 209 |     { | 
|---|
| [2908] | 210 |         // Commented by 1337: Temporarily moved to GSGraphics. | 
|---|
| [2087] | 211 |         //// Call the scene objects | 
|---|
 | 212 |         //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) | 
|---|
 | 213 |         //    it->tick(time.getDeltaTime() * this->timeFactor_); | 
|---|
| [1661] | 214 |     } | 
|---|
 | 215 |  | 
|---|
| [1670] | 216 |     void GSLevel::loadLevel() | 
|---|
 | 217 |     { | 
|---|
 | 218 |         // call the loader | 
|---|
 | 219 |         COUT(0) << "Loading level..." << std::endl; | 
|---|
| [1934] | 220 |         std::string levelName; | 
|---|
 | 221 |         CommandLine::getValue("level", &levelName); | 
|---|
| [2759] | 222 |         startFile_ = new XMLFile(Core::getMediaPathString() + "levels" + '/' + levelName); | 
|---|
| [2087] | 223 |         Loader::open(startFile_); | 
|---|
| [1670] | 224 |     } | 
|---|
 | 225 |  | 
|---|
 | 226 |     void GSLevel::unloadLevel() | 
|---|
 | 227 |     { | 
|---|
| [2087] | 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_; | 
|---|
| [1670] | 235 |     } | 
|---|
| [1887] | 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 | 
|---|
| [2908] | 253 |         the key/button/axis that has been activated. This is configured above in enter(). | 
|---|
| [1887] | 254 |     */ | 
|---|
 | 255 |     void GSLevel::keybindInternal(const std::string& command, bool bTemporary) | 
|---|
 | 256 |     { | 
|---|
| [2908] | 257 |         if (Core::showsGraphics()) | 
|---|
| [1887] | 258 |         { | 
|---|
| [2087] | 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) | 
|---|
| [1887] | 265 |             { | 
|---|
| [2087] | 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. | 
|---|
| [1887] | 275 |             } | 
|---|
| [2087] | 276 |             else | 
|---|
| [1887] | 277 |             { | 
|---|
| [2087] | 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. | 
|---|
| [1887] | 288 |             } | 
|---|
 | 289 |         } | 
|---|
 | 290 |     } | 
|---|
| [1661] | 291 | } | 
|---|