public override void OnSyncedUpdate()
    {
        base.OnSyncedUpdate();

        if (!m_RunSyncedUpdate)
        {
            return;
        }

        // Read delta time.

        FP  deltaTime   = TrueSyncManager.deltaTimeMain;
        int currentTick = TrueSyncManager.ticksMain;

        // Update timers.

        UpdateTimers(deltaTime);

        // Handle kick.

        bool kickRequested   = TrueSyncInput.HasByte(m_ButtonDownCode);
        bool cooldownOk      = (m_CooldownTimer == FP.Zero);
        bool energyAvailable = (m_Energy != null && m_Energy.CanSpend(m_EnergyCost));

        if (kickRequested && m_KickTickRequest == 0 && energyAvailable && cooldownOk)
        {
            // Cache kick tick request.

            m_KickTickRequest = currentTick + m_KickTickDelay;

            // Consume energy

            if (m_Energy != null)
            {
                m_Energy.Consume(m_EnergyCost);
            }
        }

        if ((m_KickTickRequest > 0) && (m_KickTickRequest == currentTick))
        {
            m_KickTickRequest = 0;

            bool kick   = Kick(m_KickLayerMask, m_KickForce, deltaTime, m_KickInvertVelocity);
            bool tackle = Kick(m_TackleLayerMask, m_TackleForce, deltaTime, m_TackleInvertVelocity);

            bool kickedAnything = kick || tackle;

            if (kickedAnything || m_PlayEffectWhenNothingIsHit)
            {
                int tick = TrueSyncManager.ticksMain;
                if (!m_EffectTicks.Contains(tick))
                {
                    if (m_EffectPivot != null)
                    {
                        EffectUtils.PlayEffect(m_Effect, m_EffectPivot);
                    }

                    m_EffectTicks.Add(tick);
                }
            }

            if (!kickedAnything)
            {
                m_CooldownTimer = FP.Zero;
            }
            else
            {
                m_CooldownTimer = m_Cooldown;
            }
        }

        // Clear effect ticks cache.

        for (int index = 0; index < m_EffectTicks.Count; ++index)
        {
            int tick = m_EffectTicks[index];

            if (TrueSyncManager.IsTickOutOfRollbackMain(tick))
            {
                m_EffectTicks.RemoveAt(index);
                index = -1;
            }
        }
    }
Exemple #2
0
    public override void OnSyncedUpdate()
    {
        base.OnSyncedUpdate();

        if (!m_RunSyncedUpdate)
        {
            return;
        }

        // Get delta time.

        FP deltaTime = TrueSyncManager.deltaTimeMain;

        // Handle attract.

        if (m_Running)
        {
            bool buttonPressed = TrueSyncInput.GetByte(m_ButtonPressedCode) > 0;
            if (buttonPressed)
            {
                FP cost = m_EnergyCostRate * deltaTime;
                cost = MathFP.Max(cost, FP.Zero);

                if (m_Energy != null && m_Energy.CanSpend(cost))
                {
                    m_Energy.Consume(cost);
                }
                else
                {
                    m_Running = false;
                }
            }
            else
            {
                m_Running = false;
            }
        }
        else
        {
            bool buttonDown = TrueSyncInput.HasByte(m_ButtonDownCode);
            if (buttonDown)
            {
                FP cost = m_EnergyCostRate * deltaTime;
                cost = MathFP.Max(cost, FP.Zero);

                if (m_Energy != null && m_Energy.CanSpend(cost))
                {
                    m_Energy.Consume(cost);

                    m_Running = true;
                }
            }
        }

        // Update effector.

        if (m_Running)
        {
            UpdateEffector();
        }
    }
    public override void OnSyncedUpdate()
    {
        base.OnSyncedUpdate();

        if (!m_RunSyncedUpdate)
        {
            return;
        }

        // Read delta time.

        FP  deltaTime   = TrueSyncManager.deltaTimeMain;
        int currentTick = TrueSyncManager.ticksMain;

        // Update timers.

        UpdateTimers(deltaTime);

        // Read input.

        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);
        }

        // Handle actions.

        bool actionRequested = TrueSyncInput.HasByte(m_DashRequestedCode);
        bool cooldownOk      = (m_CooldownTimer == FP.Zero);
        bool energyAvailable = (m_Energy != null && m_Energy.CanSpend(m_DashEnergyCost));

        if (actionRequested && m_DashTickRequest == 0 && energyAvailable && cooldownOk)
        {
            // Cahce dash tick request.

            m_DashTickRequest = currentTick + m_DashTickDelay;

            // Consume energy.

            if (m_Energy != null)
            {
                m_Energy.Consume(m_DashEnergyCost);
            }
        }

        if ((m_DashTickRequest > 0) && (m_DashTickRequest == currentTick))
        {
            // Consume tick input.

            m_DashTickRequest = 0;

            // Override layer, mass and drag.

            int layerOverride = m_DashLayer;

            FP massOverride = m_DashMass;
            FP dragOverride = m_DashDrag;

            SetRigidbodyLayer(layerOverride);

            SetRigidbodyMass(massOverride);
            SetRigidbodyDrag(dragOverride);

            // Apply insant force.

            TSVector2 move = new TSVector2(horizontalAxis, verticalAxis);
            move.Normalize();

            TSVector2 moveForce = move * m_DashAppliedForce;
            m_Rigidbody2D.AddForce(moveForce);

            // Update timers.

            m_DurationTimer = m_DashDuration;
            m_CooldownTimer = MathFP.Max(m_DashCooldown, m_DurationTimer);

            // Raise event.

            if (m_OnDashTriggered != null)
            {
                m_OnDashTriggered();
            }
        }
        else // Handle movement.
        {
            if (m_DurationTimer == FP.Zero)
            {
                // Restore layer, mass and drag.

                SetRigidbodyLayer(m_OriginalLayer);

                SetRigidbodyMass(m_OriginalMass);
                SetRigidbodyDrag(m_OriginalDrag);

                // Compute movement force.

                TSVector2 move      = new TSVector2(horizontalAxis, verticalAxis);
                TSVector2 moveForce = move * m_MoveForce;

                if (m_MaxSpeed > FP.Zero)
                {
                    TSVector2 currentVelocity = m_Rigidbody2D.velocity;

                    TSVector2 moveAcceleration = moveForce / m_Rigidbody2D.mass;        // F = m * a    ==> a = F / m
                    TSVector2 deltaVelocity    = moveAcceleration * deltaTime;          // a = dv / dt  ==> dv = a * dt

                    TSVector2 newVelocity = currentVelocity + deltaVelocity;

                    if (newVelocity.LengthSquared() > m_MaxSpeed * m_MaxSpeed)
                    {
                        // Modulate moveForce in order to reach a maximum speed of m_MaxSpeed.

                        TSVector2 maxVelocity     = newVelocity.normalized * m_MaxSpeed;
                        TSVector2 maxAcceleration = (maxVelocity - currentVelocity) / deltaTime;

                        moveForce = maxAcceleration * m_Rigidbody2D.mass;
                    }
                }

                m_Rigidbody2D.AddForce(moveForce);
            }
        }
    }