using System; namespace AibisDream.Kit { public interface IRepeat : ISequence { } public class Repeat : IRepeat { private Sequence _sequence; private int _repeatCount = -1; private int _currentRepeatCount; private static readonly SimpleObjectPool SimpleObjectPool = new(() => new Repeat(), null, 5); private Repeat() { } public static Repeat Allocate(int repeatCount = -1) { var repeat = SimpleObjectPool.Allocate(); repeat.ActionID = ActionKit.IDGenerator++; repeat._sequence = Sequence.Allocate(); repeat.IsFinalized = false; repeat.Reset(); repeat._repeatCount = repeatCount; return repeat; } public bool Paused { get; set; } public bool IsFinalized { get; set; } public ulong ActionID { get; set; } public ActionStatus Status { get; set; } public void OnStart() { _currentRepeatCount = 0; } public void OnExecute(float dt) { if (_repeatCount == -1 || _repeatCount == 0) { if (_sequence.Execute(dt)) { _sequence.Reset(); } } else if (_currentRepeatCount < _repeatCount) { if (_sequence.Execute(dt)) { _currentRepeatCount++; if (_currentRepeatCount >= _repeatCount) { this.Finish(); } else { _sequence.Reset(); } } } } public void OnFinish() { } public ISequence Append(IAction action) { _sequence.Append(action); return this; } public void Deinit() { if (!IsFinalized) { IsFinalized = true; _sequence.Deinit(); ActionKitMonoBehaviourEvents.AddCallback( new ActionQueueRecycleCallback(SimpleObjectPool, this)); } } public void Reset() { _currentRepeatCount = 0; Status = ActionStatus.NotStart; Paused = false; _sequence.Reset(); } } public static class RepeatExtension { public static ISequence Repeat(this ISequence self, Action repeatSetting) { var repeat = Kit.Repeat.Allocate(); repeatSetting(repeat); return self.Append(repeat); } public static ISequence Repeat(this ISequence self, int repeatCount, Action repeatSetting) { var repeat = Kit.Repeat.Allocate(repeatCount); repeatSetting(repeat); return self.Append(repeat); } } }