Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/gamestates/GSLevel.cc @ 5850

Last change on this file since 5850 was 5850, checked in by rgrieder, 15 years ago

Moved the singleton creation/destruction mess to the Core class by using just two possible Scopes:

  • ScopeID::Root for singletons that may always persists
  • ScopeID::Graphics for singletons that only work when graphics was loaded
  • Property svn:eol-style set to native
File size: 8.8 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
[2896]26 *      Benjamin Knecht
[1661]27 *
28 */
29
30#include "GSLevel.h"
31
[5695]32#include <OgreCompositorManager.h>
33
[1661]34#include "core/input/InputManager.h"
[3327]35#include "core/input/InputState.h"
[1661]36#include "core/input/KeyBinder.h"
[3196]37#include "core/Clock.h"
[1887]38#include "core/ConsoleCommand.h"
39#include "core/ConfigValueIncludes.h"
40#include "core/CoreIncludes.h"
[2896]41#include "core/Game.h"
42#include "core/GameMode.h"
[3370]43#include "core/GUIManager.h"
[3196]44#include "core/Loader.h"
45#include "core/XMLFile.h"
46
[2087]47#include "LevelManager.h"
[2662]48#include "PlayerManager.h"
[1661]49
50namespace orxonox
51{
[3370]52    DeclareGameState(GSLevel, "level", false, false);
[2896]53    SetConsoleCommand(GSLevel, showIngameGUI, true);
[1934]54
[3008]55    XMLFile* GSLevel::startFile_s = NULL;
56
[3370]57    GSLevel::GSLevel(const GameStateInfo& info)
58        : GameState(info)
[2896]59        , keyBinder_(0)
60        , gameInputState_(0)
61        , guiMouseOnlyInputState_(0)
62        , guiKeysOnlyInputState_(0)
[1661]63    {
[1887]64        RegisterObject(GSLevel);
[2662]65
66        this->ccKeybind_ = 0;
67        this->ccTkeybind_ = 0;
[1661]68    }
69
70    GSLevel::~GSLevel()
71    {
72    }
73
[1887]74    void GSLevel::setConfigValues()
75    {
76        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
77    }
78
[2896]79    void GSLevel::activate()
[1661]80    {
[2896]81        setConfigValues();
82
83        if (GameMode::showsGraphics())
[2087]84        {
[3327]85            gameInputState_ = InputManager::getInstance().createInputState("game");
[2087]86            keyBinder_ = new KeyBinder();
[2710]87            keyBinder_->loadBindings("keybindings.ini");
[2896]88            gameInputState_->setHandler(keyBinder_);
[1661]89
[3327]90            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
[2896]91            guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr());
92
[3327]93            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
[2896]94            guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr());
[2087]95        }
[1662]96
[2896]97        if (GameMode::isMaster())
[2087]98        {
99            this->loadLevel();
100        }
[1755]101
[2896]102        if (GameMode::showsGraphics())
[2087]103        {
104            // keybind console command
[5829]105            ccKeybind_ = createConsoleCommand(createFunctor(&GSLevel::keybind, this), "keybind");
[2662]106            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
[5829]107            ccTkeybind_ = createConsoleCommand(createFunctor(&GSLevel::tkeybind, this), "tkeybind");
[2662]108            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
[2087]109            // set our console command as callback for the key detector
110            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
111
112            // level is loaded: we can start capturing the input
[3327]113            InputManager::getInstance().enterState("game");
[5820]114           
115            // connect the HumanPlayer to the game
[5850]116            PlayerManager::getInstance().clientConnected(0);
[2087]117        }
[2662]118    }
[2087]119
[2896]120    void GSLevel::showIngameGUI(bool show)
[2662]121    {
[2896]122        if (show)
123        {
[3196]124            GUIManager::getInstance().showGUI("inGameTest");
125            GUIManager::getInstance().executeCode("showCursor()");
[3327]126            InputManager::getInstance().enterState("guiMouseOnly");
[2896]127        }
128        else
129        {
[3196]130            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
131            GUIManager::getInstance().executeCode("hideCursor()");
[3327]132            InputManager::getInstance().leaveState("guiMouseOnly");
[2896]133        }
134    }
135
136    void GSLevel::deactivate()
137    {
[2928]138/*
[2662]139        // destroy console commands
140        if (this->ccKeybind_)
[2087]141        {
[2662]142            delete this->ccKeybind_;
143            this->ccKeybind_ = 0;
[2087]144        }
[2662]145        if (this->ccTkeybind_)
146        {
147            delete this->ccTkeybind_;
148            this->ccTkeybind_ = 0;
149        }
[2928]150*/
[1661]151
[5695]152        if (GameMode::showsGraphics())
153        {
[5832]154            // disconnect the HumanPlayer
[5850]155            PlayerManager::getInstance().clientDisconnected(0);
[5832]156           
[5695]157            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
158            Ogre::CompositorManager::getSingleton().removeAll();
159        }
[2896]160
[1662]161        // this call will delete every BaseObject!
162        // But currently this will call methods of objects that exist no more
163        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
164        // and call a sceneNode method that has already been destroy by the corresponding space ship.
165        //Loader::close();
166
[2896]167        if (GameMode::showsGraphics())
[5820]168        {
[3327]169            InputManager::getInstance().leaveState("game");
[5820]170        }
[1662]171
[2896]172        if (GameMode::isMaster())
[2087]173            this->unloadLevel();
[1662]174
[2896]175        if (GameMode::showsGraphics())
[2087]176        {
[2896]177            gameInputState_->setHandler(0);
178            guiMouseOnlyInputState_->setHandler(0);
179            guiKeysOnlyInputState_->setHandler(0);
[3327]180            InputManager::getInstance().destroyState("game");
[5818]181            InputManager::getInstance().destroyState("guiKeysOnly");
182            InputManager::getInstance().destroyState("guiMouseOnly");
[2087]183            if (this->keyBinder_)
[2662]184            {
[5799]185                this->keyBinder_->destroy();
[2662]186                this->keyBinder_ = 0;
187            }
[2087]188        }
[1661]189    }
190
[2896]191    void GSLevel::update(const Clock& time)
[1661]192    {
[2896]193        // Note: Temporarily moved to GSGraphics.
[2087]194        //// Call the scene objects
195        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
196        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]197    }
198
[1670]199    void GSLevel::loadLevel()
200    {
201        // call the loader
202        COUT(0) << "Loading level..." << std::endl;
[5695]203        startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel());
[3008]204        Loader::open(startFile_s);
[1670]205    }
206
207    void GSLevel::unloadLevel()
208    {
[5805]209        Loader::unload(startFile_s);
[2087]210
[3008]211        delete startFile_s;
[1670]212    }
[1887]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
[2896]230        the key/button/axis that has been activated. This is configured above in activate().
[1887]231    */
232    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
233    {
[2896]234        if (GameMode::showsGraphics())
[1887]235        {
[2087]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)
[1887]242            {
[2087]243                if (bound)
244                {
245                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
[3327]246                    InputManager::getInstance().enterState("detector");
[2087]247                    bindingString = command;
248                    bTemporarySaved = bTemporary;
249                    bound = false;
250                }
251                //else:  We're still in a keybind command. ignore this call.
[1887]252            }
[2087]253            else
[1887]254            {
[2087]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);
[3327]261                    InputManager::getInstance().leaveState("detector");
[2087]262                    bound = true;
263                }
264                // else: A key was pressed within the same tick, ignore it.
[1887]265            }
266        }
267    }
[1661]268}
Note: See TracBrowser for help on using the repository browser.