lainTSX/src/core/input-handlers/handleBootSceneInput.ts

162 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-02-09 17:26:08 +00:00
import authorize_user_letters from "../../resources/authorize_user_letters.json";
import handleNameSelection from "../../helpers/name-selection-helpers";
import {
changeMainMenuComponent,
changePromptComponent,
enterLoadData,
enterUserAuthorization,
exitLoadData,
exitUserAuthorization,
failUpdatePlayerName,
loadGame,
loadGameFail,
removePlayerNameLastChar,
startNewGame,
2021-03-02 16:37:14 +00:00
updateAuthorizeUserLetterMatrixIndices,
updatePlayerName,
} from "../eventTemplates";
import { BootSceneContext, GameEvent } from "../../types/types";
2021-02-09 17:26:08 +00:00
2021-02-25 17:47:42 +00:00
const handleBootSceneInput = (
bootSceneContext: BootSceneContext,
keyPress: string
): GameEvent | undefined => {
const {
subscene,
activeMainMenuComponent,
activePromptComponent,
promptVisible,
2021-02-09 17:26:08 +00:00
playerName,
2021-03-02 16:37:14 +00:00
authorizeUserMatrixIndices,
} = bootSceneContext;
if (promptVisible) {
switch (keyPress) {
case "LEFT":
return changePromptComponent({ activePromptComponent: "yes" });
case "RIGHT":
return changePromptComponent({ activePromptComponent: "no" });
case "CIRCLE":
switch (activePromptComponent) {
case "no":
return exitLoadData;
case "yes":
const stateToLoad = localStorage.getItem("lainSaveState");
if (stateToLoad)
return loadGame({
userSaveState: JSON.parse(stateToLoad),
});
else return loadGameFail;
}
}
} else {
switch (subscene) {
case "main_menu":
switch (keyPress) {
case "UP":
case "DOWN":
const newComponent =
keyPress === "UP" ? "authorize_user" : "load_data";
return changeMainMenuComponent({
activeMainMenuComponent: newComponent,
});
case "CIRCLE":
switch (activeMainMenuComponent) {
case "authorize_user":
return enterUserAuthorization;
case "load_data":
return enterLoadData;
}
}
break;
case "authorize_user":
switch (keyPress) {
2021-02-10 16:05:20 +00:00
case "START":
2021-02-25 17:47:42 +00:00
if (playerName.length > 0) return startNewGame;
return;
case "CROSS":
2021-02-09 17:26:08 +00:00
if (playerName.length > 0) {
return removePlayerNameLastChar({
2021-02-09 17:26:08 +00:00
playerName: playerName.slice(0, -1),
});
2021-02-09 17:26:08 +00:00
} else {
return exitUserAuthorization;
2021-02-09 17:26:08 +00:00
}
2021-03-02 16:37:14 +00:00
case "LEFT": {
const newMatrixIndices = {
...authorizeUserMatrixIndices,
colIdx:
authorizeUserMatrixIndices.colIdx - 1 < 0
? authorizeUserMatrixIndices.colIdx
: authorizeUserMatrixIndices.colIdx - 1,
};
return updateAuthorizeUserLetterMatrixIndices({
authorizeUserLetterMatrixIndices: newMatrixIndices,
});
}
case "RIGHT": {
const newMatrixIndices = {
...authorizeUserMatrixIndices,
colIdx:
authorizeUserMatrixIndices.colIdx + 1 > 12
? authorizeUserMatrixIndices.colIdx
: authorizeUserMatrixIndices.colIdx + 1,
};
return updateAuthorizeUserLetterMatrixIndices({
authorizeUserLetterMatrixIndices: newMatrixIndices,
});
}
case "DOWN": {
const newMatrixIndices = {
...authorizeUserMatrixIndices,
rowIdx:
authorizeUserMatrixIndices.rowIdx + 1 > 4
? authorizeUserMatrixIndices.rowIdx
: authorizeUserMatrixIndices.rowIdx + 1,
};
return updateAuthorizeUserLetterMatrixIndices({
authorizeUserLetterMatrixIndices: newMatrixIndices,
});
}
case "UP": {
const newMatrixIndices = {
...authorizeUserMatrixIndices,
rowIdx:
authorizeUserMatrixIndices.rowIdx - 1 < 0
? authorizeUserMatrixIndices.rowIdx
: authorizeUserMatrixIndices.rowIdx - 1,
};
return updateAuthorizeUserLetterMatrixIndices({
authorizeUserLetterMatrixIndices: newMatrixIndices,
});
}
2021-02-09 17:26:08 +00:00
case "CIRCLE":
const chosenCharacter =
2021-03-02 16:37:14 +00:00
authorize_user_letters.matrix[authorizeUserMatrixIndices.rowIdx][
authorizeUserMatrixIndices.colIdx
2021-02-09 17:26:08 +00:00
];
2021-03-02 16:37:14 +00:00
if (chosenCharacter) {
const newName = handleNameSelection(playerName, chosenCharacter);
2021-02-09 17:26:08 +00:00
2021-03-02 16:37:14 +00:00
if (newName?.length === 8) return;
if (newName !== undefined)
return updatePlayerName({ playerName: newName });
else return failUpdatePlayerName;
}
}
}
}
};
2021-02-25 17:47:42 +00:00
export default handleBootSceneInput;