using System;
using UnityEngine;
using System.Collections.Generic;
namespace SK.Framework
{
///
/// 有限状态机管理器
///
public class FSM : MonoBehaviour
{
#region NonPublic Variables
private static FSM instance;
//状态机列表
private List machines;
#endregion
#region Public Properties
public static FSM Instance
{
get
{
if (instance == null)
{
instance = new GameObject("[SKFramework.FSM]").AddComponent();
instance.machines = new List();
DontDestroyOnLoad(instance);
}
return instance;
}
}
#endregion
#region NonPublic Methods
private void Update()
{
for (int i = 0; i < machines.Count; i++)
{
//更新状态机
machines[i].OnUpdate();
}
}
private void OnDestroy()
{
instance = null;
}
#endregion
#region Public Methods
///
/// 创建状态机
///
/// 状态机类型
/// 状态机名称
/// 状态机
public T Create(string stateMachineName) where T : StateMachine, new()
{
Type type = typeof(T);
stateMachineName = string.IsNullOrEmpty(stateMachineName) ? type.Name : stateMachineName;
if (machines.Find(m => m.Name == stateMachineName) == null)
{
T machine = (T)Activator.CreateInstance(type);
machine.Name = stateMachineName;
machines.Add(machine);
return machine;
}
return default;
}
///
/// 销毁状态机
///
/// 状态机名称
/// 销毁成功返回true 否则返回false
public bool Destroy(string stateMachineName)
{
var targetMachine = machines.Find(m => m.Name == stateMachineName);
if (targetMachine != null)
{
targetMachine.OnDestroy();
machines.Remove(targetMachine);
return true;
}
return false;
}
///
/// 销毁状态机
///
/// 状态机类型
/// 状态机
/// 销毁成功返回true 否则返回false
public bool Destroy(T stateMachine) where T : StateMachine, new()
{
if (machines.Contains(stateMachine))
{
stateMachine.OnDestroy();
machines.Remove(stateMachine);
return true;
}
return false;
}
///
/// 获取状态机
///
/// 状态机类型
/// 状态机名称
/// 状态机
public T GetMachine(string stateMachineName) where T : StateMachine
{
return (T)machines.Find(m => m.Name == stateMachineName);
}
#endregion
}
}