Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/gamestates/GSLevel.cc @ 3181

Last change on this file since 3181 was 3180, checked in by rgrieder, 16 years ago

Cleanup in pickup and quest (almost only cleaned the header file sections).

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