Index: code/trunk/src/orxonox/gamestates/GSLevel.cc
===================================================================
--- code/trunk/src/orxonox/gamestates/GSLevel.cc	(revision 1934)
+++ code/trunk/src/orxonox/gamestates/GSLevel.cc	(revision 2087)
@@ -30,10 +30,9 @@
 #include "GSLevel.h"
 
-#include <OgreSceneManager.h>
-#include <OgreRoot.h>
 #include "core/input/InputManager.h"
 #include "core/input/SimpleInputState.h"
 #include "core/input/KeyBinder.h"
 #include "core/Loader.h"
+#include "core/XMLFile.h"
 #include "core/CommandExecutor.h"
 #include "core/ConsoleCommand.h"
@@ -41,24 +40,26 @@
 #include "core/ConfigValueIncludes.h"
 #include "core/CoreIncludes.h"
-#include "objects/Backlight.h"
+#include "core/Core.h"
+//#include "objects/Backlight.h"
 #include "objects/Tickable.h"
 #include "objects/Radar.h"
-#include "tools/ParticleInterface.h"
+//#include "tools/ParticleInterface.h"
+#include "CameraManager.h"
+#include "LevelManager.h"
 #include "Settings.h"
-#include "GraphicsEngine.h"
 
 namespace orxonox
 {
-    SetCommandLineArgument(level, "sample.oxw").setShortcut("l");
-
-    GSLevel::GSLevel(const std::string& name)
-        : GameState<GSGraphics>(name)
-        , timeFactor_(1.0f)
-        , sceneManager_(0)
+    SetCommandLineArgument(level, "sample2.oxw").shortcut("l");
+
+    GSLevel::GSLevel()
+//        : GameState<GSGraphics>(name)
+        : timeFactor_(1.0f)
         , keyBinder_(0)
         , inputState_(0)
         , radar_(0)
-        , startLevel_(0)
-        , hud_(0)
+        , startFile_(0)
+        , cameraManager_(0)
+        , levelManager_(0)
     {
         RegisterObject(GSLevel);
@@ -75,48 +76,62 @@
     }
 
-    void GSLevel::enter()
-    {
-        inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20);
-        keyBinder_ = new KeyBinder();
-        keyBinder_->loadBindings("keybindings.ini");
-        inputState_->setHandler(keyBinder_);
-
-        // create Ogre SceneManager for the level
-        this->sceneManager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC, "LevelSceneManager");
-        COUT(4) << "Created SceneManager: " << sceneManager_->getName() << std::endl;
-
-        // temporary hack
-        GraphicsEngine::getInstance().setLevelSceneManager(this->sceneManager_);
-
-        // Start the Radar
-        this->radar_ = new Radar();
-
-        // Load the HUD
-        COUT(3) << "Orxonox: Loading HUD" << std::endl;
-        hud_ = new Level(Settings::getDataPath() + "overlay/hud.oxo");
-        Loader::load(hud_);
-
-        // reset game speed to normal
-        timeFactor_ = 1.0f;
-
-        // TODO: insert slomo console command with
-        // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
-
-        // keybind console command
-        FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
-        functor1->setObject(this);
-        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "keybind"));
-        FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
-        functor2->setObject(this);
-        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor2, "tkeybind"));
-        // set our console command as callback for the key detector
-        InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
+    void GSLevel::enter(Ogre::Viewport* viewport)
+    {
+        if (Core::showsGraphics())
+        {
+            inputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game", 20);
+            keyBinder_ = new KeyBinder();
+            keyBinder_->loadBindings("keybindings.ini");
+            inputState_->setHandler(keyBinder_);
+
+            // create the global CameraManager
+            assert(viewport);
+            this->cameraManager_ = new CameraManager(viewport);
+
+            // Start the Radar
+            this->radar_ = new Radar();
+        }
+
+        if (Core::isMaster())
+        {
+            // create the global LevelManager
+            this->levelManager_ = new LevelManager();
+
+            // reset game speed to normal
+            timeFactor_ = 1.0f;
+
+            this->loadLevel();
+        }
+
+        if (Core::showsGraphics())
+        {
+            // TODO: insert slomo console command with
+            // .accessLevel(AccessLevel::Offline).defaultValue(0, 1.0).axisParamIndex(0).isAxisRelative(false);
+
+            // keybind console command
+            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
+            functor1->setObject(this);
+            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "keybind"));
+            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
+            functor2->setObject(this);
+            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor2, "tkeybind"));
+            // set our console command as callback for the key detector
+            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
+
+            // level is loaded: we can start capturing the input
+            InputManager::getInstance().requestEnterState("game");
+        }
+
+        if (Core::isMaster())
+        {
+            // time factor console command
+            FunctorMember<GSLevel>* functor = createFunctor(&GSLevel::setTimeFactor);
+            functor->setObject(this);
+            CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor, "setTimeFactor")).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0);;
+        }
     }
 
     void GSLevel::leave()
     {
-        Loader::unload(hud_);
-        delete this->hud_;
-
         // this call will delete every BaseObject!
         // But currently this will call methods of objects that exist no more
@@ -125,18 +140,34 @@
         //Loader::close();
 
-        delete this->radar_;
-
-        Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
-
-        inputState_->setHandler(0);
-        InputManager::getInstance().requestDestroyState("game");
-        delete this->keyBinder_;
+        if (Core::showsGraphics())
+            InputManager::getInstance().requestLeaveState("game");
+
+        if (Core::isMaster())
+            this->unloadLevel();
+
+        if (this->radar_)
+            delete this->radar_;
+
+        if (this->cameraManager_)
+            delete this->cameraManager_;
+
+        if (this->levelManager_)
+            delete this->levelManager_;
+
+        if (Core::showsGraphics())
+        {
+            inputState_->setHandler(0);
+            InputManager::getInstance().requestDestroyState("game");
+            if (this->keyBinder_)
+                delete this->keyBinder_;
+        }
     }
 
     void GSLevel::ticked(const Clock& time)
     {
-        // Call the scene objects
-        for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
-            it->tick(time.getDeltaTime() * this->timeFactor_);
+        // Commented by 1337: Temporarily moved to GSGraphics.
+        //// Call the scene objects
+        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
+        //    it->tick(time.getDeltaTime() * this->timeFactor_);
     }
 
