Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSLevel.cc @ 5693

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

merged libraries branch back to trunk

  • Property svn:eol-style set to native
File size: 9.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 "core/input/InputManager.h"
33#include "core/input/InputState.h"
34#include "core/input/KeyBinder.h"
35#include "core/Clock.h"
36#include "core/ConsoleCommand.h"
37#include "core/ConfigValueIncludes.h"
38#include "core/CoreIncludes.h"
39#include "core/Game.h"
40#include "core/GameMode.h"
41#include "core/Core.h"
42#include "core/GraphicsManager.h"
43#include "core/GUIManager.h"
44#include "core/Loader.h"
45#include "core/XMLFile.h"
46
47#include "tools/interfaces/Tickable.h"
48#include "objects/Radar.h"
49#include "CameraManager.h"
50#include "LevelManager.h"
51#include "PlayerManager.h"
52
53namespace orxonox
54{
55    DeclareGameState(GSLevel, "level", false, false);
56    SetConsoleCommand(GSLevel, showIngameGUI, true);
57
58    XMLFile* GSLevel::startFile_s = NULL;
59
60    GSLevel::GSLevel(const GameStateInfo& info)
61        : GameState(info)
62        , keyBinder_(0)
63        , gameInputState_(0)
64        , guiMouseOnlyInputState_(0)
65        , guiKeysOnlyInputState_(0)
66        , radar_(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            // Start the Radar
105            this->radar_ = new Radar();
106        }
107
108        this->playerManager_ = new PlayerManager();
109
110        this->scope_GSLevel_ = new Scope<ScopeID::GSLevel>();
111
112        if (GameMode::isMaster())
113        {
114            this->loadLevel();
115        }
116
117        if (GameMode::showsGraphics())
118        {
119            // keybind console command
120            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
121            functor1->setObject(this);
122            ccKeybind_ = createConsoleCommand(functor1, "keybind");
123            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
124            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
125            functor2->setObject(this);
126            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
127            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
128            // set our console command as callback for the key detector
129            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
130
131            // level is loaded: we can start capturing the input
132            InputManager::getInstance().enterState("game");
133        }
134    }
135
136    void GSLevel::showIngameGUI(bool show)
137    {
138        if (show)
139        {
140            GUIManager::getInstance().showGUI("inGameTest");
141            GUIManager::getInstance().executeCode("showCursor()");
142            InputManager::getInstance().enterState("guiMouseOnly");
143        }
144        else
145        {
146            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
147            GUIManager::getInstance().executeCode("hideCursor()");
148            InputManager::getInstance().leaveState("guiMouseOnly");
149        }
150    }
151
152    void GSLevel::deactivate()
153    {
154/*
155        // destroy console commands
156        if (this->ccKeybind_)
157        {
158            delete this->ccKeybind_;
159            this->ccKeybind_ = 0;
160        }
161        if (this->ccTkeybind_)
162        {
163            delete this->ccTkeybind_;
164            this->ccTkeybind_ = 0;
165        }
166*/
167
168
169        // this call will delete every BaseObject!
170        // But currently this will call methods of objects that exist no more
171        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
172        // and call a sceneNode method that has already been destroy by the corresponding space ship.
173        //Loader::close();
174
175        if (GameMode::showsGraphics())
176            InputManager::getInstance().leaveState("game");
177
178        if (GameMode::isMaster())
179            this->unloadLevel();
180
181        if (this->radar_)
182        {
183            delete this->radar_;
184            this->radar_ = 0;
185        }
186
187        if (this->cameraManager_)
188        {
189            delete this->cameraManager_;
190            this->cameraManager_ = 0;
191        }
192
193        if (this->playerManager_)
194        {
195            delete this->playerManager_;
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            if (this->keyBinder_)
212            {
213                delete this->keyBinder_;
214                this->keyBinder_ = 0;
215            }
216        }
217    }
218
219    void GSLevel::update(const Clock& time)
220    {
221        // Note: Temporarily moved to GSGraphics.
222        //// Call the scene objects
223        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
224        //    it->tick(time.getDeltaTime() * this->timeFactor_);
225    }
226
227    void GSLevel::loadLevel()
228    {
229        // call the loader
230        COUT(0) << "Loading level..." << std::endl;
231        startFile_s = new XMLFile(Core::getMediaPathString() + "levels" + '/' + LevelManager::getInstance().getDefaultLevel());
232        Loader::open(startFile_s);
233    }
234
235    void GSLevel::unloadLevel()
236    {
237        //////////////////////////////////////////////////////////////////////////////////////////
238        // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO //
239        //////////////////////////////////////////////////////////////////////////////////////////
240        // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY /
241        //////////////////////////////////////////////////////////////////////////////////////////
242
243        delete startFile_s;
244    }
245
246    void GSLevel::keybind(const std::string &command)
247    {
248        this->keybindInternal(command, false);
249    }
250
251    void GSLevel::tkeybind(const std::string &command)
252    {
253        this->keybindInternal(command, true);
254    }
255
256    /**
257    @brief
258        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
259    @param command
260        Command string that can be executed by the CommandExecutor
261        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
262        the key/button/axis that has been activated. This is configured above in activate().
263    */
264    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
265    {
266        if (GameMode::showsGraphics())
267        {
268            static std::string bindingString = "";
269            static bool bTemporarySaved = false;
270            static bool bound = true;
271            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
272            // Howerver there will be no real issue if it happens anyway.
273            if (command.find(keyDetectorCallbackCode_) != 0)
274            {
275                if (bound)
276                {
277                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
278                    InputManager::getInstance().enterState("detector");
279                    bindingString = command;
280                    bTemporarySaved = bTemporary;
281                    bound = false;
282                }
283                //else:  We're still in a keybind command. ignore this call.
284            }
285            else
286            {
287                if (!bound)
288                {
289                    // user has pressed the key
290                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
291                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
292                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
293                    InputManager::getInstance().leaveState("detector");
294                    bound = true;
295                }
296                // else: A key was pressed within the same tick, ignore it.
297            }
298        }
299    }
300}
Note: See TracBrowser for help on using the repository browser.