Skip to content

Commit

Permalink
Mass refactor for endless nested menus
Browse files Browse the repository at this point in the history
  • Loading branch information
tiliv committed Apr 29, 2024
1 parent d902800 commit cef0f69
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 275 deletions.
57 changes: 1 addition & 56 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Analysis from './components/Analysis';
import useAnalyzer from './hooks/useAnalyzer';
import useInventory from './hooks/useInventory';
import useSave from './hooks/useSave';
import { TARGETED_ACTIONS, ACTIONS_ORDER } from './Actions';

const START_WORLD = 'Terra Montans.txt'
const START_Y = 17;
Expand All @@ -28,14 +27,11 @@ export default function App({
// const { ready, analyze, blocks } = useAnalyzer();

const [magnification, setMagnification] = useState(startMagnification);
const [menuChoice, setMenuChoice] = useState(null);
const [targetData, setTargetData] = useState(null);
const [width, setWidth] = useState(startWidth);
const [height, setHeight] = useState(startHeight);
const [startWorld, setStartWorld] = useState(beginWorld);
const [startX, setStartX] = useState(beginX);
const [startY, setStartY] = useState(beginY);
const [activeOptions, setActiveOptions] = useState(ACTIONS_ORDER);

const [interaction, setInteraction] = useState(null);
const { inventory, equipment, equip, acquire } = useInventory('player');
Expand Down Expand Up @@ -73,25 +69,6 @@ export default function App({
startY: [startY, setStartY],
});

// Respond to 'Load' menu choice event by throwing the real `load` event.
useEffect(() => {
const loadHandler = (e) => {
const loadEvent = new CustomEvent('load', { detail: e.detail });
window.dispatchEvent(loadEvent);
};
window.addEventListener('Load', loadHandler);
return () => window.removeEventListener('Load', loadHandler);
}, []);

// Respond to 'menuChoice' event from menu and feed it back as stable state
useEffect(() => {
const menuChoiceHandler = (e) => {
setMenuChoice(e.detail);
};
window.addEventListener('menuChoice', menuChoiceHandler);
return () => window.removeEventListener('menuChoice', menuChoiceHandler);
}, []);

// Respond to 'interaction' event from world by saving it for other displays
useEffect(() => {
const interactionHandler = (e) => {
Expand All @@ -115,35 +92,6 @@ export default function App({
return () => window.removeEventListener('destination', destinationHandler);
}, [startWorld]);

// Prepare active menu choices based on the target's data
useEffect(() => {
if (!interaction) {
setActiveOptions(ACTIONS_ORDER.filter((option) => !TARGETED_ACTIONS.includes(option)));
return;
};
const newOptions = [];

// Add official actions to the menu choices
ACTIONS_ORDER.forEach((option) => {
if (interaction[option]) {
newOptions.push(option);
}
});

// Add extra Capitalized actions from the target's data
newOptions.push(...Object.keys(interaction).filter((option) => {
if (newOptions.includes(option)) return false;
return /^[A-Z]$/.test(option[0]);
}));
setActiveOptions(newOptions);
}, [interaction]);

// Load interaction data based on both the target and the menu choice
useEffect(() => {
if (!interaction || !menuChoice) return;
setTargetData(interaction);
}, [interaction, menuChoice]);

return (
<>
<h1>FM</h1>
Expand Down Expand Up @@ -183,7 +131,7 @@ export default function App({
keyMap={statsKeyMap}
/>
<DisplayWorld
target={menuChoice ? interaction : null}
target={interaction}

inventory={inventory}
equipment={equipment}
Expand All @@ -200,9 +148,6 @@ export default function App({
/>
<DisplayMenu
target={interaction}
targetData={targetData}
activeChoice={menuChoice}
options={activeOptions}

inventory={inventory}
equipment={equipment}
Expand Down
Loading

0 comments on commit cef0f69

Please sign in to comment.