小编tot*_*ner的帖子

为系统循环创建高性能组件池

我正在设置一个非常小的Entity-Component-System示例,并且组件池存在一些问题.

目前我的实体只是ID(GUID),这很好.

每个系统都必须实现该ISystem接口

internal interface ISystem
{
    void Run();
}
Run Code Online (Sandbox Code Playgroud)

并存储在KeyedByTypeCollection.此集合确保每个系统都是唯一的.

每个组件都必须实现该IComponent接口.

internal interface IComponent
{
}
Run Code Online (Sandbox Code Playgroud)

通过这样做,我可以将所有不同的组件类型存储到其匹配的组件池中.每个游泳池都是Dictionary<Guid, IComponent>.键表示实体的ID,值是该实体的组件.每个池都存储在一个池中KeyedByTypeCollection,以确保组件池是唯一的.

目前我的EntityManager类包含核心逻辑.我不知道它是否需要静态但目前我的系统需要从中获取一些信息.

处理组件池的重要核心方法是:

internal static class EntityManager
{
    public static List<Guid> activeEntities = new List<Guid>();

    private static KeyedByTypeCollection<Dictionary<Guid, IComponent>> componentPools = new KeyedByTypeCollection<Dictionary<Guid, IComponent>>();

    public static KeyedByTypeCollection<ISystem> systems = new KeyedByTypeCollection<ISystem>(); // Hold unique Systems

    public static Guid CreateEntity() // Add a new GUID and return it
    {
        Guid entityId …
Run Code Online (Sandbox Code Playgroud)

c# entity-component-system

7
推荐指数
1
解决办法
208
查看次数

用事件连接系统

使用Entity-Component-System模式我想要将一些系统与事件连接起来.所以有些系统不应该在循环中运行,它们应该只是按需运行.

鉴于Health系统的示例,死亡系统应仅在组件低于1健康时运行.

我想过有两种类型的系统.第一种类型是周期性系统.这将运行每帧一次中,例如一个渲染运动系统.另一种类型是基于事件的系统.作为之间的连接之前提到的健康死亡.

首先,我创建了两种系统类型使用的基本接口.

internal interface ISystem
{
    List<Guid> EntityCache { get; } // Only relevant entities get stored in there

    ComponentRequirements ComponentRequirements { get; } // the required components for this system

    void InitComponentRequirements();

    void InitComponentPools(EntityManager entityManager);

    void UpdateCacheEntities(); // update all entities from the cache

    void UpdateCacheEntity(Guid cacheEntityId); // update a single entity from the cache
}
Run Code Online (Sandbox Code Playgroud)

我进一步创建了接口

internal interface IReactiveSystem : ISystem
{
// event …
Run Code Online (Sandbox Code Playgroud)

c# entity-component-system

7
推荐指数
1
解决办法
314
查看次数

标签 统计

c# ×2

entity-component-system ×2