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