Created
January 25, 2013 15:31
-
-
Save wolfflow/4635276 to your computer and use it in GitHub Desktop.
BaconJS Konami code timed sequence
This file contains 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
arrayEquals = (a,b) -> $(a).not(b).length == 0 && $(b).not(a).length == 0 | |
always = (value) -> (_) -> value | |
allKeyUps = $(document).asEventStream("keyup") | |
keyCodeIs = (keyCode) -> (event) -> event.keyCode is keyCode | |
keyUps = (keyCode) -> allKeyUps.filter keyCodeIs(keyCode) | |
keyState = (keyCode, value) -> keyUps(keyCode).map(always(value)) | |
arrowKeyState = Bacon.mergeAll([ | |
keyState(38, "UP"), | |
keyState(40, "DOWN"), | |
keyState(37, "LEFT"), | |
keyState(39, "RIGHT"), | |
keyState(66, "B"), | |
keyState(65, "A") | |
]) | |
code = ["UP", "UP", "DOWN", "DOWN", "LEFT", "RIGHT", "LEFT", "RIGHT", "B", "A", "B", "A"] | |
buf = arrowKeyState.bufferWithTime(2500) | |
buf2 = arrowKeyState.bufferWithCount(code.length) | |
seqObserver = buf.concat(buf2) | |
seqObserver.onValue((x)-> console.log "KONAMI CODE DONE!" if arrayEquals(x,code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment