Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSLevel.cc @ 2087

Last change on this file since 2087 was 2087, checked in by landauf, 15 years ago

merged objecthierarchy branch back to trunk

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