107 lines
2.8 KiB
C#
107 lines
2.8 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using Debug = UnityEngine.Debug;
|
|
|
|
namespace AibisDream.Kit
|
|
{
|
|
internal class Delay : IAction
|
|
{
|
|
public float delayTime;
|
|
|
|
public Func<float> delayTimeFactory;
|
|
|
|
public Action OnDelayFinish { get; set; }
|
|
|
|
public float CurrentSeconds { get; set; }
|
|
|
|
private Delay()
|
|
{
|
|
}
|
|
|
|
private static readonly SimpleObjectPool<Delay> Pool = new(() => new Delay(), null, 10);
|
|
|
|
public static Delay Allocate(float delayTime, Action onDelayFinish = null)
|
|
{
|
|
var retNode = Pool.Allocate();
|
|
retNode.ActionID = ActionKit.IDGenerator++;
|
|
retNode.IsFinalized = false;
|
|
retNode.Reset();
|
|
retNode.delayTime = delayTime;
|
|
retNode.OnDelayFinish = onDelayFinish;
|
|
retNode.CurrentSeconds = 0.0f;
|
|
return retNode;
|
|
}
|
|
|
|
public static Delay Allocate(Func<float> delayTimeFactory, Action onDelayFinish = null)
|
|
{
|
|
var retNode = Pool.Allocate();
|
|
retNode.IsFinalized = false;
|
|
retNode.Reset();
|
|
retNode.delayTimeFactory = delayTimeFactory;
|
|
retNode.OnDelayFinish = onDelayFinish;
|
|
retNode.CurrentSeconds = 0.0f;
|
|
return retNode;
|
|
}
|
|
|
|
|
|
public ulong ActionID { get; set; }
|
|
public ActionStatus Status { get; set; }
|
|
|
|
public void OnStart()
|
|
{
|
|
if (delayTimeFactory != null)
|
|
{
|
|
delayTime = delayTimeFactory();
|
|
}
|
|
}
|
|
|
|
public void OnExecute(float dt)
|
|
{
|
|
if (CurrentSeconds >= delayTime)
|
|
{
|
|
this.Finish();
|
|
OnDelayFinish?.Invoke();
|
|
}
|
|
|
|
CurrentSeconds += dt;
|
|
}
|
|
|
|
public void OnFinish()
|
|
{
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
Status = ActionStatus.NotStart;
|
|
Paused = false;
|
|
CurrentSeconds = 0.0f;
|
|
}
|
|
|
|
public bool Paused { get; set; }
|
|
|
|
public void Deinit()
|
|
{
|
|
if (!IsFinalized)
|
|
{
|
|
OnDelayFinish = null;
|
|
IsFinalized = true;
|
|
ActionKitMonoBehaviourEvents.AddCallback(new ActionQueueRecycleCallback<Delay>(Pool, this));
|
|
}
|
|
}
|
|
|
|
public bool IsFinalized { get; set; }
|
|
}
|
|
|
|
public static class DelayExtension
|
|
{
|
|
public static ISequence Delay(this ISequence self, float seconds, Action onDelayFinish = null)
|
|
{
|
|
return self.Append(Kit.Delay.Allocate(seconds, onDelayFinish));
|
|
}
|
|
|
|
public static ISequence Delay(this ISequence self, Func<float> delayTimeFactory, Action onDelayFinish = null)
|
|
{
|
|
return self.Append(Kit.Delay.Allocate(delayTimeFactory, onDelayFinish));
|
|
}
|
|
}
|
|
} |