This commit is contained in:
CortexCore
2023-08-18 11:03:06 +08:00
parent 0cee02523b
commit 003fff50fa
16 changed files with 36 additions and 22 deletions

View File

@@ -18,6 +18,8 @@ public partial class GodotEntitiesService : Node,IEntitiesService
}
private readonly Dictionary<ulong,IEntity> _entities=new ();
private CancellationTokenSource _cancellationTokenSource;
public event Action<IEntity> OnAdd;
public event Action<IEntity> OnRemove;
public IEntity[] Entities => _entities.Values.ToArray();
public bool Register(IEntity entity)
{
@@ -40,11 +42,28 @@ public partial class GodotEntitiesService : Node,IEntitiesService
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
public IEntity Get(ulong id)
{
throw new NotImplementedException();
}
public IEntity[] Query<T>() where T : IEntityComponent
{
return _entities.Values.Where(x => x.TryGetComponent<T>(out _)).ToArray();
}
T[] IEntitiesService.QueryComponents<T>()
{
return _entities.Values
.Where(x => x.TryGetComponent<T>(out _))
.Select(x =>
{
var component = x.Components.Single(x => x is T);
return (T)component;
})
.ToArray();
}
public ValueTuple<T>[] QueryComponents<T>() where T : IEntityComponent
{
return _entities.Values
@@ -59,16 +78,6 @@ public partial class GodotEntitiesService : Node,IEntitiesService
}
public (T, T1)[] QueryComponents<T, T1>() where T : IEntityComponent where T1 : IEntityComponent
{
// return _entities.Values
// .Where(x => x.TryGetComponent<T>(out _) && x.TryGetComponent<T1>(out _))
// .Select(x =>
// {
// var component = (T)x.Components.Single(x => x is T);
// var component1 = (T1)x.Components.Single(x => x is T1);
// (T, T1) value = new(component, component1);
// return value;
// })
// .ToArray();
var entities = _entities.Values.Where(x => x.TryGetComponent<T>(out _) && x.TryGetComponent<T1>(out _));
var result = new List<(T, T1)>();
foreach (var entity in entities)