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

113 lines
3.2 KiB
C#
Raw Normal View History

2023-08-12 01:43:24 +08:00
using System.Threading;
using System;
2023-10-02 23:24:56 +08:00
using System.ComponentModel.Design;
#if NET5_0_OR_GREATER
using Microsoft.Extensions.DependencyInjection;
#endif
2023-10-30 01:25:53 +08:00
namespace BITKit.Entities
2023-08-12 01:43:24 +08:00
{
/// <summary>
/// 基本实体
/// </summary>
public interface IEntity
{
ulong Id { get; }
2023-10-02 23:24:56 +08:00
CancellationToken CancellationToken { get; }
bool TryGetComponent<T>(out T component);
2023-08-12 01:43:24 +08:00
IEntityComponent[] Components { get; }
2023-10-02 23:24:56 +08:00
bool RegisterComponent<T>(T component);
IServiceProvider ServiceProvider { get; }
2023-11-02 20:58:55 +08:00
#if NET5_0_OR_GREATER
IServiceCollection ServiceCollection { get; }
#endif
2023-10-20 19:31:12 +08:00
void Inject(object obj);
2023-08-12 01:43:24 +08:00
}
/// <summary>
/// 基本实体组件
/// </summary>
2023-10-30 01:25:53 +08:00
public interface IEntityComponent
2023-08-12 01:43:24 +08:00
{
IEntity Entity { get; set; }
2023-10-02 23:24:56 +08:00
#if NET5_0_OR_GREATER
void BuildService(IServiceCollection serviceCollection);
#endif
2023-08-12 01:43:24 +08:00
}
2023-10-30 01:25:53 +08:00
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();
}
2023-08-12 01:43:24 +08:00
/// <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);
2023-10-02 23:24:56 +08:00
2023-08-12 01:43:24 +08:00
/// <summary>
/// 查询Entity,例如
/// </summary>
/// <para>var rotationEntities=EntitiesService.Query&lt;RotationComponent&gt;</para>
2023-10-02 23:24:56 +08:00
IEntity[] Query<T>();
2023-08-12 01:43:24 +08:00
/// <summary>
/// 查询1个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
2023-10-02 23:24:56 +08:00
T[] QueryComponents<T>();
2023-08-12 01:43:24 +08:00
/// <summary>
/// 查询2个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <returns></returns>
2023-10-02 23:24:56 +08:00
ValueTuple<T, T1>[] QueryComponents<T, T1>();
2023-08-12 01:43:24 +08:00
/// <summary>
/// 查询3个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <returns></returns>
2023-10-02 23:24:56 +08:00
ValueTuple<T, T1, T2>[] QueryComponents<T, T1, T2>();
2023-08-12 01:43:24 +08:00
}
}