Exemple #1
0
    // INTERNALS

    public void UpdateEffector()
    {
        TSCollider2D[] colliders = TSPhysics2D.OverlapCircleAll(tsTransform2D.position, m_Radius, m_LayerMask);

        if (colliders != null)
        {
            if (colliders.Length > 0)
            {
                for (int index = 0; index < colliders.Length; ++index)
                {
                    TSCollider2D currentCollider = colliders[index];

                    if (currentCollider == null)
                    {
                        continue;
                    }

                    TSTransform2D transform2d = currentCollider.tsTransform;

                    TSVector2 direction = transform2d.position - tsTransform2D.position;
                    direction = direction.normalized;

                    TSRigidBody2D rigidbody = currentCollider.GetComponent <TSRigidBody2D>();
                    if (rigidbody != null)
                    {
                        rigidbody.AddForce(direction * m_ForceMagnitude);
                    }
                }
            }
        }
    }
Exemple #2
0
 /**
  * @brief When the ball hits a player a force in Y axis is applied and also plays a sound when the collision is not with a goal.
  **/
 public void OnSyncedCollisionEnter(TSCollision2D other)
 {
     if (other.gameObject.tag == "player")
     {
         tsRigidBody.AddForce(new TSVector2(0, -500));
     }
     if (other.gameObject.tag != "goal")
     {
         audioSource.Play();
     }
 }
    // INTERNALS

    private void Respawn()
    {
        int tick = TrueSyncManager.ticksMain;

        // If rigidbody, re-config it. Move otherwise.

        if (m_Rigidbody != null)
        {
            m_Rigidbody.isKinematic = m_CachedKinematic;
            m_Rigidbody.mass        = m_CachedMass;

            m_Rigidbody.MovePosition(m_RespawnPosition);
            m_Rigidbody.AddForce(m_OutForce);
        }
        else
        {
            tsTransform2D.position = m_RespawnPosition;
        }

        // Play Effect.

        if (!m_OutEffectTicks.Contains(tick))
        {
            Effect outEffect = m_OutEffect;
            EffectUtils.PlayEffect(outEffect, currentPosition);

            m_OutEffectTicks.Add(tick);
        }

        // Clear class variables.

        m_EnteringHoleIndex = -1;
        m_TeleportTick      = 0;

        m_Teleporting = false;
        m_Timer       = FP.Zero;

        m_Delay           = FP.Zero;
        m_OutForce        = TSVector2.zero;
        m_RespawnPosition = TSVector2.zero;

        m_OutEffect           = null;
        m_TeleportRequestTick = 0;
    }
    public void Kick(TSVector2 i_Force, GameObject i_Source)
    {
        if (m_PreventMultipleKick && m_Kicked)
        {
            return;
        }

        m_Rigidbody2d.AddForce(i_Force);

        if (m_TrackTouches)
        {
            InternalAddTouch(i_Source, TrueSyncManager.timeMain /* Timestamp */);
        }

        m_Kicked = true;

        if (m_KickedEvent != null)
        {
            m_KickedEvent();
        }
    }
    public override void OnSyncedCollisionEnter(TSCollision2D i_Collision)
    {
        int tick = TrueSyncManager.ticksMain;

        // Apply forces.

        TSRigidBody2D rigidbody2D = i_Collision.gameObject.GetComponent <TSRigidBody2D>();

        if (rigidbody2D != null)
        {
            // Clear velocity.

            rigidbody2D.velocity = TSVector2.zero;

            // Add bounce force.

            TSVector2 otherPosition = rigidbody2D.position;
            TSVector2 myPostion     = tsTransform2D.position;

            TSVector2 direction = otherPosition - myPostion;
            direction.Normalize();

            TSVector2 force = direction * m_Force;

            rigidbody2D.AddForce(force);
        }

        // Play effect.

        if (!m_EffectTicks.Contains(tick))
        {
            m_Animator.SetTrigger("Hit");
            EffectUtils.PlayEffect(m_Effect, transform);

            m_EffectTicks.Add(tick);
        }
    }
    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);
            }
        }
    }
    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;
        }
    }