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 _actions = ListPool.Get(); private Sequence() { } private static readonly SimpleObjectPool 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(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 sequenceSetting) { var repeat = Kit.Sequence.Allocate(); sequenceSetting(repeat); return self.Append(repeat); } } }