Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3015 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
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 "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"
[1662]36#include "core/Loader.h"
[2087]37#include "core/XMLFile.h"
[1887]38#include "core/CommandExecutor.h"
39#include "core/ConsoleCommand.h"
[1934]40#include "core/CommandLine.h"
[1887]41#include "core/ConfigValueIncludes.h"
[2896]42#include "core/Core.h"
[1887]43#include "core/CoreIncludes.h"
[2896]44#include "core/Game.h"
45#include "core/GameMode.h"
[1694]46#include "objects/Tickable.h"
[1819]47#include "objects/Radar.h"
[2087]48#include "CameraManager.h"
[2896]49#include "GraphicsManager.h"
[2087]50#include "LevelManager.h"
[2662]51#include "PlayerManager.h"
[2896]52#include "gui/GUIManager.h"
[2911]53#include "objects/quest/QuestManager.h"
54#include "overlays/notifications/NotificationManager.h"
[1661]55
56namespace orxonox
57{
[2896]58    AddGameState(GSLevel, "level");
59
[2826]60    SetCommandLineArgument(level, "presentation_dm.oxw").shortcut("l");
[2896]61    SetConsoleCommand(GSLevel, showIngameGUI, true);
[3008]62    SetConsoleCommand(GSLevel, setLevel, true);
[1934]63
[3008]64    XMLFile* GSLevel::startFile_s = NULL;
65
[2896]66    GSLevel::GSLevel(const std::string& name)
67        : GameState(name)
68        , keyBinder_(0)
69        , gameInputState_(0)
70        , guiMouseOnlyInputState_(0)
71        , guiKeysOnlyInputState_(0)
[1662]72        , radar_(0)
[2087]73        , cameraManager_(0)
74        , levelManager_(0)
[1661]75    {
[1887]76        RegisterObject(GSLevel);
[2662]77
78        this->ccKeybind_ = 0;
79        this->ccTkeybind_ = 0;
[1661]80    }
81
82    GSLevel::~GSLevel()
83    {
84    }
85
[1887]86    void GSLevel::setConfigValues()
87    {
88        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
89    }
90
[2896]91    void GSLevel::activate()
[1661]92    {
[2896]93        setConfigValues();
94
95        if (GameMode::showsGraphics())
[2087]96        {
[2896]97            gameInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game");
[2087]98            keyBinder_ = new KeyBinder();
[2710]99            keyBinder_->loadBindings("keybindings.ini");
[2896]100            gameInputState_->setHandler(keyBinder_);
[1661]101
[2896]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
[2087]108            // create the global CameraManager
[2896]109            this->cameraManager_ = new CameraManager(GraphicsManager::getInstance().getViewport());
[1688]110
[2087]111            // Start the Radar
112            this->radar_ = new Radar();
113        }
[1662]114
[2662]115        this->playerManager_ = new PlayerManager();
116
[2911]117        this->questManager_ = new QuestManager();
118
119        this->notificationManager_ = new NotificationManager();
120
[2896]121        if (GameMode::isMaster())
[2087]122        {
123            // create the global LevelManager
124            this->levelManager_ = new LevelManager();
[1662]125
[2087]126            this->loadLevel();
127        }
[1755]128
[2896]129        if (GameMode::showsGraphics())
[2087]130        {
131            // keybind console command
132            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
133            functor1->setObject(this);
[2662]134            ccKeybind_ = createConsoleCommand(functor1, "keybind");
135            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
[2087]136            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
137            functor2->setObject(this);
[2662]138            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
139            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
[2087]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        }
[2662]146    }
[2087]147
[2896]148    void GSLevel::showIngameGUI(bool show)
[2662]149    {
[2896]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    {
[2928]166/*
[2662]167        // destroy console commands
168        if (this->ccKeybind_)
[2087]169        {
[2662]170            delete this->ccKeybind_;
171            this->ccKeybind_ = 0;
[2087]172        }
[2662]173        if (this->ccTkeybind_)
174        {
175            delete this->ccTkeybind_;
176            this->ccTkeybind_ = 0;
177        }
[2928]178*/
[1661]179
[2896]180
[1662]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
[2896]187        if (GameMode::showsGraphics())
[2087]188            InputManager::getInstance().requestLeaveState("game");
[1662]189
[2896]190        if (GameMode::isMaster())
[2087]191            this->unloadLevel();
[1662]192
[2087]193        if (this->radar_)
[2662]194        {
[2087]195            delete this->radar_;
[2662]196            this->radar_ = 0;
197        }
[2087]198
199        if (this->cameraManager_)
[2662]200        {
[2087]201            delete this->cameraManager_;
[2662]202            this->cameraManager_ = 0;
203        }
[2087]204
205        if (this->levelManager_)
[2662]206        {
[2087]207            delete this->levelManager_;
[2662]208            this->levelManager_ = 0;
209        }
[2087]210
[2662]211        if (this->playerManager_)
212        {
213            delete this->playerManager_;
214            this->playerManager_ = 0;
215        }
216
[2911]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
[2896]229        if (GameMode::showsGraphics())
[2087]230        {
[2896]231            gameInputState_->setHandler(0);
232            guiMouseOnlyInputState_->setHandler(0);
233            guiKeysOnlyInputState_->setHandler(0);
[2087]234            InputManager::getInstance().requestDestroyState("game");
235            if (this->keyBinder_)
[2662]236            {
[2087]237                delete this->keyBinder_;
[2662]238                this->keyBinder_ = 0;
239            }
[2087]240        }
[1661]241    }
242
[2896]243    void GSLevel::update(const Clock& time)
[1661]244    {
[2896]245        // Note: Temporarily moved to GSGraphics.
[2087]246        //// Call the scene objects
247        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
248        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]249    }
250
[1670]251    void GSLevel::loadLevel()
252    {
253        // call the loader
254        COUT(0) << "Loading level..." << std::endl;
[1934]255        std::string levelName;
[3008]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);
[1670]262    }
263
[3008]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
[1670]270    void GSLevel::unloadLevel()
271    {
[2087]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
[3008]278        delete startFile_s;
[1670]279    }
[1887]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
[2896]297        the key/button/axis that has been activated. This is configured above in activate().
[1887]298    */
299    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
300    {
[2896]301        if (GameMode::showsGraphics())
[1887]302        {
[2087]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)
[1887]309            {
[2087]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.
[1887]319            }
[2087]320            else
[1887]321            {
[2087]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.
[1887]332            }
333        }
334    }
[1661]335}
Note: See TracBrowser for help on using the repository browser.