readme
This commit is contained in:
46
BITKit/Scripts/ECS/GodotEntitiesService.cs
Normal file
46
BITKit/Scripts/ECS/GodotEntitiesService.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using BITKit.Core.Entites;
|
||||
// ReSharper disable All
|
||||
|
||||
namespace BITKit;
|
||||
/// <summary>
|
||||
/// 基于Godot.Node的IEntitiesService实现
|
||||
/// </summary>
|
||||
public partial class GodotEntitiesService : Node,IEntitiesService
|
||||
{
|
||||
public GodotEntitiesService()
|
||||
{
|
||||
DI.Register<IEntitiesService>(this);
|
||||
}
|
||||
private readonly Dictionary<ulong,IEntity> _entities=new ();
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
public IEntity[] Entities => _entities.Values.ToArray();
|
||||
public bool Register(IEntity entity)
|
||||
{
|
||||
return _entities.TryAdd(entity.Id, entity);
|
||||
}
|
||||
public bool UnRegister(IEntity entity)
|
||||
{
|
||||
return _entities.TryRemove(entity.Id);
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_cancellationTokenSource = new();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if(disposing)_cancellationTokenSource.Cancel();
|
||||
}
|
||||
|
||||
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
|
||||
|
||||
public IEntity[] Query<T>() where T : IEntityComponent
|
||||
{
|
||||
return _entities.Values.Where(x => x.TryGetComponent<T>(out _)).ToArray();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user