Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4740 was 3370, checked in by rgrieder, 15 years ago

Merged resource branch back to the trunk. Changes:

  • Automated graphics loading by evaluating whether a GameState requires it
  • Using native Tcl library (x3n)

Windows users: Update your dependency package!

  • Property svn:eol-style set to native
File size: 10.1 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
32#include "core/input/InputManager.h"
[3327]33#include "core/input/InputState.h"
[1661]34#include "core/input/KeyBinder.h"
[3196]35#include "core/Clock.h"
[1887]36#include "core/ConsoleCommand.h"
37#include "core/ConfigValueIncludes.h"
38#include "core/CoreIncludes.h"
[2896]39#include "core/Game.h"
40#include "core/GameMode.h"
[3196]41#include "core/Core.h"
[3370]42#include "core/GraphicsManager.h"
43#include "core/GUIManager.h"
[3196]44#include "core/Loader.h"
45#include "core/XMLFile.h"
46
47#include "interfaces/Tickable.h"
[1819]48#include "objects/Radar.h"
[3196]49#include "objects/quest/QuestManager.h"
50#include "overlays/notifications/NotificationManager.h"
[2087]51#include "CameraManager.h"
52#include "LevelManager.h"
[2662]53#include "PlayerManager.h"
[1661]54
55namespace orxonox
56{
[3370]57    DeclareGameState(GSLevel, "level", false, false);
[2896]58    SetConsoleCommand(GSLevel, showIngameGUI, true);
[1934]59
[3008]60    XMLFile* GSLevel::startFile_s = NULL;
61
[3370]62    GSLevel::GSLevel(const GameStateInfo& info)
63        : GameState(info)
[2896]64        , keyBinder_(0)
65        , gameInputState_(0)
66        , guiMouseOnlyInputState_(0)
67        , guiKeysOnlyInputState_(0)
[1662]68        , radar_(0)
[2087]69        , cameraManager_(0)
[1661]70    {
[1887]71        RegisterObject(GSLevel);
[2662]72
73        this->ccKeybind_ = 0;
74        this->ccTkeybind_ = 0;
[1661]75    }
76
77    GSLevel::~GSLevel()
78    {
79    }
80
[1887]81    void GSLevel::setConfigValues()
82    {
83        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
84    }
85
[2896]86    void GSLevel::activate()
[1661]87    {
[2896]88        setConfigValues();
89
90        if (GameMode::showsGraphics())
[2087]91        {
[3327]92            gameInputState_ = InputManager::getInstance().createInputState("game");
[2087]93            keyBinder_ = new KeyBinder();
[2710]94            keyBinder_->loadBindings("keybindings.ini");
[2896]95            gameInputState_->setHandler(keyBinder_);
[1661]96
[3327]97            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
[2896]98            guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr());
99
[3327]100            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
[2896]101            guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr());
102
[2087]103            // create the global CameraManager
[2896]104            this->cameraManager_ = new CameraManager(GraphicsManager::getInstance().getViewport());
[1688]105
[2087]106            // Start the Radar
107            this->radar_ = new Radar();
108        }
[1662]109
[2662]110        this->playerManager_ = new PlayerManager();
111
[2911]112        this->questManager_ = new QuestManager();
113
114        this->notificationManager_ = new NotificationManager();
115
[2896]116        if (GameMode::isMaster())
[2087]117        {
118            this->loadLevel();
119        }
[1755]120
[2896]121        if (GameMode::showsGraphics())
[2087]122        {
123            // keybind console command
124            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
125            functor1->setObject(this);
[2662]126            ccKeybind_ = createConsoleCommand(functor1, "keybind");
127            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
[2087]128            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
129            functor2->setObject(this);
[2662]130            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
131            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
[2087]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
[3327]136            InputManager::getInstance().enterState("game");
[2087]137        }
[2662]138    }
[2087]139
[2896]140    void GSLevel::showIngameGUI(bool show)
[2662]141    {
[2896]142        if (show)
143        {
[3196]144            GUIManager::getInstance().showGUI("inGameTest");
145            GUIManager::getInstance().executeCode("showCursor()");
[3327]146            InputManager::getInstance().enterState("guiMouseOnly");
[2896]147        }
148        else
149        {
[3196]150            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
151            GUIManager::getInstance().executeCode("hideCursor()");
[3327]152            InputManager::getInstance().leaveState("guiMouseOnly");
[2896]153        }
154    }
155
156    void GSLevel::deactivate()
157    {
[2928]158/*
[2662]159        // destroy console commands
160        if (this->ccKeybind_)
[2087]161        {
[2662]162            delete this->ccKeybind_;
163            this->ccKeybind_ = 0;
[2087]164        }
[2662]165        if (this->ccTkeybind_)
166        {
167            delete this->ccTkeybind_;
168            this->ccTkeybind_ = 0;
169        }
[2928]170*/
[1661]171
[2896]172
[1662]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
[2896]179        if (GameMode::showsGraphics())
[3327]180            InputManager::getInstance().leaveState("game");
[1662]181
[2896]182        if (GameMode::isMaster())
[2087]183            this->unloadLevel();
[1662]184
[2087]185        if (this->radar_)
[2662]186        {
[2087]187            delete this->radar_;
[2662]188            this->radar_ = 0;
189        }
[2087]190
191        if (this->cameraManager_)
[2662]192        {
[2087]193            delete this->cameraManager_;
[2662]194            this->cameraManager_ = 0;
195        }
[2087]196
[2662]197        if (this->playerManager_)
198        {
199            delete this->playerManager_;
200            this->playerManager_ = 0;
201        }
202
[2911]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
[2896]215        if (GameMode::showsGraphics())
[2087]216        {
[2896]217            gameInputState_->setHandler(0);
218            guiMouseOnlyInputState_->setHandler(0);
219            guiKeysOnlyInputState_->setHandler(0);
[3327]220            InputManager::getInstance().destroyState("game");
[2087]221            if (this->keyBinder_)
[2662]222            {
[2087]223                delete this->keyBinder_;
[2662]224                this->keyBinder_ = 0;
225            }
[2087]226        }
[1661]227    }
228
[2896]229    void GSLevel::update(const Clock& time)
[1661]230    {
[2896]231        // Note: Temporarily moved to GSGraphics.
[2087]232        //// Call the scene objects
233        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
234        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]235    }
236
[1670]237    void GSLevel::loadLevel()
238    {
239        // call the loader
240        COUT(0) << "Loading level..." << std::endl;
[3280]241        startFile_s = new XMLFile(Core::getMediaPathString() + "levels" + '/' + LevelManager::getInstance().getDefaultLevel());
[3008]242        Loader::open(startFile_s);
[1670]243    }
244
245    void GSLevel::unloadLevel()
246    {
[2087]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
[3008]253        delete startFile_s;
[1670]254    }
[1887]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
[2896]272        the key/button/axis that has been activated. This is configured above in activate().
[1887]273    */
274    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
275    {
[2896]276        if (GameMode::showsGraphics())
[1887]277        {
[2087]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)
[1887]284            {
[2087]285                if (bound)
286                {
287                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
[3327]288                    InputManager::getInstance().enterState("detector");
[2087]289                    bindingString = command;
290                    bTemporarySaved = bTemporary;
291                    bound = false;
292                }
293                //else:  We're still in a keybind command. ignore this call.
[1887]294            }
[2087]295            else
[1887]296            {
[2087]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);
[3327]303                    InputManager::getInstance().leaveState("detector");
[2087]304                    bound = true;
305                }
306                // else: A key was pressed within the same tick, ignore it.
[1887]307            }
308        }
309    }
[1661]310}
Note: See TracBrowser for help on using the repository browser.