lainTSX/src/core/mediaSceneEventHandler.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-11-25 15:14:23 +00:00
const handleMediaSceneEvent = (gameContext: any) => {
2020-11-01 15:40:46 +00:00
const keyPress = gameContext.keyPress;
const activeMediaComponent = gameContext.activeMediaComponent;
2020-12-21 16:56:06 +00:00
const wordPosStateIdx = gameContext.wordPosStateIdx;
const rightSideComponentIdx = gameContext.rightSideComponentIdx;
2020-11-25 15:14:23 +00:00
const rightSideComponents = ["fstWord", "sndWord", "thirdWord"];
2020-12-21 16:56:06 +00:00
const calculateNewRightSide = (
direction: string,
wordPosStateIdx: number,
rightSideComponentIdx: number
) => {
if (direction === "UP") {
wordPosStateIdx--;
if (wordPosStateIdx < 1) {
wordPosStateIdx = 6;
}
rightSideComponentIdx--;
if (rightSideComponentIdx < 0) {
rightSideComponentIdx = 2;
}
} else if (direction === "DOWN") {
wordPosStateIdx++;
if (wordPosStateIdx > 6) {
wordPosStateIdx = 1;
}
rightSideComponentIdx++;
if (rightSideComponentIdx > 2) {
rightSideComponentIdx = 0;
}
}
return {
newWordPosStateIdx: wordPosStateIdx,
newRightSideComponentIdx: rightSideComponentIdx,
};
};
switch (keyPress) {
case "UP":
case "DOWN":
case "RIGHT":
case "LEFT":
if (rightSideComponents.includes(activeMediaComponent)) {
const newRightSide = calculateNewRightSide(
keyPress,
wordPosStateIdx,
rightSideComponentIdx
);
2020-12-07 16:23:57 +00:00
return {
2020-12-21 16:56:06 +00:00
event: `media_rightside_${keyPress.toLowerCase()}`,
newRightSideComponentIdx: newRightSide.newRightSideComponentIdx,
newWordPosStateIdx: newRightSide.newWordPosStateIdx,
2020-12-07 16:23:57 +00:00
};
2020-12-21 16:56:06 +00:00
} else {
const newLeftSideComponentIdx = keyPress === "UP" ? 0 : 1;
2020-12-07 16:23:57 +00:00
return {
2020-12-21 16:56:06 +00:00
event: `media_leftside_${keyPress.toLowerCase()}`,
newLeftSideComponentIdx: newLeftSideComponentIdx,
2020-12-07 16:23:57 +00:00
};
2020-12-21 16:56:06 +00:00
}
case "CIRCLE":
return { event: `media_${activeMediaComponent}_select` };
2020-11-25 15:14:23 +00:00
}
2020-11-01 15:40:46 +00:00
};
export default handleMediaSceneEvent;