Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2152 was 2140, checked in by rgrieder, 16 years ago

Stats overlay should now be loaded. Could not test it yet because of still missing media files.

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