Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2255 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
Line 
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 "core/input/InputManager.h"
33#include "core/input/SimpleInputState.h"
34#include "core/input/KeyBinder.h"
35#include "core/Loader.h"
36#include "core/XMLFile.h"
37#include "core/CommandExecutor.h"
38#include "core/ConsoleCommand.h"
39#include "core/ConfigValueIncludes.h"
40#include "core/CoreIncludes.h"
41#include "core/Core.h"
42//#include "objects/Backlight.h"
43#include "objects/Tickable.h"
44#include "objects/Radar.h"
45//#include "tools/ParticleInterface.h"
46#include "CameraManager.h"
47#include "LevelManager.h"
48#include "Settings.h"
49
50namespace orxonox
51{
52    GSLevel::GSLevel()
53//        : GameState<GSGraphics>(name)
54        : timeFactor_(1.0f)
55        , keyBinder_(0)
56        , inputState_(0)
57        , radar_(0)
58        , startFile_(0)
59        , cameraManager_(0)
60        , levelManager_(0)
61    {
62        RegisterObject(GSLevel);
63        setConfigValues();
64    }
65
66    GSLevel::~GSLevel()
67    {
68    }
69
70    void GSLevel::setConfigValues()
71    {
72        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
73        SetConfigValue(defaultKeybindings_, "def_keybindings.ini")
74            .description("Filename of default keybindings.");
75    }
76
77    void GSLevel::enter(Ogre::Viewport* viewport)
78    {
79        if (Core::showsGraphics())
80        {
81            inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20);
82            keyBinder_ = new KeyBinder();
83            keyBinder_->loadBindings("keybindings.ini", defaultKeybindings_);
84            inputState_->setHandler(keyBinder_);
85
86            // create the global CameraManager
87            assert(viewport);
88            this->cameraManager_ = new CameraManager(viewport);
89
90            // Start the Radar
91            this->radar_ = new Radar();
92        }
93
94        if (Core::isMaster())
95        {
96            // create the global LevelManager
97            this->levelManager_ = new LevelManager();
98
99            // reset game speed to normal
100            timeFactor_ = 1.0f;
101
102            this->loadLevel();
103        }
104
105        if (Core::showsGraphics())
106        {
107            // TODO: insert slomo console command with
108            // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
109
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        }
131    }
132
133    void GSLevel::leave()
134    {
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
141        if (Core::showsGraphics())
142            InputManager::getInstance().requestLeaveState("game");
143
144        if (Core::isMaster())
145            this->unloadLevel();
146
147        if (this->radar_)
148            delete this->radar_;
149
150        if (this->cameraManager_)
151            delete this->cameraManager_;
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        }
163    }
164
165    void GSLevel::ticked(const Clock& time)
166    {
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_);
171    }
172
173    /**
174    @brief
175        Changes the speed of Orxonox
176    */
177    void GSLevel::setTimeFactor(float factor)
178    {
179/*
180        float change = factor / this->timeFactor_;
181*/
182        this->timeFactor_ = factor;
183/*
184        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
185            it->setSpeedFactor(it->getSpeedFactor() * change);
186
187        for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
188            it->setTimeFactor(timeFactor_);
189*/
190    }
191
192    void GSLevel::loadLevel()
193    {
194        // call the loader
195        COUT(0) << "Loading level..." << std::endl;
196        startFile_ = new XMLFile(Settings::getDataPath() + "levels/sample2.oxw");
197        Loader::open(startFile_);
198    }
199
200    void GSLevel::unloadLevel()
201    {
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
208        delete this->startFile_;
209    }
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    {
231        if (Core::showsGraphics())
232        {
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)
239            {
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.
249            }
250            else
251            {
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.
262            }
263        }
264    }
265}
Note: See TracBrowser for help on using the repository browser.