Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/towerdefenseFS15/src/orxonox/gamestates/GSLevel.cc @ 10615

Last change on this file since 10615 was 10281, checked in by landauf, 10 years ago

added command 'reloadLevel' (by default on F5) which reloads the level while the player's camera remains at the same position

  • 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        orxout(internal_info) << i << " objects remaining.";
193        if (i == 0)
194            orxout(internal_info) << " Well done!" << endl;
195        else
196            orxout(internal_info) << " Try harder!" << endl;
197    }
198
199    void GSLevel::reloadLevel()
200    {
201        // export all states
202        std::vector<GSLevelMementoState*> states;
203        for (ObjectList<GSLevelMemento>::iterator it = ObjectList<GSLevelMemento>::begin(); it != ObjectList<GSLevelMemento>::end(); ++it)
204        {
205            GSLevelMementoState* state = it->exportMementoState();
206            if (state)
207                states.push_back(state);
208        }
209
210        // reload level (or better: reload the whole gamestate)
211        this->deactivate();
212        this->activate();
213
214        // import all states
215        for (ObjectList<GSLevelMemento>::iterator it = ObjectList<GSLevelMemento>::begin(); it != ObjectList<GSLevelMemento>::end(); ++it)
216            it->importMementoState(states);
217
218        // delete states
219        for (size_t i = 0; i < states.size(); ++i)
220            delete states[i];
221    }
222
223    /**
224    @brief
225        Starts the MainMenu.
226    */
227    /*static*/ void GSLevel::startMainMenu(void)
228    {
229        // HACK
230        Game::getInstance().popState();
231        Game::getInstance().popState();
232    }
233
234    /**
235    @brief
236        Terminates the current game and starts a new game.
237    @param level
238        The filename of the level to be started.
239    */
240    /*static*/ void GSLevel::changeGame(const std::string& level)
241    {
242        if(level != "")
243            LevelManager::getInstance().setDefaultLevel(level);
244
245        // HACK
246        Game::getInstance().popState();
247        Game::getInstance().popState();
248        Game::getInstance().requestStates("standalone, level");
249    }
250
251
252
253    ///////////////////////////////////////////////////////////////////////////
254
255    RegisterAbstractClass(GSLevelMemento).inheritsFrom(Class(OrxonoxInterface));
256
257    GSLevelMemento::GSLevelMemento()
258    {
259        RegisterObject(GSLevelMemento);
260    }
261}
Note: See TracBrowser for help on using the repository browser.