三国卡牌客户端基础资源仓库
yyl
2025-05-13 e30e257ab8fcec3161337db973d0d1e6f2ce702e
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System;
 
/// <summary>
/// 启动状态机
/// </summary>
public class LaunchStateMachine
{
    // 状态列表
    private List<ILaunchState> states = new List<ILaunchState>();
    
    // 当前状态索引
    private int currentStateIndex = -1;
    
    // 当前状态
    private ILaunchState CurrentState => currentStateIndex >= 0 && currentStateIndex < states.Count ? states[currentStateIndex] : null;
    
    // 状态机是否正在运行
    private bool isRunning = false;
    
    // 状态机是否已完成
    private bool isCompleted = false;
    
    // 状态机完成事件
    public event Action OnCompleted;
    
    /// <summary>
    /// 添加状态
    /// </summary>
    /// <param name="state">状态</param>
    public void AddState(ILaunchState state)
    {
        if (state == null)
        {
            Debug.LogError("添加的状态为空");
            return;
        }
        
        states.Add(state);
        Debug.Log($"添加状态: {state.StateName}");
    }
    
    /// <summary>
    /// 启动状态机
    /// </summary>
    public async UniTask Start()
    {
        if (isRunning)
        {
            Debug.LogWarning("状态机已经在运行中");
            return;
        }
        
        if (states.Count == 0)
        {
            Debug.LogError("状态机中没有状态");
            return;
        }
        
        isRunning = true;
        isCompleted = false;
        currentStateIndex = 0;
        
        Debug.Log("启动状态机");
        
        // 进入第一个状态
        await EnterCurrentState();
        
        // 开始执行状态机
        await ExecuteStateMachine();
    }
    
    /// <summary>
    /// 停止状态机
    /// </summary>
    public async UniTask Stop()
    {
        if (!isRunning)
        {
            return;
        }
        
        // 退出当前状态
        if (CurrentState != null)
        {
            await CurrentState.OnExit();
        }
        
        isRunning = false;
        Debug.Log("停止状态机");
    }
    
    /// <summary>
    /// 进入当前状态
    /// </summary>
    private async UniTask EnterCurrentState()
    {
        if (CurrentState != null)
        {
            Debug.Log($"进入状态: {CurrentState.StateName}");
            await CurrentState.OnEnter();
        }
    }
    
    /// <summary>
    /// 执行状态机
    /// </summary>
    private async UniTask ExecuteStateMachine()
    {
        while (isRunning && currentStateIndex < states.Count)
        {
            // 执行当前状态
            bool moveToNext = await CurrentState.OnExecute();
            
            if (moveToNext)
            {
                // 退出当前状态
                await CurrentState.OnExit();
                
                // 移动到下一个状态
                currentStateIndex++;
                
                // 如果还有下一个状态,则进入
                if (currentStateIndex < states.Count)
                {
                    await EnterCurrentState();
                }
                else
                {
                    // 所有状态已执行完毕
                    isRunning = false;
                    isCompleted = true;
                    Debug.Log("状态机执行完毕");
                    OnCompleted?.Invoke();
                }
            }
            
            // 等待一帧,避免卡死
            await UniTask.Yield();
        }
    }
}