Skip to content

Instantly share code, notes, and snippets.

@lajbel
Created November 17, 2022 15:04
Show Gist options
  • Save lajbel/9f178664b17df2096d0fbb5cee083234 to your computer and use it in GitHub Desktop.
Save lajbel/9f178664b17df2096d0fbb5cee083234 to your computer and use it in GitHub Desktop.
import { Key, KaboomCtx } from "kaboom";
import "kaboom/global";
export interface GlobalInputPlugin {
setupInput(inputKeys: { [key: string]: Key }, isDebug: boolean): void;
getKey(alias: string): Key;
getGlobalInput(): any;
}
declare global {
const setupInput: GlobalInputPlugin["setupInput"];
const getKey: GlobalInputPlugin["getKey"];
const getGlobalInput: GlobalInputPlugin["getGlobalInput"];
}
export default function globalInput(_k: KaboomCtx): GlobalInputPlugin {
let keys: { [key: string]: Key };
return {
setupInput(inputKeys, _isDebug) {
keys = inputKeys;
},
getKey(alias): Key {
return keys[alias];
},
getGlobalInput() {
return keys;
}
}
}
// use:
// load input
setupInput({
"move_up": "up",
"move_down": "down",
"move_left": "left",
"move_right": "right",
"get_obj": "e",
"rotate_obj": "q",
"misc_auto_rotate": "rotate",
});
// use
onKeyDown(getKey("move_right"), () => {
ranib.move(RIGHT.scale(ranib.speed));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment