This commit is contained in:
CortexCore
2024-11-08 12:52:09 +08:00
parent 4ba741408d
commit 1650126d55
27 changed files with 851 additions and 193 deletions

View File

@@ -39,5 +39,29 @@ namespace BITKit
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3>>();
}
public static IEnumerable<Func<T0, T1, T2, T3, T4>> CastAsFunc<T0, T1, T2, T3, T4>(
this Func<T0, T1, T2, T3, T4> self)
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4>>();
}
public static IEnumerable<Func<T0, T1, T2, T3, T4, T5>> CastAsFunc<T0, T1, T2, T3, T4, T5>(
this Func<T0, T1, T2, T3, T4, T5> self)
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4, T5>>();
}
public static IEnumerable<Func<T0, T1, T2, T3, T4, T5, T6>> CastAsFunc<T0, T1, T2, T3, T4, T5, T6>(
this Func<T0, T1, T2, T3, T4, T5, T6> self)
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4, T5, T6>>();
}
public static IEnumerable<Func<T0, T1, T2, T3, T4, T5, T6, T7>> CastAsFunc<T0, T1, T2, T3, T4, T5, T6, T7>(
this Func<T0, T1, T2, T3, T4, T5, T6, T7> self)
{
return self?.GetInvocationList().Cast<Func<T0, T1, T2, T3, T4, T5, T6, T7>>();
}
}
}

View File

@@ -167,6 +167,13 @@ namespace BITKit
}
return false;
}
public static IEnumerable<T> RemoveIn<T>(this IEnumerable<T> self, T t)
{
var list = self.ToList();
list.Remove(t);
return list.ToArray();
}
public static bool TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey t)
{
if (self.ContainsKey(t))

View File

@@ -1,8 +0,0 @@
namespace BITKit.IO
{
public interface IAsset
{
string Name { get; set; }
byte[] Buffer{ get; set; }
}
}

View File

@@ -94,6 +94,11 @@ namespace BITKit
/// </summary>
/// <returns></returns>
int Value => 10;
/// <summary>
/// 创建运行时物品
/// </summary>
/// <returns></returns>
IRuntimeItem CreateRuntimeItem();
}
public interface IRuntimeItem : ICloneable
@@ -101,7 +106,7 @@ namespace BITKit
/// <summary>
/// 运行时Id
/// </summary>
public int Id { get; }
public int Id { get; set; }
/// <summary>
/// 配置Id
@@ -133,14 +138,23 @@ namespace BITKit
/// 被托管的物品
/// </summary>
[Serializable]
public record RuntimeItem : IRuntimeItem
public struct RuntimeItem : IRuntimeItem
{
public int Id { get; set; } = new Random().Next();
public static RuntimeItem Create()
{
var item = new RuntimeItem()
{
Id = new Random().Next(),
RuntimeProperties =
new Dictionary<Type, IScriptableItemProperty>()
};
return item;
}
public int Id { get; set; }
public int ScriptableId { get; set; }
public int Amount { get; set; }
public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; } =
new Dictionary<Type, IScriptableItemProperty>();
public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; }
public event Action<IRuntimeItem> OnRuntimePropertiesChanged;

View File

