use custom keys
This commit is contained in:
		| @ -70,21 +70,16 @@ const STATE = { | |||||||
|     PAUSED: "PAUSE", |     PAUSED: "PAUSE", | ||||||
|     GAME_OVER: "GAME OVER" |     GAME_OVER: "GAME OVER" | ||||||
| } | } | ||||||
| var ACTIONS = {} | const actionsDefaultKeys = { | ||||||
| ACTIONS[STATE.PLAYING] = { |     moveLeft: "ArrowLeft", | ||||||
|     "ArrowLeft":    moveLeft, |     moveRight: "ArrowRight", | ||||||
|     "ArrowRight":   moveRight, |     softDrop: "ArrowDown", | ||||||
|     "ArrowDown":    softDrop, |     hardDrop: " ", | ||||||
|     " ":            hardDrop, |     rotateCW: "ArrowUp", | ||||||
|     "ArrowUp":      rotateCW, |     rotateCCW: "z", | ||||||
|     "z":            rotateCCW, |     hold: "c", | ||||||
|     "c":            hold, |     pause: "Escape", | ||||||
|     "Escape":       pause |  | ||||||
| } | } | ||||||
| ACTIONS[STATE.PAUSED] = { |  | ||||||
|     "Escape":        resume |  | ||||||
| } |  | ||||||
| ACTIONS[STATE.GAME_OVER] = {} |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Scheduler { | class Scheduler { | ||||||
| @ -630,8 +625,8 @@ function autorepeat() { | |||||||
| function keyDownHandler(e) { | function keyDownHandler(e) { | ||||||
|     if (!pressedKeys.has(e.key)) { |     if (!pressedKeys.has(e.key)) { | ||||||
|         pressedKeys.add(e.key) |         pressedKeys.add(e.key) | ||||||
|         if (e.key in ACTIONS[state]) { |         if (e.key in actions[state]) { | ||||||
|             action = ACTIONS[state][e.key] |             action = actions[state][e.key] | ||||||
|             action() |             action() | ||||||
|             requestAnimationFrame(draw) |             requestAnimationFrame(draw) | ||||||
|             if (REPEATABLE_ACTIONS.includes(action)) { |             if (REPEATABLE_ACTIONS.includes(action)) { | ||||||
| @ -650,8 +645,8 @@ function keyDownHandler(e) { | |||||||
|  |  | ||||||
| function keyUpHandler(e) { | function keyUpHandler(e) { | ||||||
|     pressedKeys.delete(e.key) |     pressedKeys.delete(e.key) | ||||||
|     if (e.key in ACTIONS[state]) { |     if (e.key in actions[state]) { | ||||||
|         action = ACTIONS[state][e.key] |         action = actions[state][e.key] | ||||||
|         if (actionsToRepeat.includes(action)) { |         if (actionsToRepeat.includes(action)) { | ||||||
|             actionsToRepeat.splice(actionsToRepeat.indexOf(action), 1) |             actionsToRepeat.splice(actionsToRepeat.indexOf(action), 1) | ||||||
|             if (!actionsToRepeat.length) { |             if (!actionsToRepeat.length) { | ||||||
| @ -757,6 +752,12 @@ function draw() { | |||||||
|     nextQueue.draw() |     nextQueue.draw() | ||||||
| } | } | ||||||
|  |  | ||||||
|  | function getKey(action) { | ||||||
|  |     return localStorage.getItem(action) || actionsDefaultKeys[action] | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var actions = {} | ||||||
|  |  | ||||||
| window.onload = function() { | window.onload = function() { | ||||||
|     tempTexts = [] |     tempTexts = [] | ||||||
|  |  | ||||||
| @ -765,6 +766,18 @@ window.onload = function() { | |||||||
|     matrix = new Matrix() |     matrix = new Matrix() | ||||||
|     nextQueue = new NextQueue() |     nextQueue = new NextQueue() | ||||||
|      |      | ||||||
|  |     actions[STATE.PLAYING] = {} | ||||||
|  |     actions[STATE.PLAYING][getKey("moveLeft")] = moveLeft | ||||||
|  |     actions[STATE.PLAYING][getKey("moveRight")] = moveRight | ||||||
|  |     actions[STATE.PLAYING][getKey("softDrop")] = softDrop | ||||||
|  |     actions[STATE.PLAYING][getKey("hardDrop")] = hardDrop | ||||||
|  |     actions[STATE.PLAYING][getKey("rotateCW")] = rotateCW | ||||||
|  |     actions[STATE.PLAYING][getKey("rotateCCW")] = rotateCCW | ||||||
|  |     actions[STATE.PLAYING][getKey("hold")] = hold | ||||||
|  |     actions[STATE.PLAYING][getKey("pause")] = pause | ||||||
|  |     actions[STATE.PAUSED] = {} | ||||||
|  |     actions[STATE.PAUSED][getKey("pause")] = resume | ||||||
|  |     actions[STATE.GAME_OVER] = {} | ||||||
|     pressedKeys = new Set() |     pressedKeys = new Set() | ||||||
|     actionsToRepeat = [] |     actionsToRepeat = [] | ||||||
|     addEventListener("keydown", keyDownHandler, false) |     addEventListener("keydown", keyDownHandler, false) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user