hch
7 天以前 cb4ec28d83ba847f362392936b20e52e17b03081
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
119
120
121
122
123
124
125
126
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Cysharp.Threading.Tasks;
 
public class DamageContent : MonoBehaviour, IBattleFloatingUI
{
    public GameObject line;
    public RectTransform parent;
 
    [Header("Floating Config")]
    [Tooltip("请在Inspector中拖拽FloatingConfig资源")]
    public FloatingConfig floatingConfig;
 
    protected List<DamageLine> damageLineList = new List<DamageLine>();
    private BattleDmgInfo battleDmgInfo;
    private BattleFloatingUIController controller;
 
    void Awake()
    {
        line.SetActive(false);
    }
 
    private void InitController()
    {
        if (controller != null) return;
 
        if (floatingConfig == null)
        {
            Debug.LogError($"[DamageContent] FloatingConfig 未配置,请在Inspector中拖拽赋值! GameObject: {gameObject.name}");
            return;
        }
 
        RectTransform rectTransform = GetComponent<RectTransform>();
        controller = new BattleFloatingUIController(rectTransform, gameObject, ApplyColor, floatingConfig);
    }
 
    public void SetRatio(float speed, float scale)
    {
        InitController();
        controller?.SetRatio(speed, scale);
    }
 
    /// <summary>
    /// 设置飘字的起点和终点位置(运行时动态设置)
    /// </summary>
    public void SetPosition(Vector2 beginPos, Vector2 endPos)
    {
        InitController();
        controller?.SetRuntimePosition(beginPos, endPos);
    }
 
    public async void SetDamage(BattleDmgInfo _damageInfo, Action _onComplete)
    {
        battleDmgInfo = _damageInfo;
 
        var damages = battleDmgInfo.battleDamageList;
 
        for (int i = damages.Count; i < damageLineList.Count; i++)
        {
            damageLineList[i].SetActive(false);
        }
 
        // 使用控制器的Play方法
        bool isCrit = battleDmgInfo.IsCrit();
        Play(isCrit, _onComplete);
 
        for (int i = 0; i < damages.Count; i++)
        {
            if (i >= damageLineList.Count)
            {
                GameObject newLine = GameObject.Instantiate(line, parent);
                damageLineList.Add(newLine.GetComponent<DamageLine>());
            }
            damageLineList[i].SetActive(true);
            damageLineList[i].SetDamage(damages[i]);
            await UniTask.Delay(100);
        }
    }
 
    public void Play(bool isCrit, Action onComplete = null)
    {
        InitController();
        controller?.Play(isCrit, onComplete);
    }
 
    public void Run()
    {
        if (controller == null) return;
        controller.Run();
    }
 
    public void Stop()
    {
        if (controller == null) return;
        controller.Stop();
    }
 
    public void Resume()
    {
        if (controller == null) return;
        controller.Resume();
    }
 
    private void ApplyColor(Color color)
    {
        for (int i = 0; i < damageLineList.Count; i++)
        {
            if (damageLineList[i].gameObject.activeSelf)
            {
                damageLineList[i].SetColor(color);
            }
        }
    }
 
    // 运行时更新配置
    public void SetFloatingConfig(FloatingConfig config)
    {
        floatingConfig = config;
        if (controller != null)
        {
            controller.SetConfig(config);
        }
    }
}