rabidus-test/Assets/Scripts/BonusController.cs

72 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class BonusController : MonoBehaviour, ITextChangable
{
public UnityEvent<object> OnTextChange => _OnTextChange;
private UnityEvent<object> _OnTextChange = new UnityEvent<object>();
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.ToString("F2"));
if (BonusValue == 1)
_decreaseBonus = false;
}
}
}