Created
May 10, 2017 11:22
-
-
Save fielding/1025f56d48e55de2d01cf166cbf55da6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Streams | |
| /* | |
| const ticker$ = Rx.Observable | |
| .interval(tickerInterval, Rx.Scheduler.requestAnimationFrame) | |
| .map(() => ({ time: Date.now(), delta: null })) | |
| .scan((previous, current) => ({ | |
| time: current.time, | |
| delta: (current.time - previous.time) / 1000, | |
| })); | |
| */ | |
| const ticker$ = Rx.Observable | |
| .generate(0, x => true, x => x + 1, x => x, Rx.Scheduler.animationFrame) | |
| .scan( | |
| previous => { | |
| const time = Date.now(); | |
| return { | |
| time: time, | |
| delta: (time - previous.time) / 1000, | |
| }; | |
| }, | |
| { time: Date.now(), delta: 0 } | |
| ); | |
| const input$ = Rx.Observable | |
| .fromEvent(document, 'keydown') | |
| .flatMap( | |
| event => | |
| R.contains(event.keyCode, Object.keys(keyMap).map(n => parseInt(n))) | |
| ? [keyMap[event.keyCode]] | |
| : [] | |
| ); | |
| const snake$ = ticker$.withLatestFrom(input$).scan((pos, [ticker, dir]) => { | |
| return pos.add(dir.multiplyScalar(snakeSpeed)).mod(boardSize); | |
| }, snakeInitialPos); | |
| const game = Rx.Observable.combineLatest(ticker$, snake$); | |
| // go time | |
| init(); | |
| game.subscribe(([ticker, position]) => draw(ticker, position)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment