Skip to content
Snippets Groups Projects
ForceBarController.cs 1.9 KiB
Newer Older
william's avatar
william committed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ForceBarController : MonoBehaviour
{
    private Image bar;
    [SerializeField] private Image backgroundBar;
    [SerializeField] private GameObject player;
    private bool isDumping;
    private bool shotAttemped;
    private bool canShoot;
    private float force;
    private Rigidbody2D playerRb;
    private Animator playerAnimator;

    private void Start()
    {
        bar = GetComponent<Image>();
        backgroundBar.fillAmount = 0;
        canShoot = true;
        playerRb = player.GetComponent<Rigidbody2D>();
        playerAnimator = player.GetComponent<Animator>();
    }

    private void FixedUpdate()
    {
        if (playerRb.velocity == Vector2.zero)
        {
            if (shotAttemped && Input.GetAxis("Shot") == 0)
            {
                force = bar.fillAmount;
                shotAttemped = false;
                isDumping = false;
                FindObjectOfType<TestScript>().shoot(force);
                bar.fillAmount = 0;
                backgroundBar.fillAmount = 0;
                playerAnimator.SetBool("Shooting", false);
            }
            if (Input.GetAxis("Shot") != 0)
            {
                playerAnimator.SetBool("Shooting", true);
                backgroundBar.fillAmount = 1;
                if (bar.fillAmount < 1 && !isDumping)
                {
                    bar.fillAmount += Time.deltaTime;
                }
                else if (bar.fillAmount == 1)
                {
                    //perfect
                    isDumping = true;
                }
                if (isDumping)
                {
                    bar.fillAmount -= Time.deltaTime;
                }
                shotAttemped = true;
            }
        }
    }

    private void updateState()
    {
        canShoot = canShoot == true ? false : true;
    }
}