Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10295 was 10281, checked in by landauf, 10 years ago

added command 'reloadLevel' (by default on F5) which reloads the level while the player's camera remains at the same position

  • Property svn:eol-style set to native
File size: 8.4 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"
[10281]31#include "GSLevelMemento.h"
[1661]32
[5695]33#include <OgreCompositorManager.h>
34
[5855]35#include "util/Clock.h"
[1661]36#include "core/input/InputManager.h"
[3327]37#include "core/input/InputState.h"
[5863]38#include "core/input/KeyBinderManager.h"
[7870]39#include "core/Core.h"
[10281]40#include "core/CoreIncludes.h"
[2896]41#include "core/Game.h"
42#include "core/GameMode.h"
[3370]43#include "core/GUIManager.h"
[3196]44#include "core/Loader.h"
45#include "core/XMLFile.h"
[7284]46#include "core/command/ConsoleCommand.h"
[3196]47
[2087]48#include "LevelManager.h"
[2662]49#include "PlayerManager.h"
[8079]50#include "GSRoot.h"
[1661]51
52namespace orxonox
53{
[3370]54    DeclareGameState(GSLevel, "level", false, false);
[1934]55
[7876]56    static const std::string __CC_startMainMenu_name = "startMainMenu";
57    static const std::string __CC_changeGame_name = "changeGame";
[10281]58    static const std::string __CC_reloadLevel_name = "reloadLevel";
[7876]59
60    SetConsoleCommand(__CC_startMainMenu_name, &GSLevel::startMainMenu).deactivate();
[8079]61    SetConsoleCommand(__CC_changeGame_name, &GSLevel::changeGame).defaultValues("").deactivate();
[10281]62    SetConsoleCommand(__CC_reloadLevel_name, &GSLevel::reloadLevel).deactivate();
[7876]63
[3370]64    GSLevel::GSLevel(const GameStateInfo& info)
65        : GameState(info)
[2896]66        , gameInputState_(0)
67        , guiMouseOnlyInputState_(0)
68        , guiKeysOnlyInputState_(0)
[5876]69        , startFile_(0)
[6417]70        , bShowIngameGUI_(false)
[1661]71    {
72    }
73
74    GSLevel::~GSLevel()
75    {
76    }
77
[2896]78    void GSLevel::activate()
[1661]79    {
[8858]80        orxout(user_status) << "Loading level" << endl;
81
[2896]82        if (GameMode::showsGraphics())
[2087]83        {
[3327]84            gameInputState_ = InputManager::getInstance().createInputState("game");
[8729]85            gameInputState_->setMouseExclusive(true);
[5863]86            gameInputState_->setHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
87            KeyBinderManager::getInstance().setToDefault();
[1661]88
[3327]89            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
[8729]90            guiMouseOnlyInputState_->setMouseExclusive(true);
[6746]91            guiMouseOnlyInputState_->setMouseHandler(&GUIManager::getInstance());
[2896]92
[3327]93            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
[6746]94            guiKeysOnlyInputState_->setKeyHandler(&GUIManager::getInstance());
[2087]95        }
[1662]96
[2896]97        if (GameMode::isMaster())
[2087]98        {
99            this->loadLevel();
100        }
[1755]101
[2896]102        if (GameMode::showsGraphics())
[2087]103        {
104            // level is loaded: we can start capturing the input
[3327]105            InputManager::getInstance().enterState("game");
[6417]106
[5820]107            // connect the HumanPlayer to the game
[5850]108            PlayerManager::getInstance().clientConnected(0);
[7876]109
110            ModifyConsoleCommand(__CC_startMainMenu_name).activate();
[2087]111        }
[7876]112
113        if (GameMode::isStandalone())
[10281]114        {
[7876]115            ModifyConsoleCommand(__CC_changeGame_name).activate();
[10281]116            ModifyConsoleCommand(__CC_reloadLevel_name).setObject(this).activate();
117        }
[2662]118    }
[2087]119
[2896]120    void GSLevel::deactivate()
121    {
[5695]122        if (GameMode::showsGraphics())
[3327]123            InputManager::getInstance().leaveState("game");
[6417]124
[5966]125        // disconnect all HumanPlayers
126        PlayerManager::getInstance().disconnectAllClients();
[1662]127
[2896]128        if (GameMode::isMaster())
[2087]129            this->unloadLevel();
[1662]130
[2896]131        if (GameMode::showsGraphics())
[2087]132        {
[7879]133#if OGRE_VERSION < 0x010700
134            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
135            Ogre::CompositorManager::getSingleton().removeAll();
136#endif
137
[2896]138            gameInputState_->setHandler(0);
139            guiMouseOnlyInputState_->setHandler(0);
140            guiKeysOnlyInputState_->setHandler(0);
[3327]141            InputManager::getInstance().destroyState("game");
[5818]142            InputManager::getInstance().destroyState("guiKeysOnly");
143            InputManager::getInstance().destroyState("guiMouseOnly");
[7876]144
145            ModifyConsoleCommand(__CC_startMainMenu_name  ).deactivate();
[2087]146        }
[7876]147
148        if (GameMode::isStandalone())
[10281]149        {
[7876]150            ModifyConsoleCommand(__CC_changeGame_name).deactivate();
[10281]151            ModifyConsoleCommand(__CC_reloadLevel_name).setObject(NULL).deactivate();
152        }
[1661]153    }
154
[2896]155    void GSLevel::update(const Clock& time)
[1661]156    {
[5876]157        // Note: Temporarily moved to GSRoot.
[2087]158        //// Call the scene objects
159        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
160        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]161    }
162
[1670]163    void GSLevel::loadLevel()
164    {
[5922]165        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ++it)
166            this->staticObjects_.insert(*it);
[6417]167
[1670]168        // call the loader
[5876]169        startFile_ = new XMLFile(LevelManager::getInstance().getDefaultLevel());
[8079]170        bool loaded = Loader::open(startFile_);
[7870]171
172        Core::getInstance().updateLastLevelTimestamp();
[8079]173        if(!loaded)
174            GSRoot::delayedStartMainMenu();
[1670]175    }
176
177    void GSLevel::unloadLevel()
178    {
[5876]179        Loader::unload(startFile_);
180        delete startFile_;
[5922]181
[8858]182        orxout(internal_info) << "Remaining objects:" << endl;
[5922]183        unsigned int i = 0;
184        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ++it)
185        {
186            std::set<BaseObject*>::const_iterator find = this->staticObjects_.find(*it);
187            if (find == this->staticObjects_.end())
188            {
[8858]189                orxout(internal_info) << ++i << ": " << it->getIdentifier()->getName() << " (" << *it << "), references: " << it->getReferenceCount() << endl;
[5922]190            }
191        }
[8858]192        orxout(internal_info) << i << " objects remaining.";
[5922]193        if (i == 0)
[8858]194            orxout(internal_info) << " Well done!" << endl;
[5922]195        else
[8858]196            orxout(internal_info) << " Try harder!" << endl;
[1670]197    }
[7876]198
[10281]199    void GSLevel::reloadLevel()
200    {
201        // export all states
202        std::vector<GSLevelMementoState*> states;
203        for (ObjectList<GSLevelMemento>::iterator it = ObjectList<GSLevelMemento>::begin(); it != ObjectList<GSLevelMemento>::end(); ++it)
204        {
205            GSLevelMementoState* state = it->exportMementoState();
206            if (state)
207                states.push_back(state);
208        }
209
210        // reload level (or better: reload the whole gamestate)
211        this->deactivate();
212        this->activate();
213
214        // import all states
215        for (ObjectList<GSLevelMemento>::iterator it = ObjectList<GSLevelMemento>::begin(); it != ObjectList<GSLevelMemento>::end(); ++it)
216            it->importMementoState(states);
217
218        // delete states
219        for (size_t i = 0; i < states.size(); ++i)
220            delete states[i];
221    }
222
[7876]223    /**
224    @brief
225        Starts the MainMenu.
226    */
227    /*static*/ void GSLevel::startMainMenu(void)
228    {
229        // HACK
230        Game::getInstance().popState();
231        Game::getInstance().popState();
232    }
233
234    /**
235    @brief
236        Terminates the current game and starts a new game.
237    @param level
238        The filename of the level to be started.
239    */
240    /*static*/ void GSLevel::changeGame(const std::string& level)
241    {
[8079]242        if(level != "")
[7876]243            LevelManager::getInstance().setDefaultLevel(level);
244
245        // HACK
246        Game::getInstance().popState();
247        Game::getInstance().popState();
248        Game::getInstance().requestStates("standalone, level");
249    }
[10281]250
251
252
253    ///////////////////////////////////////////////////////////////////////////
254
255    RegisterAbstractClass(GSLevelMemento).inheritsFrom(Class(OrxonoxInterface));
256
257    GSLevelMemento::GSLevelMemento()
258    {
259        RegisterObject(GSLevelMemento);
260    }
[1661]261}
Note: See TracBrowser for help on using the repository browser.