Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/orxonox/gamestates/GSLevel.cc @ 10566

Last change on this file since 10566 was 10566, checked in by landauf, 5 years ago

moved destruction of all level objects from Client to GSLevel. (there's still a bug with unloading in client mode which was introduced in r10563)

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