mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-05 07:41:10 +00:00
feat: add state machine
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5636415f9a840ab8ff88d96719179ec
|
||||
timeCreated: 1772456820
|
||||
@@ -0,0 +1,18 @@
|
||||
using Assets._Project.Develop.Runtime.Infrastructure.DI;
|
||||
using Assets._Project.Develop.Runtime.Utilities.CoroutinesManagement;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.Timer
|
||||
{
|
||||
public class TimerServiceFactory
|
||||
{
|
||||
private readonly DIContainer _container;
|
||||
|
||||
public TimerServiceFactory(DIContainer container)
|
||||
{
|
||||
_container = container;
|
||||
}
|
||||
|
||||
public TimerService Create(float cooldown)
|
||||
=> new TimerService(cooldown, _container.Resolve<ICoroutinesPerformer>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6fbab9cab89b4ad094fc636facb8597a
|
||||
timeCreated: 1772456820
|
||||
Reference in New Issue
Block a user