|
| 1 | +import { |
| 2 | + App, |
| 3 | + Camera, |
| 4 | + Canvas, |
| 5 | + Children, |
| 6 | + Circle, |
| 7 | + Commands, |
| 8 | + DOMAdapter, |
| 9 | + DefaultPlugins, |
| 10 | + Entity, |
| 11 | + FillSolid, |
| 12 | + Grid, |
| 13 | + Parent, |
| 14 | + Pen, |
| 15 | + Plugin, |
| 16 | + PreStartUp, |
| 17 | + Renderable, |
| 18 | + Stroke, |
| 19 | + System, |
| 20 | + Theme, |
| 21 | + Transform, |
| 22 | + Visibility, |
| 23 | + system, |
| 24 | +} from '../../packages/ecs/src'; |
| 25 | +import { NodeJSAdapter, sleep } from '../utils'; |
| 26 | + |
| 27 | +DOMAdapter.set(NodeJSAdapter); |
| 28 | + |
| 29 | +describe('Hierarchy', () => { |
| 30 | + it('should create a hierarchy', async () => { |
| 31 | + const app = new App(); |
| 32 | + |
| 33 | + let canvasEntity: Entity | undefined; |
| 34 | + let cameraEntity: Entity | undefined; |
| 35 | + let parentEntity: Entity | undefined; |
| 36 | + let childEntity: Entity | undefined; |
| 37 | + |
| 38 | + const MyPlugin: Plugin = () => { |
| 39 | + system(PreStartUp)(StartUpSystem); |
| 40 | + }; |
| 41 | + |
| 42 | + class StartUpSystem extends System { |
| 43 | + private readonly commands = new Commands(this); |
| 44 | + |
| 45 | + q = this.query( |
| 46 | + (q) => |
| 47 | + q.using( |
| 48 | + Canvas, |
| 49 | + Theme, |
| 50 | + Grid, |
| 51 | + Camera, |
| 52 | + Parent, |
| 53 | + Children, |
| 54 | + Transform, |
| 55 | + Renderable, |
| 56 | + FillSolid, |
| 57 | + Stroke, |
| 58 | + Circle, |
| 59 | + Visibility, |
| 60 | + ).write, |
| 61 | + ); |
| 62 | + |
| 63 | + initialize(): void { |
| 64 | + const $canvas = DOMAdapter.get().createCanvas( |
| 65 | + 200, |
| 66 | + 200, |
| 67 | + ) as HTMLCanvasElement; |
| 68 | + |
| 69 | + const canvas = this.commands.spawn( |
| 70 | + new Canvas({ |
| 71 | + element: $canvas, |
| 72 | + width: 200, |
| 73 | + height: 200, |
| 74 | + devicePixelRatio: 1, |
| 75 | + }), |
| 76 | + ); |
| 77 | + |
| 78 | + const camera = this.commands.spawn( |
| 79 | + new Camera({ |
| 80 | + canvas: canvas.id(), |
| 81 | + }), |
| 82 | + new Transform(), |
| 83 | + ); |
| 84 | + |
| 85 | + const parent = this.commands.spawn( |
| 86 | + new Transform(), |
| 87 | + new Renderable(), |
| 88 | + new FillSolid('red'), |
| 89 | + new Circle({ cx: 0, cy: 0, r: 100 }), |
| 90 | + new Visibility(), |
| 91 | + ); |
| 92 | + |
| 93 | + camera.appendChild(parent); |
| 94 | + |
| 95 | + const child = this.commands.spawn( |
| 96 | + new Transform(), |
| 97 | + new Renderable(), |
| 98 | + new FillSolid('green'), |
| 99 | + new Stroke({ |
| 100 | + color: 'black', |
| 101 | + width: 10, |
| 102 | + alignment: 'center', |
| 103 | + dasharray: [10, 10], |
| 104 | + }), |
| 105 | + new Circle({ cx: 0, cy: 0, r: 50 }), |
| 106 | + new Visibility(), |
| 107 | + ); |
| 108 | + parent.appendChild(child); |
| 109 | + |
| 110 | + canvasEntity = canvas.id().hold(); |
| 111 | + cameraEntity = camera.id().hold(); |
| 112 | + parentEntity = parent.id().hold(); |
| 113 | + childEntity = child.id().hold(); |
| 114 | + |
| 115 | + this.commands.execute(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + app.addPlugins(...DefaultPlugins, MyPlugin); |
| 120 | + |
| 121 | + await app.run(); |
| 122 | + |
| 123 | + await sleep(300); |
| 124 | + |
| 125 | + if (canvasEntity && cameraEntity && parentEntity && childEntity) { |
| 126 | + const canvas = canvasEntity.read(Canvas); |
| 127 | + expect(canvas.devicePixelRatio).toBe(1); |
| 128 | + expect(canvas.width).toBe(200); |
| 129 | + expect(canvas.height).toBe(200); |
| 130 | + expect(canvas.renderer).toBe('webgl'); |
| 131 | + expect(canvas.pen).toBe(Pen.HAND); |
| 132 | + expect(canvas.cameras).toHaveLength(1); |
| 133 | + |
| 134 | + const camera = cameraEntity.read(Camera); |
| 135 | + expect(camera.canvas.isSame(canvasEntity)).toBeTruthy(); |
| 136 | + expect(cameraEntity.read(Parent).children).toHaveLength(1); |
| 137 | + expect( |
| 138 | + cameraEntity.read(Parent).children[0].isSame(parentEntity), |
| 139 | + ).toBeTruthy(); |
| 140 | + |
| 141 | + const parent = parentEntity.read(Parent); |
| 142 | + expect(parent.children).toHaveLength(1); |
| 143 | + expect(parent.children[0].isSame(childEntity)).toBeTruthy(); |
| 144 | + |
| 145 | + const child = childEntity.read(Children); |
| 146 | + expect(child.parent.isSame(parentEntity)).toBeTruthy(); |
| 147 | + } |
| 148 | + |
| 149 | + await app.exit(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments