Files
plugin-library/Assets/03.FiniteStateMachine/RunTime/Base/IFSMController.cs

93 lines
2.2 KiB
C#
Raw Normal View History

2025-04-10 11:20:22 +08:00
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);
}
2025-04-10 11:27:16 +08:00
public abstract class AbstractFSMController : MonoBehaviour,IFSMController
2025-04-10 11:20:22 +08:00
{
2025-04-10 11:46:42 +08:00
private IFsmSystem manager;
2025-04-10 11:20:22 +08:00
public void Start()
{
2025-04-10 11:46:42 +08:00
manager = new FsmSystem();
2025-04-10 11:20:22 +08:00
Initialize();
}
public void AddState(IFSMIState state)
{
if (manager != null)
{
manager.AddState(state);
}
else
{
2025-04-10 11:57:13 +08:00
Debug.LogError("UnityEvo:FSMManager未初始化");
2025-04-10 11:20:22 +08:00
}
}
public void RemoveState(IFSMIState state)
{
if (manager != null)
{
manager.RemoveState(state);
}
else
{
2025-04-10 11:57:13 +08:00
Debug.LogError("UnityEvo:FSMManager未初始化");
2025-04-10 11:20:22 +08:00
}
}
/// <summary>
/// FSM在此类中初始化,进行状态的添加等操作
/// </summary>
public abstract void Initialize();
private void Update()
{
if (manager != null)
{
manager.CurState.Update();
}
else
{
2025-04-10 11:57:13 +08:00
Debug.LogError("UnityEvo:FSMManager未初始化");
2025-04-10 11:20:22 +08:00
}
}
private void FixedUpdate()
{
if (manager != null)
{
manager.CurState.FixedUpdate();
}
else
{
2025-04-10 11:57:13 +08:00
Debug.LogError("UnityEvo:FSMManager未初始化");
2025-04-10 11:20:22 +08:00
}
}
private void OnDestroy()
{
if (manager != null)
{
foreach (var state in manager. States)
{
state.Value.OnDestory();
}
}
else
{
2025-04-10 11:57:13 +08:00
Debug.LogError("UnityEvo:FSMManager未初始化");
2025-04-10 11:20:22 +08:00
}
}
}
}