Files
aibis-dream/Assets/Scripts/FixSystemNew/FixStateMachine.cs
T

87 lines
3.1 KiB
C#

using System.Collections;
using AibisDream.Framework;
namespace AibisDream.FixSystem
{
public class FixStateMachine
{
public FixState CurState => _stateMachine.CurState;
private StateMachine<FixState> _stateMachine;
private FixSystemData _data;
public FixStateMachine(FixSystemData data)
{
_data = data;
_stateMachine = new StateMachine<FixState>(CreateState);
_stateMachine.OnStateSwitch += state => EnumEventSystem.Global.Send(EventEnum.FixStateSwitch, state);
}
public IEnumerator Init(FixState initState)
{
_data.curState = initState;
_data.args = "";
yield return _stateMachine.Init(initState);
}
public IEnumerator SwitchState(FixState nextState, string args = "")
{
_data.curState = nextState;
_data.args = args;
yield return _stateMachine.SwitchState(nextState, args);
}
/// <summary>
/// 读档专用:跳过动画直接切换状态。不触发 Exit/Enter 动画,只执行 EnterImmediate。
/// 通过 <see cref="StateMachine{FixState}.SetStateImmediate"/> 同步 _stateMachine 内部状态,避免与正常 SwitchState 分叉。
/// </summary>
public IEnumerator SwitchStateImmediate(FixState nextState, string args = "")
{
if (_stateMachine.HasState && CurState.Equals(nextState)) yield break;
_stateMachine.SetStateImmediate(nextState, args);
_data.curState = nextState;
_data.args = args;
if (_stateMachine.CurrentStateObj is IFixState fixState)
yield return fixState.EnterImmediate();
else
yield return _stateMachine.CurrentStateObj.Enter();
}
public IEnumerator QuitState()
{
_data.curState = FixState.Default;
yield return _stateMachine.QuitState();
}
private static IState<FixState> CreateState(FixState nextState, string args)
{
IState<FixState> res = nextState switch
{
FixState.Default => new DefaultState(),
FixState.Clinic => new ClinicState(),
FixState.BodyModule => new BodyModuleState(),
FixState.Memory => new MemoryState(),
FixState.Eye => new EyeState(),
FixState.UF => new UfState(),
FixState.Expression => new ExpressionState(),
FixState.Emo => new EmoState(),
FixState.Op => new OpState(),
FixState.EmoCut => new EmoCutState(),
FixState.MemoryCut => new MemoryCutState(),
FixState.LogicCut => new LogicCutState(),
FixState.Dream => new DreamState(),
FixState.AnalysisMode => new AnalysisModeState(),
FixState.Sales => new SalesState(),
FixState.BlockPuzzle => new BlockPuzzleState(),
_ => new DefaultState()
};
res.SetArgs(args);
return res;
}
}
}