rabidus-test/Assets/Scripts/BonusController.cs

72 lines
1.7 KiB
C#
Raw Permalink Normal View History

2023-08-22 15:41:12 +03:00
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);
2023-09-04 12:48:24 +03:00
_OnTextChange?.Invoke(BonusValue.ToString("F2"));
2023-08-22 15:41:12 +03:00
if (BonusValue == 1)
_decreaseBonus = false;
}
}
}