Skip to content

Commit

Permalink
fixed bug where keybinds dont update until refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Spentine committed Jul 27, 2024
1 parent c835600 commit 9e63a72
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
9 changes: 7 additions & 2 deletions input.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
class InputHandler {
constructor(keyMap) {
this.keyMap = keyMap;
this.directInterface = {}; // direct layer from input
this.inputs = {}; // reformats direct layer to be usable
this.keyMapKeys = Object.keys(this.keyMap); // all keys
this.changeInputs(keyMap);

for (let i in this.keyMapKeys) {
this.directInterface[this.keyMapKeys[i]] = null;
this.inputs[keyMap[this.keyMapKeys[i]]] = null;
}
}

changeInputs(keyMap) {
this.keyMap = keyMap;
this.keyMapKeys = Object.keys(this.keyMap); // all keys
}

keyDown(e) {
if (!e.repeat) { // this function will also be called bc of built-in key ARR
this.directInterface[e.code] = Date.now(); // the code for the keyDown is here
Expand Down Expand Up @@ -41,6 +45,7 @@ class InputHandler {
}
}
}

return this.inputs;
}
}
Expand Down
7 changes: 5 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ function DOMLoaded(event) {
startGame(data);
renderer.updateScene(scene);
}
renderer.updateKeybindInputs = function() {
inputHandler.changeInputs(userSettings.inGame.keyMappings);
}
renderer.saveBlockStackerStorage = saveBlockStackerStorage;

const UIStartElement = document.getElementById('UI-play');
Expand Down Expand Up @@ -199,7 +202,7 @@ function DOMLoaded(event) {
renderer.updateKeybindButtons();

saveBlockStackerStorage();
renderer.updateHandlingInputs();
inputHandler.changeInputs(userSettings.inGame.keyMappings);
});

const UIHandlingElement = document.getElementById('UI-handling');
Expand All @@ -220,7 +223,7 @@ function DOMLoaded(event) {
userSettings.inGame.handling = defaultUserSettings.inGame.handling;

saveBlockStackerStorage();
renderer.updateHandlingInputs();
inputHandler.changeInputs(userSettings.inGame.keyMappings);
});

const trainingPackInput = document.getElementById('UI-trainingPackInput');
Expand Down
3 changes: 3 additions & 0 deletions render.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ class GameRenderer {
this.uiScaling = 1;
this.persistentEffects = {};
this.saveBlockStackerStorage = function() {}
this.updateKeybindInputs = function() {};
this.startGame = null;
this.pendingKeydown = {
"keybind": [],
Expand Down Expand Up @@ -578,6 +579,7 @@ class GameRenderer {
keyButton.addEventListener("click", () => {
delete this.userSettings.inGame.keyMappings[allKeys[keyIndex]];
this.saveBlockStackerStorage();
this.updateKeybindInputs();
this.updateKeybindButtons();
});
}
Expand Down Expand Up @@ -615,6 +617,7 @@ class GameRenderer {
if (active && !e.repeat) {
this.userSettings.inGame.keyMappings[e.code] = currentAction;
this.saveBlockStackerStorage();
this.updateKeybindInputs();
this.updateKeybindButtons();
active = false;
}
Expand Down

0 comments on commit 9e63a72

Please sign in to comment.