SanderMertens/flecs
GitHub: SanderMertens/flecs
Flecs 是一个专为 C 和 C++ 打造的高性能实体组件系统(ECS)框架,通过优化的数据存储和并行调度,支持构建大规模、高并发的游戏与模拟应用。
Stars: 8193 | Forks: 588

[](https://github.com/SanderMertens/flecs/releases)
[](https://github.com/SanderMertens/flecs/blob/master/LICENSE)
[](https://www.flecs.dev/flecs/md_docs_2Docs.html)
[](https://github.com/SanderMertens/flecs/actions?query=workflow%3ACI)
[](https://discord.gg/BEzP5Rgrrp)
Flecs 是一个快速轻量的 Entity Component System(实体组件系统),让你能够构建拥有数百万实体的游戏和模拟([加入 Discord!](https://discord.gg/BEzP5Rgrrp))。以下是该框架的一些亮点:
- 快速且[可移植](#language-bindings)、零依赖的 [C99 API](https://www.flecs.dev/flecs/group__c.html)
- 现代类型安全的 [C++17 API](https://www.flecs.dev/flecs/group__cpp.html),不使用 STL 容器
- 首个完全支持 [Entity Relationships](https://www.flecs.dev/flecs/md_docs_2Relationships.html) 的开源 ECS!
- 对 [hierarchies](https://www.flecs.dev/flecs/md_docs_2HierarchiesManual.html) 和 [prefabs](https://www.flecs.dev/flecs/md_docs_2PrefabsManual.html) 的快速原生支持
- 代码库构建时间少于 5 秒
- 使用 emscripten 在[浏览器中](https://flecs.dev/city) 无需修改即可运行
- 缓存友好的 [archetype/SoA storage](https://ajmmertens.medium.com/building-an-ecs-2-archetypes-and-vectorization-fe21690805f9),每帧可处理数百万实体
- 跨共享库/DLL 开箱即用的自动组件注册
- 使用 [queries](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/queries/basics) 编写自由函数,或在 [systems](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/systems/pipeline) 中自动运行代码
- 使用快速无锁调度器在多个 CPU 核心上运行游戏
- 通过运行超过 13000 个测试的 [CI](https://github.com/SanderMertens/flecs/actions),在所有主要编译器和平台上进行了验证
- 集成的 [reflection framework](https://www.flecs.dev/flecs/group__c__addons__meta.html),包含 [JSON serializer](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/reflection/basics_json) 并支持 [runtime components](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/reflection/runtime_component)
- 组件的 [Unit annotations](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/reflection/units)
- 强大的 [query language](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/queries),支持 [joins](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/queries/setting_variables) 和 [inheritance](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/queries/component_inheritance)
- 用于分析 ECS 性能的 [Statistics addon](https://www.flecs.dev/flecs/group__c__addons__stats.html)
- 用于监控和控制应用程序的基于 Web 的 UI:
[](https://flecs.dev/explorer)
## 什么是 Entity Component System?
ECS 是一种组织代码和数据的方式,让你能够构建规模更大、更复杂且更易于扩展的游戏。当一个系统满足以下条件时,它被称为 ECS:
- 拥有唯一标识游戏中对象的 _entities_(实体)
- 拥有可以添加到实体的 _components_(组件),它们是数据类型
- 拥有为所有匹配组件 _query_(查询)的实体运行的 _systems_(系统),它们是函数
欲了解更多信息,请查看 [ECS FAQ](https://github.com/SanderMertens/ecs-faq)!
## 让我看看代码!
C99 示例:
```
typedef struct {
float x, y;
} Position, Velocity;
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);
for (int i = 0; i < it->count; i++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init();
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Velocity);
ECS_SYSTEM(ecs, Move, EcsOnUpdate, Position, Velocity);
ecs_entity_t e = ecs_insert(ecs,
ecs_value(Position, {10, 20}),
ecs_value(Velocity, {1, 2}));
while (ecs_progress(ecs, 0)) { }
}
```
同样的 C++ 示例:
```
struct Position {
float x, y;
};
struct Velocity {
float x, y;
};
int main(int argc, char *argv[]) {
flecs::world ecs;
ecs.system()
.each([](Position& p, const Velocity& v) {
p.x += v.x;
p.y += v.y;
});
auto e = ecs.entity()
.insert([](Position& p, Velocity& v) {
p = {10, 20};
v = {1, 2};
});
while (ecs.progress()) { }
}
```
## 使用 Flecs 的项目
如果你有想要分享的项目,请在 [Discord](https://discord.gg/BEzP5Rgrrp) 上告诉我!
### Tempest Rising
[](https://store.steampowered.com/app/1486920/Tempest_Rising/)
### Territory Control 2
[](https://store.steampowered.com/app/690290/Territory_Control_2/)
### Resistance is Brutal
[](https://store.steampowered.com/app/3378140/Resistance_Is_Brutal/)
### Rescue Ops: Wildfire
[](https://store.steampowered.com/app/2915770/Rescue_Ops_Wildfire/)
### Age of Respair
[](https://store.steampowered.com/app/3164360/Age_of_Respair/)
### FEAST
[](https://store.steampowered.com/app/3823480/FEAST/)
### Gloam Vault
[](https://store.steampowered.com/app/3460840/Gloamvault/)
### Antimatcher
[](https://store.steampowered.com/app/4336520/AntiMatcher/)
### Writ of Battle
[](https://store.steampowered.com/app/4445990/Writ_of_Battle/)
### Extermination Shock
[](https://store.steampowered.com/app/2510820/Extermination_Shock/)
### The Forge
[](https://github.com/ConfettiFX/The-Forge)
### ECS survivors
[](https://laurent-voisard.itch.io/ecs-survivors/)
### Tome Tumble Tournament
[](https://terzalo.itch.io/tome-tumble-tournament)
### Sol Survivor
[](https://nicok.itch.io/sol-survivor-demo)
### After Sun
[](https://github.com/foxnne/aftersun)
## Flecs Hub
[Flecs Hub](https://github.com/flecs-hub) 是一个仓库集合,展示了如何使用 Flecs 构建游戏系统,例如输入处理、层级变换和渲染。
模块 | 描述
------------|------------------
[flecs.components.cglm](https://github.com/flecs-hub/flecs-components-cglm) | cglm(数学)类型的组件注册
[flecs.components.input](https://github.com/flecs-hub/flecs-components-input) | 描述键盘和鼠标输入的组件
[flecs.components.transform](https://github.com/flecs-hub/flecs-components-transform) | 描述位置、旋转和缩放的组件
[flecs.components.physics](https://github.com/flecs-hub/flecs-components-physics) | 描述物理和运动的组件
[flecs.components.geometry](https://github.com/flecs-hub/flecs-components-geometry) | 描述几何形状的组件
[flecs.components.graphics](https://github.com/flecs-hub/flecs-components-graphics) | 用于计算机图形学的组件
[flecs.components.gui](https://github.com/flecs-hub/flecs-components-gui) | 用于描述 GUI 组件的组件
[flecs.systems.transform](https://github.com/flecs-hub/flecs-systems-transform) | 用于场景图的层级变换
[flecs.systems.physics](https://github.com/flecs-hub/flecs-systems-physics) | 用于移动物体和碰撞检测的系统
[flecs.systems.sokol](https://github.com/flecs-hub/flecs-systems-sokol) | 基于 Sokol 的渲染器
[flecs.game](https://github.com/flecs-hub/flecs-game) | 通用游戏系统,例如相机控制器
## 语言绑定
以下语言绑定已基于 Flecs 开发!请注意,这些项目由热心的社区成员构建和维护,可能并不总是与 master 分支的最新提交保持同步!
- C#:
- [BeanCheeseBurrito/Flecs.NET](https://github.com/BeanCheeseBurrito/Flecs.NET)
- Rust:
- [Flecs-Rust](https://github.com/Indra-db/Flecs-Rust)
- [flecs-polyglot](https://github.com/flecs-hub/flecs-polyglot)
- Zig:
- [zig-gamedev/zflecs](https://github.com/zig-gamedev/zflecs)
- Lua:
- [sro5h/flecs-luajit](https://github.com/sro5h/flecs-luajit)
- [flecs-hub/flecs-lua](https://github.com/flecs-hub/flecs-lua)
- Clojure
- [vybe-flecs](https://vybegame.dev/vybe-flecs)
标签:C, C++, C17, C99, ECS, Emscripten, Entity Component System, LangChain, SoA, Terraform, WebAssembly, 原型存储, 实体关系, 客户端加密, 层级结构, 开源库, 搜索引擎爬虫, 数据导向设计, 数据擦除, 架构模式, 模拟仿真, 游戏开发, 组件注册, 轻量级, 零依赖