81 lines
2.2 KiB
C#
81 lines
2.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.Kit
|
|
{
|
|
internal class DelayFrame : IAction
|
|
{
|
|
public bool Paused { get; set; }
|
|
public bool IsFinalized { get; set; }
|
|
public ulong ActionID { get; set; }
|
|
public ActionStatus Status { get; set; }
|
|
|
|
private static SimpleObjectPool<DelayFrame> _simpleObjectPool = new(() => new DelayFrame(), null, 10);
|
|
|
|
private Action _onDelayFinish;
|
|
|
|
public static DelayFrame Allocate(int frameCount, Action onDelayFinish = null)
|
|
{
|
|
var delayFrame = _simpleObjectPool.Allocate();
|
|
delayFrame.ActionID = ActionKit.IDGenerator++;
|
|
delayFrame.Reset();
|
|
delayFrame.IsFinalized = false;
|
|
delayFrame._delayedFrameCount = frameCount;
|
|
delayFrame._onDelayFinish = onDelayFinish;
|
|
|
|
return delayFrame;
|
|
}
|
|
|
|
private int _startFrameCount;
|
|
private int _delayedFrameCount;
|
|
|
|
public void OnStart()
|
|
{
|
|
_startFrameCount = Time.frameCount;
|
|
}
|
|
|
|
public void OnExecute(float dt)
|
|
{
|
|
if (Time.frameCount >= _startFrameCount + _delayedFrameCount)
|
|
{
|
|
_onDelayFinish?.Invoke();
|
|
this.Finish();
|
|
}
|
|
}
|
|
|
|
public void OnFinish()
|
|
{
|
|
}
|
|
|
|
public void Deinit()
|
|
{
|
|
if (!IsFinalized)
|
|
{
|
|
IsFinalized = true;
|
|
_onDelayFinish = null;
|
|
ActionKitMonoBehaviourEvents.AddCallback(
|
|
new ActionQueueRecycleCallback<DelayFrame>(_simpleObjectPool, this));
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
Status = ActionStatus.NotStart;
|
|
Paused = false;
|
|
_startFrameCount = 0;
|
|
}
|
|
}
|
|
|
|
public static class DelayFrameExtension
|
|
{
|
|
public static ISequence DelayFrame(this ISequence self, int frameCount)
|
|
{
|
|
return self.Append(Kit.DelayFrame.Allocate(frameCount));
|
|
}
|
|
|
|
public static ISequence NextFrame(this ISequence self)
|
|
{
|
|
return self.Append(Kit.DelayFrame.Allocate(1));
|
|
}
|
|
}
|
|
} |