using System.Collections.Generic;
using ColdMint.scripts.debug;
namespace ColdMint.scripts.stateMachine;
///
/// State machine template
/// 状态机模板
///
public abstract class StateMachineTemplate : IStateMachine
{
private StateContext? _context;
private IStateProcessor? _activeStatusrocessor;
public StateContext? Context
{
get => _context;
set
{
if (value == null)
{
return;
}
if (_context != null)
{
_context.OnStateChange -= OnStateChange;
}
_context = value;
value.OnStateChange += OnStateChange;
}
}
private bool _isRunning;
private Dictionary? _processors;
public bool IsRunning => _isRunning;
///
/// When the state in the context changes
/// 当上下文内的状态改变时
///
///
///
private void OnStateChange(State oldState, State newState)
{
if (_context == null)
{
LogCat.LogError("state_machine_does_not_specify_context", label: LogCat.LogLabel.StateMachineTemplate);
return;
}
if (_processors == null)
{
LogCat.LogError("state_machine_does_not_specify_processor", label: LogCat.LogLabel.StateMachineTemplate);
return;
}
if (_processors.TryGetValue(oldState, out var processor))
{
processor.Exit(_context);
}
if (_processors.TryGetValue(newState, out processor))
{
processor.Enter(_context);
_activeStatusrocessor = processor;
}
else
{
LogCat.LogErrorWithFormat("state_processor_not_found", label: LogCat.LogLabel.StateMachineTemplate,
LogCat.UploadFormat,newState);
}
}
///
/// Registration status handler
/// 注册状态处理器
///
///
protected void RegisterProcessor(IStateProcessor processor)
{
_processors ??= new Dictionary();
if (!_processors.TryAdd(processor.State, processor))
{
LogCat.LogError("state_processor_already_registered", label: LogCat.LogLabel.StateMachineTemplate);
}
}
public void Start()
{
if (_isRunning)
{
LogCat.LogError("try_to_open_state_machine_that_is_on", label: LogCat.LogLabel.StateMachineTemplate);
return;
}
if (Context == null)
{
LogCat.LogError("state_machine_does_not_specify_context", label: LogCat.LogLabel.StateMachineTemplate);
return;
}
OnStart(Context);
_activeStatusrocessor = _processors?[Context.CurrentState];
_isRunning = true;
}
///
/// When the state machine is turned on
/// 在状态机开启时
///
///
///Register the status handler in this method.
///请在此方法内注册状态处理器。
///
protected abstract void OnStart(StateContext context);
///
/// When the state switch is off
/// 在状态机关闭时
///
protected virtual void OnStop()
{
}
public void Stop()
{
if (!_isRunning)
{
return;
}
_isRunning = false;
OnStop();
}
public void Execute()
{
if (!_isRunning)
{
return;
}
if (Context == null)
{
LogCat.LogError("state_machine_does_not_specify_context", label: LogCat.LogLabel.StateMachineTemplate);
return;
}
if (_activeStatusrocessor == null)
{
LogCat.LogError("state_machine_does_not_specify_active_processor",
label: LogCat.LogLabel.StateMachineTemplate);
return;
}
_activeStatusrocessor.Execute(Context);
}
}