Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/gamestates/GSLevel.cc @ 2400

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

moved setTimeFactor to GSRoot because tickables are currently ticked there

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