Files
aibis-dream/Assets/Scripts/Framework/ActionKit/Action/Sequence.cs
T
2025-04-25 20:24:11 +08:00

149 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
namespace AibisDream.Kit
{
public interface ISequence : IAction
{
ISequence Append(IAction action);
}
internal class Sequence : ISequence
{
private IAction _currentAction;
private int _currentActionIndex;
private readonly List<IAction> _actions = ListPool<IAction>.Get();
private Sequence()
{
}
private static readonly SimpleObjectPool<Sequence> SimpleObjectPool = new(() => new Sequence(), null, 10);
public static Sequence Allocate()
{
var sequence = SimpleObjectPool.Allocate();
sequence.ActionID = ActionKit.IDGenerator++;
sequence.Reset();
sequence.IsFinalized = false;
return sequence;
}
public bool Paused { get; set; }
public bool IsFinalized { get; set; }
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
if (_actions.Count > 0)
{
_currentActionIndex = 0;
_currentAction = _actions[_currentActionIndex];
_currentAction.Reset();
TryExecuteUntilNextNotFinished();
}
else
{
this.Finish();
}
}
void TryExecuteUntilNextNotFinished()
{
while (_currentAction != null && _currentAction.Execute(0))
{
_currentActionIndex++;
if (_currentActionIndex < _actions.Count)
{
_currentAction = _actions[_currentActionIndex];
_currentAction.Reset();
}
else
{
_currentAction = null;
this.Finish();
}
}
}
public void OnExecute(float dt)
{
if (_currentAction != null)
{
if (_currentAction.Execute(dt))
{
_currentActionIndex++;
if (_currentActionIndex < _actions.Count)
{
_currentAction = _actions[_currentActionIndex];
_currentAction.Reset();
TryExecuteUntilNextNotFinished();
}
else
{
this.Finish();
}
}
}
else
{
this.Finish();
}
}
public void OnFinish()
{
}
public ISequence Append(IAction action)
{
_actions.Add(action);
return this;
}
public void Deinit()
{
if (!IsFinalized)
{
IsFinalized = true;
foreach (var action in _actions)
{
action.Deinit();
}
_actions.Clear();
ActionKitMonoBehaviourEvents.AddCallback(
new ActionQueueRecycleCallback<Sequence>(SimpleObjectPool, this));
}
}
public void Reset()
{
_currentActionIndex = 0;
Status = ActionStatus.NotStart;
Paused = false;
foreach (var action in _actions)
{
action.Reset();
}
}
}
public static class SequenceExtension
{
public static ISequence Sequence(this ISequence self, Action<ISequence> sequenceSetting)
{
var repeat = Kit.Sequence.Allocate();
sequenceSetting(repeat);
return self.Append(repeat);
}
}
}