using UnityEngine;
using System;
///
/// 战斗飘字UI控制器:处理缩放、透明度、位置动画逻辑
///
[SerializeField]
public class BattleFloatingUIController
{
// Position Settings
public Vector2 beginPos = Vector2.zero;
public Vector2 endPos = new Vector2(0, 150);
// Time Settings
public float scaleChangeTime = 1f / BattleConst.skillMotionFps * 16f + 0.1f; // 7~8帧 (8/30=0.2667秒)
public float totalShowTime = 1f / BattleConst.skillMotionFps * 48f + 0.1f; // 总时间约24帧 (8+16=24帧)
// Normal Settings
public Vector3 normalBeginScale = new Vector3(2f, 2f, 2f);
public Vector3 normalEndScale = new Vector3(1f, 1f, 1f);
// Critical Settings
public Vector3 critBeginScale = new Vector3(3f, 3f, 3f);
public Vector3 critEndScale = new Vector3(1.5f, 1.5f, 1.5f);
// Color Settings
public Color beginColor = new Color(1f, 1f, 1f, 0.5f);
public Color endColor = new Color(1f, 1f, 1f, 1f);
private RectTransform rectTransform;
private float timer = 0f;
private float speedRatio = 1f;
private float scaleRatio = 1f;
private bool isCritical = false;
private Action onFinishCallback;
private Action applyColorCallback;
private GameObject gameObject;
// 添加只读属性以对外暴露
public float Timer => timer;
public float SpeedRatio => speedRatio;
public float ScaleRatio => scaleRatio;
public BattleFloatingUIController(RectTransform rect, GameObject go, Action applyColor)
{
rectTransform = rect;
gameObject = go;
applyColorCallback = applyColor;
}
public void SetRatio(float speed, float scale)
{
speedRatio = speed;
scaleRatio = scale;
}
public void Play(bool isCrit, Action onComplete = null)
{
isCritical = isCrit;
onFinishCallback = onComplete;
timer = 0f;
Vector3 beginScale = isCritical ? critBeginScale : normalBeginScale;
rectTransform.anchoredPosition = beginPos;
rectTransform.localScale = beginScale * scaleRatio;
gameObject.SetActive(true);
}
public void Run()
{
if (!gameObject.activeSelf)
return;
if (timer >= totalShowTime)
{
gameObject.SetActive(false);
onFinishCallback?.Invoke();
onFinishCallback = null;
return;
}
// 整个过程都往上飘
float moveProgress = timer / totalShowTime;
rectTransform.anchoredPosition = Vector2.Lerp(beginPos, endPos, moveProgress);
Vector3 currentBeginScale = isCritical ? critBeginScale : normalBeginScale;
Vector3 currentEndScale = isCritical ? critEndScale : normalEndScale;
// 阶段1: 7~8帧内缩放和透明度变化
if (timer < scaleChangeTime)
{
float scaleProgress = timer / scaleChangeTime;
rectTransform.localScale = Vector3.Lerp(currentBeginScale, currentEndScale, scaleProgress) * scaleRatio;
Color currentColor = Color.Lerp(beginColor, endColor, scaleProgress);
applyColorCallback?.Invoke(currentColor);
}
// 阶段2: 保持缩放和透明度,继续往上飘
else
{
rectTransform.localScale = currentEndScale * scaleRatio;
applyColorCallback?.Invoke(endColor);
}
timer += 1f / BattleConst.skillMotionFps * speedRatio;
}
public void Stop()
{
// 可以添加暂停逻辑
}
public void Resume()
{
// 可以添加恢复逻辑
}
}