Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSLevel.cc @ 6350

Last change on this file since 6350 was 5966, checked in by scheusso, 15 years ago

(kind of) fixed HumanPlayer bodys beeing around from clients that have not yet disconnected when unloading level

  • Property svn:eol-style set to native
File size: 6.0 KB
RevLine 
[1661]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:
[1662]25 *      Fabian 'x3n' Landau
[2896]26 *      Benjamin Knecht
[1661]27 *
28 */
29
30#include "GSLevel.h"
31
[5695]32#include <OgreCompositorManager.h>
33
[5855]34#include "util/Clock.h"
[1661]35#include "core/input/InputManager.h"
[3327]36#include "core/input/InputState.h"
[5863]37#include "core/input/KeyBinderManager.h"
[1887]38#include "core/ConsoleCommand.h"
[2896]39#include "core/Game.h"
40#include "core/GameMode.h"
[3370]41#include "core/GUIManager.h"
[3196]42#include "core/Loader.h"
43#include "core/XMLFile.h"
44
[2087]45#include "LevelManager.h"
[2662]46#include "PlayerManager.h"
[1661]47
48namespace orxonox
49{
[3370]50    DeclareGameState(GSLevel, "level", false, false);
[1934]51
[3370]52    GSLevel::GSLevel(const GameStateInfo& info)
53        : GameState(info)
[2896]54        , gameInputState_(0)
55        , guiMouseOnlyInputState_(0)
56        , guiKeysOnlyInputState_(0)
[5876]57        , startFile_(0)
[1661]58    {
59    }
60
61    GSLevel::~GSLevel()
62    {
63    }
64
[2896]65    void GSLevel::activate()
[1661]66    {
[2896]67        if (GameMode::showsGraphics())
[2087]68        {
[3327]69            gameInputState_ = InputManager::getInstance().createInputState("game");
[5910]70            gameInputState_->setMouseMode(MouseMode::Exclusive);
[5863]71            gameInputState_->setHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
72            KeyBinderManager::getInstance().setToDefault();
[1661]73
[3327]74            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly");
[5910]75            guiMouseOnlyInputState_->setMouseMode(MouseMode::Exclusive);
[2896]76            guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr());
77
[3327]78            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly");
[2896]79            guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr());
[5876]80
81            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&GSLevel::showIngameGUI, this), "showIngameGUI"));
[2087]82        }
[1662]83
[2896]84        if (GameMode::isMaster())
[2087]85        {
86            this->loadLevel();
87        }
[1755]88
[2896]89        if (GameMode::showsGraphics())
[2087]90        {
91            // level is loaded: we can start capturing the input
[3327]92            InputManager::getInstance().enterState("game");
[5820]93           
94            // connect the HumanPlayer to the game
[5850]95            PlayerManager::getInstance().clientConnected(0);
[2087]96        }
[2662]97    }
[2087]98
[2896]99    void GSLevel::showIngameGUI(bool show)
[2662]100    {
[2896]101        if (show)
102        {
[3196]103            GUIManager::getInstance().showGUI("inGameTest");
104            GUIManager::getInstance().executeCode("showCursor()");
[3327]105            InputManager::getInstance().enterState("guiMouseOnly");
[2896]106        }
107        else
108        {
[3196]109            GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")");
110            GUIManager::getInstance().executeCode("hideCursor()");
[3327]111            InputManager::getInstance().leaveState("guiMouseOnly");
[2896]112        }
113    }
114
115    void GSLevel::deactivate()
116    {
[5695]117        if (GameMode::showsGraphics())
118        {
119            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
120            Ogre::CompositorManager::getSingleton().removeAll();
[2896]121
[3327]122            InputManager::getInstance().leaveState("game");
[5820]123        }
[5966]124       
125        // disconnect all HumanPlayers
126        PlayerManager::getInstance().disconnectAllClients();
[1662]127
[2896]128        if (GameMode::isMaster())
[2087]129            this->unloadLevel();
[1662]130
[2896]131        if (GameMode::showsGraphics())
[2087]132        {
[2896]133            gameInputState_->setHandler(0);
134            guiMouseOnlyInputState_->setHandler(0);
135            guiKeysOnlyInputState_->setHandler(0);
[3327]136            InputManager::getInstance().destroyState("game");
[5818]137            InputManager::getInstance().destroyState("guiKeysOnly");
138            InputManager::getInstance().destroyState("guiMouseOnly");
[2087]139        }
[1661]140    }
141
[2896]142    void GSLevel::update(const Clock& time)
[1661]143    {
[5876]144        // Note: Temporarily moved to GSRoot.
[2087]145        //// Call the scene objects
146        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
147        //    it->tick(time.getDeltaTime() * this->timeFactor_);
[1661]148    }
149
[1670]150    void GSLevel::loadLevel()
151    {
[5922]152        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ++it)
153            this->staticObjects_.insert(*it);
154       
[1670]155        // call the loader
156        COUT(0) << "Loading level..." << std::endl;
[5876]157        startFile_ = new XMLFile(LevelManager::getInstance().getDefaultLevel());
158        Loader::open(startFile_);
[1670]159    }
160
161    void GSLevel::unloadLevel()
162    {
[5876]163        Loader::unload(startFile_);
164        delete startFile_;
[5922]165
166        COUT(3) << "Unloaded level. Remaining objects:" << std::endl;
167        unsigned int i = 0;
168        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ++it)
169        {
170            std::set<BaseObject*>::const_iterator find = this->staticObjects_.find(*it);
171            if (find == this->staticObjects_.end())
172            {
173                COUT(3) << ++i << ": " << it->getIdentifier()->getName() << " (" << *it << ")" << std::endl;
174            }
175        }
176        COUT(3) << i << " objects remaining.";
177        if (i == 0)
178            COUT(3) << " Well done!" << std::endl;
179        else
180            COUT(3) << " Try harder!" << std::endl;
[1670]181    }
[1661]182}
Note: See TracBrowser for help on using the repository browser.