This commit is contained in:
MuHua-123
2025-03-11 23:45:41 +08:00
parent a86e32c89e
commit 9f56eff9c0
48 changed files with 319 additions and 158 deletions
@@ -0,0 +1,33 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 有限状态机
/// </summary>
public abstract class IMachine : MonoBehaviour {
protected IMachineState currentState;
protected Dictionary<string, IMachineState> states = new Dictionary<string, IMachineState>();
protected virtual void Start() => InitializeStates();
protected virtual void Update() => currentState?.Update();
#region
protected abstract void InitializeStates();
protected virtual void RegisterState(string stateType, IMachineState state) {
if (!states.ContainsKey(stateType)) { states.Add(stateType, state); }
}
public virtual void ChangeState(string stateType) {
if (states.ContainsKey(stateType)) {
currentState?.Exit();
currentState = states[stateType];
currentState.Enter();
}
else {
Debug.LogWarning($"State {stateType} is not registered.");
}
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0cd74b3acde213f489a11a9e0ccd81e7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: