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