using System.Collections.Generic; using UnityEngine; namespace SLSUtilities.Feedback { /// /// 后处理震动实例的运行时状态。 /// 由 Shaker 维护,支持多个实例的叠加混合。 /// public class ShakeInstanceBase { public FeedbackTimeSettings timeSettings; public IFeedbackTimeProvider timeProvider; public float timer; public float duration; public FeedbackPlayer player; public int runtimeFeedbackId; public int trackIndex; public int clipIndex; public ShakeInstanceBase(FeedbackTimeSettings timeSettings, IFeedbackTimeProvider timeProvider, float duration) { this.timeSettings = timeSettings; this.timeProvider = timeProvider; this.duration = duration; timer = 0f; } public void BindContext(FeedbackContext context) { player = context.player; runtimeFeedbackId = context.runtimeFeedbackId; trackIndex = context.trackIndex; clipIndex = context.clipIndex; } public bool Matches(FeedbackContext context) { return player == context.player && runtimeFeedbackId == context.runtimeFeedbackId && trackIndex == context.trackIndex && clipIndex == context.clipIndex; } public void Tick() { IFeedbackTimeProvider provider = timeProvider ?? DefaultFeedbackTimeProvider.Instance; timer += provider.GetDeltaTime(timeSettings); } /// /// 当前震动是否已结束。 /// public bool IsFinished => timer >= duration; } public static class ShakeInstanceListExtensions { public static void AddWithContext(this List list, T instance, FeedbackContext context) where T : ShakeInstanceBase { if (instance == null) return; instance.BindContext(context); list.Add(instance); } public static bool StopMatching(this List list, FeedbackContext context) where T : ShakeInstanceBase { if (list == null) return false; int removed = list.RemoveAll(instance => instance != null && instance.Matches(context)); return removed > 0; } } }