This commit is contained in:
CortexCore
2024-11-03 16:38:17 +08:00
parent 056e2cada5
commit 4ba741408d
4693 changed files with 2445 additions and 5443 deletions

View File

@@ -54,79 +54,87 @@ namespace BITKit.Entities
public T[] QueryComponents<T>()
{
return _queryCache.GetOrAdd(typeof(T), type =>
{
return _entities.Values.Where(entity => entity.TryGetComponent(out T component)).ToArray();
}).Cast<T>().ToArray();
throw new NotImplementedException();
// return _queryCache.GetOrAdd(typeof(T), type =>
// {
// return _entities.Values.Where(entity => entity.TryGetComponent(out T component)).ToArray();
// }).Cast<T>().ToArray();
}
public (T, T1)[] QueryComponents<T, T1>()
{
List<(T, T1)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1))
list.Add((t, t1));
}
return list.ToArray();
throw new NotImplementedException();
// List<(T, T1)> list = new();
// foreach (var entity in _entities.Values)
// {
// if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1))
// list.Add((t, t1));
// }
// return list.ToArray();
}
public (T, T1, T2)[] QueryComponents<T, T1, T2>()
{
List<(T, T1, T2)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2))
list.Add((t, t1, t2));
}
return list.ToArray();
throw new NotImplementedException();
// List<(T, T1, T2)> list = new();
// foreach (var entity in _entities.Values)
// {
// if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2))
// list.Add((t, t1, t2));
// }
// return list.ToArray();
}
public (T, T1, T2, T3)[] QueryComponents<T, T1, T2, T3>()
{
List<(T, T1, T2, T3)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3))
list.Add((t, t1, t2, t3));
}
return list.ToArray();
throw new NotImplementedException();
// List<(T, T1, T2, T3)> list = new();
// foreach (var entity in _entities.Values)
// {
// if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3))
// list.Add((t, t1, t2, t3));
// }
// return list.ToArray();
}
public (T, T1, T2, T3, T4)[] QueryComponents<T, T1, T2, T3, T4>()
{
List<(T, T1, T2, T3, T4)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3) && entity.TryGetComponent(out T4 t4))
list.Add((t, t1, t2, t3, t4));
}
return list.ToArray();
throw new NotImplementedException();
// List<(T, T1, T2, T3, T4)> list = new();
// foreach (var entity in _entities.Values)
// {
// if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3) && entity.TryGetComponent(out T4 t4))
// list.Add((t, t1, t2, t3, t4));
// }
// return list.ToArray();
}
public (T, T1, T2, T3, T4, T5)[] QueryComponents<T, T1, T2, T3, T4, T5>()
{
List<(T, T1, T2, T3, T4, T5)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3) && entity.TryGetComponent(out T4 t4) && entity.TryGetComponent(out T5 t5))
list.Add((t, t1, t2, t3, t4, t5));
}
return list.ToArray();
throw new NotImplementedException();
// List<(T, T1, T2, T3, T4, T5)> list = new();
// foreach (var entity in _entities.Values)
// {
// if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3) && entity.TryGetComponent(out T4 t4) && entity.TryGetComponent(out T5 t5))
// list.Add((t, t1, t2, t3, t4, t5));
// }
// return list.ToArray();
}
public (T, T1, T2, T3, T4, T5, T6)[] QueryComponents<T, T1, T2, T3, T4, T5, T6>()
{
List<(T, T1, T2, T3, T4, T5, T6)> list = new();
foreach (var entity in _entities.Values)
{
if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3) && entity.TryGetComponent(out T4 t4) && entity.TryGetComponent(out T5 t5) && entity.TryGetComponent(out T6 t6))
list.Add((t, t1, t2, t3, t4, t5, t6));
}
return list.ToArray();
throw new NotImplementedException();
// List<(T, T1, T2, T3, T4, T5, T6)> list = new();
// foreach (var entity in _entities.Values)
// {
// if (entity.TryGetComponent(out T t) && entity.TryGetComponent(out T1 t1) && entity.TryGetComponent(out T2 t2) && entity.TryGetComponent(out T3 t3) && entity.TryGetComponent(out T4 t4) && entity.TryGetComponent(out T5 t5) && entity.TryGetComponent(out T6 t6))
// list.Add((t, t1, t2, t3, t4, t5, t6));
// }
// return list.ToArray();
}
public ValueTuple<T, T1, T2, T3, T4, T5, T6, TRest>[] QueryComponents<T, T1, T2, T3, T4, T5, T6, TRest>() where TRest : struct
{
throw new NotImplementedException();
// throw new NotImplementedException();
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
@@ -7,9 +8,25 @@ namespace BITKit.Entities
{
public class Entity : IEntity, IDisposable
{
private class EntityServiceProvider : IServiceProvider
{
public ServiceProvider ServiceProvider;
public readonly List<Object> Services = new();
public object GetService(Type serviceType)
{
var value = ServiceProvider.GetService(serviceType);
if (value != null)
{
Services.TryAdd(value);
}
return value;
}
}
public Entity()
{
RegisterComponent<IEntity>(this);
ServiceCollection.AddSingleton<IEntity>(this);
}
public void WaitForInitializationComplete()
{
@@ -19,51 +36,26 @@ namespace BITKit.Entities
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
private readonly CancellationTokenSource _cancellationTokenSource = new();
public bool TryGetComponent<T>(out T component)
public IServiceProvider ServiceProvider
{
var value = ServiceProvider.GetService<T>();
if (value != null)
get
{
component = value;
return true;
if (_serviceProvider is not null)
{
return _serviceProvider;
}
var value = new EntityServiceProvider()
{
ServiceProvider = ServiceCollection.BuildServiceProvider()
};
_serviceProvider = value;
return _serviceProvider;
}
component = default!;
return false;
}
public bool TryGetComponent(Type type, out IEntityComponent component)
{
var value = ServiceProvider.GetService(type);
if (value != null)
{
component = (IEntityComponent)value;
return true;
}
component = default!;
return false;
}
public IEntityComponent[] Components => ServiceCollection.OfType<IEntityComponent>().ToArray();
public bool RegisterComponent<T>(T component)
{
_services.Add(component);
ServiceCollection.AddSingleton(typeof(T), component);
return true;
}
public IServiceProvider ServiceProvider => _serviceProvider ??= ServiceCollection.BuildServiceProvider();
public IServiceCollection ServiceCollection { get; } = new ServiceCollection();
private IServiceProvider _serviceProvider;
private readonly CacheList<object> _services = new();
public object[] GetServices()
{
return _services.ToArray();
}
private EntityServiceProvider _serviceProvider;
public object[] GetServices()=> _serviceProvider.Services.ToArray();
public void Inject(object obj)
{
foreach (var fieldInfo in obj.GetType().GetFields(ReflectionHelper.Flags))
@@ -78,14 +70,10 @@ namespace BITKit.Entities
public void Dispose()
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
foreach (var service in _services)
_serviceProvider.ServiceProvider.Dispose();
foreach (var x in GetServices().OfType<IDisposable>())
{
if (service is IDisposable disposable)
{
disposable.Dispose();
}
x.Dispose();
}
}
}

View File

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

View File

@@ -1,6 +1,7 @@
using System.Threading;
using System;
using System.ComponentModel.Design;
using Microsoft.Extensions.DependencyInjection;
#if NET5_0_OR_GREATER
using Microsoft.Extensions.DependencyInjection;
#endif
@@ -11,44 +12,14 @@ namespace BITKit.Entities
/// </summary>
public interface IEntity
{
/// <summary>
/// 等待初始化完成,通常用于其他系统需要等待实体初始化完成
/// </summary>
void WaitForInitializationComplete();
int Id { get; }
CancellationToken CancellationToken { get; }
bool TryGetComponent<T>(out T component);
bool TryGetComponent(Type type, out IEntityComponent component);
IEntityComponent[] Components { get; }
bool RegisterComponent<T>(T component);
IServiceProvider ServiceProvider { get; }
#if NET5_0_OR_GREATER
IServiceCollection ServiceCollection { get; }
object[] GetServices();
#endif
void Inject(object obj);
}
/// <summary>
/// 基本实体组件
/// </summary>
public interface IEntityComponent
{
IEntity Entity { get; set; }
#if NET5_0_OR_GREATER
void BuildService(IServiceCollection serviceCollection);
#endif
}
public interface IEntityBehavior:IEntityComponent
{
void Initialize(IEntity _entity);
void OnAwake();
void OnStart();
void OnUpdate(float deltaTime);
void OnFixedUpdate(float deltaTime);
void OnLateUpdate(float deltaTime);
void OnDestroyComponent();
}
/// <summary>
/// 基本实体服务
/// </summary>
public interface IEntitiesService