Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2925 was 2911, checked in by landauf, 16 years ago

Merged r1-2096 of questsystem5 back to trunk

I hope there weren't more "hidden merge changes" in r2909 than the one in OverlayGroup (removeElement) (and related to this the adjustments in NotificationQueue).

The corresponding media commit seems not yet to be done, but it doesn't break the build.

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