コード例 #1
0
ファイル: Client.cs プロジェクト: generatives/VoidTraveler
        public override void Update()
        {
            var deltaSeconds = Stopwatch.Elapsed.TotalSeconds;
            InfoViewer.Log("Client FPS", Math.Round(1f / deltaSeconds, 2).ToString());

            InfoViewer.Log("Roundtrip", _server?.Roundtrip.ToString());
            InfoViewer.Log("Roundtrip Varience", _server?.RoundtripVarience.ToString());

            Stopwatch.Restart();

            NetworkEvent networkEvent = _client.Poll();
            while (networkEvent.Type != NetworkEventType.Nothing)
            {
                switch (networkEvent.Type)
                {
                    case NetworkEventType.Connect:
                        _server = networkEvent.Connection;
                        break;
                    case NetworkEventType.Data:
                        var diff = _messageTimer.Elapsed.TotalMilliseconds;
                        //InfoViewer.Values["Message Time"] = Math.Round(diff, 4).ToString();
                        _messageTimer.Restart();
                        MessageRecieved(networkEvent.Data);
                        break;
                }
                networkEvent.Recycle();
                networkEvent = _client.Poll();
            }

            _imGuiRenderer.Update((float)deltaSeconds, _window.InputSnapshot);
            var imGuiWantsMouse = ImGuiNET.ImGui.GetIO().WantCaptureMouse;

            var cameraEntities = _camerasSet.GetEntities();
            var cameraTransform = cameraEntities.Length > 0 ? cameraEntities[0].Get<Transform>() : new Transform();

            var inputTrackerTransform = Matrix3x2.CreateTranslation(-_window.Width / 2f, -_window.Height / 2f) *
                Matrix3x2.CreateScale(1 / Settings.GRAPHICS_SCALE, -1 / Settings.GRAPHICS_SCALE) *
                Matrix3x2.CreateScale(1 / _zoom) *
                cameraTransform.Matrix;

            _cameraSpaceInputTracker.SetTransform(inputTrackerTransform);
            _cameraSpaceGameInputTracker.SetActive(!imGuiWantsMouse);

            _zoom += _window.InputSnapshot.WheelDelta * 0.1f;

            PreUpdate(deltaSeconds);
            Scene.Update(new LogicUpdate((float)deltaSeconds, _cameraSpaceGameInputTracker));
            PostUpdate(deltaSeconds);

            var serverMessages = new List<object>();

            _editorMenu.Run(new EditorUpdate()
            {
                CameraSpaceInput = _cameraSpaceInputTracker,
                CameraSpaceGameInput = _cameraSpaceGameInputTracker,
                Scene = Scene,
                ServerMessages = serverMessages
            });

            var clientUpdate = new ClientSystemUpdate()
            {
                Messages = serverMessages,
                Input = _cameraSpaceGameInputTracker
            };

            foreach (var system in _clientSystems)
            {
                system.Update(clientUpdate);
            }

            if(_server != null)
            {
                var messages = SerializeMessages(serverMessages);
                _server.Send(messages, 5, true, _messagesSent++);
            }

            cameraEntities = _camerasSet.GetEntities();
            cameraTransform = cameraEntities.Length > 0 ? cameraEntities[0].Get<Transform>() : new Transform();

            var cameraMatrix = cameraTransform.GetCameraMatrix(Settings.GRAPHICS_SCALE) * Matrix4x4.CreateScale(_zoom);

            var vp = _viewport.Viewport;
            _drawDevice.Begin(cameraMatrix * _viewport.GetScalingTransform(), vp);

            Scene.Render(_drawDevice);

            float gridSize = 20;
            var gridCenter = new Vector2((int)Math.Round(cameraTransform.WorldPosition.X / gridSize) * gridSize, (int)Math.Round(cameraTransform.WorldPosition.Y / gridSize) * gridSize);
            for (int x = -2; x <= 2; x++)
            {
                for (int y = -2; y <= 2; y++)
                {
                    SpriteBatchExtensions.DrawCircle(_drawDevice, (gridCenter + new Vector2(x, y) * gridSize) * Settings.GRAPHICS_SCALE, 0.2f * Settings.GRAPHICS_SCALE, 8, RgbaFloat.Red);
                }
            }

            _drawDevice.Draw();

            _imGuiRenderer.Render(_drawDevice.GraphicsDevice, _drawDevice.CommandList);

            _drawDevice.End();

            _window.GraphicsDevice.SwapBuffers(_window.MainSwapchain);
            _window.GraphicsDevice.WaitForIdle();
        }