1.0.2 • Published 5 months ago

ecs-lite v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

🚀 ecs-lite

gzip coverage

基于 ts 实现的纯 ecs 库,可用于学习交流及 H5 游戏开发!

🚚 文档

更多内容请参考 文档,您可以关注 changelog 获取最新进展!

示例:flappy bird

🛠 安装与使用

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()