|
| 1 | +import { component } from "../../src/entity"; |
| 2 | +import type { Query } from "../../src/query"; |
| 3 | +import type { System } from "../../src/system"; |
| 4 | +import { World } from "../../src/world"; |
| 5 | + |
| 6 | +// 定义组件类型 |
| 7 | +type Position = { x: number; y: number }; |
| 8 | +type Velocity = { x: number; y: number }; |
| 9 | +type Health = { value: number }; |
| 10 | + |
| 11 | +// 定义组件ID |
| 12 | +const PositionId = component<Position>(); |
| 13 | +const VelocityId = component<Velocity>(); |
| 14 | +const HealthId = component<Health>(); |
| 15 | + |
| 16 | +// 输入系统 - 处理用户输入 |
| 17 | +class InputSystem implements System { |
| 18 | + update(world: World, deltaTime: number): void { |
| 19 | + console.log(`[InputSystem] Processing input at ${Date.now()}`); |
| 20 | + // 这里可以处理键盘/鼠标输入等 |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// 移动系统 - 依赖输入系统 |
| 25 | +class MovementSystem implements System { |
| 26 | + private query: Query; |
| 27 | + |
| 28 | + constructor(world: World) { |
| 29 | + this.query = world.createQuery([PositionId, VelocityId]); |
| 30 | + } |
| 31 | + |
| 32 | + update(world: World, deltaTime: number): void { |
| 33 | + console.log(`[MovementSystem] Updating positions`); |
| 34 | + |
| 35 | + this.query.forEach([PositionId, VelocityId], (entity, position, velocity) => { |
| 36 | + position.x += velocity.x * deltaTime; |
| 37 | + position.y += velocity.y * deltaTime; |
| 38 | + console.log(` Entity ${entity}: Position (${position.x.toFixed(2)}, ${position.y.toFixed(2)})`); |
| 39 | + }); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// 伤害系统 - 依赖移动系统 |
| 44 | +class DamageSystem implements System { |
| 45 | + private query: Query; |
| 46 | + |
| 47 | + constructor(world: World) { |
| 48 | + this.query = world.createQuery([PositionId, HealthId]); |
| 49 | + } |
| 50 | + |
| 51 | + update(world: World, deltaTime: number): void { |
| 52 | + console.log(`[DamageSystem] Applying damage based on position`); |
| 53 | + |
| 54 | + this.query.forEach([PositionId, HealthId], (entity, position, health) => { |
| 55 | + // 根据位置计算伤害(示例逻辑) |
| 56 | + const damage = Math.abs(position.x) * 0.1; |
| 57 | + health.value -= damage; |
| 58 | + console.log(` Entity ${entity}: Health reduced by ${damage.toFixed(2)}, now ${health.value.toFixed(2)}`); |
| 59 | + }); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// 渲染系统 - 依赖所有其他系统最后执行 |
| 64 | +class RenderSystem implements System { |
| 65 | + private query: Query; |
| 66 | + |
| 67 | + constructor(world: World) { |
| 68 | + this.query = world.createQuery([PositionId]); |
| 69 | + } |
| 70 | + |
| 71 | + update(world: World, deltaTime: number): void { |
| 72 | + console.log(`[RenderSystem] Rendering entities`); |
| 73 | + |
| 74 | + this.query.forEach([PositionId], (entity, position) => { |
| 75 | + console.log(` Rendering Entity ${entity} at (${position.x.toFixed(2)}, ${position.y.toFixed(2)})`); |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function main() { |
| 81 | + console.log("ECS Advanced Scheduling Demo - System Dependencies"); |
| 82 | + console.log("================================================="); |
| 83 | + |
| 84 | + const world = new World(); |
| 85 | + |
| 86 | + // 创建系统实例 |
| 87 | + const inputSystem = new InputSystem(); |
| 88 | + const movementSystem = new MovementSystem(world); |
| 89 | + const damageSystem = new DamageSystem(world); |
| 90 | + const renderSystem = new RenderSystem(world); |
| 91 | + |
| 92 | + // 注册系统并指定依赖关系 |
| 93 | + // 输入系统没有依赖 |
| 94 | + world.registerSystem(inputSystem); |
| 95 | + |
| 96 | + // 移动系统依赖输入系统 |
| 97 | + world.registerSystem(movementSystem, [inputSystem]); |
| 98 | + |
| 99 | + // 伤害系统依赖移动系统 |
| 100 | + world.registerSystem(damageSystem, [movementSystem]); |
| 101 | + |
| 102 | + // 渲染系统依赖伤害系统(确保所有更新都完成后才渲染) |
| 103 | + world.registerSystem(renderSystem, [damageSystem]); |
| 104 | + |
| 105 | + // 创建一些实体 |
| 106 | + const entity1 = world.createEntity(); |
| 107 | + world.addComponent(entity1, PositionId, { x: 0, y: 0 }); |
| 108 | + world.addComponent(entity1, VelocityId, { x: 2, y: 1 }); |
| 109 | + world.addComponent(entity1, HealthId, { value: 100 }); |
| 110 | + |
| 111 | + const entity2 = world.createEntity(); |
| 112 | + world.addComponent(entity2, PositionId, { x: 5, y: 3 }); |
| 113 | + world.addComponent(entity2, VelocityId, { x: -1, y: 0.5 }); |
| 114 | + world.addComponent(entity2, HealthId, { value: 80 }); |
| 115 | + |
| 116 | + // 运行几帧 |
| 117 | + console.log("\n--- Frame 1 ---"); |
| 118 | + world.update(1.0); |
| 119 | + |
| 120 | + console.log("\n--- Frame 2 ---"); |
| 121 | + world.update(1.0); |
| 122 | + |
| 123 | + console.log("\nDemo completed!"); |
| 124 | +} |
| 125 | + |
| 126 | +if (import.meta.main) { |
| 127 | + main(); |
| 128 | +} |
0 commit comments