77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace SLSUtilities.Feedback
|
|
{
|
|
/// <summary>
|
|
/// 后处理震动实例的运行时状态。
|
|
/// 由 Shaker 维护,支持多个实例的叠加混合。
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前震动是否已结束。
|
|
/// </summary>
|
|
public bool IsFinished => timer >= duration;
|
|
}
|
|
|
|
public static class ShakeInstanceListExtensions
|
|
{
|
|
public static void AddWithContext<T>(this List<T> list, T instance, FeedbackContext context)
|
|
where T : ShakeInstanceBase
|
|
{
|
|
if (instance == null) return;
|
|
instance.BindContext(context);
|
|
list.Add(instance);
|
|
}
|
|
|
|
public static bool StopMatching<T>(this List<T> list, FeedbackContext context)
|
|
where T : ShakeInstanceBase
|
|
{
|
|
if (list == null) return false;
|
|
|
|
int removed = list.RemoveAll(instance => instance != null && instance.Matches(context));
|
|
return removed > 0;
|
|
}
|
|
}
|
|
}
|