Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Extracted path related parts of Core into a new PathConfig class. This should decrease the mess in Core.cc a little bit.

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