示例#1
0
    public override void OnSyncedUpdate()
    {
        base.OnSyncedUpdate();

        if (!m_IsActive || m_HoleIndex < 0)
        {
            return;
        }

        // Get Simulation info.

        int currentTick    = TrueSyncManager.ticksMain;
        int rollbackWindow = TrueSyncManager.rollbackWindowMain;

        // Check current collision

        TSVector2 myPosition = tsTransform2D.position;

        for (int targetIndex = 0; targetIndex < m_Targets.Count; ++targetIndex)
        {
            tnHoleTarget holeTarget = m_Targets[targetIndex];

            if (holeTarget == null)
            {
                continue;
            }

            TSTransform2D otherTransform = holeTarget.GetComponent <TSTransform2D>();

            if (otherTransform == null)
            {
                continue;
            }

            TSVector2 targetPosition = otherTransform.position;

            TSVector2 positionDelta = targetPosition - myPosition;

            FP distance2 = positionDelta.LengthSquared();
            if (distance2 < m_Threshold * m_Threshold)
            {
                // Notify collision.

                holeTarget.CollidingWithHole();

                // Add object to pending list.

                if (holeTarget.canEnterHole && !holeTarget.isTeleporting)
                {
                    Internal_CacheTarget(currentTick, holeTarget);
                }
            }
        }

        // Check pending objects.

        for (int index = 0; index < m_Pending.count; ++index)
        {
            int tick = m_Pending.GetKey(index);

            if (currentTick == tick + rollbackWindow)
            {
                List <tnHoleTarget> holeTargets = m_Pending.GetValue(tick);
                if (holeTargets != null)
                {
                    for (int targetIndex = 0; targetIndex < holeTargets.Count; ++targetIndex)
                    {
                        tnHoleTarget holeTarget = holeTargets[targetIndex];

                        if (holeTarget == null)
                        {
                            continue;
                        }

                        RespawnPoint respawnPoint = GetRandomSpawnPoint();

                        if (respawnPoint == null || respawnPoint.transform == null)
                        {
                            continue;
                        }

                        TSTransform2D targetTransform = holeTarget.tsTransform2D;
                        TSRigidBody2D targetRigidbody = holeTarget.GetComponent <TSRigidBody2D>();

                        // Snap position.

                        if (targetRigidbody != null)
                        {
                            targetRigidbody.MovePosition(tsTransform2D.position);
                        }
                        else
                        {
                            targetTransform.position = tsTransform2D.position;
                        }

                        // Set rigidbody velocity,

                        if (targetRigidbody != null)
                        {
                            targetRigidbody.velocity = TSVector2.zero;
                        }

                        // Eavluate force

                        TSVector2 forceDirection = respawnPoint.forceDirection;
                        forceDirection.Normalize();

                        if (MathFP.Abs(respawnPoint.errorAngle) > FP.Zero)
                        {
                            int random      = TSRandom.Range(0, 101);
                            FP  t           = ((FP)random) / 100;
                            FP  randomError = MathFP.Lerp(-respawnPoint.errorAngle, respawnPoint.errorAngle, t);
                            forceDirection = forceDirection.Rotate(randomError);
                        }

                        TSVector2 outForce = forceDirection * respawnPoint.forceIntensity;

                        // Teleport.

                        holeTarget.Teleport(m_HoleIndex, respawnPoint.respawnPosition, outForce, m_RespawnTime, m_InEffect, m_OutEffect);
                    }
                }
            }
        }

        // Remove old data from dictionary.

        for (int index = 0; index < m_Pending.count; ++index)
        {
            int tick          = m_Pending.GetKey(index);
            int executionTick = tick + rollbackWindow;

            bool isSafeTick = TrueSyncManager.IsTickOutOfRollbackMain(executionTick);
            if (isSafeTick)
            {
                m_Pending.Remove(tick);
                index = -1;
            }
        }
    }
    public override void OnSyncedUpdate()
    {
        base.OnSyncedUpdate();

        if (!m_RunSyncedUpdate)
        {
            return;
        }

        // Get delta time.

        FP deltaTime = TrueSyncManager.deltaTimeMain;

        // Update movement direction.

        FP horizontalAxis;
        FP verticalAxis;

        if (m_EnableInputCompression)
        {
            int intX = TrueSyncInput.GetInt(m_HorizontalAxisCode);
            int intY = TrueSyncInput.GetInt(m_VerticalAxisCode);

            horizontalAxis = intX / (FP)s_InputPrecision;
            verticalAxis   = intY / (FP)s_InputPrecision;
        }
        else
        {
            horizontalAxis = TrueSyncInput.GetFP(m_HorizontalAxisCode);
            verticalAxis   = TrueSyncInput.GetFP(m_VerticalAxisCode);
        }

        FP horizontalAxisAbs = MathFP.Abs(horizontalAxis);
        FP verticalAxisAbs   = MathFP.Abs(verticalAxis);

        TSVector2 moveDirection = new TSVector2(horizontalAxis, verticalAxis);

        moveDirection.Normalize();

        m_MoveDirection = moveDirection;

        // Update player action.

        if (m_CooldownTimer > FP.Zero)
        {
            m_CooldownTimer -= deltaTime;

            if (m_CooldownTimer < FP.Zero)
            {
                m_CooldownTimer = FP.Zero;
            }
        }

        if (m_CooldownTimer == FP.Zero)
        {
            bool buttonPressed = (TrueSyncInput.GetByte(m_ButtonPressedCode) > 0);
            if (buttonPressed)
            {
                m_PressureTime += deltaTime;
                m_ChargeLevel   = MathFP.GetClampedPercentage(m_PressureTime, m_PressureTimeRange.min, m_PressureTimeRange.max);
            }
            else
            {
                if (m_PrevPressed)
                {
                    FP axisThreshold = FP.One / FP.Ten;

                    if (horizontalAxisAbs > axisThreshold || verticalAxisAbs > axisThreshold)
                    {
                        // Apply an insant force.

                        FP        requestForceMagnitude = MathFP.Lerp(m_ForceRange.min, m_ForceRange.max, m_ChargeLevel);
                        TSVector2 moveForce             = m_MoveDirection * requestForceMagnitude;
                        m_Rigidbody2D.AddForce(moveForce);

                        // Update cooldown timer.

                        m_CooldownTimer = MathFP.Lerp(m_CooldownRange.min, m_CooldownRange.max, m_ChargeLevel);
                    }
                    else
                    {
                        m_CooldownTimer = FP.Zero;
                    }
                }

                m_PressureTime = FP.Zero;
                m_ChargeLevel  = FP.Zero;
            }

            m_PrevPressed = buttonPressed;
        }
        else
        {
            m_PressureTime = FP.Zero;
            m_ChargeLevel  = FP.Zero;

            m_PrevPressed = false;
        }
    }