1.0.2 • Published 3 years ago
@aodazhang/ecs-lite v1.0.2
🚀 ecs-lite
基于 typescript 实现的纯 ecs 库,用于学习交流及简易的 H5 游戏开发!
🚚 文档
更多内容请参考 文档,您可以关注 changelog 获取最新进展!
🛠 安装与使用
1.安装
npm i @aodazhang/ecs-lite -S
# or
yarn add @aodazhang/ecs-lite -S
2.使用
import { Component, System, World } from '@aodazhang/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()