@@ -1,4 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.SqlServer.Server;
namespace BITKit
@@ -67,4 +71,83 @@ namespace BITKit
/// </summary>
event Action<bool> OnRelease;
}
public class RuntimeItemContainer : IRuntimeItemContainer
{
private readonly ConcurrentDictionary<int, IRuntimeItem> _items = new();
public void Read(BinaryReader r)
{
throw new NotImplementedException();
}
public void Write(BinaryWriter w)
{
throw new NotImplementedException();
}
public int Id { get; set; }
public IRuntimeItem[] GetItems()=>_items.Values.ToArray();
public bool Add(IRuntimeItem item)
{
foreach (var func in AddFactory.CastAsFunc())
{
if (func.Invoke(item) is false)
{
return false;
}
}
_items.Set(item.Id,item);
OnAdd?.Invoke(item);
//BIT4Log.Log<RuntimeItemContainer>($"添加了了:{item.Id}");
return true;
}
public bool Remove(int id)
{
if (_items.TryGetValue(id, out var item) is false) return false;
foreach (var func in RemoveFactory.CastAsFunc())
{
if (func.Invoke(item) is false)
{
return false;
}
}
_items.TryRemove(item.Id);
OnRemove?.Invoke(item);
//BIT4Log.Log<RuntimeItemContainer>($"移除了:{id}");
return true;
}
public bool Drop(int id)
{
if (_items.TryGetValue(id, out var item) is false) return false;
foreach (var func in DropFactory.CastAsFunc())
{
if (func.Invoke(item) is false)
{
return false;
}
}
_items.TryRemove(item.Id);
OnDrop?.Invoke(item);
//BIT4Log.Log<RuntimeItemContainer>($"丢下了:{id}");
return true;
}
public event Func<IRuntimeItem, bool> AddFactory;
public event Func<IRuntimeItem, bool> RemoveFactory;
public event Func<IRuntimeItem, bool> DropFactory;
public event Action<IRuntimeItem> OnAdd;
public event Action<IRuntimeItem> OnRemove;
public event Action<IRuntimeItem> OnSet;
public event Action<IRuntimeItem> OnDrop;
public event Action<IRuntimeItemContainer> OnRebuild;
public event Action<bool> OnRelease;
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: fe7ae68fb2f193640afdc79aef634c98
guid: 787baa1105305fb4683a3196b038b1b3
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -0,0 +1,32 @@
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace BITKit.Pool
{
/// <summary>
/// 对象池服务
/// </summary>
public interface IPoolService
{
/// <summary>
/// 生成对象
/// </summary>
/// <param name="path">可寻址路径</param>
/// <typeparam name="T">类型</typeparam>
/// <returns></returns>
UniTask<T> Spawn<T>(string path) where T : class;
/// <summary>
/// 回收对象
/// </summary>
/// <param name="obj">对象实例</param>
/// <param name="path">可寻址路径</param>
/// <typeparam name="T">类型</typeparam>
void Despawn<T>(T obj,string path) where T : class;
/// <summary>
/// 初始化,在此提前生成所有对象
/// </summary>
/// <returns></returns>
UniTask InitializeAsync();
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 3eb498b90aa487048a9fd9a3e9253dd8
guid: 05f75f7f468db924bb3d58fc4a5aad99
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

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

View File

@@ -0,0 +1,133 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace BITKit.StateMachine
{
/// <summary>
/// 动态异步状态机
/// </summary>
/// <typeparam name="T">异步状态</typeparam>
public class AsyncStateMachine<T>:IStateMachine<T> ,IDisposable where T : IStateAsync
{
private enum AsyncState
{
None,
Initializing,
InEntering,
InExiting,
InUpdating,
}
private readonly IServiceCollection _serviceCollection;
public AsyncStateMachine(IServiceCollection serviceCollection)
{
_serviceCollection = serviceCollection;
}
public bool Enabled { get; set; }
public T CurrentState { get; set; }
private T _nextState;
public event Action<T, T> OnStateChanging;
public event Action<T, T> OnStateChanged;
public event Action<T> OnStateRegistered;
public event Action<T> OnStateUnRegistered;
public IDictionary<Type, T> StateDictionary { get; } = new Dictionary<Type, T>();
private AsyncState _currentState;
private Task _currentTask;
private readonly CancellationTokenSource _cancellationTokenSource=new();
public async void Initialize()
{
if (_currentState is not AsyncState.None)
{
throw new InvalidOperationException("状态机可能已初始化");
}
_currentState = AsyncState.Initializing;
foreach (var (type,value) in StateDictionary)
{
await value.InitializeAsync();
}
foreach (var (type,value) in StateDictionary)
{
value.Initialize();
}
_currentState = AsyncState.None;
}
public void UpdateState(float deltaTime)
{
if(_cancellationTokenSource.IsCancellationRequested)return;
switch (_currentState)
{
case AsyncState.None:
if (_nextState is not null)
{
_currentState = AsyncState.InEntering;
_currentTask = CurrentState.OnStateEntryAsync(CurrentState).AsTask();
OnStateChanging?.Invoke(CurrentState,_nextState);
return;
}
_currentState = AsyncState.InUpdating;
if (CurrentState is not null)
{
_currentTask = CurrentState.OnStateUpdateAsync(deltaTime).AsTask();
}
break;
case AsyncState.InExiting:
if (_currentTask.IsCompleted)
{
_currentState =_nextState is not null ? AsyncState.InEntering : AsyncState.None;
CurrentState.OnStateExit(CurrentState,_nextState);
if (_nextState is not null)
{
_currentTask = _nextState.OnStateEntryAsync(_nextState).AsTask();
}
}
break;
case AsyncState.InEntering:
if (_currentTask.IsCompleted)
{
_currentState = AsyncState.None;
_nextState.OnStateEntry(CurrentState);
OnStateChanged?.Invoke(CurrentState,_nextState);
CurrentState = _nextState;
_nextState = default;
}
break;
case AsyncState.InUpdating:
if (_currentTask.IsCompleted)
{
_currentTask = null;
_currentState = AsyncState.None;
}
break;
}
}
public void DisposeState()
{
}
public void TransitionState<State>() where State : T
{
throw new InvalidOperationException("动态异步状态机不支持,请使用TransitionAsync");
}
public void TransitionState(T state)
{
}
public void Dispose()
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
}
}
}

View File

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

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace BITKit.StateMachine
{
@@ -31,6 +32,18 @@ namespace BITKit.StateMachine
void OnStateExit(IState old, IState newState);
}
public interface IStateAsync:IState
{
/// <summary>
/// 识别符,用于识别多个相同状态但不同用途的状态机
/// </summary>
int Identifier { get; set; }
UniTask InitializeAsync();
UniTask OnStateEntryAsync(IState old);
UniTask OnStateUpdateAsync(float deltaTime);
UniTask OnStateExitAsync(IState old, IState newState);
}
public interface IStateMachine<T> where T:IState
{
bool Enabled { get; set; }
@@ -50,7 +63,6 @@ namespace BITKit.StateMachine
void InvokeOnStateRegistered(T state){}
void InvokeOnStateUnRegistered(T state){}
}
public static class StateMachineUtils
{
public static void Register<T>(this IStateMachine<T> stateMachine, T newState) where T : IState

View File

@@ -43,6 +43,14 @@ namespace BITKit.UX
/// </summary>
/// <param name="panelName">面板名称</param>
void Entry(string panelName);
/// <summary>
/// 当前面板
/// </summary>
IUXPanel CurrentPanel { get; }
/// <summary>
/// 面板改变回调
/// </summary>
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
}
}