Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3356 was 3327, checked in by rgrieder, 15 years ago

Merged all remaining revisions from core4 back to the trunk.

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