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