2024-06-30 23:55:58 +00:00
|
|
|
|
using System;
|
|
|
|
|
using ColdMint.scripts.debug;
|
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.stateMachine;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Context of the state machine</para>
|
|
|
|
|
/// <para>状态机的上下文环境</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class StateContext
|
|
|
|
|
{
|
|
|
|
|
private State _currentState;
|
|
|
|
|
|
2024-07-06 14:55:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Previous state</para>
|
|
|
|
|
/// <para>前一个状态</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private State _previousState;
|
|
|
|
|
|
2024-06-30 23:55:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>The state context holds the current state</para>
|
|
|
|
|
/// <para>状态上下文持有当前状态</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public State CurrentState
|
|
|
|
|
{
|
|
|
|
|
get => _currentState;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_currentState == value)
|
|
|
|
|
{
|
2024-07-06 14:55:07 +00:00
|
|
|
|
LogCat.LogWarning("try_to_set_the_same_state", label: LogCat.LogLabel.StateContext);
|
2024-06-30 23:55:58 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 14:55:07 +00:00
|
|
|
|
LogCat.LogWithFormat("state_change", label: LogCat.LogLabel.StateContext, _currentState, value);
|
2024-06-30 23:55:58 +00:00
|
|
|
|
OnStateChange?.Invoke(_currentState, value);
|
2024-07-06 14:55:07 +00:00
|
|
|
|
_previousState = _currentState;
|
2024-06-30 23:55:58 +00:00
|
|
|
|
_currentState = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 14:55:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Previous state</para>
|
|
|
|
|
/// <para>前一个状态</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public State PreviousState
|
|
|
|
|
{
|
|
|
|
|
get => _previousState;
|
|
|
|
|
set => _previousState = value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-30 23:55:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>When the state changes</para>
|
|
|
|
|
/// <para>当状态改变时</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<State, State>? OnStateChange;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>owner</para>
|
|
|
|
|
/// <para>主人</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Node? Owner { get; set; }
|
|
|
|
|
}
|