@@ -147,6 +178,9 @@
     void GSLevel::setTimeFactor(float factor)
     {
+/*
         float change = factor / this->timeFactor_;
+*/
         this->timeFactor_ = factor;
+/*
         for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
             it->setSpeedFactor(it->getSpeedFactor() * change);
@@ -154,4 +188,5 @@
         for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it)
             it->setTimeFactor(timeFactor_);
+*/
     }
 
@@ -162,12 +197,17 @@
         std::string levelName;
         CommandLine::getValue("level", &levelName);
-        startLevel_ = new Level(Settings::getDataPath() + std::string("levels/") + levelName);
-        Loader::open(startLevel_);
+        startFile_ = new XMLFile(Settings::getDataPath() + std::string("levels/") + levelName);
+        Loader::open(startFile_);
     }
 
     void GSLevel::unloadLevel()
     {
-        Loader::unload(startLevel_);
-        delete this->startLevel_;
+        //////////////////////////////////////////////////////////////////////////////////////////
+        // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO //
+        //////////////////////////////////////////////////////////////////////////////////////////
+        // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY /
+        //////////////////////////////////////////////////////////////////////////////////////////
+
+        delete this->startFile_;
     }
 
@@ -192,33 +232,36 @@
     void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
     {
-        static std::string bindingString = "";
-        static bool bTemporarySaved = false;
-        static bool bound = true;
-        // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
-        // Howerver there will be no real issue if it happens anyway.
-        if (command.find(keyDetectorCallbackCode_) != 0)
-        {
-            if (bound)
+        if (Core::showsGraphics())
+        {
+            static std::string bindingString = "";
+            static bool bTemporarySaved = false;
+            static bool bound = true;
+            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
+            // Howerver there will be no real issue if it happens anyway.
+            if (command.find(keyDetectorCallbackCode_) != 0)
             {
-                COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
-                InputManager::getInstance().requestEnterState("detector");
-                bindingString = command;
-                bTemporarySaved = bTemporary;
-                bound = false;
+                if (bound)
+                {
+                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
+                    InputManager::getInstance().requestEnterState("detector");
+                    bindingString = command;
+                    bTemporarySaved = bTemporary;
+                    bound = false;
+                }
+                //else:  We're still in a keybind command. ignore this call.
             }
-            //else:  We're still in a keybind command. ignore this call.
-        }
-        else
-        {
-            if (!bound)
+            else
             {
-                // user has pressed the key
-                std::string name = command.substr(this->keyDetectorCallbackCode_.size());
-                COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
-                this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
-                InputManager::getInstance().requestLeaveState("detector");
-                bound = true;
+                if (!bound)
+                {
+                    // user has pressed the key
+                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
+                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
+                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
+                    InputManager::getInstance().requestLeaveState("detector");
+                    bound = true;
+                }
+                // else: A key was pressed within the same tick, ignore it.
             }
-            // else: A key was pressed within the same tick, ignore it.
         }
     }
