Exemplo n.º 1
0
    private int PushUntilLastStep(IEntity entity, Type[] inputTypes, INetworkTimeline timeline)
    {
        var networkIdentityComponent = entity.GetComponent <NetworkIdentityComponent>();
        var identity          = networkIdentityComponent.Identity;
        var timePointWithLerp = timePointWithLerpDict[identity][timeline];
        var tickId            = timePointWithLerp.TickId;

        if (tickId < networkIdentityComponent.TickIdWhenCreated)
        {
            tickId = networkIdentityComponent.TickIdWhenCreated;
        }
        if (!timePointWithLerp.IsPlaying)
        {
            while (LockstepUtility.HasTickId(tickId))
            {
                var userInputData = GetUserInputDataByInputTypes(tickId, identity.UserId, inputTypes);
                for (int i = 0; i < userInputData.Length; i++)
                {
                    timePointWithLerp.AddRealtimeData(new TimePointData(tickId, LockstepUtility.GetDeltaTime(tickId), userInputData[i]));
                }
                tickId++;
            }
        }
        timePointWithLerp.TickId = tickId;
        return(tickId);
    }
Exemplo n.º 2
0
    private UserInputData[][][] GetUserInputDataByInputTypes(int tickId, Type[] inputTypes)
    {
        var dataList = new List <UserInputData[][]>();

        for (int i = 0; i < inputTypes.Length; i++)
        {
            var userInputData = LockstepUtility.GetAllUserInputData(tickId, inputTypes[i]);
            if (userInputData == null)
            {
                continue;
            }
            for (int j = 0; j < userInputData.Length; j++)
            {
                while (j >= dataList.Count)
                {
                    dataList.Add(new UserInputData[inputTypes.Length][]);
                }
                dataList[j][i] = userInputData[j];
            }
        }
        if (dataList.Count <= 0)
        {
            var data = new UserInputData[inputTypes.Length][];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = new UserInputData[1] {
                    LockstepUtility.CreateUesrInputData(tickId)
                };
            }
            dataList.Add(data);
        }
        return(dataList.ToArray());
    }
Exemplo n.º 3
0
    private void Update()
    {
        LockstepFactory.Update();
        UserInputs userInputs = LockstepUtility.GetUserInputs();

        byte[] dataBytes = MessagePackUtility.Serialize(userInputs);
        NetworkSystem.Publish(RequestCode.Input, dataBytes);
    }
Exemplo n.º 4
0
    public override void OnEnable()
    {
        base.OnEnable();

        RoomComponents.OnAdd().Subscribe(entity1 =>
        {
            var roomComponent = entity1.GetComponent <RoomComponent>();

            UserComponents.Entities.OnAdd().Subscribe(entity2 =>
            {
                var userComponent = entity2.GetComponent <UserComponent>();

                userComponent.IsRoomOwner.DistinctUntilChanged().Subscribe(_ =>
                {
                    UpdateRoomState(roomComponent);
                }).AddTo(this.Disposer).AddTo(userComponent.Disposer);
            }).AddTo(this.Disposer);

            UserComponents.Entities.OnRemove().Select(_ => true)
            .Merge(Observable.ReturnUnit().Select(_ => true))
            .Subscribe(_ =>
            {
                UpdateRoomState(roomComponent);
            }).AddTo(this.Disposer);

            roomComponent.StartButton.OnClickAsObservable().Subscribe(_ =>
            {
                foreach (var entity3 in UserComponents.Entities)
                {
                    var userComponent = entity3.GetComponent <UserComponent>();
                    if (userComponent.IsLocalPlayer)
                    {
                        LockstepUtility.AddInput(new EventInput().WithType(EventCode.GameStart).Add(userComponent.IsRoomOwner.Value));
                        break;
                    }
                }
            }).AddTo(this.Disposer).AddTo(roomComponent.Disposer);

            roomComponent.ExitButton.OnClickAsObservable().Subscribe(_ =>
            {
                foreach (var entity4 in UserComponents.Entities)
                {
                    var userComponent = entity4.GetComponent <UserComponent>();
                    if (userComponent.IsLocalPlayer)
                    {
                        NetworkSystem.Publish(RequestCode.QuitRoom, userComponent.UserId.ToString());
                        break;
                    }
                }
            }).AddTo(this.Disposer).AddTo(roomComponent.Disposer);
        }).AddTo(this.Disposer);
    }
Exemplo n.º 5
0
    private void UpdateInputs()
    {
        if (ThridPersonCameraComponents.Entities.Count <= 0)
        {
            return;
        }

        var virtualCamera = ThridPersonCameraComponents.Entities[0].GetComponent <CinemachineVirtualCamera>();
        var viewComponent = ThridPersonCameraComponents.Entities[0].GetComponent <ViewComponent>();

        LockstepUtility.AddInput(new EventInput()
                                 .WithType(EventCode.PlayerCamera)
                                 .Add((FixVector3)viewComponent.Transforms[0].position)
                                 .Add((FixVector3)viewComponent.Transforms[0].forward)
                                 .Add((Fix64)virtualCamera.m_Lens.FarClipPlane));
    }
