UnityGameProjectsCode / Rise2Point0Game / Player / ApplyForceTouch.cs
ApplyForceTouch.cs
Raw
using UnityEngine;

public class ApplyForceTouch : MonoBehaviour
{
    private Rigidbody2D playerObject;
    private LineRenderer dragLine;
    private InputManager inputMgr;
    private AchievementSystem achSys;
    private Vector2 dragLineStartPosition;
    private Vector2 dragLineEndPosition;
    private Vector2 touchStartPos;
    private Vector2 touchEndPos;
    private bool forceApplied = false;
    private float timeSinceForce = 0.0f;
    private LevelCameraEffects levelCamera;
    private bool overUI;
    private bool manuallyDisabledMovement;
    private bool unlockBoosted;

    public int forceMultiplier;
    public AnimationCurve forceCurve;
    private int defaultForceMultiplierValue;
    private float timeBeforeNextForce;
    public float forceMaxDistance;

    public float TimeBeforeNextForce { get => timeBeforeNextForce; set => timeBeforeNextForce = value; }
    public bool ForceApplied { get => forceApplied; set => forceApplied = value; }
    public bool ManuallyDisabledMovement { get => manuallyDisabledMovement; set => manuallyDisabledMovement = value; }

    private void Start()
    {
        playerObject = ObjectManager.GetObject(0).GetComponent<Rigidbody2D>();
        dragLine = ObjectManager.GetObject(1).GetComponent<LineRenderer>();
        inputMgr = ObjectManager.GetObject(4).GetComponent<InputManager>();
        levelCamera = GUIManager.GetObject(12).GetComponent<LevelCameraEffects>();
        achSys = ObjectManager.GetObject(4).GetComponent<AchievementSystem>();
        defaultForceMultiplierValue = forceMultiplier;

        GUIManager.SetSpeedBoostTextState(false);
    }

    private void Update()
    {
        if (!manuallyDisabledMovement)
        {
            dragLineStartPosition = playerObject.transform.position;

            if (!forceApplied)
            {
                float positionDifferece = Vector2.Distance(touchStartPos, touchEndPos);

                //use the curve value to determine where the green and red should start and end based on the custom curve
                float curveValue = forceCurve.Evaluate(Mathf.InverseLerp(0, forceMaxDistance, positionDifferece));

                float redColor = curveValue;
                float greenColor = 1 - redColor;
                Color lineColor = new Color(redColor, greenColor, 0);
                dragLine.startColor = lineColor;
                dragLine.endColor = lineColor;
            }

            if (forceApplied)
            {
                timeSinceForce += Time.deltaTime;
                GUIManager.SetCooldownCircleValue(timeSinceForce, timeBeforeNextForce);

                if (timeSinceForce >= timeBeforeNextForce)
                {
                    GUIManager.FadeOutCooldownCircle();
                    forceApplied = false;
                    timeSinceForce = 0.0f;
                }
            }

            if (Input.touchCount > 0)
            {
                Touch touch = Input.GetTouch(0);

                //------------------------------------------------ Begining of touch control ---------------------------------------------------------------------------------

                if (touch.phase == TouchPhase.Began) //start of touch
                {
                    if (inputMgr != null)
                    {
                        if (!inputMgr.GetTouchOverUI())
                        {
                            dragLine.enabled = true;

                            Vector2 touchVector = touch.position;

                            Vector2 point = Camera.main.ScreenToWorldPoint(new Vector3(touchVector.x, touchVector.y, 0));
                            touchStartPos = point;

                            dragLine.SetPosition(0, dragLineStartPosition);
                            dragLine.SetPosition(1, dragLineStartPosition);
                        }
                        else
                            overUI = true;
                    }
                }

                if (touch.phase == TouchPhase.Moved) //during touch
                {
                    if (inputMgr != null && !overUI)
                    {
                        Vector2 touchVector = touch.position;

                        Vector2 point = Camera.main.ScreenToWorldPoint(new Vector3(touchVector.x, touchVector.y, 0));
                        touchEndPos = point;

                        float xPosDifference = touchEndPos.x - touchStartPos.x;
                        float yPosDifference = touchEndPos.y - touchStartPos.y;

                        dragLineEndPosition = new Vector2(dragLineStartPosition.x + xPosDifference, dragLineStartPosition.y + yPosDifference);

                        dragLine.SetPosition(0, dragLineStartPosition);
                        dragLine.SetPosition(1, dragLineEndPosition);
                    }
                }

                if (touch.phase == TouchPhase.Ended) //end of touch
                {
                    if (inputMgr != null && !overUI)
                    {
                        dragLine.enabled = false;

                        float positionDifferece = Vector2.Distance(dragLineStartPosition, dragLineEndPosition);

                        if (positionDifferece > forceMaxDistance)
                            positionDifferece = forceMaxDistance;

                        Transform linePos = dragLine.gameObject.transform.parent.transform;

                        if (forceApplied == false && positionDifferece > 0.0001f)
                        {
                            //using a custom curve and the position difference of the drag line, find the interpolated value that determines how much force should be applied
                            float curveValue = forceCurve.Evaluate(Mathf.InverseLerp(0, forceMaxDistance, positionDifferece));

                            //multiply the curve value by 10 to get a value between 0 and 10 and then multiply that by the force multiplier
                            playerObject.AddForce(linePos.forward * (curveValue * 10 * forceMultiplier));

                            levelCamera.ApplyZoomEffect(positionDifferece);
                            GUIManager.FadeInCooldownCircle();
                            forceApplied = true;
                        }
                    }
                    else
                        overUI = false;
                }
            }
        }
    }

    public void AdjustForceMultiplier(int newValue)
    {
        forceMultiplier += newValue;

        AdjustSpeedBoostTextVisibility();
    }

    public void SetUnlockBoosted(bool state)
    {
        //when true, the force multiplier gets boosted by 30
        unlockBoosted = state;

        if (unlockBoosted)
            forceMultiplier = defaultForceMultiplierValue + 30;
        else
            forceMultiplier = defaultForceMultiplierValue;

        AdjustSpeedBoostTextVisibility();
    }

    public void ResetForceMultiplier()
    {
        if (unlockBoosted)
            forceMultiplier = defaultForceMultiplierValue + 30;
        else
            forceMultiplier = defaultForceMultiplierValue;

        AdjustSpeedBoostTextVisibility();
    }

    public void AdjustSpeedBoostTextVisibility()
    {
        if (forceMultiplier < defaultForceMultiplierValue)
            forceMultiplier = defaultForceMultiplierValue;

        if (forceMultiplier - defaultForceMultiplierValue > 0)
        {
            GUIManager.SetSpeedBoostTextState(true);
            GUIManager.ChangeSpeedBoostText(forceMultiplier - defaultForceMultiplierValue);

            if ((forceMultiplier - defaultForceMultiplierValue) > 140)
                achSys.SetMaxSpeedBoost(forceMultiplier - defaultForceMultiplierValue);
        }
        else
            GUIManager.SetSpeedBoostTextState(false);
    }

    public void ResetCoolDown()
    {
        //called to reset cooldown instantly.
        if (forceApplied)
            timeSinceForce = timeBeforeNextForce;
    }
}