Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_HS17/src/orxonox/gamestates/GSLevel.cc @ 11629

Last change on this file since 11629 was 11583, checked in by kohlia, 7 years ago

Near object, near point and at area work!

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