yyl
2025-09-15 6a10188f3eddc740b9b8b1e7eefb0e2fdb3850e3
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System.Collections.Generic;
using System;
using UnityEngine;
 
public class RecordPlayer
{
    protected BattleField battleField;
 
    private Queue<RecordAction> recordActionQueue = new Queue<RecordAction>();
    protected RecordAction currentRecordAction;
 
    protected List<RecordAction> immediatelyActionList = new List<RecordAction>();
 
    private bool isWaitingNextAction = false;
    private float waitTimer = 0f;
    private const float waitInterval = 0.2f;
 
    private float speedRatio = 1.5f;
 
    public void Init(BattleField _battleField)
    {
        Release();
        battleField = _battleField;
    }
 
    public bool IsPlaying()
    {
        return currentRecordAction != null || recordActionQueue.Count > 0 || immediatelyActionList.Count > 0;
    }
 
    public void PlayRecord(RecordAction recordAction)
    {
        // BattleDebug.LogError("Enqueue record action " + recordAction.GetType());
        recordActionQueue.Enqueue(recordAction);
    }
 
    public void PlayRecord(List<RecordAction> recordActions)
    {
        for (int i = 0; i < recordActions.Count; i++)
        {
            PlayRecord(recordActions[i]);
        }
    }
 
    public void InsertRecord(RecordAction recordAction)
    {
        // BattleDebug.LogError("Insert record action " + recordAction.GetType());
        if (currentRecordAction != null)
        {
            Queue<RecordAction> tempQueue = new Queue<RecordAction>();
            tempQueue.Enqueue(recordAction);
            while (recordActionQueue.Count > 0)
            {
                tempQueue.Enqueue(recordActionQueue.Dequeue());
            }
            recordActionQueue = tempQueue;
        }
        else
        {
            recordActionQueue.Enqueue(recordAction);
        }
    }
 
    public void ImmediatelyPlay(RecordAction recordAction)
    {
        immediatelyActionList.Add(recordAction);
    }
 
    protected void ImmediatelyPlayRun()
    {
        if (immediatelyActionList.Count > 0)
        {
            List<int> removeIndexList = new List<int>();
 
            for (int i = immediatelyActionList.Count - 1; i >= 0; i--)
            {
                var action = immediatelyActionList[i];
                if (!action.IsFinished())
                {
                    action.Run();
                    continue;
                }
 
                removeIndexList.Add(i);
            }
 
            for (int i = removeIndexList.Count - 1; i >= 0; i--)
            {
                immediatelyActionList.RemoveAt(removeIndexList[i]);
            }
        }
    }
 
    public virtual void Run()
    {
        ImmediatelyPlayRun();
 
        // 等待下一个action
        if (isWaitingNextAction)
        {
            waitTimer += Time.deltaTime * speedRatio;
            if (waitTimer >= waitInterval)
            {
                isWaitingNextAction = false;
                waitTimer = 0f;
            }
            else
            {
                return;
            }
        }
 
        if (currentRecordAction == null)
        {
            if (recordActionQueue.Count <= 0)
            {
                return;
            }
        }
 
        if (currentRecordAction != null && !currentRecordAction.IsFinished())
        {
            currentRecordAction.Run();
            return;
        }
 
        if (currentRecordAction != null && currentRecordAction.IsFinished())
        {
            // BattleDebug.LogError("record action " + currentRecordAction.GetType() + " play finished");
            currentRecordAction = null;
            isWaitingNextAction = true;
            waitTimer = 0f;
            return;
        }
 
        if (currentRecordAction == null)
        {
            if (recordActionQueue.Count > 0)
            {
                currentRecordAction = recordActionQueue.Dequeue();
                // BattleDebug.LogError("play record action " + currentRecordAction.GetType());
            }
        }
    }
 
    //  先预留 感觉用的上
    public virtual void ResumeGame()
    {
 
    }
 
    public virtual void PauseGame()
    {
 
    }
 
    public void HaveRest()
    {
        for (int i = 0; i < immediatelyActionList.Count; i++)
        {
            immediatelyActionList[i].ForceFinish();
        }
 
        immediatelyActionList.Clear();
 
        if (currentRecordAction != null)
        {
            currentRecordAction.ForceFinish();
        }
        currentRecordAction = null;
 
        while (recordActionQueue.Count > 0)
        {
            recordActionQueue.Dequeue().ForceFinish();
        }
    }
 
    public void Release()
    {
        if (null != currentRecordAction)
        {
            currentRecordAction.ForceFinish();
        }
        currentRecordAction = null;
        recordActionQueue.Clear();
        immediatelyActionList.Clear();
    }
 
    public void SetSpeedRatio(float ratio)
    {
        speedRatio = ratio;
    }
}