首次提交

This commit is contained in:
Bob.Song
2026-03-05 18:07:55 +08:00
commit e125bb869e
4534 changed files with 563920 additions and 0 deletions

View File

@@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using Unity.VisualScripting;
namespace NBC
{
public class Fsm<TOwnerClass>
{
private FsmBaseState<TOwnerClass> _currentState;
private Dictionary<uint, FsmBaseState<TOwnerClass>> _states = new Dictionary<uint, FsmBaseState<TOwnerClass>>();
private Dictionary<Type, uint> _stateType2Id = new Dictionary<Type, uint>();
private bool _doDebugOutput;
private TOwnerClass _owner;
private string _description;
public uint CurrentStateId => _currentState.StateId;
public FsmBaseState<TOwnerClass> CurrentState => _currentState;
public Fsm(string description, TOwnerClass owner, bool debugOutput = false)
{
_description = description;
_owner = owner;
_doDebugOutput = debugOutput;
}
public void RegisterState<T>() where T : FsmBaseState<TOwnerClass>, new()
{
var type = typeof(T);
if (_stateType2Id.ContainsKey(type))
{
LogError("State {0} already registered", type);
return;
}
T result = new T();
_stateType2Id[type] = result.StateId;
_states[result.StateId] = result;
result.Init(_owner, this);
}
public void Start<T>() where T : FsmBaseState<TOwnerClass>, new()
{
Log("start");
EnterState(typeof(T));
}
public void EnterState(Type type, FsmBaseState<TOwnerClass> sourceState = null)
{
if (_stateType2Id.TryGetValue(type, out var id))
{
EnterState(id, sourceState);
}
}
public void EnterState(uint stateId, FsmBaseState<TOwnerClass> sourceState = null)
{
uint prevState = 0;
FsmTransmit param = null;
if (_currentState != null)
{
prevState = _currentState.StateId;
param = _currentState.Params;
_currentState.Exit();
}
_currentState = _states[stateId];
_currentState.PrevState = prevState;
if (param != null)
{
_currentState.Params.AddRange(param);
param.Clear();
}
if (sourceState != null)
{
_currentState.Transition(sourceState);
}
else
{
_currentState.Enter();
}
}
public void Update()
{
if (_currentState != null)
{
_currentState.PreUpdate();
var type = _currentState.Update();
if (type != 0)
{
_currentState.NextState = type;
EnterState(type);
}
}
}
public void FixedUpdate()
{
if (_currentState != null)
{
var type = _currentState.FixedUpdate();
if (type != 0)
{
_currentState.NextState = type;
EnterState(type);
}
}
}
public void LateUpdate()
{
if (_currentState != null)
{
var type = _currentState.LateUpdate();
if (type != 0)
{
_currentState.NextState = type;
EnterState(type);
}
}
}
public void LogError(string textPattern, params object[] args)
{
}
public void Log(string text)
{
if (!_doDebugOutput)
{
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 53d2b03339c14d70af4039451671366f

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace NBC
{
public abstract class FsmBaseState<TOwnerClass>
{
protected TOwnerClass _owner;
protected Fsm<TOwnerClass> Root;
public FsmTransmit Params = new FsmTransmit();
public abstract uint StateId { get; }
public uint PrevState { get; set; }
public uint NextState { get; set; }
protected float EnterTime { get; set; }
public void Init(TOwnerClass owner, Fsm<TOwnerClass> fsm)
{
_owner = owner;
Root = fsm;
}
public virtual void Transition(FsmBaseState<TOwnerClass> source)
{
Enter();
}
public void Enter()
{
EnterTime = Time.time;
onEnter();
}
protected virtual void onEnter()
{
}
public void Exit()
{
onExit();
}
protected virtual void onExit()
{
}
public uint Update()
{
return onUpdate();
}
protected virtual uint onUpdate()
{
return 0;
}
public uint FixedUpdate()
{
return onFixedUpdate();
}
protected virtual uint onFixedUpdate()
{
return 0;
}
public void PreUpdate()
{
OnPreUpdate();
}
protected virtual void OnPreUpdate()
{
}
public uint LateUpdate()
{
return OnLateUpdate();
}
protected virtual uint OnLateUpdate()
{
return 0;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6fefa29788c940ebb9bfb95358cca1a0
timeCreated: 1732110077

View File

@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Linq;
namespace NBC
{
public class FsmTransmit
{
private readonly List<object> _queue = new List<object>();
public List<object> AllParam => _queue;
public void Add(object o)
{
_queue.Add(o);
}
public void AddRange(FsmTransmit transmit)
{
_queue.AddRange(transmit.AllParam);
}
public void AddRange(IEnumerable<object> o)
{
_queue.AddRange(o);
}
public object Get(int index)
{
if (index >= 0 && index < _queue.Count)
{
return _queue[index];
}
return null;
}
public void Clear()
{
_queue.Clear();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4288fa116ccf436983b60932fbe19d3b