/// <summary> /// 切换状态 /// </summary> /// <param name="type">状态类型</param> public void SwitchState(Type type) { if (_stateInstances.ContainsKey(type)) { if (CurrentState == _stateInstances[type]) { return; } FiniteStateBase lastState = CurrentState; FiniteStateBase nextState = _stateInstances[type]; if (lastState != null) { lastState.OnLeave(nextState); } nextState.OnEnter(lastState); CurrentState = nextState; AnyStateSwitchEvent?.Invoke(lastState, nextState); } else { Log.Warning("切换状态失败:有限状态机 " + Name + " 不存在状态 " + type.Name + "!"); } }
/// <summary> /// 切换状态 /// </summary> /// <param name="type">状态类型</param> public void SwitchState(Type type) { if (_stateInstances.ContainsKey(type)) { if (_currentState == _stateInstances[type]) { return; } FiniteStateBase lastState = _currentState; FiniteStateBase nextState = _stateInstances[type]; if (lastState != null) { lastState.OnLeave(nextState); } nextState.OnEnter(lastState); _currentState = nextState; AnyStateSwitchEvent?.Invoke(lastState, nextState); } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "切换状态失败:有限状态机 " + Name + " 不存在状态 " + type.Name + "!"); } }
/// <summary> /// 进行自动化任务(状态) /// </summary> private void DoAutomaticTaskOfState(Type type, FiniteStateBase finiteState) { if (_isAutomate) { FieldInfo[] fieldInfos = AutomaticTask.GetAutomaticFields(type); AutomaticTask.ApplyObjectPath(finiteState, fieldInfos); if (_isSupportedDataDriver) { AutomaticTask.ApplyDataBinding(finiteState, fieldInfos); } } }
/// <summary> /// 附加状态 /// </summary> /// <param name="type">状态类型</param> public void AppendState(Type type) { if (!_stateInstances.ContainsKey(type)) { FiniteStateBase state = Activator.CreateInstance(type) as FiniteStateBase; state.StateMachine = this; state.OnInit(); _stateInstances.Add(type, state); } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "附加状态失败:有限状态机 " + Name + " 已存在状态 " + type.Name + "!"); } }
/// <summary> /// 附加状态 /// </summary> /// <param name="type">状态类型</param> public void AppendState(Type type) { if (!_stateInstances.ContainsKey(type)) { FiniteStateBase state = Activator.CreateInstance(type) as FiniteStateBase; state.StateMachine = this; state.OnInit(); _stateInstances.Add(type, state); } else { Log.Warning("附加状态失败:有限状态机 " + Name + " 已存在状态 " + type.Name + "!"); } }
private void Start() { //进入默认状态 if (_defaultState != null) { if (_stateInstances.ContainsKey(_defaultState)) { _currentState = _stateInstances[_defaultState]; _currentState.OnEnter(null); } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "切换状态失败:有限状态机 " + Name + " 不存在状态 " + _defaultState.Name + "!"); } } }
private void Start() { if (_data != null) { _data.OnInit(); } foreach (var state in _stateInstances) { state.Value.OnInit(); } if (_defaultState != null) { if (_stateInstances.ContainsKey(_defaultState)) { _currentState = _stateInstances[_defaultState]; _currentState.OnEnter(null); } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "切换状态失败:有限状态机 " + Name + " 不存在状态 " + _defaultState.Name + "!"); } } }
private void Awake() { if (IsAutoRegister) { Main.m_FSM.RegisterFSM(this); } //加载数据类 if (Data != "<None>") { Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(Data); if (type != null) { if (type.IsSubclassOf(typeof(FSMDataBase))) { _data = Activator.CreateInstance(type) as FSMDataBase; _data.StateMachine = this; _data.OnInit(); } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "创建有限状态机数据类失败:数据类 " + Data + " 必须继承至有限状态机数据基类:FSMDataBase!"); } } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "创建有限状态机数据类失败:丢失数据类 " + Data + " !"); } } //加载所有状态 for (int i = 0; i < States.Count; i++) { Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(States[i]); if (type != null) { if (type.IsSubclassOf(typeof(FiniteStateBase))) { if (!_stateInstances.ContainsKey(type)) { FiniteStateBase state = Activator.CreateInstance(type) as FiniteStateBase; state.StateMachine = this; state.OnInit(); _stateInstances.Add(type, state); } } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "加载有限状态失败:有限状态类 " + States[i] + " 必须继承至有限状态基类:FiniteStateBase!"); } } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "加载有限状态失败:丢失有限状态类 " + States[i] + " !"); } } //设置默认状态、最终状态 if (DefaultState == "" || FinalState == "" || _stateInstances.Count <= 0) { throw new HTFrameworkException(HTFrameworkModule.FSM, "有限状态机 " + Name + " 的状态为空!或未指定默认状态、最终状态!"); } _defaultState = ReflectionToolkit.GetTypeInRunTimeAssemblies(DefaultState); if (_defaultState == null) { throw new HTFrameworkException(HTFrameworkModule.FSM, "有限状态机 " + Name + " 丢失了默认状态 " + DefaultState + "!"); } _finalState = ReflectionToolkit.GetTypeInRunTimeAssemblies(FinalState); if (_finalState == null) { throw new HTFrameworkException(HTFrameworkModule.FSM, "有限状态机 " + Name + " 丢失了最终状态 " + FinalState + "!"); } }
protected override void Awake() { base.Awake(); if (IsAutoRegister) { Main.m_FSM.RegisterFSM(this); } //加载数据类 if (Data != "<None>") { Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(Data); if (type != null) { if (type.IsSubclassOf(typeof(FSMDataBase))) { CurrentData = Activator.CreateInstance(type) as FSMDataBase; CurrentData.StateMachine = this; } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "创建有限状态机数据类失败:数据类 " + Data + " 必须继承至有限状态机数据基类:FSMDataBase!"); } } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "创建有限状态机数据类失败:丢失数据类 " + Data + " !"); } } //加载所有状态 for (int i = 0; i < States.Count; i++) { Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(States[i]); if (type != null) { if (type.IsSubclassOf(typeof(FiniteStateBase))) { if (!_stateInstances.ContainsKey(type)) { FiniteStateBase state = Activator.CreateInstance(type) as FiniteStateBase; state.StateMachine = this; _stateInstances.Add(type, state); } } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "加载有限状态失败:有限状态类 " + States[i] + " 必须继承至有限状态基类:FiniteStateBase!"); } } else { throw new HTFrameworkException(HTFrameworkModule.FSM, "加载有限状态失败:丢失有限状态类 " + States[i] + " !"); } } //设置默认状态、最终状态 if (string.IsNullOrEmpty(DefaultState) || string.IsNullOrEmpty(FinalState) || _stateInstances.Count <= 0) { throw new HTFrameworkException(HTFrameworkModule.FSM, "有限状态机 " + Name + " 的状态为空!或未指定默认状态、最终状态!"); } _defaultState = ReflectionToolkit.GetTypeInRunTimeAssemblies(DefaultState); if (_defaultState == null) { throw new HTFrameworkException(HTFrameworkModule.FSM, "有限状态机 " + Name + " 丢失了默认状态 " + DefaultState + "!"); } _finalState = ReflectionToolkit.GetTypeInRunTimeAssemblies(FinalState); if (_finalState == null) { throw new HTFrameworkException(HTFrameworkModule.FSM, "有限状态机 " + Name + " 丢失了最终状态 " + FinalState + "!"); } _isAutomate = CurrentData != null ? CurrentData.IsAutomate : false; _isSupportedDataDriver = CurrentData != null ? CurrentData.IsSupportedDataDriver : false; DoAutomaticTask(); }
/// <summary> /// 应用FSM状态的对象路径定义 /// </summary> /// <param name="fsmState">FSM状态实例</param> /// <param name="fieldInfos">所有自动化字段</param> public static void ApplyObjectPath(FiniteStateBase fsmState, FieldInfo[] fieldInfos) { ApplyObjectPath(fsmState, fsmState.StateMachine.gameObject, fieldInfos); }
/// <summary> /// 应用FSM状态的数据绑定 /// </summary> /// <param name="fsmState">FSM状态实例</param> /// <param name="fieldInfos">所有自动化字段</param> public static void ApplyDataBinding(FiniteStateBase fsmState, FieldInfo[] fieldInfos) { ApplyDataBinding(fsmState, fsmState.StateMachine.CurrentData, fieldInfos); }