using System; using System.Collections; using UnityEngine; using UnityEngine.UI; using Random = UnityEngine.Random; // ReSharper disable once CheckNamespace namespace BlueWaterProject { [Serializable] public class ProcessBar { [field: SerializeField] public GameObject Obj { get; set; } [field: SerializeField] public Image Fill { get; set; } [field: SerializeField] public Transform PreviousGaugeLine { get; set; } [field: SerializeField] public Slider ReloadSlider { get; set; } [field: SerializeField] public float ShakeDuration { get; set; } = 0.5f; [field: SerializeField] public float ShakePower { get; set; } = 10f; private AudioSource reloadingAttackSound; private bool isShaking; public ProcessBar(GameObject obj, Image fill, Transform previousGaugeLine, Slider reloadSlider) { Obj = obj; Fill = fill; PreviousGaugeLine = previousGaugeLine; ReloadSlider = reloadSlider; reloadingAttackSound = ReloadSlider.GetComponent(); SetFillAmount(0f); } public void SetActive(bool value) => Obj.SetActive(value); public void SetPosition(Vector3 value) => Obj.transform.position = value; public void SetFillAmount(float value) => Fill.fillAmount = value; public void SetRotateZ(float value) => PreviousGaugeLine.rotation = Quaternion.Euler(0f, 0f, value); public void SetActiveReloadSlider(bool value) => ReloadSlider.gameObject.SetActive(value); public void SetSliderValue(float value) => ReloadSlider.value = value; public IEnumerator ShakeProcessBarCoroutine() { if (isShaking) yield break; isShaking = true; reloadingAttackSound.Play(); var time = 0f; var processBarOriginalPos = Obj.transform.localPosition; while (time < ShakeDuration) { if (!Obj.gameObject.activeSelf) { Obj.transform.localPosition = processBarOriginalPos; isShaking = false; yield break; } time += Time.deltaTime; var shakeAmount = Random.Range(-1f, 1f) * ShakePower; var processBarLocalPos = Obj.transform.localPosition; Obj.transform.localPosition = new Vector3(processBarLocalPos.x + shakeAmount, processBarLocalPos.y, processBarLocalPos.z); yield return null; } Obj.transform.localPosition = processBarOriginalPos; isShaking = false; } } }