53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
using SepCore.Definition;
|
|
|
|
namespace SepCore.Entity
|
|
{
|
|
/// <summary>
|
|
/// 武器状态机。负责状态注册、切换与驱动。
|
|
/// </summary>
|
|
public class WeaponStateMachine
|
|
{
|
|
private readonly WeaponBase _weapon;
|
|
private readonly Dictionary<WeaponStateType, WeaponStateBase> _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;
|
|
}
|
|
}
|
|
}
|