lainTSX/src/core/scene-keypress-handlers/handleMediaSceneKeyPress.ts

140 lines
3.9 KiB
TypeScript
Raw Normal View History

import { findNodeFromWord } from "../../helpers/media-helpers";
import { MediaSceneContext, RightMediaComponent } from "../../store";
import {
changeLeftMediaComponent,
changeMediaSide,
changeRightMediaComponent,
exitMedia,
playMedia,
selectWord,
wordNotFound,
} from "../eventTemplates";
import { GameEvent } from "../handleEvent";
import { isNodeVisible } from "../../helpers/node-helpers";
const handleMediaSceneKeyPress = (
mediaSceneContext: MediaSceneContext
): GameEvent | undefined => {
2021-02-01 18:13:32 +00:00
const {
keyPress,
activeMediaComponent,
wordPosStateIdx,
activeNode,
activeSite,
gameProgress,
currentMediaSide,
lastActiveMediaComponents,
2021-02-01 18:13:32 +00:00
} = mediaSceneContext;
2020-12-21 16:56:06 +00:00
switch (currentMediaSide) {
case "left":
switch (keyPress) {
case "UP":
case "DOWN": {
const newComponent = keyPress === "UP" ? "play" : "exit";
return changeLeftMediaComponent({ activeComponent: newComponent });
}
case "RIGHT": {
return changeMediaSide({
activeMediaComponent: lastActiveMediaComponents.right,
lastActiveMediaComponents: {
...lastActiveMediaComponents,
left: activeMediaComponent as "play" | "exit",
},
currentMediaSide: "right",
});
}
case "CIRCLE":
switch (activeMediaComponent) {
case "play":
return playMedia;
case "exit":
return exitMedia;
}
2020-12-21 16:56:06 +00:00
}
break;
case "right":
switch (keyPress) {
case "UP": {
const newWordPosStateIdx =
wordPosStateIdx - 1 < 1 ? 6 : wordPosStateIdx - 1;
const newComponent = (() => {
switch (activeMediaComponent) {
default:
case "fstWord":
return "thirdWord";
case "sndWord":
return "fstWord";
case "thirdWord":
return "sndWord";
}
})();
return changeRightMediaComponent({
activeComponent: newComponent,
wordPosStateIdx: newWordPosStateIdx,
});
}
case "DOWN": {
const newWordPosStateIdx =
wordPosStateIdx + 1 > 6 ? 1 : wordPosStateIdx + 1;
const newComponent = (() => {
switch (activeMediaComponent) {
default:
case "fstWord":
return "sndWord";
case "sndWord":
return "thirdWord";
case "thirdWord":
return "fstWord";
}
})();
2020-12-21 16:56:06 +00:00
return changeRightMediaComponent({
activeComponent: newComponent,
wordPosStateIdx: newWordPosStateIdx,
});
}
2020-12-21 16:56:06 +00:00
case "LEFT":
return changeMediaSide({
activeMediaComponent: lastActiveMediaComponents.left,
lastActiveMediaComponents: {
...lastActiveMediaComponents,
right: activeMediaComponent as RightMediaComponent,
},
currentMediaSide: "left",
});
case "CIRCLE":
const wordIdx = (() => {
switch (activeMediaComponent as RightMediaComponent) {
case "fstWord":
return 1;
case "sndWord":
return 2;
case "thirdWord":
return 3;
}
})();
const nodeName = activeNode.node_name;
const wordToFind = activeNode.words[wordIdx];
const data = findNodeFromWord(wordToFind, nodeName, activeSite);
const { node, level, siteRotY } = { ...data };
if (!isNodeVisible(node, gameProgress)) return wordNotFound;
return selectWord({
activeNode: node,
activeLevel: level,
siteRot: [0, siteRotY, 0],
});
}
2020-11-25 15:14:23 +00:00
}
2020-11-01 15:40:46 +00:00
};
export default handleMediaSceneKeyPress;