Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3034 was 3008, checked in by bknecht, 16 years ago

You don't need no —level or -l anymore now. You may choose your favorite level from the main menu ;-)

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