FSM_Over
This commit is contained in:
146
Assets/03.FiniteStateMachine/RunTime/FSMManager.cs
Normal file
146
Assets/03.FiniteStateMachine/RunTime/FSMManager.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class FSMManager
|
||||
{
|
||||
public GameObject FSMMangerObject;
|
||||
private Dictionary<int, FSMController> controllers = new Dictionary<int, FSMController>();
|
||||
private Dictionary<int, SingleState> states = new Dictionary<int, SingleState>();
|
||||
private int nextControllerId = 0;
|
||||
private int nextStateId = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 创建FSMController
|
||||
/// </summary>
|
||||
/// <returns>返回创建的FSMController的识别Int值</returns>
|
||||
public int CreateFSMController()
|
||||
{
|
||||
if(FSMMangerObject == null)
|
||||
{
|
||||
Debug.LogError("FSM状态机还未初始化!!!");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int id = nextControllerId++;
|
||||
GameObject newController = new GameObject("FSMController" + id);
|
||||
newController.transform.SetParent(FSMMangerObject.transform);
|
||||
var controller = newController.AddComponent<FSMController>();
|
||||
controllers[id] = controller;
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁指定FSMController
|
||||
/// </summary>
|
||||
/// <param name="controllerId"></param>
|
||||
public void DestroyFSMController(int controllerId)
|
||||
{
|
||||
if (controllers.TryGetValue(controllerId, out var controller))
|
||||
{
|
||||
GameObject.Destroy(controller.gameObject);
|
||||
controllers.Remove(controllerId);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("该Id的FSMController不存在或已被销毁!!!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 退出指定Controller中的所有状态
|
||||
/// </summary>
|
||||
/// <param name="controllerId"></param>
|
||||
public void ExitAllStateInController(int controllerId)
|
||||
{
|
||||
if (controllers.TryGetValue(controllerId, out var controller))
|
||||
{
|
||||
controller.ExitAll();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("该Id的FSMController不存在或已被销毁!!!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建状态
|
||||
/// </summary>
|
||||
/// <param name="controllerId">状态挂载的ControllerId</param>
|
||||
/// <param name="stateDateAction">状态行为数据</param>
|
||||
/// <returns></returns>
|
||||
public int CreateState(int controllerId, StateDateAction stateDateAction)
|
||||
{
|
||||
if (controllers.TryGetValue(controllerId, out var controller))
|
||||
{
|
||||
// 创建state并将其添加到指定controller生命周期中
|
||||
SingleState state = new SingleState();
|
||||
state.stateDate = stateDateAction;
|
||||
controller.AddState(state);
|
||||
int id = nextStateId++;
|
||||
states[id] = state;
|
||||
return id;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("该Id的FSMController不存在或已被销毁,状态初始化失败!!!");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入指定状态
|
||||
/// </summary>
|
||||
/// <param name="stateId"></param>
|
||||
public void EnterState(int stateId)
|
||||
{
|
||||
if(states.TryGetValue(stateId, out var state))
|
||||
{
|
||||
state.Machine.Enter(state.GetType());
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("该Id的状态不存在!!!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 离开指定状态
|
||||
/// </summary>
|
||||
/// <param name="stateId"></param>
|
||||
public void ExitState(int stateId)
|
||||
{
|
||||
if (states.TryGetValue(stateId, out var state))
|
||||
{
|
||||
state.Machine.Exit(state.GetType());
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("该Id的状态不存在!!!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁指定状态
|
||||
/// </summary>
|
||||
/// <param name="stateId"></param>
|
||||
public void DestroyState(int stateId)
|
||||
{
|
||||
if (states.TryGetValue(stateId, out var state))
|
||||
{
|
||||
states.Remove(stateId);
|
||||
}
|
||||
}
|
||||
|
||||
// 将状态添加到FSMController
|
||||
public void AddStateToController(int controllerId, int stateId)
|
||||
{
|
||||
if (controllers.TryGetValue(controllerId, out var controller) && states.TryGetValue(stateId, out var state))
|
||||
{
|
||||
controller.AddState(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user