50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
namespace BrewMonster
|
|
{
|
|
public class GameSpeedModifier : MonoBehaviour
|
|
{
|
|
[SerializeField] private Button _speedUpButton;
|
|
[SerializeField] private Button _speedDownButton;
|
|
[SerializeField] private TextMeshProUGUI _gameSpeedText;
|
|
|
|
private readonly float[] _speedStages = { 0.25f, 0.5f, 0.75f, 1f, 2f };
|
|
private int _currentSpeedIndex = 3; // Default = 1x
|
|
private void Awake()
|
|
{
|
|
_speedUpButton.onClick.AddListener(OnSpeedUpButtonClicked);
|
|
_speedDownButton.onClick.AddListener(OnSpeedDownButtonClicked);
|
|
_currentSpeedIndex = 3; // Default = 1x
|
|
Time.timeScale = 1.0f;
|
|
ApplySpeed();
|
|
}
|
|
|
|
private void OnSpeedUpButtonClicked()
|
|
{
|
|
if (_currentSpeedIndex >= _speedStages.Length - 1)
|
|
return;
|
|
|
|
_currentSpeedIndex++;
|
|
ApplySpeed();
|
|
}
|
|
|
|
private void OnSpeedDownButtonClicked()
|
|
{
|
|
if (_currentSpeedIndex <= 0)
|
|
return;
|
|
|
|
_currentSpeedIndex--;
|
|
ApplySpeed();
|
|
}
|
|
|
|
private void ApplySpeed()
|
|
{
|
|
float speed = _speedStages[_currentSpeedIndex];
|
|
|
|
Time.timeScale = speed;
|
|
_gameSpeedText.text = speed.ToString("0.##X");
|
|
}
|
|
}
|
|
} |