Exemplo n.º 6
0
    public override void OnEnable()
    {
        base.OnEnable();

        EventSystem.OnEvent <SpawnUserEvent>(true).Subscribe(evt =>
        {
            foreach (var entity in UserComponents.Entities)
            {
                var userComponent = entity.GetComponent <UserComponent>();
                if (userComponent.UserId == evt.UserId)
                {
                    userComponent.IsRoomOwner.Value = evt.IsRoomOwner;
                    return;
                }
            }
            CreateUser(evt.IsLocalPlayer, evt.IsRoomOwner, evt.UserId, evt.Username, evt.TotalCount, evt.WinCount);
        }).AddTo(this.Disposer);

        NetworkSystem.Receive(RequestCode.QuitRoom).Subscribe(data =>
        {
            foreach (var entity in UserComponents.Entities)
            {
                var userComponent = entity.GetComponent <UserComponent>();
                var viewComponent = entity.GetComponent <ViewComponent>();

                if (userComponent.UserId == int.Parse(data.StringValue))
                {
                    if (userComponent.IsLocalPlayer || userComponent.IsRoomOwner.Value)
                    {
                        ClearOtherPlayers();
                    }
                    else
                    {
                        Destroy(viewComponent.Transforms[0].gameObject);
                    }
                    break;
                }
            }
        }).AddTo(this.Disposer);

        if (offline)
        {
            CreateUser(true, true, 0, "Offline Player", 0, 0);
            NetworkSystem.Mode = SessionMode.Offline;
            LockstepUtility.AddInput(new EventInput().WithType(EventCode.GameStart).Add(true));
        }
    }
Exemplo n.º 7
0
    private void Forecast(Type[] inputTypes, INetworkTimeline timeline)
    {
        var timePointWithLerp = defaultTimePointWithLerpDict[timeline];

        if (UseForecast && !timePointWithLerp.IsPlaying)
        {
            var tickId = timePointWithLerp.TickId - 1;
            if (LockstepUtility.HasTickId(tickId))
            {
                var deltaTime     = LockstepUtility.GetDeltaTime(tickId);
                var userInputData = GetUserInputDataByInputTypes(tickId, inputTypes);
                for (int i = 0; i < userInputData.Length; i++)
                {
                    timePointWithLerp.Forecast(new TimePointData(tickId, deltaTime, userInputData[i]), MaxForecastSteps);
                }
            }
        }
    }
Exemplo n.º 8
0
    private int PushUntilLastStep(Type[] inputTypes, INetworkTimeline timeline)
    {
        var timePointWithLerp = defaultTimePointWithLerpDict[timeline];
        var tickId            = timePointWithLerp.TickId;

        if (!timePointWithLerp.IsPlaying)
        {
            while (LockstepUtility.HasTickId(tickId))
            {
                var userInputData = GetUserInputDataByInputTypes(tickId, inputTypes);
                for (int i = 0; i < userInputData.Length; i++)
                {
                    timePointWithLerp.AddRealtimeData(new TimePointData(tickId, LockstepUtility.GetDeltaTime(tickId), userInputData[i]));
                }
                tickId++;
            }
        }
        timePointWithLerp.TickId = tickId;
        return(tickId);
    }
Exemplo n.º 9
0
    private void UpdateInputs()
    {
        LockstepUtility.AddInput(new EventInput().WithType(EventCode.HeartBeat));

        var axisInput = new AxisInput();

        axisInput.Horizontal = (Fix64)Input.GetAxis(InputParameters.Horizontal);
        axisInput.Vertical   = (Fix64)Input.GetAxis(InputParameters.Vertical);
        LockstepUtility.AddInput(axisInput);

        var keyInput = new KeyInput()
        {
            KeyCodes = new List <int>()
        };

        foreach (var obj in Enum.GetValues(typeof(KeyCode)))
        {
            if (Input.GetKey((KeyCode)obj))
            {
                keyInput.KeyCodes.Add((int)obj);
            }
        }
        LockstepUtility.AddInput(keyInput);

        var mouseInput = new MouseInput()
        {
            MouseButtons = new List <int>()
        };

        for (int i = 0; i < 3; i++)
        {
            if (Input.GetMouseButton(i))
            {
                mouseInput.MouseButtons.Add(i);
            }
        }
        mouseInput.ScrollDelta = (FixVector2)Input.mouseScrollDelta;
        mouseInput.Position    = (FixVector3)Input.mousePosition;
        mouseInput.Delta       = new FixVector2((Fix64)Input.GetAxis(InputParameters.MouseX), (Fix64)Input.GetAxis(InputParameters.MouseY));
        LockstepUtility.AddInput(mouseInput);
    }
Exemplo n.º 10
0
    public override void OnEnable()
    {
        base.OnEnable();

        NetworkSystem.Receive(RequestCode.Input).Subscribe(data =>
        {
            if (data.Mode == SessionMode.Offline)
            {
                UserInputs userInputs         = MessagePackUtility.Deserialize <UserInputs>(data.Value);
                userInputs.UserId             = 0;
                LockstepInputs lockstepInputs = LockstepUtility.CreateLockstepInputs(userInputs);
                NetworkSystem.Publish(RequestCode.Lockstep, MessagePackUtility.Serialize(lockstepInputs));
            }
        }).AddTo(this.Disposer);

        NetworkSystem.Receive(RequestCode.Lockstep).Subscribe(data =>
        {
            LockstepInputs lockstepInputs = MessagePackUtility.Deserialize <LockstepInputs>(data.Value);
            LockstepUtility.AddToTimeline(lockstepInputs);
        }).AddTo(this.Disposer);
    }