This commit is contained in:
CortexCore 2024-11-20 11:36:51 +08:00
parent 5d19061fab
commit ce049035e2
20 changed files with 180 additions and 184 deletions

View File

@ -172,22 +172,16 @@ namespace BITKit
/// <summary> /// <summary>
/// 依赖服务集合 /// 依赖服务集合
/// </summary> /// </summary>
public static ServiceCollection ServiceCollection { get; internal set; } = new(); public static ServiceCollection ServiceCollection { get; set; } = new();
/// <summary> /// <summary>
/// 依赖服务提供接口 /// 依赖服务提供接口
/// </summary> /// </summary>
public static ServiceProvider ServiceProvider { get; private set; } public static ServiceProvider ServiceProvider
public static ServiceProvider BuildServiceProvider()
{ {
var value = ServiceProvider = ServiceCollection.BuildServiceProvider(); get=>_serviceProvider??=ServiceCollection.BuildServiceProvider();
OnServiceProviderBuild?.Invoke(value); set => _serviceProvider = value;
return value;
} }
private static ServiceProvider _serviceProvider;
/// <summary>
/// 服务创建后的回调
/// </summary>
public static Action<ServiceProvider> OnServiceProviderBuild;
/// <summary> /// <summary>
/// 主线程 /// 主线程
/// </summary> /// </summary>

View File

@ -116,7 +116,7 @@ namespace BITKit
/// <summary> /// <summary>
/// 数量 /// 数量
/// </summary> /// </summary>
public int Amount { get; } public int Amount { get; set; }
/// <summary> /// <summary>
/// 运行时属性 /// 运行时属性

View File

