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