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

public class ApplyForceToPlayer : MonoBehaviour
{
    //instead of drag line coming from the mouse location, it should come from the ball

    //when the player clicks down, the drag line will appear at the center of the player, over its sprite.
    //based on the mouses current position and direction of travel the drag line will extend from the player.
    //on release the drag line will disappear.

    //on pressing down, collect the position of the mouse. when dragging, lengthen the drag line by the
    // difference of the starting mouse position from the current position. 

    private Rigidbody2D playerObject;
    private LineRenderer dragLine;
    private InputManager inputMgr;
    private LevelCameraEffects levelCamera;
    private AchievementSystem achSys;
    private PlayerTrailParticles playerParticles;
    private Vector2 dragLineStartPos, dragLineEndPos, mouseStartPos, mouseEndPos;
    private bool forceApplied = false;
    private float timeSinceForce = 0.0f;
    private bool overUI, manuallyDisabledMovement, classicMovementEnabled, invertedMovementEnabled, dragCanceled, unlockBoosted;
    [SerializeField] private int forceMultiplier;
    [SerializeField] private AnimationCurve forceCurve;
    private int defaultForceMultiplierValue;
    [SerializeField] private float timeBeforeNextForce = 1.0f;
    [SerializeField] private 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>();
        playerParticles = ObjectManager.GetObject(0).GetComponent<PlayerTrailParticles>();
        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)
        {
            if (!classicMovementEnabled)
                dragLineStartPos = playerObject.transform.position;

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

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

            SetDragLineColor();
        }
        else
            dragLine.enabled = 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 SetClassicMovementState(bool newState)
    {
        classicMovementEnabled = newState;
    }

    public void SetInvertedMovementState(bool newState)
    {
        invertedMovementEnabled = newState;
    }

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

    private void SetDragLineColor()
    {
        //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, Vector2.Distance(dragLineStartPos, dragLineEndPos)));

        float redColor = curveValue;
        float greenColor = 1 - redColor;

        Color lineColor;

        if(!forceApplied)
            lineColor = new Color(redColor, greenColor, 0);
        else
            lineColor = new Color(redColor, greenColor, 0, 0.05f);

        dragLine.startColor = lineColor;
        dragLine.endColor = lineColor;
    }

    public void OnMouseButtonDown()
    {
        if (!manuallyDisabledMovement && this.enabled)
        {
            if (inputMgr != null)
            {
                if (!inputMgr.GetMouseOverUI())
                {
                    dragLine.enabled = true;

                    Vector2 mouseVector = inputMgr.GetMousePosition();

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

                    if (!classicMovementEnabled)
                    {
                        mouseStartPos = point;
                    }
                    else
                    {
                        dragLineStartPos = point;
                    }

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

    public void OnMouseButtonHeldDown()
    {
        if (!manuallyDisabledMovement && !dragCanceled && this.enabled)
        {
            if (inputMgr != null && !overUI)
            {
                Vector2 mouseVector = inputMgr.GetMousePosition();

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

                dragLineEndPos = point;

                if (!invertedMovementEnabled)
                {
                    dragLine.SetPosition(0, dragLineStartPos);
                    dragLine.SetPosition(1, dragLineEndPos);
                }
                else
                {
                    dragLine.SetPosition(1, dragLineStartPos);
                    dragLine.SetPosition(0, dragLineEndPos);
                }
            }
        }
    }

    public void OnMouseButtonUp()
    {
        if (!manuallyDisabledMovement && !dragCanceled && this.enabled)
        {
            if (inputMgr != null && !overUI)
            {
                dragLine.enabled = false;

                float positionDifferece = Vector2.Distance(dragLineStartPos, dragLineEndPos);

                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;
                    playerParticles.EmitBurst();
                }
            }
            else
                overUI = false;
        }
        else
            dragCanceled = false;
    }

    public void OnDragCanceled()
    {
        if (Input.GetMouseButton(0))
        {
            dragCanceled = true;

            dragLine.enabled = false;
        }
    }

    public void OnDisable()
    {
        ForceApplied = false;
        overUI = false;

        ResetCoolDown();
        ResetForceMultiplier();
        timeSinceForce = 0;
    }
}