Files
Fishing2/Assets/Scripts/Fishing/Player/Player.State.cs
2026-05-06 21:11:55 +08:00

84 lines
2.2 KiB
C#

using System.Collections.Generic;
namespace NBF
{
public partial class Player
{
#region
/// <summary>
/// 上一个状态
/// </summary>
public PlayerState PreviousState;
/// <summary>
/// 当前状态
/// </summary>
public PlayerState State;
/// <summary>
/// 状态参数
/// </summary>
public StateEnterParams StateParams;
private readonly Dictionary<PlayerState, PlayerStageViewBase> _stageViews =
new Dictionary<PlayerState, PlayerStageViewBase>();
private PlayerStageViewBase _currentStateView;
public PlayerStageViewBase CurrentStateView => _currentStateView;
private void InitState()
{
_stageViews.Add(PlayerState.Idle, new PlayerStageViewIdle());
_stageViews.Add(PlayerState.Prepare, new PlayerStageViewPrepare());
_stageViews.Add(PlayerState.Throw, new PlayerStageViewThrow());
_stageViews.Add(PlayerState.Fishing, new PlayerStageViewFishing());
_stageViews.Add(PlayerState.Fight, new PlayerStageViewFight());
foreach (var playerStageView in _stageViews.Values)
{
playerStageView.Init(this);
}
ChangeState(PlayerState.Idle);
}
public void UpdateState()
{
_currentStateView?.Update();
}
/// <summary>
/// 切换状态
/// </summary>
/// <param name="state"></param>
/// <param name="stateParams"></param>
public void ChangeState(PlayerState state, StateEnterParams stateParams = null)
{
if (state == State)
{
return;
}
PreviousState = State;
State = state;
StateParams = stateParams;
OnStageChange();
}
public void OnStageChange()
{
if (_currentStateView != null)
{
_currentStateView.Exit();
}
_currentStateView = _stageViews.GetValueOrDefault(State);
_currentStateView?.Enter(StateParams, PreviousState);
}
#endregion
}
}