using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class BonusController : MonoBehaviour, ITextChangable { public UnityEvent OnTextChange => _OnTextChange; private UnityEvent _OnTextChange = new UnityEvent(); private float _bonusValue = 1f; public float BonusValue { get => _bonusValue; set { _bonusValue = value; _OnTextChange?.Invoke(_bonusValue); } } [SerializeField] private float _bonusOffset = 0.1f; [SerializeField] private float _decreaseReloadTime = 5f; [SerializeField] private float _decreaseSpeed = 0.6f; private bool _decreaseBonus = false; private Coroutine _decreaseReloadCoroutine; private void Start() { BonusValue = 1; } private IEnumerator DecreaseReload_Coroutine() { _decreaseBonus = false; yield return new WaitForSeconds(_decreaseReloadTime); _decreaseBonus = true; } public void DecreaseReload() { if (_decreaseReloadCoroutine != null) { StopCoroutine(_decreaseReloadCoroutine); } _decreaseReloadCoroutine = StartCoroutine(DecreaseReload_Coroutine()); } public void UseBonus() { BonusValue += _bonusOffset; } private void FixedUpdate() { if (_decreaseBonus) { BonusValue -= Time.deltaTime * _decreaseSpeed; BonusValue = Mathf.Clamp(BonusValue, 1, float.MaxValue); _OnTextChange?.Invoke(BonusValue); if (BonusValue == 1) _decreaseBonus = false; } } }