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