Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/Presentation_HS16/src/orxonox/gamestates/GSLevel.cc @ 11642

Last change on this file since 11642 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
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 "Level.h"
50#include "PlayerManager.h"
51#include "GSRoot.h"
52// #include "overlays/hud/HUDPickupSystem.h"
53
54namespace orxonox
55{
56    DeclareGameState(GSLevel, "level", false, false);
57
58    static const std::string __CC_startMainMenu_name = "startMainMenu";
59    static const std::string __CC_changeGame_name = "changeGame";
60    static const std::string __CC_reloadLevel_name = "reloadLevel";
61
62    SetConsoleCommand(__CC_startMainMenu_name, &GSLevel::startMainMenu).deactivate();
63    SetConsoleCommand(__CC_changeGame_name, &GSLevel::changeGame).defaultValues("").deactivate();
64    SetConsoleCommand(__CC_reloadLevel_name, &GSLevel::reloadLevel).deactivate();
65
66    GSLevel::GSLevel(const GameStateInfo& info)
67        : GameState(info)
68        , gameInputState_(nullptr)
69        , guiMouseOnlyInputState_(nullptr)
70        , guiKeysOnlyInputState_(nullptr)
71        , startFile_(nullptr)
72        , bShowIngameGUI_(false)
73    {
74    }
75
76    GSLevel::~GSLevel()
77    {
78    }
79
80    void GSLevel::activate()
81    {
82        orxout(user_status) << "Loading level" << endl;
83
84        if (GameMode::showsGraphics())
85        {
86            gameInputState_ = InputManager::getInstance().createInputState("game");
87            gameInputState_->setMouseExclusive(true);
88            gameInputState_->setHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
89            KeyBinderManager::getInstance().setToDefault();
90
91            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
92            guiMouseOnlyInputState_->setMouseExclusive(true);
93            guiMouseOnlyInputState_->setMouseHandler(&GUIManager::getInstance());
94
95            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
96            guiKeysOnlyInputState_->setKeyHandler(&GUIManager::getInstance());
97        }
98
99        this->prepareObjectTracking();
100
101        if (GameMode::isMaster())
102        {
103            this->loadLevel();
104        }
105
106        if (GameMode::showsGraphics())
107        {
108            // level is loaded: we can start capturing the input
109            InputManager::getInstance().enterState("game");
110
111            // connect the HumanPlayer to the game
112            PlayerManager::getInstance().clientConnected(0);
113
114            ModifyConsoleCommand(__CC_startMainMenu_name).activate();
115        }
116
117        if (GameMode::isStandalone())
118        {
119            ModifyConsoleCommand(__CC_changeGame_name).activate();
120            ModifyConsoleCommand(__CC_reloadLevel_name).setObject(this).activate();
121        }
122    }
123
124    void GSLevel::deactivate()
125    {
126        if (GameMode::showsGraphics())
127            InputManager::getInstance().leaveState("game");
128
129        // HUDPickupSystem* pickupSystem;
130
131        // for (HUDPickupSystem* hud : ObjectList<HUDPickupSystem>())
132        //     pickupSystem = hud;
133
134        // pickupSystem->repaint=false;
135
136        // disconnect all HumanPlayers
137        PlayerManager::getInstance().disconnectAllClients();
138
139        if (GameMode::isMaster())
140            this->unloadLevel();
141        else
142            this->unloadLevelAsClient();
143
144        this->performObjectTracking();
145
146        if (GameMode::showsGraphics())
147        {
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
153            gameInputState_->setHandler(nullptr);
154            guiMouseOnlyInputState_->setHandler(nullptr);
155            guiKeysOnlyInputState_->setHandler(nullptr);
156            InputManager::getInstance().destroyState("game");
157            InputManager::getInstance().destroyState("guiKeysOnly");
158            InputManager::getInstance().destroyState("guiMouseOnly");
159
160            ModifyConsoleCommand(__CC_startMainMenu_name  ).deactivate();
161        }
162
163        if (GameMode::isStandalone())
164        {
165            ModifyConsoleCommand(__CC_changeGame_name).deactivate();
166            ModifyConsoleCommand(__CC_reloadLevel_name).setObject(nullptr).deactivate();
167        }
168    }
169
170    void GSLevel::update(const Clock& time)
171    {
172        // Note: Temporarily moved to GSRoot.
173        //// Call the scene objects
174        //for (Tickable* tickable : ObjectList<Tickable>())
175        //    tickable->tick(time.getDeltaTime() * this->timeFactor_);
176    }
177
178    void GSLevel::prepareObjectTracking()
179    {
180        for (BaseObject* baseObject : ObjectList<BaseObject>())
181            this->staticObjects_.insert(baseObject);
182    }
183
184    void GSLevel::performObjectTracking()
185    {
186        orxout(internal_info) << "Remaining objects:" << endl;
187        unsigned int i = 0;
188        for (BaseObject* baseObject : ObjectList<BaseObject>())
189        {
190            std::set<BaseObject*>::const_iterator find = this->staticObjects_.find(baseObject);
191            if (find == this->staticObjects_.end())
192            {
193                orxout(internal_warning) << ++i << ": " << baseObject->getIdentifier()->getName() << " (" << baseObject << "), references: " << baseObject->getReferenceCount() << endl;
194            }
195        }
196        if (i == 0)
197            orxout(internal_info) << i << " objects remaining. Well done!" << endl;
198        else
199            orxout(internal_warning) << i << " objects remaining. Try harder!" << endl;
200    }
201
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    {
225        ObjectList<Level> listLevel;
226        for (ObjectList<Level>::iterator it = listLevel.begin(); it != listLevel.end(); )
227        {
228            StrongPtr<Level> level = *(it++); // StrongPtr prevents that the Level gets destroyed while we loop over it
229            ObjectList<BaseObject> listBaseObject(level);
230            for (ObjectList<BaseObject>::iterator it = listBaseObject.begin(); it != listBaseObject.end(); )
231                (it++)->destroy();
232        }
233
234        ObjectList<Synchronisable> listSynchronisable;
235        for (ObjectList<Synchronisable>::iterator it = listSynchronisable.begin(); it != listSynchronisable.end(); )
236        {
237            if (it->getSyncMode() != 0x0)
238                (it++)->destroy();
239            else
240                ++it;
241        }
242    }
243
244    void GSLevel::reloadLevel()
245    {
246        // export all states
247        std::vector<GSLevelMementoState*> states;
248        for (GSLevelMemento* memento : ObjectList<GSLevelMemento>())
249        {
250            GSLevelMementoState* state = memento->exportMementoState();
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
260        for (GSLevelMemento* memento : ObjectList<GSLevelMemento>())
261            memento->importMementoState(states);
262
263        // delete states
264        for (GSLevelMementoState* state : states)
265            delete state;
266    }
267
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    {
287        if(level != "")
288            LevelManager::getInstance().setDefaultLevel(level);
289
290        // HACK
291        Game::getInstance().popState();
292        Game::getInstance().popState();
293        Game::getInstance().requestStates("standalone, level");
294    }
295
296
297
298    ///////////////////////////////////////////////////////////////////////////
299
300    RegisterAbstractClass(GSLevelMemento).inheritsFrom<OrxonoxInterface>();
301
302    GSLevelMemento::GSLevelMemento()
303    {
304        RegisterObject(GSLevelMemento);
305    }
306}
Note: See TracBrowser for help on using the repository browser.