Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/modules/gamestates/GSLevel.cc @ 5813

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

Moved Radar Management from GSLevel to Scene.

  • 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 *      Benjamin Knecht
27 *
28 */
29
30#include "GSLevel.h"
31
32#include <OgreCompositorManager.h>
33
34#include "core/input/InputManager.h"
35#include "core/input/InputState.h"
36#include "core/input/KeyBinder.h"
37#include "core/Clock.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/Core.h"
44#include "core/GraphicsManager.h"
45#include "core/GUIManager.h"
46#include "core/Loader.h"
47#include "core/XMLFile.h"
48
49#include "tools/interfaces/Tickable.h"
50#include "CameraManager.h"
51#include "LevelManager.h"
52#include "PlayerManager.h"
53#include "infos/HumanPlayer.h"
54
55namespace orxonox
56{
57    DeclareGameState(GSLevel, "level", false, false);
58    SetConsoleCommand(GSLevel, showIngameGUI, true);
59
60    XMLFile* GSLevel::startFile_s = NULL;
61
62    GSLevel::GSLevel(const GameStateInfo& info)
63        : GameState(info)
64        , keyBinder_(0)
65        , gameInputState_(0)
66        , guiMouseOnlyInputState_(0)
67        , guiKeysOnlyInputState_(0)
68        , cameraManager_(0)
69    {
70        RegisterObject(GSLevel);
71
72        this->ccKeybind_ = 0;
73        this->ccTkeybind_ = 0;
74    }
75
76    GSLevel::~GSLevel()
77    {
78    }
79
80    void GSLevel::setConfigValues()
81    {
82        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
83    }
84
85    void GSLevel::activate()
86    {
87        setConfigValues();
88
89        if (GameMode::showsGraphics())
90        {
91            gameInputState_ = InputManager::getInstance().createInputState("game");
92            keyBinder_ = new KeyBinder();
93            keyBinder_->loadBindings("keybindings.ini");
94            gameInputState_->setHandler(keyBinder_);
95
96            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
97            guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr());
98
99            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
100            guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr());
101
102            // create the global CameraManager
103            this->cameraManager_ = new CameraManager(GraphicsManager::getInstance().getViewport());
104        }
105
106        this->playerManager_ = new PlayerManager();
107
108        this->scope_GSLevel_ = new Scope<ScopeID::GSLevel>();
109
110        if (GameMode::isMaster())
111        {
112            this->loadLevel();
113        }
114
115        if (GameMode::showsGraphics())
116        {
117            // keybind console command
118            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
119            functor1->setObject(this);
120            ccKeybind_ = createConsoleCommand(functor1, "keybind");
121            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
122            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
123            functor2->setObject(this);
124            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
125            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
126            // set our console command as callback for the key detector
127            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
128
129            // level is loaded: we can start capturing the input
130            InputManager::getInstance().enterState("game");
131        }
132    }
133
134    void GSLevel::showIngameGUI(bool show)
135    {
136        if (show)
137        {
138            GUIManager::getInstance().showGUI("inGameTest");
139            GUIManager::getInstance().executeCode("showCursor()");
140            InputManager::getInstance().enterState("guiMouseOnly");
141        }
142        else
143        {
144            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
145            GUIManager::getInstance().executeCode("hideCursor()");
146            InputManager::getInstance().leaveState("guiMouseOnly");
147        }
148    }
149
150    void GSLevel::deactivate()
151    {
152/*
153        // destroy console commands
154        if (this->ccKeybind_)
155        {
156            delete this->ccKeybind_;
157            this->ccKeybind_ = 0;
158        }
159        if (this->ccTkeybind_)
160        {
161            delete this->ccTkeybind_;
162            this->ccTkeybind_ = 0;
163        }
164*/
165
166        if (GameMode::showsGraphics())
167        {
168            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
169            Ogre::CompositorManager::getSingleton().removeAll();
170        }
171
172        // this call will delete every BaseObject!
173        // But currently this will call methods of objects that exist no more
174        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
175        // and call a sceneNode method that has already been destroy by the corresponding space ship.
176        //Loader::close();
177
178        if (GameMode::showsGraphics())
179            InputManager::getInstance().leaveState("game");
180
181        if (GameMode::isMaster())
182            this->unloadLevel();
183
184        if (this->cameraManager_)
185        {
186            delete this->cameraManager_;
187            this->cameraManager_ = 0;
188        }
189
190        if (this->playerManager_)
191        {
192            this->playerManager_->destroy();
193            this->playerManager_ = 0;
194        }
195
196        if (this->scope_GSLevel_)
197        {
198            delete this->scope_GSLevel_;
199            this->scope_GSLevel_ = NULL;
200        }
201
202        if (GameMode::showsGraphics())
203        {
204            gameInputState_->setHandler(0);
205            guiMouseOnlyInputState_->setHandler(0);
206            guiKeysOnlyInputState_->setHandler(0);
207            InputManager::getInstance().destroyState("game");
208            if (this->keyBinder_)
209            {
210                this->keyBinder_->destroy();
211                this->keyBinder_ = 0;
212            }
213        }
214    }
215
216    void GSLevel::update(const Clock& time)
217    {
218        // Note: Temporarily moved to GSGraphics.
219        //// Call the scene objects
220        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
221        //    it->tick(time.getDeltaTime() * this->timeFactor_);
222    }
223
224    void GSLevel::loadLevel()
225    {
226        // call the loader
227        COUT(0) << "Loading level..." << std::endl;
228        startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel());
229        Loader::open(startFile_s);
230    }
231
232    void GSLevel::unloadLevel()
233    {
234        for (ObjectList<HumanPlayer>::iterator it = ObjectList<HumanPlayer>::begin(); it; ++it)
235            it->setGametype(0);
236       
237        Loader::unload(startFile_s);
238
239        delete startFile_s;
240    }
241
242    void GSLevel::keybind(const std::string &command)
243    {
244        this->keybindInternal(command, false);
245    }
246
247    void GSLevel::tkeybind(const std::string &command)
248    {
249        this->keybindInternal(command, true);
250    }
251
252    /**
253    @brief
254        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
255    @param command
256        Command string that can be executed by the CommandExecutor
257        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
258        the key/button/axis that has been activated. This is configured above in activate().
259    */
260    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
261    {
262        if (GameMode::showsGraphics())
263        {
264            static std::string bindingString = "";
265            static bool bTemporarySaved = false;
266            static bool bound = true;
267            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
268            // Howerver there will be no real issue if it happens anyway.
269            if (command.find(keyDetectorCallbackCode_) != 0)
270            {
271                if (bound)
272                {
273                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
274                    InputManager::getInstance().enterState("detector");
275                    bindingString = command;
276                    bTemporarySaved = bTemporary;
277                    bound = false;
278                }
279                //else:  We're still in a keybind command. ignore this call.
280            }
281            else
282            {
283                if (!bound)
284                {
285                    // user has pressed the key
286                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
287                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
288                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
289                    InputManager::getInstance().leaveState("detector");
290                    bound = true;
291                }
292                // else: A key was pressed within the same tick, ignore it.
293            }
294        }
295    }
296}
Note: See TracBrowser for help on using the repository browser.