Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2931 was 2928, checked in by rgrieder, 16 years ago

Deleting console commands currently seems to be a rather bad idea (though on by box the console still worked, including tab completion).
—> Commented all the temporary console commands in the GameStates until we have a proper solution.

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