Exemplo n.º 1
0
        public void Run()
        {
            Logger.Debug("Initialising GameController.");

            ShowSplashScreen();

            _configurationManager.LoadFromFile(PathHelpers.ExecutableRelativeFile("client_config.toml"));

            _resourceManager.LoadBaseResources();
            _resourceManager.LoadLocalResources();

            //Setup Cluwne first, as the rest depends on it.
            SetupCluwne();
            CleanupSplashScreen();

            //Initialization of private members
            _messageLogger.Initialize();
            _entityNetworkManager.Initialize();
            _tileDefinitionManager.InitializeResources();

            var prototypeManager = IoCManager.Resolve <IPrototypeManager>();

            prototypeManager.LoadDirectory(PathHelpers.ExecutableRelativeFile("Prototypes"));
            prototypeManager.Resync();
            _networkManager.Initialize();
            _netGrapher.Initialize();
            _userInterfaceManager.Initialize();

            _stateManager.RequestStateChange <MainScreen>();

            FrameEventArgs _frameEvent;

            // EventArgs _frameEventArgs;
            _clock = new Clock();

            while (CluwneLib.IsRunning == true)
            {
                var lastFrameTime = _clock.ElapsedTime.AsSeconds();
                _clock.Restart();
                _frameEvent = new FrameEventArgs(lastFrameTime);
                CluwneLib.ClearCurrentRendertarget(Color.Black);
                CluwneLib.Screen.DispatchEvents();
                CluwneLib.RunIdle(this, _frameEvent);
                CluwneLib.Screen.Display();
            }
            _networkManager.Disconnect();
            CluwneLib.Terminate();
            Logger.Info("GameController terminated.");

            IoCManager.Resolve <IConfigurationManager>().SaveToFile();
        }
Exemplo n.º 2
0
        public void Run()
        {
            Logger.Debug("Initializing GameController.");

            _configurationManager.LoadFromFile(PathHelpers.ExecutableRelativeFile("client_config.toml"));

            _resourceCache.LoadBaseResources();
            // Load resources used by splash screen and main menu.
            LoadSplashResources();
            ShowSplashScreen();

            _resourceCache.LoadLocalResources();

            //identical code for server in baseserver
            if (!AssemblyLoader.TryLoadAssembly <GameShared>(_resourceManager, $"Content.Shared"))
            {
                if (!AssemblyLoader.TryLoadAssembly <GameShared>(_resourceManager, $"Sandbox.Shared"))
                {
                    Logger.Warning($"[ENG] Could not load any Shared DLL.");
                }
            }

            if (!AssemblyLoader.TryLoadAssembly <GameClient>(_resourceManager, $"Content.Client"))
            {
                if (!AssemblyLoader.TryLoadAssembly <GameClient>(_resourceManager, $"Sandbox.Client"))
                {
                    Logger.Warning($"[ENG] Could not load any Client DLL.");
                }
            }

            IoCManager.Resolve <ILightManager>().Initialize();

            // Call Init in game assemblies.
            AssemblyLoader.BroadcastRunLevel(AssemblyLoader.RunLevel.Init);

            //Setup Cluwne first, as the rest depends on it.
            SetupCluwne();
            CleanupSplashScreen();

            //Initialization of private members
            _tileDefinitionManager.InitializeResources();

            _serializer.Initialize();
            var prototypeManager = IoCManager.Resolve <IPrototypeManager>();

            prototypeManager.LoadDirectory(@"Prototypes");
            prototypeManager.Resync();
            _networkManager.Initialize(false);
            _console.Initialize();
            _netGrapher.Initialize();
            _userInterfaceManager.Initialize();
            _mapManager.Initialize();
            _placementManager.Initialize();
            _entityManager.Initialize();

            _networkManager.RegisterNetMessage <MsgFullState>(MsgFullState.NAME, message => IoCManager.Resolve <IGameStateManager>().HandleFullStateMessage((MsgFullState)message));
            _networkManager.RegisterNetMessage <MsgStateUpdate>(MsgStateUpdate.NAME, message => IoCManager.Resolve <IGameStateManager>().HandleStateUpdateMessage((MsgStateUpdate)message));

            _client.Initialize();

            _stateManager.RequestStateChange <MainScreen>();

            #region GameLoop

            // maximum number of ticks to queue before the loop slows down.
            const int maxTicks = 5;

            _time.ResetRealTime();
            var maxTime = TimeSpan.FromTicks(_time.TickPeriod.Ticks * maxTicks);

            while (CluwneLib.IsRunning)
            {
                var accumulator = _time.RealTime - _lastTick;

                // If the game can't keep up, limit time.
                if (accumulator > maxTime)
                {
                    // limit accumulator to max time.
                    accumulator = maxTime;

                    // pull lastTick up to the current realTime
                    // This will slow down the simulation, but if we are behind from a
                    // lag spike hopefully it will be able to catch up.
                    _lastTick = _time.RealTime - maxTime;

                    // announce we are falling behind
                    if ((_time.RealTime - _lastKeepUpAnnounce).TotalSeconds >= 15.0)
                    {
                        Logger.Warning("[ENG] MainLoop: Cannot keep up!");
                        _lastKeepUpAnnounce = _time.RealTime;
                    }
                }

                _time.StartFrame();

                var realFrameEvent = new FrameEventArgs((float)_time.RealFrameTime.TotalSeconds);

                // process Net/KB/Mouse input
                Process(realFrameEvent);

                _time.InSimulation = true;
                // run the simulation for every accumulated tick
                while (accumulator >= _time.TickPeriod)
                {
                    accumulator -= _time.TickPeriod;
                    _lastTick   += _time.TickPeriod;

                    // only run the sim if unpaused, but still use up the accumulated time
                    if (!_time.Paused)
                    {
                        // update the simulation
                        var simFrameEvent = new FrameEventArgs((float)_time.FrameTime.TotalSeconds);
                        Update(simFrameEvent);
                        _time.CurTick++;
                    }
                }

                // if not paused, save how close to the next tick we are so interpolation works
                if (!_time.Paused)
                {
                    _time.TickRemainder = accumulator;
                }

                _time.InSimulation = false;

                // render the simulation
                Render(realFrameEvent);
            }

            #endregion GameLoop

            _networkManager.ClientDisconnect("Client disconnected from game.");
            CluwneLib.Terminate();
            Logger.Info("GameController terminated.");

            IoCManager.Resolve <IConfigurationManager>().SaveToFile();
        }