🚀 ecs-lite
基于 ts 实现的纯 ecs 库,可用于学习交流及 H5 游戏开发!
🚚 文档
更多内容请参考 文档,您可以关注 changelog 获取最新进展!
🛠 安装与使用
1.安装
npm i ecs-lite -S
# or
yarn add ecs-lite -S
2.使用
import { Component, System, World } from 'ecs-lite'
// 1.定义组件
class Position extends Component {
constructor(x, y) {
super()
this.x = x
this.y = y
}
}
// 2.定义系统
class PositionSystem extends System {
constructor() {
super()
}
update(world) {
for (const [entity, componentMap] of world.view(Position)) {
const position = componentMap.get(Position)
position.x += 10
console.log('组件实例', position)
}
}
}
// 3.初始化实体、组件、系统
const world = new World()
world.addEntityWithComponents(world.createEntity('car'), new Position(0, 100))
world.addSystem(new PositionSystem())
// 4.启动world
world.start()