using System;
namespace SK.Framework
{
///
/// 状态构建器
///
/// 状态类型
public class StateBuilder where T : State, new()
{
//构建的状态
private readonly T state;
//构建的状态所属的状态机
private readonly StateMachine stateMachine;
///
/// 构造函数
///
///
///
public StateBuilder(T state, StateMachine stateMachine)
{
this.state = state;
this.stateMachine = stateMachine;
}
///
/// 设置状态初始化事件
///
/// 状态初始化事件
/// 状态构建器
public StateBuilder OnInitialization(Action onInitialization)
{
state.onInitialization = () => onInitialization(state);
return this;
}
///
/// 设置状态进入事件
///
/// 状态进入事件
/// 状态构建器
public StateBuilder OnEnter(Action onEnter)
{
state.onEnter = () => onEnter(state);
return this;
}
///
/// 设置状态停留事件
///
/// 状态停留事件
/// 状态构建器
public StateBuilder OnStay(Action onStay)
{
state.onStay = () => onStay(state);
return this;
}
///
/// 设置状态退出事件
///
/// 状态退出事件
/// 状态构建器
public StateBuilder OnExit(Action onExit)
{
state.onExit = () => onExit(state);
return this;
}
///
/// 设置状态终止事件
///
/// 状态终止事件
/// 状态构建器
public StateBuilder OnTermination(Action onTermination)
{
state.onTermination = () => onTermination(state);
return this;
}
///
/// 设置状态切换条件
///
/// 切换条件
/// 目标状态名称
/// 状态构建器
public StateBuilder SwitchWhen(Func predicate, string targetStateName)
{
state.SwitchWhen(predicate, targetStateName);
return this;
}
///
/// 构建完成
///
/// 状态机
public StateMachine Complete()
{
state.OnInitialization();
return stateMachine;
}
}
}