更新demo
This commit is contained in:
@@ -2,7 +2,9 @@ import { DemoBase, DemoInfo } from './DemoBase';
|
|||||||
import {
|
import {
|
||||||
Component,
|
Component,
|
||||||
ECSComponent,
|
ECSComponent,
|
||||||
|
Entity,
|
||||||
EntitySystem,
|
EntitySystem,
|
||||||
|
Matcher,
|
||||||
Serializable,
|
Serializable,
|
||||||
Serialize,
|
Serialize,
|
||||||
IncrementalSerializer
|
IncrementalSerializer
|
||||||
@@ -47,13 +49,14 @@ class RenderableComponent extends Component {
|
|||||||
|
|
||||||
// ===== 系统定义 =====
|
// ===== 系统定义 =====
|
||||||
class MovementSystem extends EntitySystem {
|
class MovementSystem extends EntitySystem {
|
||||||
update() {
|
constructor() {
|
||||||
if (!this.scene) return;
|
super(Matcher.all(PositionComponent, VelocityComponent));
|
||||||
const entities = this.scene.entities.buffer;
|
}
|
||||||
|
|
||||||
|
protected override process(entities: readonly Entity[]): void {
|
||||||
for (const entity of entities) {
|
for (const entity of entities) {
|
||||||
const pos = entity.getComponent(PositionComponent);
|
const [pos, vel] = this.getComponents(entity, PositionComponent, VelocityComponent);
|
||||||
const vel = entity.getComponent(VelocityComponent);
|
|
||||||
if (pos && vel) {
|
|
||||||
pos.x += vel.vx;
|
pos.x += vel.vx;
|
||||||
pos.y += vel.vy;
|
pos.y += vel.vy;
|
||||||
|
|
||||||
@@ -62,31 +65,26 @@ class MovementSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class RenderSystem extends EntitySystem {
|
class RenderSystem extends EntitySystem {
|
||||||
private canvas: HTMLCanvasElement;
|
private canvas: HTMLCanvasElement;
|
||||||
private ctx: CanvasRenderingContext2D;
|
private ctx: CanvasRenderingContext2D;
|
||||||
|
|
||||||
constructor(canvas: HTMLCanvasElement) {
|
constructor(canvas: HTMLCanvasElement) {
|
||||||
super();
|
super(Matcher.all(PositionComponent, RenderableComponent));
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
if (!ctx) throw new Error('Failed to get canvas context');
|
if (!ctx) throw new Error('Failed to get canvas context');
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
protected override process(entities: readonly Entity[]): void {
|
||||||
if (!this.scene) return;
|
|
||||||
|
|
||||||
this.ctx.fillStyle = '#0a0a15';
|
this.ctx.fillStyle = '#0a0a15';
|
||||||
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
||||||
|
|
||||||
const entities = this.scene.entities.buffer;
|
|
||||||
for (const entity of entities) {
|
for (const entity of entities) {
|
||||||
const pos = entity.getComponent(PositionComponent);
|
const [pos, render] = this.getComponents(entity, PositionComponent, RenderableComponent);
|
||||||
const render = entity.getComponent(RenderableComponent);
|
|
||||||
if (pos && render) {
|
|
||||||
this.ctx.fillStyle = render.color;
|
this.ctx.fillStyle = render.color;
|
||||||
this.ctx.beginPath();
|
this.ctx.beginPath();
|
||||||
this.ctx.arc(pos.x, pos.y, render.radius, 0, Math.PI * 2);
|
this.ctx.arc(pos.x, pos.y, render.radius, 0, Math.PI * 2);
|
||||||
@@ -99,7 +97,6 @@ class RenderSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export class IncrementalSerializationDemo extends DemoBase {
|
export class IncrementalSerializationDemo extends DemoBase {
|
||||||
private renderSystem!: RenderSystem;
|
private renderSystem!: RenderSystem;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
ECSComponent,
|
ECSComponent,
|
||||||
Entity,
|
Entity,
|
||||||
EntitySystem,
|
EntitySystem,
|
||||||
|
Matcher,
|
||||||
Serializable,
|
Serializable,
|
||||||
Serialize,
|
Serialize,
|
||||||
SerializeAsMap
|
SerializeAsMap
|
||||||
@@ -61,12 +62,14 @@ class PlayerComponent extends Component {
|
|||||||
|
|
||||||
// ===== 系统定义 =====
|
// ===== 系统定义 =====
|
||||||
class MovementSystem extends EntitySystem {
|
class MovementSystem extends EntitySystem {
|
||||||
update() {
|
constructor() {
|
||||||
const entities = this.scene.entities.buffer;
|
super(Matcher.all(PositionComponent, VelocityComponent));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override process(entities: readonly Entity[]): void {
|
||||||
for (const entity of entities) {
|
for (const entity of entities) {
|
||||||
const pos = entity.getComponent(PositionComponent);
|
const [pos, vel] = this.getComponents(entity, PositionComponent, VelocityComponent);
|
||||||
const vel = entity.getComponent(VelocityComponent);
|
|
||||||
if (pos && vel) {
|
|
||||||
pos.x += vel.vx;
|
pos.x += vel.vx;
|
||||||
pos.y += vel.vy;
|
pos.y += vel.vy;
|
||||||
|
|
||||||
@@ -76,29 +79,26 @@ class MovementSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class RenderSystem extends EntitySystem {
|
class RenderSystem extends EntitySystem {
|
||||||
private canvas: HTMLCanvasElement;
|
private canvas: HTMLCanvasElement;
|
||||||
private ctx: CanvasRenderingContext2D;
|
private ctx: CanvasRenderingContext2D;
|
||||||
|
|
||||||
constructor(canvas: HTMLCanvasElement) {
|
constructor(canvas: HTMLCanvasElement) {
|
||||||
super();
|
super(Matcher.all(PositionComponent, RenderableComponent));
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
this.ctx = canvas.getContext('2d')!;
|
this.ctx = canvas.getContext('2d')!;
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
protected override process(entities: readonly Entity[]): void {
|
||||||
// 清空画布
|
// 清空画布
|
||||||
this.ctx.fillStyle = '#0a0a15';
|
this.ctx.fillStyle = '#0a0a15';
|
||||||
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
||||||
|
|
||||||
// 渲染所有实体
|
// 渲染所有实体
|
||||||
const entities = this.scene.entities.buffer;
|
|
||||||
for (const entity of entities) {
|
for (const entity of entities) {
|
||||||
const pos = entity.getComponent(PositionComponent);
|
const [pos, render] = this.getComponents(entity, PositionComponent, RenderableComponent);
|
||||||
const render = entity.getComponent(RenderableComponent);
|
|
||||||
if (pos && render) {
|
|
||||||
this.ctx.fillStyle = render.color;
|
this.ctx.fillStyle = render.color;
|
||||||
this.ctx.beginPath();
|
this.ctx.beginPath();
|
||||||
this.ctx.arc(pos.x, pos.y, render.radius, 0, Math.PI * 2);
|
this.ctx.arc(pos.x, pos.y, render.radius, 0, Math.PI * 2);
|
||||||
@@ -115,7 +115,6 @@ class RenderSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export class SerializationDemo extends DemoBase {
|
export class SerializationDemo extends DemoBase {
|
||||||
private renderSystem!: RenderSystem;
|
private renderSystem!: RenderSystem;
|
||||||
|
|||||||
@@ -447,20 +447,18 @@ class RenderSystem extends EntitySystem {
|
|||||||
private ctx: CanvasRenderingContext2D;
|
private ctx: CanvasRenderingContext2D;
|
||||||
|
|
||||||
constructor(canvas: HTMLCanvasElement) {
|
constructor(canvas: HTMLCanvasElement) {
|
||||||
super(Matcher.empty().all(Position, Renderable));
|
super(Matcher.all(Position, Renderable));
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
this.ctx = canvas.getContext('2d')!;
|
this.ctx = canvas.getContext('2d')!;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override process(entities: Entity[]): void {
|
protected override process(entities: readonly Entity[]): void {
|
||||||
this.ctx.fillStyle = '#000';
|
this.ctx.fillStyle = '#000';
|
||||||
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
||||||
|
|
||||||
for (const entity of entities) {
|
for (const entity of entities) {
|
||||||
const position = entity.getComponent(Position);
|
const position = this.requireComponent(entity, Position);
|
||||||
const renderable = entity.getComponent(Renderable);
|
const renderable = this.requireComponent(entity, Renderable);
|
||||||
|
|
||||||
if (!position || !renderable) continue;
|
|
||||||
|
|
||||||
this.ctx.fillStyle = renderable.color;
|
this.ctx.fillStyle = renderable.color;
|
||||||
this.ctx.beginPath();
|
this.ctx.beginPath();
|
||||||
@@ -473,15 +471,14 @@ class RenderSystem extends EntitySystem {
|
|||||||
@ECSSystem('LifetimeSystem')
|
@ECSSystem('LifetimeSystem')
|
||||||
class LifetimeSystem extends EntitySystem {
|
class LifetimeSystem extends EntitySystem {
|
||||||
constructor() {
|
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;
|
const deltaTime = Time.deltaTime;
|
||||||
|
|
||||||
for (const entity of entities) {
|
for (const entity of entities) {
|
||||||
const lifetime = entity.getComponent(Lifetime);
|
const lifetime = this.requireComponent(entity, Lifetime);
|
||||||
if (!lifetime) continue;
|
|
||||||
|
|
||||||
lifetime.currentAge += deltaTime;
|
lifetime.currentAge += deltaTime;
|
||||||
if (lifetime.isDead()) {
|
if (lifetime.isDead()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user