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