Skip to content

Instantly share code, notes, and snippets.

@naosim
Last active October 18, 2024 19:04
Show Gist options
  • Save naosim/45fe48cb7f62af3cb280cb3ddbcd5162 to your computer and use it in GitHub Desktop.
Save naosim/45fe48cb7f62af3cb280cb3ddbcd5162 to your computer and use it in GitHub Desktop.
【microScript】ゲームパッドのラッパー
class GamePad {
UP = false;
DOWN = false;
RIGHT = false;
LEFT = false;
A = false;
B = false;
press = {
UP: false,
DOWN: false,
RIGHT: false,
LEFT: false,
A: false,
B: false,
};
release = {
UP: false,
DOWN: false,
RIGHT: false,
LEFT: false,
A: false,
B: false,
}
update() {
const arrowKeys = ["UP", "DOWN", "RIGHT", "LEFT", "A", "B"];
const keyboardMap = {
UP: ["ARROW_UP", "E"],
DOWN: ["ARROW_DOWN", "D"],
LEFT: ["ARROW_LEFT", "S"],
RIGHT: ["ARROW_RIGHT", "F"],
A: ["SPACE", "K", "A"],
B: ["ENTER", "J", "B"],
}
arrowKeys.forEach(k => {
this[k] = gamepad[k] || keyboardMap[k].some(v => keyboard[v])
this.press[k] = gamepad.press[k] || keyboardMap[k].some(v => keyboard.press[v])
this.release[k] = gamepad.release[k] || keyboardMap[k].some(v => keyboard.release[v])
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment