예제 #1
0
    // Description: Called in the update function every frame. Passes the latest packet to the server if it contains
    //              new data. If there is no new packets to send then the function returns since there is no data
    //              to send
    void ServerUpdate()
    {
        if (!isServer || isLocalPlayer)
        {
            return;
        }

        Package packageData = PacketManager.GetNextDataRecieved();

        if (packageData == null)
        {
            return;
        }

        Move(packageData.horizontal * Time.deltaTime * moveSpeed, packageData.vertical * Time.deltaTime * moveSpeed);

        if (transform.position == lastPosition)
        {
            return;
        }

        lastPosition = transform.position;

        ServerPacketManager.AddPackage(new RecievePackage
        {
            x         = transform.position.x,
            y         = transform.position.y,
            z         = transform.position.z,
            timeStamp = packageData.timeStamp
        });
    }
예제 #2
0
    // Description: Called in the update function every frame. Updates all player locations using the prediction
    //              list. Also performs server reconciliation is the distance between the server position and
    //              client position is greater than the threshold
    public void RemoteClientUpdate()
    {
        if (isServer)
        {
            return;
        }

        var data = ServerPacketManager.GetNextDataRecieved();

        if (data == null)
        {
            return;
        }

        if (isLocalPlayer && isPredictionEnabled)
        {
            var transmittedPackage = predictedPackages.Where(x => x.timeStamp == data.timeStamp).FirstOrDefault();
            if (transmittedPackage == null)
            {
                return;
            }

            if (Vector3.Distance(new Vector3(transmittedPackage.x, transmittedPackage.y, transmittedPackage.z), new Vector3(data.x, data.y, data.z)) > correctionThreshold)
            {
                transform.position = new Vector3(data.x, data.y, data.z);
            }

            predictedPackages.RemoveAll(x => x.timeStamp <= data.timeStamp);
        }
        else
        {
            transform.position = new Vector3(data.x, data.y, data.z);
        }
    }
        private void ServerUpdate()
        {
            // isLocalPlayer added to make sure that the server is just used for computation
            // and not for anything else. Its a dumb dedicated sever
            if (!isServer || isLocalPlayer)
            {
                return;
            }

            InputSendPackage inputSendPackageData = PacketManager.GetNextDataReceived();

            if (inputSendPackageData == null)
            {
                return;
            }

            RotatePlayer(inputSendPackageData.horizontal);
            MovePlayerForward(inputSendPackageData.vertical);
            SetMovementAnimation(inputSendPackageData.vertical);
            _screenWrapper.CheckObjectOutOfScreen();

            if (_lastPosition == transform.position && _lastRotation == transform.rotation.eulerAngles)
            {
                return;
            }

            Vector3 position = transform.position;
            Vector3 rotation = transform.rotation.eulerAngles;
            Vector3 velocity = _playerRb.velocity;

            ServerPacketManager.AddPackage(new PositionReceivePackage
            {
                percentX = ExtensionFunctions.Map(position.x, _screenWrapper.LeftMostPoint,
                                                  _screenWrapper.RightMostPoint, -1, 1),
                percentY = ExtensionFunctions.Map(position.y, _screenWrapper.TopMostPoint,
                                                  _screenWrapper.BottomMostPoint, 1, -1),

                velocityX = velocity.x,
                velocityY = velocity.y,

                rotationZ = rotation.z,
                roll      = _roll,

                movementAnimation = inputSendPackageData.vertical > 0,

                timestamp = inputSendPackageData.timestamp
            });

            _lastPosition = position;
            _lastRotation = rotation;
        }
        private void RemoteClientUpdate()
        {
            if (isServer)
            {
                return;
            }

            PositionReceivePackage data = ServerPacketManager.GetNextDataReceived();

            if (data == null)
            {
                return;
            }

            Vector2 normalizedPosition = new Vector2(
                ExtensionFunctions.Map(data.percentX, -1, 1,
                                       _screenWrapper.LeftMostPoint, _screenWrapper.RightMostPoint),
                ExtensionFunctions.Map(data.percentY, 1, -1,
                                       _screenWrapper.TopMostPoint, _screenWrapper.BottomMostPoint)
                );

            if (isLocalPlayer && isPredictionEnabled)
            {
                PositionReceivePackage predictedPackage =
                    _predictedPackages.FirstOrDefault(_ => _.timestamp == data.timestamp);
                if (predictedPackage == null)
                {
                    return;
                }

                Vector2 normalizedPredictedPosition = new Vector2(
                    ExtensionFunctions.Map(predictedPackage.percentX, -1, 1,
                                           _screenWrapper.LeftMostPoint, _screenWrapper.RightMostPoint),
                    ExtensionFunctions.Map(predictedPackage.percentY, 1, -1,
                                           _screenWrapper.TopMostPoint, _screenWrapper.BottomMostPoint)
                    );

                if (Vector2.Distance(normalizedPredictedPosition, normalizedPosition) > positionCorrectionThreshold)
                {
                    _playerRb.velocity = new Vector2(data.velocityX, data.velocityY);
                    transform.position = normalizedPosition;
                    SetMovementAnimation(data.movementAnimation);
                }


                if (Mathf.Abs(ExtensionFunctions.To360Angle(data.rotationZ) -
                              ExtensionFunctions.To360Angle(predictedPackage.rotationZ)) >
                    rotationCorrectionThreshold)
                {
                    _roll = data.roll;
                    transform.rotation = Quaternion.Euler(Vector3.forward * data.rotationZ);
                }

                _predictedPackages.RemoveAll(_ => _.timestamp <= data.timestamp);
            }
            else
            {
                transform.position = normalizedPosition;
                _playerRb.velocity = new Vector2(data.velocityX, data.velocityY);
                SetMovementAnimation(data.movementAnimation);

                _roll = data.roll;
                transform.rotation = Quaternion.Euler(Vector3.forward * data.rotationZ);
            }
        }