Files
plugin-library/Assets/03.FiniteStateMachine/RunTime/Base/IFSMController.cs
2025-04-10 11:20:22 +08:00

93 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Stary.Evo.FiniteStateMachine
{
public interface IFSMController
{
void Initialize();
void AddState(IFSMIState state);
void RemoveState(IFSMIState state);
}
public abstract class FSMController : MonoBehaviour,IFSMController
{
private IFSMManager manager;
public void Start()
{
manager = new FSMManager();
Initialize();
}
public void AddState(IFSMIState state)
{
if (manager != null)
{
manager.AddState(state);
}
else
{
Debug.LogError("FSMManager未初始化");
}
}
public void RemoveState(IFSMIState state)
{
if (manager != null)
{
manager.RemoveState(state);
}
else
{
Debug.LogError("FSMManager未初始化");
}
}
/// <summary>
/// FSM在此类中初始化,进行状态的添加等操作
/// </summary>
public abstract void Initialize();
private void Update()
{
if (manager != null)
{
manager.CurState.Update();
}
else
{
Debug.LogError("FSMManager未初始化");
}
}
private void FixedUpdate()
{
if (manager != null)
{
manager.CurState.FixedUpdate();
}
else
{
Debug.LogError("FSMManager未初始化");
}
}
private void OnDestroy()
{
if (manager != null)
{
foreach (var state in manager. States)
{
state.Value.OnDestory();
}
}
else
{
Debug.LogError("FSMManager未初始化");
}
}
}
}