-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (139 loc) · 4.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import rxjs from 'https://dev.jspm.io/rxjs@6';
import operators from 'https://dev.jspm.io/rxjs@6/operators';
import { generateWorld, updateWorld, renderWorld } from './world.js';
import { getInputNumberStream, selectButtonFromKeyEventStream, renderAsText } from './utils.js';
const { fromEvent, BehaviorSubject, combineLatest, merge, NEVER } = rxjs;
const { tap, share, startWith, scan, switchMap, withLatestFrom, map, shareReplay } = operators;
// sources
const canvasContext$ = new BehaviorSubject(canvas.getContext('2d'));
const startX$ = getInputNumberStream(startX);
const startY$ = getInputNumberStream(startY);
const dimX$ = getInputNumberStream(dimX).pipe(
tap(val => {
startX.max = String(val - 1);
if (Number(startX.value) > Number(startX.max)) {
startX.value = startX.max;
startX.dispatchEvent(new Event('input'));
}
})
);
const dimY$ = getInputNumberStream(dimY).pipe(
tap(val => {
startY.max = String(val - 1);
if (Number(startY.value) > Number(startY.max)) {
startY.value = startY.max;
startY.dispatchEvent(new Event('input'));
}
})
);
const resize$ = fromEvent(window, 'resize').pipe(
startWith(undefined),
shareReplay(1)
);
const keydown$ = fromEvent(document, 'keydown').pipe(
share()
);
const configurationOpen$ = fromEvent(configuration, 'toggle').pipe(
map(({ target }) => target.open),
startWith(configuration.open),
shareReplay(1)
);
// intermediates
const controlKey$ = configurationOpen$.pipe(
switchMap(open => open ? NEVER : keydown$)
);
const configurationKey$ = configurationOpen$.pipe(
switchMap(open => open ? keydown$ : NEVER)
);
const moveUpKey$ = selectButtonFromKeyEventStream(moveUp, configurationKey$);
const moveDownKey$ = selectButtonFromKeyEventStream(moveDown, configurationKey$);
const moveLeftKey$ = selectButtonFromKeyEventStream(moveLeft, configurationKey$);
const moveRightKey$ = selectButtonFromKeyEventStream(moveRight, configurationKey$);
const dimension$ = combineLatest(
dimX$,
dimY$
).pipe(
map(([x, y]) => ({ x, y })),
shareReplay(1)
);
const playerStartPosition$ = combineLatest(
startX$,
startY$
).pipe(
map(([x, y]) => ({ x, y })),
shareReplay(1)
);
const canvasCenter$ = resize$.pipe(
tap(_ => (canvas.width = window.innerWidth) && (canvas.height = window.innerHeight)),
map(_ => ({ x: window.innerWidth / 2, y: window.innerHeight / 2 })),
);
const canvasFieldLength$ = combineLatest(
resize$,
dimension$
).pipe(
map(([_, { x, y }]) => Math.floor(Math.min(window.innerWidth / x, window.innerHeight / y))),
shareReplay(1)
);
const canvasWorldStart$ = canvasFieldLength$.pipe(
withLatestFrom(dimension$, canvasCenter$),
map(([length, { x: dimX, y: dimY }, { x, y }]) => ({
x: Math.round(x - length * dimX / 2),
y: Math.round(y - length * dimY / 2)
}))
);
const initWorld$ = combineLatest(
dimension$,
playerStartPosition$
).pipe(
map(([{ x: dimX, y: dimY }, { x, y }]) => {
const worldMap = generateWorld(dimX, dimY);
worldMap[y][x] = 1;
return { worldMap, player: { x, y } };
})
);
const playerPositionDelta$ = controlKey$.pipe(
withLatestFrom(
moveUpKey$,
moveDownKey$,
moveLeftKey$,
moveRightKey$
),
map(([{ key }, moveUp, moveDown, moveLeft, moveRight]) => {
switch (key) {
case moveUp:
return { event: 'MovePlayer', payload: [0, -1]};
case moveDown:
return { event: 'MovePlayer', payload: [0, +1]};
case moveLeft:
return { event: 'MovePlayer', payload: [-1, 0]};
case moveRight:
return { event: 'MovePlayer', payload: [+1, 0]};
default:
return { event: 'MovePlayer', payload: [0, 0] };
}
}),
);
const updateWorld$ = initWorld$.pipe(
switchMap(initialWorld => merge(
playerPositionDelta$
).pipe(
scan(updateWorld, initialWorld),
startWith(initialWorld)
)),
);
const render$ = combineLatest(
updateWorld$,
canvasFieldLength$
).pipe(
withLatestFrom(
canvasWorldStart$,
canvasContext$
),
tap(renderWorld)
);
const keyName$ = keydown$.pipe(
map(({ key }) => key)
);
// sinks
renderAsText(keyName$, lastPressed);
render$.subscribe();