Files
Cielonos/Assets/Scripts/MainGame/Characters/Base/Subcontrollers/FeedbackSubcontroller.cs

151 lines
5.2 KiB
C#
Raw Normal View History

2025-11-25 08:19:33 -05:00
using System.Collections.Generic;
2026-04-12 02:11:15 -04:00
using Cielonos.MainGame.Effects.Feedback;
2026-02-13 09:22:11 -05:00
using Lean.Pool;
using MoreMountains.Feedbacks;
using MoreMountains.FeedbacksForThirdParty;
2026-04-12 02:11:15 -04:00
using SLSUtilities.Feedback;
2026-02-13 09:22:11 -05:00
using SLSUtilities.FeelAssistance;
2025-11-25 08:19:33 -05:00
using UnityEngine;
namespace Cielonos.MainGame.Characters
{
2026-02-13 09:22:11 -05:00
public partial class FeedbackSubcontroller : SubcontrollerBase<CharacterBase>
2025-11-25 08:19:33 -05:00
{
2026-04-12 02:11:15 -04:00
// === 旧系统Feel—— 保留向后兼容 ===
2025-11-25 08:19:33 -05:00
public Dictionary<string, FeedbackUnit> feedbacks;
2026-03-20 12:07:44 -04:00
public FeedbackUnit this[string feedbackName] => feedbacks?.GetValueOrDefault(feedbackName, null);
2025-11-25 08:19:33 -05:00
2026-04-12 02:11:15 -04:00
// === 新系统Feedback System===
public FeedbackDataCollection feedbackDataCollection;
private CharacterFeedbackTimeProvider _timeProvider;
private readonly List<FeedbackPlayer> _activePlayers = new List<FeedbackPlayer>(8);
public override void Initialize()
{
base.Initialize();
if (owner != null)
{
_timeProvider = new CharacterFeedbackTimeProvider(owner);
}
}
/// <summary>
/// 通过新系统播放一个 FeedbackData。
/// </summary>
public FeedbackPlayer PlayFeedback(FeedbackData data, bool stopPrevious = false)
{
if (data == null) return null;
if (stopPrevious)
{
StopFeedback(data);
}
var player = new FeedbackPlayer(data, _timeProvider, owner?.transform);
player.Play();
_activePlayers.Add(player);
return player;
}
/// <summary>
/// 通过名称从 FeedbackDataCollection 中查找并播放。
/// </summary>
public FeedbackPlayer PlayFeedback(string name, bool stopPrevious = false)
{
if (feedbackDataCollection == null)
{
Debug.LogWarning($"[FeedbackSubcontroller] feedbackDataCollection is null on {owner?.name}.");
return null;
}
if (!feedbackDataCollection.TryGet(name, out FeedbackData data))
{
Debug.LogWarning($"[FeedbackSubcontroller] FeedbackData '{name}' not found on {owner?.name}.");
return null;
}
return PlayFeedback(data, stopPrevious);
}
/// <summary>
/// 停止指定 FeedbackData 的所有活跃播放器。
/// </summary>
public void StopFeedback(FeedbackData data)
{
for (int i = _activePlayers.Count - 1; i >= 0; i--)
{
if (_activePlayers[i].Data == data)
{
_activePlayers[i].Stop();
_activePlayers.RemoveAt(i);
}
}
}
/// <summary>
/// 停止所有活跃的新系统播放器。
/// </summary>
public void StopAllFeedbacks()
{
for (int i = _activePlayers.Count - 1; i >= 0; i--)
{
_activePlayers[i].Stop();
}
_activePlayers.Clear();
}
2025-11-25 08:19:33 -05:00
private void Update()
{
2026-04-12 02:11:15 -04:00
// 旧系统驱动
if (feedbacks != null)
2025-11-25 08:19:33 -05:00
{
2026-04-12 02:11:15 -04:00
foreach (var feedbackUnit in feedbacks.Values)
{
float timeScaleMultiplier = owner.selfTimeSm.TimeScale;
feedbackUnit.feedback.ExternalTimeScale = timeScaleMultiplier;
feedbackUnit.Update();
}
}
// 新系统驱动
float dt = Time.unscaledDeltaTime;
for (int i = _activePlayers.Count - 1; i >= 0; i--)
{
FeedbackPlayer player = _activePlayers[i];
player.Tick(dt);
if (player.IsCompleted || !player.IsActive)
{
_activePlayers.RemoveAt(i);
}
2025-11-25 08:19:33 -05:00
}
}
}
2026-02-13 09:22:11 -05:00
public partial class FeedbackSubcontroller
{
protected void Swing(Vector3 swingRotation, float rotationDuration, Vector3 swingPosition, float positionDuration)
{
MMF_Player swing = LeanPool.Spawn(MainGameBaseCollection.Instance.feedbackCollection["Swing"]).GetComponent<MMF_Player>();
MMF_CinemachineRotation cinemachineRotation = swing.GetFeedbackOfType<MMF_CinemachineRotation>();
if (cinemachineRotation != null)
{
cinemachineRotation.RotationAmplitude = swingRotation != default ? swingRotation : Vector3.zero;
cinemachineRotation.Duration = rotationDuration;
}
MMF_CinemachinePosition cinemachinePosition = swing.GetFeedbackOfType<MMF_CinemachinePosition>();
if (cinemachinePosition != null)
{
cinemachinePosition.PositionAmplitude = swingPosition != default ? swingPosition : Vector3.zero;
cinemachinePosition.Duration = positionDuration;
}
swing.Events.OnComplete.RemoveAllListeners();
swing.Events.OnComplete.AddListener(()=> LeanPool.Despawn(swing.gameObject));
swing.PlayFeedbacks();
}
}
2025-11-25 08:19:33 -05:00
}