mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-02 14:29:23 +00:00
68 lines
1.8 KiB
C#
68 lines
1.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using _Project.Develop.Runtime.Utils.ReactiveManagement;
|
|
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
|
|
using Assets._Project.Develop.Runtime.Utilities.CoroutinesManagement;
|
|
using UnityEngine;
|
|
|
|
namespace Assets._Project.Develop.Runtime.Utilities.Timer
|
|
{
|
|
public class TimerService : IDisposable
|
|
{
|
|
private float _cooldown;
|
|
|
|
private ReactiveEvent _cooldownEnded;
|
|
|
|
private ReactiveVariable<float> _currentTime;
|
|
|
|
private ICoroutinesPerformer _coroutinePerformer;
|
|
private Coroutine _cooldownProcess;
|
|
|
|
public TimerService(
|
|
float cooldown,
|
|
ICoroutinesPerformer coroutinePerformer)
|
|
{
|
|
_cooldown = cooldown;
|
|
_coroutinePerformer = coroutinePerformer;
|
|
|
|
_cooldownEnded = new ReactiveEvent();
|
|
_currentTime = new ReactiveVariable<float>();
|
|
}
|
|
|
|
public IReadOnlyEvent CooldownEnded => _cooldownEnded;
|
|
public IReadOnlyVariable<float> CurrentTime => _currentTime;
|
|
public bool IsOver => _currentTime.Value <= 0;
|
|
|
|
public void Dispose()
|
|
{
|
|
Stop();
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (_cooldownProcess != null)
|
|
_coroutinePerformer.StopPerform(_cooldownProcess);
|
|
}
|
|
|
|
public void Restart()
|
|
{
|
|
Stop();
|
|
|
|
_cooldownProcess = _coroutinePerformer.StartPerform(CooldownProcess());
|
|
}
|
|
|
|
private IEnumerator CooldownProcess()
|
|
{
|
|
_currentTime.Value = _cooldown;
|
|
|
|
while (IsOver == false)
|
|
{
|
|
_currentTime.Value -= Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
_cooldownEnded.Invoke();
|
|
}
|
|
}
|
|
}
|