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
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
53namespace orxonox
54{
55    DeclareGameState(GSLevel, "level", false, false);
56
57    static const std::string __CC_startMainMenu_name = "startMainMenu";
58    static const std::string __CC_changeGame_name = "changeGame";
59    static const std::string __CC_reloadLevel_name = "reloadLevel";
60
61    SetConsoleCommand(__CC_startMainMenu_name, &GSLevel::startMainMenu).deactivate();
62    SetConsoleCommand(__CC_changeGame_name, &GSLevel::changeGame).defaultValues("").deactivate();
63    SetConsoleCommand(__CC_reloadLevel_name, &GSLevel::reloadLevel).deactivate();
64
65    GSLevel::GSLevel(const GameStateInfo& info)
66        : GameState(info)
67        , gameInputState_(nullptr)
68        , guiMouseOnlyInputState_(nullptr)
69        , guiKeysOnlyInputState_(nullptr)
70        , startFile_(nullptr)
71        , bShowIngameGUI_(false)
72    {
73    }
74
75    GSLevel::~GSLevel()
76    {
77    }
78
79    void GSLevel::activate()
80    {
81        orxout(user_status) << "Loading level" << endl;
82
83        if (GameMode::showsGraphics())
84        {
85            gameInputState_ = InputManager::getInstance().createInputState("game");
86            gameInputState_->setMouseExclusive(true);
87            gameInputState_->setHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
88            KeyBinderManager::getInstance().setToDefault();
89
90            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
91            guiMouseOnlyInputState_->setMouseExclusive(true);
92            guiMouseOnlyInputState_->setMouseHandler(&GUIManager::getInstance());
93
94            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
95            guiKeysOnlyInputState_->setKeyHandler(&GUIManager::getInstance());
96        }
97
98        this->prepareObjectTracking();
99
100        if (GameMode::isMaster())
101        {
102            orxout(user_info) << "Load level" << std::endl;
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        orxout(user_info) << "All loaded" << std::endl;
117
118        if (GameMode::isStandalone())
119        {
120            ModifyConsoleCommand(__CC_changeGame_name).activate();
121            ModifyConsoleCommand(__CC_reloadLevel_name).setObject(this).activate();
122        }
123    }
124
125    void GSLevel::deactivate()
126    {
127        if (GameMode::showsGraphics())
128            InputManager::getInstance().leaveState("game");
129
130        // disconnect all HumanPlayers
131        PlayerManager::getInstance().disconnectAllClients();
132
133        if (GameMode::isMaster())
134            this->unloadLevel();
135        else
136            this->unloadLevelAsClient();
137
138        this->performObjectTracking();
139
140        if (GameMode::showsGraphics())
141        {
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
147            gameInputState_->setHandler(nullptr);
148            guiMouseOnlyInputState_->setHandler(nullptr);
149            guiKeysOnlyInputState_->setHandler(nullptr);
150            InputManager::getInstance().destroyState("game");
151            InputManager::getInstance().destroyState("guiKeysOnly");
152            InputManager::getInstance().destroyState("guiMouseOnly");
153
154            ModifyConsoleCommand(__CC_startMainMenu_name  ).deactivate();
155        }
156
157        if (GameMode::isStandalone())
158        {
159            ModifyConsoleCommand(__CC_changeGame_name).deactivate();
160            ModifyConsoleCommand(__CC_reloadLevel_name).setObject(nullptr).deactivate();
161        }
162    }
163
164    void GSLevel::update(const Clock& time)
165    {
166        // Note: Temporarily moved to GSRoot.
167        //// Call the scene objects
168        //for (Tickable* tickable : ObjectList<Tickable>())
169        //    tickable->tick(time.getDeltaTime() * this->timeFactor_);
170    }
171
172    void GSLevel::prepareObjectTracking()
173    {
174        for (BaseObject* baseObject : ObjectList<BaseObject>())
175            this->staticObjects_.insert(baseObject);
176    }
177
178    void GSLevel::performObjectTracking()
179    {
180        orxout(internal_info) << "Remaining objects:" << endl;
181        unsigned int i = 0;
182        for (BaseObject* baseObject : ObjectList<BaseObject>())
183        {
184            std::set<BaseObject*>::const_iterator find = this->staticObjects_.find(baseObject);
185            if (find == this->staticObjects_.end())
186            {
187                orxout(internal_warning) << ++i << ": " << baseObject->getIdentifier()->getName() << " (" << baseObject << "), references: " << baseObject->getReferenceCount() << endl;
188            }
189        }
190        if (i == 0)
191            orxout(internal_info) << i << " objects remaining. Well done!" << endl;
192        else
193            orxout(internal_warning) << i << " objects remaining. Try harder!" << endl;
194    }
195
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    {
219        ObjectList<Level> listLevel;
220        for (ObjectList<Level>::iterator it = listLevel.begin(); it != listLevel.end(); )
221        {
222            StrongPtr<Level> level = *(it++); // StrongPtr prevents that the Level gets destroyed while we loop over it
223            ObjectList<BaseObject> listBaseObject(level);
224            for (ObjectList<BaseObject>::iterator it = listBaseObject.begin(); it != listBaseObject.end(); )
225                (it++)->destroy();
226        }
227
228        ObjectList<Synchronisable> listSynchronisable;
229        for (ObjectList<Synchronisable>::iterator it = listSynchronisable.begin(); it != listSynchronisable.end(); )
230        {
231            if (it->getSyncMode() != 0x0)
232                (it++)->destroy();
233            else
234                ++it;
235        }
236    }
237
238    void GSLevel::reloadLevel()
239    {
240        // export all states
241        std::vector<GSLevelMementoState*> states;
242        for (GSLevelMemento* memento : ObjectList<GSLevelMemento>())
243        {
244            GSLevelMementoState* state = memento->exportMementoState();
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
254        for (GSLevelMemento* memento : ObjectList<GSLevelMemento>())
255            memento->importMementoState(states);
256
257        // delete states
258        for (GSLevelMementoState* state : states)
259            delete state;
260    }
261
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    {
281        if(level != "")
282            LevelManager::getInstance().setDefaultLevel(level);
283
284        // HACK
285        Game::getInstance().popState();
286        Game::getInstance().popState();
287        Game::getInstance().requestStates("standalone, level");
288    }
289
290
291
292    ///////////////////////////////////////////////////////////////////////////
293
294    RegisterAbstractClass(GSLevelMemento).inheritsFrom<OrxonoxInterface>();
295
296    GSLevelMemento::GSLevelMemento()
297    {
298        RegisterObject(GSLevelMemento);
299    }
300}
Note: See TracBrowser for help on using the repository browser.