166 |
167 |
Level 6
168 |
169 |
3 / 10
170 |
Re-Wire
171 |
Bring the System back online by rewiring the power nodes.
The game saves progress automatically, you can come back later and continue from the last played level.
172 |
173 |
177 |
181 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
--------------------------------------------------------------------------------
/src/input.ts:
--------------------------------------------------------------------------------
1 | type Mouse = { pos: Vec2, leftDown: boolean; }
2 | type InputCallback = (() => void) | undefined;
3 | type InputCallbacks = {
4 | mouseOver?: InputCallback;
5 | mouseOut?: InputCallback;
6 | mouseDown?: InputCallback;
7 | mouseUp?: InputCallback;
8 | mouseDownUpdate?: InputCallback;
9 | }
10 |
11 | interface InputControl {
12 | mousePos: Vec2;
13 | isMouseDown: ()=>boolean;
14 |
15 | targets: [MouseDragEntity, InputCallbacks][];
16 |
17 | shutdown(): void;
18 |
19 | update(): void;
20 |
21 | dragControl(target: MouseDragEntity, callbacks: InputCallbacks): void;
22 | }
23 |
24 | const createInputControl = (canvas: Canvas): InputControl => {
25 | let mouseDown: boolean = false;
26 | const mousePos: Vec2 = {x: 0, y: 0};
27 |
28 | const mouseOverTargets: [MouseDragEntity, InputCallbacks][] = [];
29 | const mouseOutTargets: [MouseDragEntity, InputCallbacks][] = [];
30 | const mouseDownTargets: [MouseDragEntity, InputCallbacks][] = [];
31 |
32 | const mouseMoveListener = (e: MouseEvent) => {
33 | let rect = canvas.getBoundingClientRect();
34 | mousePos.x = e.clientX - rect.left;
35 | mousePos.y = e.clientY - rect.top;
36 | e.preventDefault();
37 | };
38 | const mouseDownListener = (e: MouseEvent) => {
39 | mouseDown = true;
40 | mouseOverTargets.forEach(watch => {
41 | const mouseDownCallback = watch[1].mouseDown;
42 | mouseDownCallback && mouseDownCallback();
43 | mouseDownTargets.push(watch);
44 | });
45 | e.preventDefault();
46 | };
47 | const mouseUpListener = (e: MouseEvent) => {
48 | mouseDown = false;
49 | mouseDownTargets.forEach(watch => {
50 | const mouseUpCallback = watch[1].mouseUp;
51 | mouseUpCallback && mouseUpCallback();
52 | });
53 | mouseDownTargets.length = 0;
54 | };
55 |
56 | document.addEventListener('mousemove', mouseMoveListener);
57 | document.addEventListener('mousedown', mouseDownListener);
58 | document.addEventListener('mouseup', mouseUpListener);
59 |
60 | const dragControl = (target: MouseDragEntity, callbacks: InputCallbacks) => {
61 | mouseOutTargets.push([target, callbacks]);
62 | };
63 |
64 | const update = () => {
65 | for (let i = mouseOutTargets.length - 1; i >= 0; --i) {
66 | const watch = mouseOutTargets[i];
67 | const callbacks = watch[1];
68 | if (distV(mousePos, watch[0].pos) <= watch[0].mouseDrag.size) {
69 | callbacks.mouseOver && callbacks.mouseOver();
70 | mouseOutTargets.splice(i, 1);
71 | mouseOverTargets.push(watch);
72 | }
73 | }
74 | for (let i = mouseOverTargets.length - 1; i >= 0; --i) {
75 | const watch = mouseOverTargets[i];
76 | const callbacks = watch[1];
77 |
78 | mouseDown && callbacks.mouseDownUpdate && callbacks.mouseDownUpdate();
79 | if (distV(mousePos, watch[0].pos) > watch[0].mouseDrag.size) {
80 | callbacks.mouseOut && callbacks.mouseOut();
81 | mouseOverTargets.splice(i, 1);
82 | mouseOutTargets.push(watch);
83 | }
84 | }
85 | };
86 | const shutdown = () => {
87 | document.removeEventListener('mousemove', mouseMoveListener);
88 | document.removeEventListener('mousedown', mouseDownListener);
89 | document.removeEventListener('mouseup', mouseUpListener);
90 | };
91 |
92 | return {
93 | update,
94 | dragControl,
95 | mousePos,
96 | isMouseDown: () => (mouseDown),
97 | shutdown,
98 | targets:mouseOverTargets
99 | };
100 | };
101 |
102 |
103 |
104 |
--------------------------------------------------------------------------------
/src/level-editor.ts:
--------------------------------------------------------------------------------
1 | const createLevelEditorSystem = (space: Space, inputControl: InputControl): UpdateSystem => {
2 | const mouseWheelListener = (e: WheelEvent) => {
3 | e.preventDefault();
4 | const spool = (inputControl.targets[0][0] as Entity).spool || (inputControl.targets[0][0] as Entity).block;
5 |
6 | if (!spool) {
7 | return;
8 | }
9 | let min = 30;
10 | let max = 160;
11 | if (spool.type == NodeType.isolator) {
12 | max = 80;
13 | }
14 |
15 | if (e.deltaY < 0) {
16 | spool.size !== max && (spool.size += 10);
17 | } else {
18 | spool.size !== min && (spool.size -= 10);
19 | }
20 | };
21 |
22 | const keydownListener = (e: KeyboardEvent) => {
23 | if (e.key === '1') {
24 | const spoolEntity: SpoolNodeEntity = {
25 | pos: {x: inputControl.mousePos.x - 1, y: inputControl.mousePos.y},
26 | spool: {size: 50, type: NodeType.spool},
27 | render: {type: NodeType.spool},
28 | };
29 | // (spoolEntity as any).mouseDrag = {size: 20};
30 | space.addEntity(spoolEntity);
31 | }
32 | if (e.key === '2') {
33 | const spoolEntity: BlockNodeEntity = {
34 | pos: {x: inputControl.mousePos.x, y: inputControl.mousePos.y},
35 | block: {size: 50},
36 | render: {type: NodeType.block},
37 | };
38 | // (spoolEntity as any).mouseDrag = {size: 20};
39 | space.addEntity(spoolEntity);
40 | }
41 | if (e.key === '3') {
42 | const spoolEntity: SpoolNodeEntity = {
43 | pos: {x: inputControl.mousePos.x, y: inputControl.mousePos.y},
44 | spool: {size: 40, type: NodeType.isolator},
45 | render: {type: NodeType.isolator},
46 | };
47 | // (spoolEntity as any).mouseDrag = {size: 20};
48 | space.addEntity(spoolEntity);
49 | }
50 | if (e.key === 'F2') {
51 | const level: Partial