Net.Project.B/Src/PlayerAction/PlayerActionService.cs

80 lines
2.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using BITKit;
using BITKit.Tween;
using Cysharp.Threading.Tasks;
using Unity.Mathematics;
namespace Net.Project.B.PlayerAction
{
public interface IPlayerAction
{
string Name { get; }
float Progress { get; }
float Duration { get;}
bool IsCancelable { get; }
UniTaskCompletionSource CompletionSource { get; }
public event Action<float> OnProgress;
UniTask StartAsync();
}
public class PlayerAction:IPlayerAction,IDisposable
{
public PlayerAction()
{
OnProgress += SetProgress;
}
private void SetProgress(float obj)
{
Progress = obj;
}
public string Name { get; set; } = "Busy";
public float Duration { get; set; } = 1;
public float Progress { get;private set; }
public bool IsCancelable { get; set; }
public UniTaskCompletionSource CompletionSource { get; } = new();
public event Action<float> OnProgress;
public async UniTask StartAsync()
{
try
{
await BITween.Lerp(OnProgress, 0f, 1f, Duration, math.lerp);
CompletionSource.TrySetResult();
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
}
public void Dispose()
{
CompletionSource.TrySetCanceled();
}
}
public interface IPlayerActionComponent
{
public event Action<IPlayerAction> OnPlayerActionStarted;
UniTask StartAction(IPlayerAction action);
}
public class PlayerActionComponent : IPlayerActionComponent
{
public event Action<IPlayerAction> OnPlayerActionStarted;
public async UniTask StartAction(IPlayerAction action)
{
OnPlayerActionStarted?.Invoke(action);
await action.StartAsync();
}
}
}