Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/HUD_HS16/src/orxonox/gamestates/GSLevel.cc @ 11436

Last change on this file since 11436 was 11336, checked in by patricwi, 8 years ago

finished version of HUD and PickupSystem. crash bug remaining

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