diff --git a/examples/core-demos/src/demos/IncrementalSerializationDemo.ts b/examples/core-demos/src/demos/IncrementalSerializationDemo.ts index 0b3c3331..2ea605e5 100644 --- a/examples/core-demos/src/demos/IncrementalSerializationDemo.ts +++ b/examples/core-demos/src/demos/IncrementalSerializationDemo.ts @@ -2,7 +2,9 @@ import { DemoBase, DemoInfo } from './DemoBase'; import { Component, ECSComponent, + Entity, EntitySystem, + Matcher, Serializable, Serialize, IncrementalSerializer @@ -47,19 +49,19 @@ class RenderableComponent extends Component { // ===== 系统定义 ===== class MovementSystem extends EntitySystem { - update() { - if (!this.scene) return; - const entities = this.scene.entities.buffer; - for (const entity of entities) { - const pos = entity.getComponent(PositionComponent); - const vel = entity.getComponent(VelocityComponent); - if (pos && vel) { - pos.x += vel.vx; - pos.y += vel.vy; + constructor() { + super(Matcher.all(PositionComponent, VelocityComponent)); + } - if (pos.x < 0 || pos.x > 1200) vel.vx *= -1; - if (pos.y < 0 || pos.y > 600) vel.vy *= -1; - } + protected override process(entities: readonly Entity[]): void { + for (const entity of entities) { + const [pos, vel] = this.getComponents(entity, PositionComponent, VelocityComponent); + + pos.x += vel.vx; + pos.y += vel.vy; + + if (pos.x < 0 || pos.x > 1200) vel.vx *= -1; + if (pos.y < 0 || pos.y > 600) vel.vy *= -1; } } } @@ -69,34 +71,29 @@ class RenderSystem extends EntitySystem { private ctx: CanvasRenderingContext2D; constructor(canvas: HTMLCanvasElement) { - super(); + super(Matcher.all(PositionComponent, RenderableComponent)); this.canvas = canvas; const ctx = canvas.getContext('2d'); if (!ctx) throw new Error('Failed to get canvas context'); this.ctx = ctx; } - update() { - if (!this.scene) return; - + protected override process(entities: readonly Entity[]): void { this.ctx.fillStyle = '#0a0a15'; this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height); - const entities = this.scene.entities.buffer; for (const entity of entities) { - const pos = entity.getComponent(PositionComponent); - const render = entity.getComponent(RenderableComponent); - if (pos && render) { - this.ctx.fillStyle = render.color; - this.ctx.beginPath(); - this.ctx.arc(pos.x, pos.y, render.radius, 0, Math.PI * 2); - this.ctx.fill(); + const [pos, render] = this.getComponents(entity, PositionComponent, RenderableComponent); - this.ctx.fillStyle = 'white'; - this.ctx.font = '10px Arial'; - this.ctx.textAlign = 'center'; - this.ctx.fillText(entity.name, pos.x, pos.y - render.radius - 5); - } + this.ctx.fillStyle = render.color; + this.ctx.beginPath(); + this.ctx.arc(pos.x, pos.y, render.radius, 0, Math.PI * 2); + this.ctx.fill(); + + this.ctx.fillStyle = 'white'; + this.ctx.font = '10px Arial'; + this.ctx.textAlign = 'center'; + this.ctx.fillText(entity.name, pos.x, pos.y - render.radius - 5); } } } diff --git a/examples/core-demos/src/demos/SerializationDemo.ts b/examples/core-demos/src/demos/SerializationDemo.ts index d5ff884e..e6afa6a1 100644 --- a/examples/core-demos/src/demos/SerializationDemo.ts +++ b/examples/core-demos/src/demos/SerializationDemo.ts @@ -4,6 +4,7 @@ import { ECSComponent, Entity, EntitySystem, + Matcher, Serializable, Serialize, SerializeAsMap @@ -61,19 +62,20 @@ class PlayerComponent extends Component { // ===== 系统定义 ===== class MovementSystem extends EntitySystem { - update() { - const entities = this.scene.entities.buffer; - for (const entity of entities) { - const pos = entity.getComponent(PositionComponent); - const vel = entity.getComponent(VelocityComponent); - if (pos && vel) { - pos.x += vel.vx; - pos.y += vel.vy; + constructor() { + super(Matcher.all(PositionComponent, VelocityComponent)); + } - // 边界反弹 - if (pos.x < 0 || pos.x > 1200) vel.vx *= -1; - if (pos.y < 0 || pos.y > 600) vel.vy *= -1; - } + protected override process(entities: readonly Entity[]): void { + for (const entity of entities) { + const [pos, vel] = this.getComponents(entity, PositionComponent, VelocityComponent); + + pos.x += vel.vx; + pos.y += vel.vy; + + // 边界反弹 + if (pos.x < 0 || pos.x > 1200) vel.vx *= -1; + if (pos.y < 0 || pos.y > 600) vel.vy *= -1; } } } @@ -83,35 +85,32 @@ class RenderSystem extends EntitySystem { private ctx: CanvasRenderingContext2D; constructor(canvas: HTMLCanvasElement) { - super(); + super(Matcher.all(PositionComponent, RenderableComponent)); this.canvas = canvas; this.ctx = canvas.getContext('2d')!; } - update() { + protected override process(entities: readonly Entity[]): void { // 清空画布 this.ctx.fillStyle = '#0a0a15'; this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height); // 渲染所有实体 - const entities = this.scene.entities.buffer; for (const entity of entities) { - const pos = entity.getComponent(PositionComponent); - const render = entity.getComponent(RenderableComponent); - if (pos && render) { - this.ctx.fillStyle = render.color; - this.ctx.beginPath(); - this.ctx.arc(pos.x, pos.y, render.radius, 0, Math.PI * 2); - this.ctx.fill(); + const [pos, render] = this.getComponents(entity, PositionComponent, RenderableComponent); - // 如果是玩家,显示名字 - const player = entity.getComponent(PlayerComponent); - if (player) { - this.ctx.fillStyle = 'white'; - this.ctx.font = '12px Arial'; - this.ctx.textAlign = 'center'; - this.ctx.fillText(player.name, pos.x, pos.y - render.radius - 5); - } + this.ctx.fillStyle = render.color; + this.ctx.beginPath(); + this.ctx.arc(pos.x, pos.y, render.radius, 0, Math.PI * 2); + this.ctx.fill(); + + // 如果是玩家,显示名字 + const player = entity.getComponent(PlayerComponent); + if (player) { + this.ctx.fillStyle = 'white'; + this.ctx.font = '12px Arial'; + this.ctx.textAlign = 'center'; + this.ctx.fillText(player.name, pos.x, pos.y - render.radius - 5); } } } diff --git a/examples/core-demos/src/demos/WorkerSystemDemo.ts b/examples/core-demos/src/demos/WorkerSystemDemo.ts index 8d3349ab..96cf832b 100644 --- a/examples/core-demos/src/demos/WorkerSystemDemo.ts +++ b/examples/core-demos/src/demos/WorkerSystemDemo.ts @@ -447,20 +447,18 @@ class RenderSystem extends EntitySystem { private ctx: CanvasRenderingContext2D; constructor(canvas: HTMLCanvasElement) { - super(Matcher.empty().all(Position, Renderable)); + super(Matcher.all(Position, Renderable)); this.canvas = canvas; this.ctx = canvas.getContext('2d')!; } - protected override process(entities: Entity[]): void { + protected override process(entities: readonly Entity[]): void { this.ctx.fillStyle = '#000'; this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height); for (const entity of entities) { - const position = entity.getComponent(Position); - const renderable = entity.getComponent(Renderable); - - if (!position || !renderable) continue; + const position = this.requireComponent(entity, Position); + const renderable = this.requireComponent(entity, Renderable); this.ctx.fillStyle = renderable.color; this.ctx.beginPath(); @@ -473,15 +471,14 @@ class RenderSystem extends EntitySystem { @ECSSystem('LifetimeSystem') class LifetimeSystem extends EntitySystem { constructor() { - super(Matcher.empty().all(Lifetime)); + super(Matcher.all(Lifetime)); } - protected override process(entities: Entity[]): void { + protected override process(entities: readonly Entity[]): void { const deltaTime = Time.deltaTime; for (const entity of entities) { - const lifetime = entity.getComponent(Lifetime); - if (!lifetime) continue; + const lifetime = this.requireComponent(entity, Lifetime); lifetime.currentAge += deltaTime; if (lifetime.isDead()) {