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
RevLine 
[1661]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:
[1662]25 *      Fabian 'x3n' Landau
[2896]26 *      Benjamin Knecht
[1661]27 *
28 */
29
30#include "GSLevel.h"
31
[3173]32#include "util/Debug.h"
[1661]33#include "core/input/InputManager.h"
34#include "core/input/SimpleInputState.h"
35#include "core/input/KeyBinder.h"
[3173]36#include "core/Clock.h"
37#include "core/CommandLine.h"
[1887]38#include "core/ConsoleCommand.h"
39#include "core/ConfigValueIncludes.h"
[3180]40#include "core/CoreIncludes.h"
[2896]41#include "core/Game.h"
42#include "core/GameMode.h"
[3180]43#include "core/Core.h"
[3173]44#include "core/Loader.h"
45#include "core/ObjectList.h"
46#include "core/XMLFile.h"
47
[3177]48#include "interfaces/Tickable.h"
[3173]49#include "objects/Radar.h"
50#include "objects/quest/QuestManager.h"
51#include "overlays/notifications/NotificationManager.h"
52#include "gui/GUIManager.h"
[2087]53#include "CameraManager.h"
[2896]54#include "GraphicsManager.h"
[2087]55#include "LevelManager.h"
[2662]56#include "PlayerManager.h"
[1661]57
58namespace orxonox
59{
[2896]60    AddGameState(GSLevel, "level");
61
[3036]62    SetCommandLineArgument(level, "").shortcut("l");
[2896]63    SetConsoleCommand(GSLevel, showIngameGUI, true);
[1934]64
[3008]65    XMLFile* GSLevel::startFile_s = NULL;
66
[2896]67    GSLevel::GSLevel(const std::string& name)
68        : GameState(name)
69        , keyBinder_(0)
70        , gameInputState_(0)
71        , guiMouseOnlyInputState_(0)
72        , guiKeysOnlyInputState_(0)
[1662]73        , radar_(0)
[2087]74        , cameraManager_(0)
75        , levelManager_(0)
[1661]76    {
[1887]77        RegisterObject(GSLevel);
[2662]78
79        this->ccKeybind_ = 0;
80        this->ccTkeybind_ = 0;
[1661]81    }
82
83    GSLevel::~GSLevel()
84    {
85    }
86
[1887]87    void GSLevel::setConfigValues()
88    {
89        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
90    }
91
[2896]92    void GSLevel::activate()
[1661]93    {
[2896]94        setConfigValues();
95
96        if (GameMode::showsGraphics())
[2087]97        {
[2896]98            gameInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game");
[2087]99            keyBinder_ = new KeyBinder();
[2710]100            keyBinder_->loadBindings("keybindings.ini");
[2896]101            gameInputState_->setHandler(keyBinder_);
[1661]102
[2896]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
[2087]109            // create the global CameraManager
[2896]110            this->cameraManager_ = new CameraManager(GraphicsManager::getInstance().getViewport());
[1688]111
[2087]112            // Start the Radar
113            this->radar_ = new Radar();
114        }
[1662]115
[2662]116        this->playerManager_ = new PlayerManager();
117
[2911]118        this->questManager_ = new QuestManager();
119
120        this->notificationManager_ = new NotificationManager();
121
[2896]122        if (GameMode::isMaster())
[2087]123        {
124            // create the global LevelManager
125            this->levelManager_ = new LevelManager();
[1662]126
[2087]127            this->loadLevel();
128        }
[1755]129
[2896]130        if (GameMode::showsGraphics())
[2087]131        {
132            // keybind console command
133            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
134            functor1->setObject(this);
[2662]135            ccKeybind_ = createConsoleCommand(functor1, "keybind");
136            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
[2087]137            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
138            functor2->setObject(this);
[2662]139            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
140            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
[2087]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        }
[2662]147    }
[2087]148
[2896]149    void GSLevel::showIngameGUI(bool show)
[2662]150    {
[2896]151        if (show)
152        {
[3154]153            GUIManager::getInstance().showGUI("inGameTest");
154            GUIManager::getInstance().executeCode("showCursor()");
[2896]155            InputManager::getInstance().requestEnterState("guiMouseOnly");
156        }
157        else
158        {
[3154]159            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
160            GUIManager::getInstance().executeCode("hideCursor()");
[2896]161            InputManager::getInstance().requestLeaveState("guiMouseOnly");
162        }
163    }
164
165    void GSLevel::deactivate()
166    {
[2928]167/*
[2662]168        // destroy console commands
169        if (this->ccKeybind_)
[2087]170        {
[2662]171            delete this->ccKeybind_;
172            this->ccKeybind_ = 0;
[2087]173        }
[2662]174        if (this->ccTkeybind_)
175        {
176            delete this->ccTkeybind_;
177            this->ccTkeybind_ = 0;
178        }
[2928]179*/
[1661]180
[2896]181
[1662]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
[2896]188        if (GameMode::showsGraphics())
[2087]189            InputManager::getInstance().requestLeaveState("game");
[1662]190
[2896]191        if (GameMode::isMaster())
[2087]192            this->unloadLevel();
[1662]193
[2087]194        if (this->radar_)
[2662]195        {
[2087]196            delete this->radar_;
[2662]197            this->radar_ = 0;
198        }
[2087]199
200        if (this->cameraManager_)
[2662]201        {
[2087]202            delete this->cameraManager_;
[2662]203            this->cameraManager_ = 0;
204        }
[2087]205
206        if (this->levelManager_)
[2662]207        {
[2087]208            delete this->levelManager_;
[2662]209            this->levelManager_ = 0;
210        }
[2087]211
[2662]212        if (this->playerManager_)
213        {
214            delete this->playerManager_;
215            this->playerManager_ = 0;
216        }
217
[2911]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
[2896]230        if (GameMode::showsGraphics())
[2087]231        {
[2896]232            gameInputState_->setHandler(0);
233            guiMouseOnlyInputState_->setHandler(0);
234            guiKeysOnlyInputState_->setHandler(0);
[2087]235            InputManager::getInstance().requestDestroyState("game");
236            if (this->keyBinder_)
[2662]237            {
[2087]238                delete this->keyBinder_;
[2662]239                this->keyBinder_ = 0;
240            }
[2087]241        }
[1661]242    }
243
[2896]244    void GSLevel::update(const Clock& time)
[1661]245    {
[2896]246        // Note: Temporarily moved to GSGraphics.
[2087]247        //// Call the scene objects
248        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
249        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]250    }
251
[1670]252    void GSLevel::loadLevel()
253    {
254        // call the loader
255        COUT(0) << "Loading level..." << std::endl;
[1934]256        std::string levelName;
[3036]257        CommandLine::getValue("level", &levelName);
258        if (levelName == "")
259            startFile_s = new XMLFile(Core::getMediaPathString() + "levels" + '/' + Game::getInstance().getLevel());
260        else
[3008]261            startFile_s = new XMLFile(Core::getMediaPathString() + "levels" + '/' + levelName);
262        Loader::open(startFile_s);
[1670]263    }
264
265    void GSLevel::unloadLevel()
266    {
[2087]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
[3008]273        delete startFile_s;
[1670]274    }
[1887]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
[2896]292        the key/button/axis that has been activated. This is configured above in activate().
[1887]293    */
294    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
295    {
[2896]296        if (GameMode::showsGraphics())
[1887]297        {
[2087]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)
[1887]304            {
[2087]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.
[1887]314            }
[2087]315            else
[1887]316            {
[2087]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.
[1887]327            }
328        }
329    }
[1661]330}
Note: See TracBrowser for help on using the repository browser.