feat(js): m11 pong capstone (model, rules, view, app, page) and polling smoke test
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fd7c5f2acf
commit
1d1d918805
16 files changed
+3090
-3
No files matched your search
@@ -0,0 +1,31 @@
|
||||
// m11 pong app: the same wiring as app.js (streams in, drawings out), reusing the course's modules.
|
||||
import { initialPong, PongAction } from './model.js'
|
||||
import { pongUpdate } from './rules.js'
|
||||
import { pongView } from './view.js'
|
||||
import { map as mapIO, randomSeed, drawScene } from '../io.js'
|
||||
import { keyDowns, keyUps, interval, map, filterMap, merge, scan, startWith } from '../stream.js'
|
||||
|
||||
export const FRAME_MS_PONG = 30
|
||||
|
||||
const keys = {
|
||||
ArrowUp: 'Up', w: 'Up',
|
||||
ArrowDown: 'Down', s: 'Down',
|
||||
}
|
||||
|
||||
// hasOwn, not `key in keys`: "constructor" and "toString" are inherited, not keys we mapped.
|
||||
export const pongKey = (isDown, key) =>
|
||||
Object.hasOwn(keys, key) ? (isDown ? PongAction.Press : PongAction.Release)(keys[key])
|
||||
: key === 'r' && isDown ? PongAction.Restart
|
||||
: null
|
||||
|
||||
export const pongActions = merge(
|
||||
map(interval(FRAME_MS_PONG), () => PongAction.Frame),
|
||||
merge(
|
||||
filterMap(keyDowns, (key) => pongKey(true, key)),
|
||||
filterMap(keyUps, (key) => pongKey(false, key))))
|
||||
|
||||
export const pongStates = (seed, actions) =>
|
||||
startWith(scan(actions, pongUpdate, initialPong(seed)), initialPong(seed))
|
||||
|
||||
export const pongMain = mapIO(randomSeed, (seed) =>
|
||||
map(pongStates(seed, pongActions), (state) => drawScene(pongView(state))))
|
||||
Reference in new issue
Block a user