Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/gamestates/GSLevel.cc @ 2219

Last change on this file since 2219 was 2101, checked in by rgrieder, 16 years ago

Finally managed to have a master InputState which enables:

  • Console always opens
  • Debug overlay toggles visibility in gui mode too

Had to change several other code:

  • ConfigFileManager uses special class instead of enum for ConfigFileType
  • You can add an arbitrary config file and get the ConfigFileType
  • ConfigFileManager is an Ogre singleton too. Created in Main.cc
  • CommandLineArgument "optionsFile" specifies another file for command line arguments
  • CommandLineArgument "settingsFile" declares the file used for settings (orxonox.ini)
  • changed all fileNames to filenames
  • "Loaded config file blah" now uses COUT(3) instead of COUT(0)
  • fixed a bug in ConfigFileManager::load() that cause orxonox.ini to double its size after every call
  • Property svn:eol-style set to native
File size: 9.1 KB
RevLine 
[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"
[2073]46#include "CameraManager.h"
[2023]47#include "LevelManager.h"
[1670]48#include "Settings.h"
[1661]49
50namespace 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)
[2073]59        , cameraManager_(0)
[2023]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=");
[2101]73        SetConfigValue(defaultKeybindings_, "def_keybindings.ini")
74            .description("Filename of default keybindings.");
[1887]75    }
76
[2023]77    void GSLevel::enter(Ogre::Viewport* viewport)
[1661]78    {
[2023]79        if (Core::showsGraphics())
80        {
81            inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20);
82            keyBinder_ = new KeyBinder();
[2101]83            keyBinder_->loadBindings("keybindings.ini", defaultKeybindings_);
[2023]84            inputState_->setHandler(keyBinder_);
[1661]85
[2073]86            // create the global CameraManager
[2023]87            assert(viewport);
[2073]88            this->cameraManager_ = new CameraManager(viewport);
[1688]89
[2023]90            // Start the Radar
91            this->radar_ = new Radar();
92        }
[2006]93
[2023]94        if (Core::isMaster())
95        {
96            // create the global LevelManager
97            this->levelManager_ = new LevelManager();
[1662]98
[2023]99            // reset game speed to normal
100            timeFactor_ = 1.0f;
[1662]101
[2023]102            this->loadLevel();
103        }
[1755]104
[2023]105        if (Core::showsGraphics())
106        {
107            // TODO: insert slomo console command with
108            // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
[1887]109
[2023]110            // keybind console command
111            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
112            functor1->setObject(this);
113            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "keybind"));
114            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
115            functor2->setObject(this);
116            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor2, "tkeybind"));
117            // set our console command as callback for the key detector
118            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
119
120            // level is loaded: we can start capturing the input
121            InputManager::getInstance().requestEnterState("game");
122        }
123
124        if (Core::isMaster())
125        {
126            // time factor console command
127            FunctorMember<GSLevel>* functor = createFunctor(&GSLevel::setTimeFactor);
128            functor->setObject(this);
129            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor, "setTimeFactor")).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);;
130        }
[1661]131    }
132
133    void GSLevel::leave()
134    {
[1662]135        // this call will delete every BaseObject!
136        // But currently this will call methods of objects that exist no more
137        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
138        // and call a sceneNode method that has already been destroy by the corresponding space ship.
139        //Loader::close();
140
[2023]141        if (Core::showsGraphics())
142            InputManager::getInstance().requestLeaveState("game");
[1662]143
[2023]144        if (Core::isMaster())
145            this->unloadLevel();
[1662]146
[2023]147        if (this->radar_)
148            delete this->radar_;
149
[2073]150        if (this->cameraManager_)
151            delete this->cameraManager_;
[2023]152
153        if (this->levelManager_)
154            delete this->levelManager_;
155
156        if (Core::showsGraphics())
157        {
158            inputState_->setHandler(0);
159            InputManager::getInstance().requestDestroyState("game");
160            if (this->keyBinder_)
161                delete this->keyBinder_;
162        }
[1661]163    }
164
[1674]165    void GSLevel::ticked(const Clock& time)
[1661]166    {
[2084]167        // Commented by 1337: Temporarily moved to GSGraphics.
168        //// Call the scene objects
169        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
170        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]171    }
172
173    /**
174    @brief
175        Changes the speed of Orxonox
176    */
[1662]177    void GSLevel::setTimeFactor(float factor)
178    {
[1962]179/*
[1674]180        float change = factor / this->timeFactor_;
[1962]181*/
[1674]182        this->timeFactor_ = factor;
[1916]183/*
[1755]184        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
[1662]185            it->setSpeedFactor(it->getSpeedFactor() * change);
[1661]186
[1755]187        for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
[1674]188            it->setTimeFactor(timeFactor_);
[1916]189*/
[1662]190    }
[1670]191
192    void GSLevel::loadLevel()
193    {
194        // call the loader
195        COUT(0) << "Loading level..." << std::endl;
[2010]196        startFile_ = new XMLFile(Settings::getDataPath() + "levels/sample2.oxw");
197        Loader::open(startFile_);
[1670]198    }
199
200    void GSLevel::unloadLevel()
201    {
[2034]202        //////////////////////////////////////////////////////////////////////////////////////////
203        // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO //
204        //////////////////////////////////////////////////////////////////////////////////////////
205        // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY /
206        //////////////////////////////////////////////////////////////////////////////////////////
207
[2010]208        delete this->startFile_;
[1670]209    }
[1887]210
211    void GSLevel::keybind(const std::string &command)
212    {
213        this->keybindInternal(command, false);
214    }
215
216    void GSLevel::tkeybind(const std::string &command)
217    {
218        this->keybindInternal(command, true);
219    }
220
221    /**
222    @brief
223        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
224    @param command
225        Command string that can be executed by the CommandExecutor
226        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
227        the key/button/axis that has been activated. This is configured above in enter().
228    */
229    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
230    {
[2023]231        if (Core::showsGraphics())
[1887]232        {
[2023]233            static std::string bindingString = "";
234            static bool bTemporarySaved = false;
235            static bool bound = true;
236            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
237            // Howerver there will be no real issue if it happens anyway.
238            if (command.find(keyDetectorCallbackCode_) != 0)
[1887]239            {
[2023]240                if (bound)
241                {
242                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
243                    InputManager::getInstance().requestEnterState("detector");
244                    bindingString = command;
245                    bTemporarySaved = bTemporary;
246                    bound = false;
247                }
248                //else:  We're still in a keybind command. ignore this call.
[1887]249            }
[2023]250            else
[1887]251            {
[2023]252                if (!bound)
253                {
254                    // user has pressed the key
255                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
256                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
257                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
258                    InputManager::getInstance().requestLeaveState("detector");
259                    bound = true;
260                }
261                // else: A key was pressed within the same tick, ignore it.
[1887]262            }
263        }
264    }
[1661]265}
Note: See TracBrowser for help on using the repository browser.