@ -15,6 +15,7 @@ namespace BITKit
/// </summary> /// </summary>
public interface IRuntimeItemContainer:IBinarySerialize public interface IRuntimeItemContainer:IBinarySerialize
{ {
public ValidHandle IsBusy { get; }
/// <summary> /// <summary>
/// 物品容器的唯一Id /// 物品容器的唯一Id
/// </summary> /// </summary>
@ -32,6 +33,11 @@ namespace BITKit
/// </summary> /// </summary>
bool Remove(int id); bool Remove(int id);
/// <summary> /// <summary>
/// 设置物品
/// </summary>
/// <param name="id"></param>
void Set(IRuntimeItem id);
/// <summary>
/// 通过通过Id丢下物品 /// 通过通过Id丢下物品
/// </summary> /// </summary>
bool Drop(int id); bool Drop(int id);
@ -75,6 +81,10 @@ namespace BITKit
public class RuntimeItemContainer : IRuntimeItemContainer public class RuntimeItemContainer : IRuntimeItemContainer
{ {
public RuntimeItemContainer()
{
IsBusy.AddListener(x=>OnRelease?.Invoke(!x));
}
public readonly ConcurrentDictionary<int, IRuntimeItem> Items = new(); public readonly ConcurrentDictionary<int, IRuntimeItem> Items = new();
public void Read(BinaryReader r) public void Read(BinaryReader r)
{ {
@ -86,11 +96,13 @@ namespace BITKit
throw new NotImplementedException(); throw new NotImplementedException();
} }
public ValidHandle IsBusy { get; } = new();
public int Id { get; set; } public int Id { get; set; }
public IRuntimeItem[] GetItems()=>Items.Values.ToArray(); public IRuntimeItem[] GetItems()=>Items.Values.ToArray();
public bool Add(IRuntimeItem item) public bool Add(IRuntimeItem item)
{ {
using var _ = IsBusy.GetHandle();
foreach (var func in AddFactory.CastAsFunc()) foreach (var func in AddFactory.CastAsFunc())
{ {
if (func.Invoke(item) is false) if (func.Invoke(item) is false)
@ -109,6 +121,7 @@ namespace BITKit
public bool Remove(int id) public bool Remove(int id)
{ {
using var _ = IsBusy.GetHandle();
if (Items.TryGetValue(id, out var item) is false) return false; if (Items.TryGetValue(id, out var item) is false) return false;
foreach (var func in RemoveFactory.CastAsFunc()) foreach (var func in RemoveFactory.CastAsFunc())
@ -124,8 +137,17 @@ namespace BITKit
return true; return true;
} }
public void Set(IRuntimeItem id)
{
using var _ = IsBusy.GetHandle();
if (!Items.TryGetValue(id.Id, out var item)) return;
item = id;
OnSet?.Invoke(item);
}
public bool Drop(int id) public bool Drop(int id)
{ {
using var _ = IsBusy.GetHandle();
if (Items.TryGetValue(id, out var item) is false) return false; if (Items.TryGetValue(id, out var item) is false) return false;
foreach (var func in DropFactory.CastAsFunc()) foreach (var func in DropFactory.CastAsFunc())
{ {

View File

@ -1,9 +1,9 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using Cysharp.Threading.Tasks; using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Random = UnityEngine.Random;
namespace BITKit.StateMachine namespace BITKit.StateMachine
{ {
@ -11,15 +11,8 @@ namespace BITKit.StateMachine
/// 动态异步状态机 /// 动态异步状态机
/// </summary> /// </summary>
/// <typeparam name="T">异步状态</typeparam> /// <typeparam name="T">异步状态</typeparam>
public class AsyncStateMachine<T>:IStateMachine<T> ,IDisposable where T : IStateAsync public class AsyncStateMachine<T>:IStateMachine<T> ,IDisposable where T : class, IStateAsync
{ {
private enum AsyncState
{
Initializing,
InEntering,
InExiting,
}
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
public AsyncStateMachine(IServiceProvider serviceProvider) public AsyncStateMachine(IServiceProvider serviceProvider)
{ {
@ -31,113 +24,153 @@ namespace BITKit.StateMachine
} }
public bool Enabled { get; set; } = true; public bool Enabled { get; set; } = true;
public T CurrentState { get; set; } public T CurrentState { get;private set; }
public T NextOrCurrentState => _nextTargetState.IfNotAllow(CurrentState);
private T _nextState; private T _nextState;
public event Action<T, T> OnStateChanging; public event Action<T, T> OnStateChanging;
public event Action<T, T> OnStateChanged; public event Action<T, T> OnStateChanged;
public event Action<T> OnStateRegistered; public event Action<T> OnStateRegistered;
public IReadOnlyDictionary<int, T> Dictionary => _dictionary;
public event Action<T> OnStateUnRegistered; public event Action<T> OnStateUnRegistered;
public IDictionary<Type, T> StateDictionary { get; } = new Dictionary<Type, T>(); public IDictionary<Type, T> StateDictionary { get; } = new Dictionary<Type, T>();
public readonly ValidHandle IsBusy=new();
private readonly CancellationTokenSource _cancellationTokenSource=new(); private readonly CancellationTokenSource _cancellationTokenSource=new();
private readonly ValidHandle _isBusy=new(); private readonly Dictionary<int, T> _dictionary = new();
private readonly ConcurrentQueue<(UniTask task,AsyncState state,T value)> _taskQueue=new(); private readonly Optional<T> _nextTargetState = new();
public void Initialize() public async void Initialize()
{ {
await IsBusy;
if(_cancellationTokenSource.IsCancellationRequested)return;
using var _ = IsBusy.GetHandle();
foreach (var (_,value) in StateDictionary) foreach (var (_,value) in StateDictionary)
{ {
_taskQueue.Enqueue(new (value.InitializeAsync(),AsyncState.Initializing,value)); await value.InitializeAsync();
value.Initialize();
} }
} }
public async void UpdateState(float deltaTime) public async void UpdateState(float deltaTime)
{ {
if(Enabled is false)return; if (CurrentState is null) return;
CurrentState?.OnStateUpdate(deltaTime); using var _ = IsBusy.GetHandle();
if(_isBusy)return; await CurrentState.OnStateUpdateAsync(deltaTime);
using var _ = _isBusy.GetHandle(); CurrentState.OnStateUpdate(deltaTime);
if (!_taskQueue.TryDequeue(out var task))
{
if(Enabled is false)return;
if (CurrentState is not null)
{
await CurrentState.OnStateUpdateAsync(deltaTime);
}
return;
}
await task.task;
if(_cancellationTokenSource.IsCancellationRequested|| Enabled is false)return;
switch (task.state)
{
case AsyncState.Initializing:
task.value.Initialize();
OnStateRegistered?.Invoke(task.value);
break;
case AsyncState.InEntering:
if (_nextState is not null)
{
_nextState.OnStateEntry(_nextState);
OnStateChanged?.Invoke(CurrentState,_nextState);
}
else
{
OnStateChanged?.Invoke(CurrentState,default);
}
CurrentState = _nextState;
break;
case AsyncState.InExiting:
CurrentState?.OnStateExit(CurrentState,_nextState);
break;
}
} }
public void DisposeState()
public async void DisposeState()
{ {
_taskQueue.Clear(); await IsBusy;
if (CurrentState is not null) if(_cancellationTokenSource.IsCancellationRequested)return;
{ if (CurrentState is null) return;
_taskQueue.Enqueue(new(CurrentState.OnStateExitAsync(CurrentState,null),AsyncState.InExiting,CurrentState)); using var _ = IsBusy.GetHandle();
} await CurrentState.OnStateExitAsync(CurrentState, null);
if(_cancellationTokenSource.IsCancellationRequested)return;
CurrentState.OnStateExit(CurrentState, null);
CurrentState = null;
} }
public T TransitionState<TState>() where TState : T public T TransitionState<TState>() where TState : T
{ {
T nextState; T nextState;
foreach (var (type, value) in StateDictionary) foreach (var (type, value) in Dictionary)
{ {
if (!type.IsAssignableFrom(typeof(TState))) continue; if ((typeof(TState) == value.GetType()) is false) continue;
nextState = value; nextState = value;
TransitionState(nextState); TransitionState(nextState);
return nextState; return nextState;
} }
nextState = _serviceProvider.GetRequiredService<TState>(); nextState = _serviceProvider.GetRequiredService<TState>();
_taskQueue.Enqueue(new(nextState.InitializeAsync(),AsyncState.Initializing,nextState));
return TransitionState(nextState);; return TransitionState(nextState);;
} }
public async UniTask TransitionStateAsync(T nextState)
{
if (nextState is not null)
{
if (nextState.Identifier == 0)
{
nextState.Identifier = nextState.GetHashCode();
}
}
if (Equals(nextState, CurrentState)) return;
if(_nextTargetState.Allow && Equals(_nextTargetState.Value,nextState))return;
if (_nextTargetState.Allow)
{
_nextTargetState.Value = nextState;
return;
}
_nextTargetState.SetValueThenAllow(nextState);
await IsBusy;
if(_cancellationTokenSource.IsCancellationRequested)return;
using var _ = IsBusy.GetHandle();
OnStateChanging?.Invoke(CurrentState,nextState);
if (_dictionary.TryAdd(nextState.Identifier, nextState))
{
await nextState.InitializeAsync();
if(_cancellationTokenSource.IsCancellationRequested)return;
nextState.Initialize();
}
if (CurrentState is not null)
{
CurrentState.Enabled = false;
await CurrentState.OnStateExitAsync(CurrentState, nextState);
if(_cancellationTokenSource.IsCancellationRequested)return;
CurrentState.OnStateExit(CurrentState,nextState);
}
var tempState = CurrentState;
CurrentState = _nextTargetState.Value;
_nextTargetState.Clear();
nextState.Enabled = true;
await nextState.OnStateEntryAsync(tempState);
if(_cancellationTokenSource.IsCancellationRequested)return;
nextState.OnStateEntry(tempState);
OnStateChanged?.Invoke(tempState,nextState);
}
public T TransitionState(T nextState) public T TransitionState(T nextState)
{ {
if(Equals(CurrentState,nextState))return nextState; TransitionStateAsync(nextState).Forget();
OnStateChanging?.Invoke(CurrentState,nextState); return nextState;
if (CurrentState is not null)
{
_taskQueue.Enqueue(new(CurrentState.OnStateExitAsync(CurrentState,nextState),AsyncState.InExiting,CurrentState));
}
if (nextState is not null)
{
_taskQueue.Enqueue(new(nextState.OnStateEntryAsync(nextState),AsyncState.InEntering,nextState));
}
return _nextState= nextState;
} }
public async void UnRegister(T newState)
{
if (newState is null) return;
if (Dictionary.ContainsKey(newState.Identifier) is false) return;
_dictionary.Remove(newState.Identifier);
if (Equals(CurrentState, newState))
{
await CurrentState.OnStateExitAsync(CurrentState, null);
CurrentState.OnStateExit(CurrentState, null);
if (CurrentState is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
if (CurrentState is IDisposable disposable)
{
disposable.Dispose();
}
CurrentState = null;
}
OnStateUnRegistered?.Invoke(newState);
}
public void Dispose() public void Dispose()
{ {
if(_isDisposed)return;
_cancellationTokenSource.Cancel(); _cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose(); _cancellationTokenSource.Dispose();
_isDisposed = true;
} }
private bool _isDisposed;
} }
} }

View File

@ -47,7 +47,7 @@ namespace BITKit.StateMachine
public interface IStateMachine<T> where T:IState public interface IStateMachine<T> where T:IState
{ {
bool Enabled { get; set; } bool Enabled { get; set; }
T CurrentState { get; set; } T CurrentState { get; }
event Action<T,T> OnStateChanged; event Action<T,T> OnStateChanged;
event Action<T> OnStateRegistered; event Action<T> OnStateRegistered;
event Action<T> OnStateUnRegistered; event Action<T> OnStateUnRegistered;

View File

@ -26,19 +26,37 @@ namespace BITKit
public class MyHandle:IDisposable public class MyHandle:IDisposable
{ {
private readonly ValidHandle _validHandle; private readonly ValidHandle _validHandle;
private bool _isDisable = false;
public MyHandle(ValidHandle validHandle) public MyHandle(ValidHandle validHandle,bool isDisable = false)
{ {
_validHandle = validHandle; _validHandle = validHandle;
_validHandle.AddElement(this);
_isDisable = isDisable;
if (isDisable)
{
_validHandle.AddDisableElements(this);
}
else
{
_validHandle.AddElement(this);
}
} }
public void Dispose() public void Dispose()
{ {
_validHandle.RemoveElement(this); if (_isDisable)
{
_validHandle.RemoveDisableElements(this);
}
else
{
_validHandle.RemoveElement(this);
}
} }
} }
public MyHandle GetHandle() => new MyHandle(this); public MyHandle GetHandle() => new MyHandle(this);
public MyHandle GetDisableHandle()=> new MyHandle(this,true);
public override string ToString() public override string ToString()
{ {
@ -69,7 +87,7 @@ namespace BITKit
public readonly List<object> disableObjs = new List<object>(); public readonly List<object> disableObjs = new List<object>();
private bool tempEnable; private bool tempEnable;
private Action<bool> EventOnEnableChanged; private Action<bool> EventOnEnableChanged;
private readonly List<UniTaskCompletionSource> _completionSources = new(); private readonly Queue<UniTaskCompletionSource> _completionSources = new();
public void AddElement(object obj) public void AddElement(object obj)
@ -96,7 +114,7 @@ namespace BITKit
EventOnEnableChanged.Invoke(enableHandle); EventOnEnableChanged.Invoke(enableHandle);
} }
if (tempEnable) return; if (tempEnable) return;
foreach (var cs in _completionSources) if (_completionSources.TryDequeue(out var cs))
{ {
cs.TrySetResult(); cs.TrySetResult();
} }
@ -197,7 +215,7 @@ namespace BITKit
return UniTask.CompletedTask.GetAwaiter(); return UniTask.CompletedTask.GetAwaiter();
} }
var cs = new UniTaskCompletionSource(); var cs = new UniTaskCompletionSource();
_completionSources.Add(cs); _completionSources.Enqueue(cs);
return cs.Task.GetAwaiter(); return cs.Task.GetAwaiter();
} }
public void Clear() public void Clear()

View File

@ -72,9 +72,9 @@
"name": "Aim", "name": "Aim",
"type": "Button", "type": "Button",
"id": "f521b9d3-bfbb-4ce2-85c8-15cb3fcc2534", "id": "f521b9d3-bfbb-4ce2-85c8-15cb3fcc2534",
"expectedControlType": "Button", "expectedControlType": "",
"processors": "", "processors": "",
"interactions": "Tap,Hold,Press", "interactions": "Press",
"initialStateCheck": false "initialStateCheck": false
}, },
{ {
@ -135,7 +135,7 @@
"name": "Primary", "name": "Primary",
"type": "Button", "type": "Button",
"id": "113a3c02-0fbb-4081-9fac-c39878ddfd09", "id": "113a3c02-0fbb-4081-9fac-c39878ddfd09",
"expectedControlType": "Button", "expectedControlType": "",
"processors": "", "processors": "",
"interactions": "Press,Hold", "interactions": "Press,Hold",
"initialStateCheck": false "initialStateCheck": false
@ -144,7 +144,7 @@
"name": "Secondary", "name": "Secondary",
"type": "Button", "type": "Button",
"id": "57a12f28-2fde-45e5-957f-d8d61afe44de", "id": "57a12f28-2fde-45e5-957f-d8d61afe44de",
"expectedControlType": "Button", "expectedControlType": "",
"processors": "", "processors": "",
"interactions": "Press,Hold", "interactions": "Press,Hold",
"initialStateCheck": false "initialStateCheck": false
@ -153,7 +153,7 @@
"name": "Tertiary", "name": "Tertiary",
"type": "Button", "type": "Button",
"id": "c2535f43-fec8-42bd-b680-6d1cee5246e5", "id": "c2535f43-fec8-42bd-b680-6d1cee5246e5",
"expectedControlType": "Button", "expectedControlType": "",
"processors": "", "processors": "",
"interactions": "Press,Hold", "interactions": "Press,Hold",
"initialStateCheck": false "initialStateCheck": false
@ -162,7 +162,7 @@
"name": "Quaternary", "name": "Quaternary",
"type": "Button", "type": "Button",
"id": "6740f3a7-7571-4277-96e9-15aa08af7302", "id": "6740f3a7-7571-4277-96e9-15aa08af7302",
"expectedControlType": "Button", "expectedControlType": "",
"processors": "", "processors": "",
"interactions": "Press,Hold", "interactions": "Press,Hold",
"initialStateCheck": false "initialStateCheck": false
@ -171,7 +171,7 @@
"name": "Quinary", "name": "Quinary",
"type": "Button", "type": "Button",
"id": "97511588-e441-4e27-953e-e60cbea80489", "id": "97511588-e441-4e27-953e-e60cbea80489",
"expectedControlType": "Button", "expectedControlType": "",
"processors": "", "processors": "",
"interactions": "", "interactions": "",
"initialStateCheck": false "initialStateCheck": false
@ -180,7 +180,7 @@
"name": "Senary", "name": "Senary",
"type": "Button", "type": "Button",
"id": "46173186-2ef3-4ab9-8ff4-b94cc113df67", "id": "46173186-2ef3-4ab9-8ff4-b94cc113df67",
"expectedControlType": "Button", "expectedControlType": "",
"processors": "", "processors": "",
"interactions": "", "interactions": "",
"initialStateCheck": false "initialStateCheck": false

View File

@ -2,10 +2,10 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using AYellowpaper.SerializedCollections; using AYellowpaper.SerializedCollections;
using YooAsset;
#if UNITY_EDITOR #if UNITY_EDITOR
using UnityEditor; using UnityEditor;
using YooAsset;
#endif #endif
using UnityEngine; using UnityEngine;

View File

@ -111,6 +111,7 @@ namespace BITKit.UX
private readonly IUXService _uxService; private readonly IUXService _uxService;
private VisualElement _root; private VisualElement _root;
private VisualElement _container; private VisualElement _container;
private bool _isInitialized = false;
public UXContextMenu(IUXService uxService) public UXContextMenu(IUXService uxService)
{ {
_uxService = uxService; _uxService = uxService;
@ -121,7 +122,7 @@ namespace BITKit.UX
private async void InitializeAsync() private async void InitializeAsync()
{ {
var go = new GameObject("UXConsole"); var go = new GameObject(nameof(UXContextMenu));
Object.DontDestroyOnLoad(go); Object.DontDestroyOnLoad(go);
var document = go.AddComponent<UIDocument>(); var document = go.AddComponent<UIDocument>();
document.sortingOrder = 1; document.sortingOrder = 1;
@ -143,6 +144,7 @@ namespace BITKit.UX
{ {
Close(); Close();
}); });
_isInitialized = true;
Close(); Close();
} }
public void Create(ContextMenuBuilder builder) public void Create(ContextMenuBuilder builder)
@ -166,6 +168,7 @@ namespace BITKit.UX
} }
private void Close() private void Close()
{ {
if(_isInitialized is false)return;
_container.Clear(); _container.Clear();
_root.SetActive(false); _root.SetActive(false);
} }

View File

@ -29,6 +29,7 @@ namespace BITKit.UX
public VisualElement RootVisualElement { get; set; } public VisualElement RootVisualElement { get; set; }
protected VisualTreeAsset VisualTreeAsset { get; private set; } protected VisualTreeAsset VisualTreeAsset { get; private set; }
private readonly ValidHandle _isBusy = new(); private readonly ValidHandle _isBusy = new();
public readonly UniTaskCompletionSource WaitUtilInitialized = new();
protected UIToolKitPanel(IUXService uxService) protected UIToolKitPanel(IUXService uxService)
{ {
UXService = uxService; UXService = uxService;
@ -76,6 +77,8 @@ namespace BITKit.UX
UXUtils.Inject(this); UXUtils.Inject(this);
RootVisualElement.SetActive(false); RootVisualElement.SetActive(false);
WaitUtilInitialized.TrySetResult();
} }
} }

View File

@ -1,11 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using BITKit.UX;
using UnityEngine;
public class UIToolkitSubPanel
{
public UIToolkitSubPanel()
{
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 398253ee7f746d143bbbcd9effab9da1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -31,3 +31,4 @@
scale: 1.2 1.2; scale: 1.2 1.2;
opacity: 0.5; opacity: 0.5;
} }

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 6be2ea5bfce9e6c44b9227b2084f2e85
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 8f39c17a963315c4ea9df830636a76cf
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: ea845eb2a3c47f54c9e70466915ce6f0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: bb2db3744d41b7e47a230183dc579273
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 650ee3d040a06c3409e069a838480a53
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 3b817d6ebbc547f4f8a3405d33e18c85
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 7e6a775310ef1f845ab84594ee93ed03
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: