Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

replaced most occurrences of setObject(o) in combination with createFunctor(f) with the more convenient createFunctor(f, o)

  • 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/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            ccKeybind_ = createConsoleCommand(createFunctor(&GSLevel::keybind, this), "keybind");
119            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
120            ccTkeybind_ = createConsoleCommand(createFunctor(&GSLevel::tkeybind, this), "tkeybind");
121            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
122            // set our console command as callback for the key detector
123            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
124
125            // level is loaded: we can start capturing the input
126            InputManager::getInstance().enterState("game");
127           
128            // connect the HumanPlayer to the game
129            this->playerManager_->clientConnected(0);
130        }
131    }
132
133    void GSLevel::showIngameGUI(bool show)
134    {
135        if (show)
136        {
137            GUIManager::getInstance().showGUI("inGameTest");
138            GUIManager::getInstance().executeCode("showCursor()");
139            InputManager::getInstance().enterState("guiMouseOnly");
140        }
141        else
142        {
143            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
144            GUIManager::getInstance().executeCode("hideCursor()");
145            InputManager::getInstance().leaveState("guiMouseOnly");
146        }
147    }
148
149    void GSLevel::deactivate()
150    {
151/*
152        // destroy console commands
153        if (this->ccKeybind_)
154        {
155            delete this->ccKeybind_;
156            this->ccKeybind_ = 0;
157        }
158        if (this->ccTkeybind_)
159        {
160            delete this->ccTkeybind_;
161            this->ccTkeybind_ = 0;
162        }
163*/
164
165        if (GameMode::showsGraphics())
166        {
167            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
168            Ogre::CompositorManager::getSingleton().removeAll();
169        }
170
171        // this call will delete every BaseObject!
172        // But currently this will call methods of objects that exist no more
173        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
174        // and call a sceneNode method that has already been destroy by the corresponding space ship.
175        //Loader::close();
176
177        if (GameMode::showsGraphics())
178        {
179            // disconnect the HumanPlayer
180            this->playerManager_->clientDisconnected(0);
181           
182            InputManager::getInstance().leaveState("game");
183        }
184
185        if (GameMode::isMaster())
186            this->unloadLevel();
187
188        if (this->cameraManager_)
189        {
190            delete this->cameraManager_;
191            this->cameraManager_ = 0;
192        }
193
194        if (this->playerManager_)
195        {
196            this->playerManager_->destroy();
197            this->playerManager_ = 0;
198        }
199
200        if (this->scope_GSLevel_)
201        {
202            delete this->scope_GSLevel_;
203            this->scope_GSLevel_ = NULL;
204        }
205
206        if (GameMode::showsGraphics())
207        {
208            gameInputState_->setHandler(0);
209            guiMouseOnlyInputState_->setHandler(0);
210            guiKeysOnlyInputState_->setHandler(0);
211            InputManager::getInstance().destroyState("game");
212            InputManager::getInstance().destroyState("guiKeysOnly");
213            InputManager::getInstance().destroyState("guiMouseOnly");
214            if (this->keyBinder_)
215            {
216                this->keyBinder_->destroy();
217                this->keyBinder_ = 0;
218            }
219        }
220    }
221
222    void GSLevel::update(const Clock& time)
223    {
224        // Note: Temporarily moved to GSGraphics.
225        //// Call the scene objects
226        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
227        //    it->tick(time.getDeltaTime() * this->timeFactor_);
228    }
229
230    void GSLevel::loadLevel()
231    {
232        // call the loader
233        COUT(0) << "Loading level..." << std::endl;
234        startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel());
235        Loader::open(startFile_s);
236    }
237
238    void GSLevel::unloadLevel()
239    {
240        Loader::unload(startFile_s);
241
242        delete startFile_s;
243    }
244
245    void GSLevel::keybind(const std::string &command)
246    {
247        this->keybindInternal(command, false);
248    }
249
250    void GSLevel::tkeybind(const std::string &command)
251    {
252        this->keybindInternal(command, true);
253    }
254
255    /**
256    @brief
257        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
258    @param command
259        Command string that can be executed by the CommandExecutor
260        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
261        the key/button/axis that has been activated. This is configured above in activate().
262    */
263    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
264    {
265        if (GameMode::showsGraphics())
266        {
267            static std::string bindingString = "";
268            static bool bTemporarySaved = false;
269            static bool bound = true;
270            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
271            // Howerver there will be no real issue if it happens anyway.
272            if (command.find(keyDetectorCallbackCode_) != 0)
273            {
274                if (bound)
275                {
276                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
277                    InputManager::getInstance().enterState("detector");
278                    bindingString = command;
279                    bTemporarySaved = bTemporary;
280                    bound = false;
281                }
282                //else:  We're still in a keybind command. ignore this call.
283            }
284            else
285            {
286                if (!bound)
287                {
288                    // user has pressed the key
289                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
290                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
291                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
292                    InputManager::getInstance().leaveState("detector");
293                    bound = true;
294                }
295                // else: A key was pressed within the same tick, ignore it.
296            }
297        }
298    }
299}
Note: See TracBrowser for help on using the repository browser.