miniplex
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMiniplex ECS
Miniplex ECS
Use Miniplex for minimalistic Entity Component System with TypeScript support.
When setting up or undertaking important changes with Miniplex, fetch the documentations:
使用Miniplex构建支持TypeScript的极简式实体组件系统(ECS)。
在设置Miniplex或对其进行重要更改时,请查阅以下文档:
Technique
技术实现
Define an entity type with optional properties, create a world, and query entities based on their properties. Use for React bindings with the component.
miniplex-reactEntities定义包含可选属性的实体类型,创建world,并根据实体属性查询实体。使用提供的组件实现React绑定。
miniplex-reactEntitiesKey Concepts
核心概念
- Entity type defines all possible components as optional properties
- creates typed queries
world.with('prop1', 'prop2') - and
world.add()for entity lifecycleworld.remove() - and
world.addComponent()for component lifecycleworld.removeComponent() - renders entities reactively
<Entities in={query}>{Component}</Entities> - Entity props are passed directly to child components
- 实体类型将所有可能的组件定义为可选属性
- 用于创建类型化查询
world.with('prop1', 'prop2') - 使用和
world.add()管理实体生命周期world.remove() - 使用和
world.addComponent()管理组件生命周期world.removeComponent() - 可响应式渲染实体
<Entities in={query}>{Component}</Entities> - 实体属性会直接传递给子组件
What NOT to Use
不推荐使用的功能
- /
onEntityAdded- Prefer using data and systems to trigger things (e.g., timers, flags)onEntityRemoved - - Don't use predicate-based filtering
.where()
- /
onEntityAdded- 建议使用数据和系统来触发操作(例如计时器、标记)onEntityRemoved - - 不要使用基于谓词的过滤
.where()
Preferred Methods
推荐使用的方法
Only use these world methods:
- - Add a new entity
world.add(entity) - - Remove an entity
world.remove(entity) - - Add component to existing entity
world.addComponent(entity, 'component', value) - - Remove component from entity
world.removeComponent(entity, 'component') - - Create queries
world.with('prop1', 'prop2')
仅使用以下world方法:
- - 添加新实体
world.add(entity) - - 删除实体
world.remove(entity) - - 为现有实体添加组件
world.addComponent(entity, 'component', value) - - 从实体中移除组件
world.removeComponent(entity, 'component') - - 创建查询
world.with('prop1', 'prop2')
Queries
查询
Declare queries at module level and import them where needed:
tsx
// ecs/queries.ts
export const characterQuery = world.with('position', 'isCharacter')
export const enemyQuery = world.with('position', 'isEnemy')
export const movingEntities = world.with('position', 'velocity')
// In a system file
import { movingEntities } from './ecs/queries'在模块级别声明查询,并在需要的地方导入:
tsx
// ecs/queries.ts
export const characterQuery = world.with('position', 'isCharacter')
export const enemyQuery = world.with('position', 'isEnemy')
export const movingEntities = world.with('position', 'velocity')
// 在系统文件中
import { movingEntities } from './ecs/queries'Usage
使用示例
tsx
type Entity = {
position?: { x: number; y: number }
isCharacter?: true
}
const world = new World<Entity>()
const characters = world.with('position', 'isCharacter')
// Add entity
world.add({ position: { x: 0, y: 0 }, isCharacter: true })
// Render
<Entities in={characters}>{Character}</Entities>Note: Miniplex is feature-complete but no longer maintained.
This skill is part of verekia's r3f-gamedev.
tsx
type Entity = {
position?: { x: number; y: number }
isCharacter?: true
}
const world = new World<Entity>()
const characters = world.with('position', 'isCharacter')
// 添加实体
world.add({ position: { x: 0, y: 0 }, isCharacter: true })
// 渲染
<Entities in={characters}>{Character}</Entities>注意:Miniplex功能已完善,但不再维护。
本技能属于verekia的r3f-gamedev项目。