Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/formationFS15/src/orxonox/gamestates/GSLevel.cc @ 10487

Last change on this file since 10487 was 10299, checked in by landauf, 10 years ago

fixed crash in SimpleRocket (during level-unloading).
improved debug output in GSLevel

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