using System.Collections.Generic; using System.Linq; using UnityEngine; using BaseGames.Core.Events; namespace BaseGames.Core { /// /// 状态机核心,持有状态注册表与当前状态。 /// GameManager 持有一个实例;状态对象在 Awake 注入。 /// public class GameStateMachine { private readonly Dictionary _states = new(); private IGameState _current; public GameStateId CurrentStateId => _current?.Id ?? default; /// 注册状态实现(同 Id 注册多次以最后一次为准)。 public void Register(IGameState state) => _states[state.Id] = state; /// 转换到目标状态。失败时返回 false 并填充 error 字符串。 public bool TransitionTo(GameStateId nextId, out string error) { if (!_states.TryGetValue(nextId, out var next)) { error = $"[GameStateMachine] 未知状态 '{nextId}'"; return false; } if (_current != null && !_current.ValidNextStates.Contains(nextId)) { error = $"[GameStateMachine] 非法转换 {_current.Id} → {nextId}"; return false; } var prev = _current?.Id ?? default; _current?.OnExit(nextId); _current = next; _current.OnEnter(prev); error = null; return true; } public void Tick(float deltaTime) => _current?.Tick(deltaTime); } }