yyl
8 天以前 4e41975b0c62fa10ab571b0c9ad0690754762b6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using UnityEngine;
using System;
 
/// <summary>
/// 战斗飘字UI控制器:处理缩放、透明度、位置动画逻辑
/// </summary>
[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 * 8f + 0.1f; // 7~8帧 (8/30=0.2667秒)
    public float totalShowTime = 1f / BattleConst.skillMotionFps * 24f + 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<Color> applyColorCallback;
    private GameObject gameObject;
 
    // 添加只读属性以对外暴露
    public float Timer => timer;
    public float SpeedRatio => speedRatio;
    public float ScaleRatio => scaleRatio;
 
    public BattleFloatingUIController(RectTransform rect, GameObject go, Action<Color> 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()
    {
        // 可以添加恢复逻辑
    }
}