Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12343 was 11355, checked in by patricwi, 8 years ago

deleted comments

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