Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Moved Clock from core to util (used in Scope anyway).

  • Property svn:eol-style set to native
File size: 8.8 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 *      Benjamin Knecht
27 *
28 */
29
30#include "GSLevel.h"
31
32#include <OgreCompositorManager.h>
33
34#include "util/Clock.h"
35#include "core/input/InputManager.h"
36#include "core/input/InputState.h"
37#include "core/input/KeyBinder.h"
38#include "core/ConsoleCommand.h"
39#include "core/ConfigValueIncludes.h"
40#include "core/CoreIncludes.h"
41#include "core/Game.h"
42#include "core/GameMode.h"
43#include "core/GUIManager.h"
44#include "core/Loader.h"
45#include "core/XMLFile.h"
46
47#include "LevelManager.h"
48#include "PlayerManager.h"
49
50namespace orxonox
51{
52    DeclareGameState(GSLevel, "level", false, false);
53    SetConsoleCommand(GSLevel, showIngameGUI, true);
54
55    XMLFile* GSLevel::startFile_s = NULL;
56
57    GSLevel::GSLevel(const GameStateInfo& info)
58        : GameState(info)
59        , keyBinder_(0)
60        , gameInputState_(0)
61        , guiMouseOnlyInputState_(0)
62        , guiKeysOnlyInputState_(0)
63    {
64        RegisterObject(GSLevel);
65
66        this->ccKeybind_ = 0;
67        this->ccTkeybind_ = 0;
68    }
69
70    GSLevel::~GSLevel()
71    {
72    }
73
74    void GSLevel::setConfigValues()
75    {
76        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
77    }
78
79    void GSLevel::activate()
80    {
81        setConfigValues();
82
83        if (GameMode::showsGraphics())
84        {
85            gameInputState_ = InputManager::getInstance().createInputState("game");
86            keyBinder_ = new KeyBinder();
87            keyBinder_->loadBindings("keybindings.ini");
88            gameInputState_->setHandler(keyBinder_);
89
90            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
91            guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr());
92
93            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
94            guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr());
95        }
96
97        if (GameMode::isMaster())
98        {
99            this->loadLevel();
100        }
101
102        if (GameMode::showsGraphics())
103        {
104            // keybind console command
105            ccKeybind_ = createConsoleCommand(createFunctor(&GSLevel::keybind, this), "keybind");
106            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
107            ccTkeybind_ = createConsoleCommand(createFunctor(&GSLevel::tkeybind, this), "tkeybind");
108            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
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
113            InputManager::getInstance().enterState("game");
114           
115            // connect the HumanPlayer to the game
116            PlayerManager::getInstance().clientConnected(0);
117        }
118    }
119
120    void GSLevel::showIngameGUI(bool show)
121    {
122        if (show)
123        {
124            GUIManager::getInstance().showGUI("inGameTest");
125            GUIManager::getInstance().executeCode("showCursor()");
126            InputManager::getInstance().enterState("guiMouseOnly");
127        }
128        else
129        {
130            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
131            GUIManager::getInstance().executeCode("hideCursor()");
132            InputManager::getInstance().leaveState("guiMouseOnly");
133        }
134    }
135
136    void GSLevel::deactivate()
137    {
138/*
139        // destroy console commands
140        if (this->ccKeybind_)
141        {
142            delete this->ccKeybind_;
143            this->ccKeybind_ = 0;
144        }
145        if (this->ccTkeybind_)
146        {
147            delete this->ccTkeybind_;
148            this->ccTkeybind_ = 0;
149        }
150*/
151
152        if (GameMode::showsGraphics())
153        {
154            // disconnect the HumanPlayer
155            PlayerManager::getInstance().clientDisconnected(0);
156           
157            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
158            Ogre::CompositorManager::getSingleton().removeAll();
159        }
160
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
167        if (GameMode::showsGraphics())
168        {
169            InputManager::getInstance().leaveState("game");
170        }
171
172        if (GameMode::isMaster())
173            this->unloadLevel();
174
175        if (GameMode::showsGraphics())
176        {
177            gameInputState_->setHandler(0);
178            guiMouseOnlyInputState_->setHandler(0);
179            guiKeysOnlyInputState_->setHandler(0);
180            InputManager::getInstance().destroyState("game");
181            InputManager::getInstance().destroyState("guiKeysOnly");
182            InputManager::getInstance().destroyState("guiMouseOnly");
183            if (this->keyBinder_)
184            {
185                this->keyBinder_->destroy();
186                this->keyBinder_ = 0;
187            }
188        }
189    }
190
191    void GSLevel::update(const Clock& time)
192    {
193        // Note: Temporarily moved to GSGraphics.
194        //// Call the scene objects
195        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
196        //    it->tick(time.getDeltaTime() * this->timeFactor_);
197    }
198
199    void GSLevel::loadLevel()
200    {
201        // call the loader
202        COUT(0) << "Loading level..." << std::endl;
203        startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel());
204        Loader::open(startFile_s);
205    }
206
207    void GSLevel::unloadLevel()
208    {
209        Loader::unload(startFile_s);
210
211        delete startFile_s;
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 activate().
231    */
232    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
233    {
234        if (GameMode::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().enterState("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().leaveState("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.