Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/overlay/src/orxonox/gamestates/GSLevel.cc @ 2036

Last change on this file since 2036 was 2036, checked in by rgrieder, 17 years ago

Added minimal test files for a statistics overlay.

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