using System.Collections.Generic; using SepCore.Definition; namespace SepCore.Entity { /// /// 武器状态机。负责状态注册、切换与驱动。 /// public class WeaponStateMachine { private readonly WeaponBase _weapon; private readonly Dictionary _states = new(); private WeaponStateBase _currentState; public WeaponStateMachine(WeaponBase weapon) { _weapon = weapon; } public bool IsBuilt => _states.Count > 0; public bool Contains(WeaponStateType stateType) { return _states.ContainsKey(stateType); } public void Register(WeaponStateBase state) { state.OnInit(_weapon); _states[state.State] = state; } public void Update(float elapseSeconds, float realElapseSeconds) { _currentState?.OnUpdate(elapseSeconds, realElapseSeconds); } public bool TryTransition(WeaponStateType stateType) { if (!_states.TryGetValue(stateType, out WeaponStateBase nextState)) { return false; } _currentState?.OnLeave(); _currentState = nextState; _currentState.OnEnter(); return true; } } }