使用 ECS 为 C# 编写代码可能是最简单的方式
A Simple and easy to use C# Entity Component System. Min C# Framework 4.7
Warning - This project is a very much a hobby project to learn more about ECS. Because of this, breaking API changes may occur as I tinker on the framework.
An Entity is simply an ID that associates a group of components together. To create an entity you first need to create a new world, then using that world create your entity with it's components as arguments.
…Anything that can be put into a list can be a component. Only one component of each type can be associated with an entity, however there's nothing stopping you having a List or array as a component. Setting more than one component of the same type will simply overwrite the old one. The function can take up to 32 components, but entities themselves have no component limit. Due to how entities are structured, it's more efficient to set all components upfront during creation than adding them one at a time with entity.Set().
Manipulating entities is pretty simple
…There are 2 component callbacks in SimpleECS, world.OnSet() and world.OnRemove().
OnSet is invoked anytime a valid entity uses entity.Set(value). OnSet is also invoked on all components added during entity creation with world.CreateEntity(). OnSet is not invoked on the defaulted components created during archetype.CreateEntity(). On set has 3 overloads depending on how much information you need during the callback.
…OnRemove is invoked anytime a valid entity removes that component. OnRemove is also invoked when an entity with that component is destroyed. OnRemove has 2 overloads depending on how much information you need during the callback.
// the callback is invoked whenever an entity in the world with the component removes the component or is destroyed
world.OnRemove((int val) =>
Console.WriteLine($"an entity removed int {val}"));
// put the entity as the first paramter to get the invoking entity
world.OnRemove((Entity e, int val) =>
Console.WriteLine($"{e} removed int {val}");
// like OnSet, OnRemove can be named and registered and unregistered
void IntRemoveCallback(int value)
{
// do something
}
world.OnRemove(IntRemoveCallback);
world.OnRemove(IntRemoveCallback, false);Queries let you iterate over entities based on specified components. You can specify up to 12 components to iterate over. Queries cache their results and only update when new archetypes are created or destroyed or their filter changes.
…During query.Foreach, functions that cause structural changes to the ECS are cached and applied after iteration is complete. This is to prevent iterator invalidation. Some functions that cause structural changes are:
Entities created during query.Foreach() will be invalid during the function. However you can still set or remove components on that entity, they will simply be applied when the Foreach function completes.
…For maximum control over query iteration or a small performance boost, manual iteration is possible. When iterating manually though, make sure not to do any structural changes as this may invalidate iterators.
…The world class is what manages all the underlying archetypes and their entities.
…暂无开放 Issues,或尚未同步最近议题。