Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/gamestates/GSLevel.cc @ 5763

Last change on this file since 5763 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

  • Property svn:eol-style set to native
File size: 10.0 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
[5695]32#include <OgreCompositorManager.h>
33
[1661]34#include "core/input/InputManager.h"
[3327]35#include "core/input/InputState.h"
[1661]36#include "core/input/KeyBinder.h"
[3196]37#include "core/Clock.h"
[1887]38#include "core/ConsoleCommand.h"
39#include "core/ConfigValueIncludes.h"
40#include "core/CoreIncludes.h"
[2896]41#include "core/Game.h"
42#include "core/GameMode.h"
[3196]43#include "core/Core.h"
[3370]44#include "core/GraphicsManager.h"
45#include "core/GUIManager.h"
[3196]46#include "core/Loader.h"
47#include "core/XMLFile.h"
48
[5693]49#include "tools/interfaces/Tickable.h"
[5735]50#include "Radar.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
[5693]112        this->scope_GSLevel_ = new Scope<ScopeID::GSLevel>();
[2911]113
[2896]114        if (GameMode::isMaster())
[2087]115        {
116            this->loadLevel();
117        }
[1755]118
[2896]119        if (GameMode::showsGraphics())
[2087]120        {
121            // keybind console command
122            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
123            functor1->setObject(this);
[2662]124            ccKeybind_ = createConsoleCommand(functor1, "keybind");
125            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
[2087]126            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
127            functor2->setObject(this);
[2662]128            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
129            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
[2087]130            // set our console command as callback for the key detector
131            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
132
133            // level is loaded: we can start capturing the input
[3327]134            InputManager::getInstance().enterState("game");
[2087]135        }
[2662]136    }
[2087]137
[2896]138    void GSLevel::showIngameGUI(bool show)
[2662]139    {
[2896]140        if (show)
141        {
[3196]142            GUIManager::getInstance().showGUI("inGameTest");
143            GUIManager::getInstance().executeCode("showCursor()");
[3327]144            InputManager::getInstance().enterState("guiMouseOnly");
[2896]145        }
146        else
147        {
[3196]148            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
149            GUIManager::getInstance().executeCode("hideCursor()");
[3327]150            InputManager::getInstance().leaveState("guiMouseOnly");
[2896]151        }
152    }
153
154    void GSLevel::deactivate()
155    {
[2928]156/*
[2662]157        // destroy console commands
158        if (this->ccKeybind_)
[2087]159        {
[2662]160            delete this->ccKeybind_;
161            this->ccKeybind_ = 0;
[2087]162        }
[2662]163        if (this->ccTkeybind_)
164        {
165            delete this->ccTkeybind_;
166            this->ccTkeybind_ = 0;
167        }
[2928]168*/
[1661]169
[5695]170        if (GameMode::showsGraphics())
171        {
172            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
173            Ogre::CompositorManager::getSingleton().removeAll();
174        }
[2896]175
[1662]176        // this call will delete every BaseObject!
177        // But currently this will call methods of objects that exist no more
178        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
179        // and call a sceneNode method that has already been destroy by the corresponding space ship.
180        //Loader::close();
181
[2896]182        if (GameMode::showsGraphics())
[3327]183            InputManager::getInstance().leaveState("game");
[1662]184
[2896]185        if (GameMode::isMaster())
[2087]186            this->unloadLevel();
[1662]187
[2087]188        if (this->radar_)
[2662]189        {
[2087]190            delete this->radar_;
[2662]191            this->radar_ = 0;
192        }
[2087]193
194        if (this->cameraManager_)
[2662]195        {
[2087]196            delete this->cameraManager_;
[2662]197            this->cameraManager_ = 0;
198        }
[2087]199
[2662]200        if (this->playerManager_)
201        {
202            delete this->playerManager_;
203            this->playerManager_ = 0;
204        }
205
[5693]206        if (this->scope_GSLevel_)
[2911]207        {
[5693]208            delete this->scope_GSLevel_;
209            this->scope_GSLevel_ = NULL;
[2911]210        }
211
[2896]212        if (GameMode::showsGraphics())
[2087]213        {
[2896]214            gameInputState_->setHandler(0);
215            guiMouseOnlyInputState_->setHandler(0);
216            guiKeysOnlyInputState_->setHandler(0);
[3327]217            InputManager::getInstance().destroyState("game");
[2087]218            if (this->keyBinder_)
[2662]219            {
[2087]220                delete this->keyBinder_;
[2662]221                this->keyBinder_ = 0;
222            }
[2087]223        }
[1661]224    }
225
[2896]226    void GSLevel::update(const Clock& time)
[1661]227    {
[2896]228        // Note: Temporarily moved to GSGraphics.
[2087]229        //// Call the scene objects
230        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
231        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]232    }
233
[1670]234    void GSLevel::loadLevel()
235    {
236        // call the loader
237        COUT(0) << "Loading level..." << std::endl;
[5695]238        startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel());
[3008]239        Loader::open(startFile_s);
[1670]240    }
241
242    void GSLevel::unloadLevel()
243    {
[2087]244        //////////////////////////////////////////////////////////////////////////////////////////
245        // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO //
246        //////////////////////////////////////////////////////////////////////////////////////////
247        // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY /
248        //////////////////////////////////////////////////////////////////////////////////////////
249
[3008]250        delete startFile_s;
[1670]251    }
[1887]252
253    void GSLevel::keybind(const std::string &command)
254    {
255        this->keybindInternal(command, false);
256    }
257
258    void GSLevel::tkeybind(const std::string &command)
259    {
260        this->keybindInternal(command, true);
261    }
262
263    /**
264    @brief
265        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
266    @param command
267        Command string that can be executed by the CommandExecutor
268        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
[2896]269        the key/button/axis that has been activated. This is configured above in activate().
[1887]270    */
271    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
272    {
[2896]273        if (GameMode::showsGraphics())
[1887]274        {
[2087]275            static std::string bindingString = "";
276            static bool bTemporarySaved = false;
277            static bool bound = true;
278            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
279            // Howerver there will be no real issue if it happens anyway.
280            if (command.find(keyDetectorCallbackCode_) != 0)
[1887]281            {
[2087]282                if (bound)
283                {
284                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
[3327]285                    InputManager::getInstance().enterState("detector");
[2087]286                    bindingString = command;
287                    bTemporarySaved = bTemporary;
288                    bound = false;
289                }
290                //else:  We're still in a keybind command. ignore this call.
[1887]291            }
[2087]292            else
[1887]293            {
[2087]294                if (!bound)
295                {
296                    // user has pressed the key
297                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
298                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
299                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
[3327]300                    InputManager::getInstance().leaveState("detector");
[2087]301                    bound = true;
302                }
303                // else: A key was pressed within the same tick, ignore it.
[1887]304            }
305        }
306    }
[1661]307}
Note: See TracBrowser for help on using the repository browser.