Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/orxonox/gamestates/GSLevel.cc @ 4073

Last change on this file since 4073 was 3196, checked in by rgrieder, 16 years ago

Merged pch branch back to trunk.

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