BITFALL/Assets/BITKit/Core/ECS/EntityComponentSystem.cs

113 lines
3.2 KiB
C#

using System.Threading;
using System;
using System.ComponentModel.Design;
#if NET5_0_OR_GREATER
using Microsoft.Extensions.DependencyInjection;
#endif
namespace BITKit.Entities
{
/// <summary>
/// 基本实体
/// </summary>
public interface IEntity
{
ulong Id { get; }
CancellationToken CancellationToken { get; }
bool TryGetComponent<T>(out T component);
IEntityComponent[] Components { get; }
bool RegisterComponent<T>(T component);
IServiceProvider ServiceProvider { get; }
#if NET5_0_OR_GREATER
IServiceCollection ServiceCollection { get; }
#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
{
/// <summary>
/// 当添加Entity时
/// </summary>
public event Action<IEntity> OnAdd;
/// <summary>
/// 当移除Entity时
/// </summary>
public event Action<IEntity> OnRemove;
/// <summary>
/// 所有Entity
/// </summary>
IEntity[] Entities { get; }
/// <summary>
/// 注册Entity
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
bool Register(IEntity entity);
/// <summary>
/// 注销Entity
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
bool UnRegister(IEntity entity);
CancellationToken CancellationToken { get; }
/// <summary>
/// 通过Id获取Entity
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
IEntity Get(ulong id);
/// <summary>
/// 查询Entity,例如
/// </summary>
/// <para>var rotationEntities=EntitiesService.Query&lt;RotationComponent&gt;</para>
IEntity[] Query<T>();
/// <summary>
/// 查询1个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T[] QueryComponents<T>();
/// <summary>
/// 查询2个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <returns></returns>
ValueTuple<T, T1>[] QueryComponents<T, T1>();
/// <summary>
/// 查询3个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <returns></returns>
ValueTuple<T, T1, T2>[] QueryComponents<T, T1, T2>();
}
}