@flare-engine/ ecs (英语)
拱形比特面具ECS - World (弹出/销毁, query + 键 queryBuilder + 键 removeQuery, (中文(简体) ), forEachEntity, (中文(简体) ), entityCount, (中文(简体) ), Query 零分数c entities 走着走着 onEntityAdded 编号 : onEntityRemoved 信号, defineComponent, (中文(简体) ), defineSystem, (中文(简体) ), definePrefab, (中文(简体) ), FSM。内置组件: Position, (中文(简体) ), Velocity, (中文(简体) ), Rotation, (中文(简体) ), Scale, (中文(简体) ), Parent, (中文(简体) ), Lifetime, (中文(简体) ), Follower五个系统工厂: createMovementSystem, (中文(简体) ), createLifetimeSystem, (中文(简体) ), createBoundsCullSystem, (中文(简体) ), createFollowerSystem, (中文(简体) ), createOrphanCullSystem. 最大 32 个组件类型总计—— 组件- id 位模是模块- 全球, 由所有组件共享 World 实例.
安装
bun add @flare-engine/ecs
使用量
import { World, defineComponent, Position, Velocity } from '@flare-engine/ecs';
const Health = defineComponent<{ hp: number }>('Health', () => ({ hp: 100 }));
const world = new World();
const e = world.spawn();
world.add(e, Position, { x: 0, y: 0 });
world.add(e, Velocity, { x: 1, y: 0 });
world.add(e, Health);
const movers = world.query(Position, Velocity);
const ids = movers.entities;
for (let i = 0; i < ids.length; i++) {
const id = ids[i]!;
/* ... */
}
正在查询
query.entities 是现场直播 readonly number[] 匹配的标识符——按指数排列,如上,没有分配. for (const id of query) 也可以工作,在框架外很好,但它每回循环生成一个执行器,在Hermes(反应原生)上,每个实体有一个结果对象,所以使其远离系统并渲染代码. 阵列是活的:摧毁一个实体或改变其组件中行走将另一个实体移动入你下面的槽中——首先收集,然后行动.
系统
defineSystem 将查询绑定为每帧函数; 登记返回的 worldFn 与 world.addSystem (优先级别较低者优先) removeSystem。 。 。 .
import { World, defineSystem, Position, Velocity } from '@flare-engine/ecs';
const world = new World();
const movers = world.query(Position, Velocity);
const moveSys = defineSystem(movers, (query, dt, w) => {
const ids = query.entities;
for (let i = 0; i < ids.length; i++) {
const id = ids[i]!;
const pos = w.get(id, Position)!;
const vel = w.get(id, Velocity)!;
pos.x += vel.x * dt;
pos.y += vel.y * dt;
}
});
world.addSystem(moveSys.worldFn, 0);
world.step(1 / 60);
world.removeSystem(moveSys.worldFn);
预发数据
definePrefab plain-object数据通过参考存储,由每个产卵共享;通过 () => T 每实体可变状态:
import { definePrefab, Position, Velocity } from '@flare-engine/ecs';
// Every bullet gets its own Position and Velocity object.
const Bullet = definePrefab(
'Bullet',
[Position, () => ({ x: 0, y: 0 })],
[Velocity, () => ({ x: 0, y: -300 })],
);
见 API 规格。 。 。 .
许可证
阿帕奇 - 2.0