mirror of
https://github.com/ryzij/Cube-Tower-Defense.git
synced 2026-07-14 01:56:57 +00:00
Добавил таймер для автоматического переключения волн
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class TurretsBuildTimer : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameManager _gameManager;
|
||||
[SerializeField] private float _buildTime = 10f;
|
||||
[SerializeField] private UnityEvent<OnTickEventArgs> _onTick;
|
||||
|
||||
public event UnityAction<OnTickEventArgs> OnTick
|
||||
{
|
||||
add => _onTick.AddListener(value);
|
||||
remove => _onTick.RemoveListener(value);
|
||||
}
|
||||
|
||||
private static readonly WaitForSeconds _wait = new(1f);
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_gameManager.OnStateChanged += OnStateChanged;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_gameManager.OnStateChanged -= OnStateChanged;
|
||||
}
|
||||
|
||||
private void OnStateChanged(GameManager.GameState newState)
|
||||
{
|
||||
if (newState == GameManager.GameState.BuildingPath)
|
||||
return;
|
||||
|
||||
StopCoroutine(nameof(Timer));
|
||||
if (newState == GameManager.GameState.BuildingTurrets)
|
||||
StartCoroutine(nameof(Timer));
|
||||
}
|
||||
|
||||
private IEnumerator Timer()
|
||||
{
|
||||
for (float i = 0; i < _buildTime; i++)
|
||||
{
|
||||
_onTick?.Invoke(new OnTickEventArgs
|
||||
{
|
||||
ElapsedSeconds = i,
|
||||
TotalSeconds = _buildTime
|
||||
});
|
||||
yield return _wait;
|
||||
}
|
||||
|
||||
_gameManager.NextLevel();
|
||||
}
|
||||
|
||||
public class OnTickEventArgs
|
||||
{
|
||||
public float SecondsLeft => TotalSeconds - ElapsedSeconds;
|
||||
public float ElapsedSeconds { get; set; }
|
||||
public float TotalSeconds { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 330d868d3d6a72c489dce65e1ca03edc
|
||||
Reference in New Issue
Block a user