🚀 add support for talking quests

This commit is contained in:
Chase Manning 2023-10-12 08:14:53 +01:00
commit 875200fb4d

View file

@ -1,14 +1,18 @@
import { useDispatch, useSelector } from "react-redux";
import { selectMapId, selectPos } from "../state/gameSlice";
import { selectDirection, selectMapId, selectPos } from "../state/gameSlice";
import { useActiveMapQuests } from "../app/use-quests";
import { useEffect } from "react";
import { showTextThenAction } from "../state/uiSlice";
import useEvent from "../app/use-event";
import { Event } from "../app/emitter";
import { directionModifier } from "../app/map-helper";
const QuestHandler = () => {
const dispatch = useDispatch();
const mapId = useSelector(selectMapId);
const quests = useActiveMapQuests(mapId);
const pos = useSelector(selectPos);
const facing = useSelector(selectDirection);
useEffect(() => {
quests.forEach((quest) => {
@ -25,6 +29,23 @@ const QuestHandler = () => {
});
}, [quests, pos, dispatch]);
useEvent(Event.A, () => {
const mod = directionModifier(facing);
const questPos = { x: pos.x + mod.x, y: pos.y + mod.y };
quests.forEach((quest) => {
if (quest.trigger !== "talk") return;
const yPos = quest.positions[questPos.y];
if (!yPos) return;
if (!yPos.includes(questPos.x)) return;
dispatch(
showTextThenAction({
text: quest.text,
action: () => quest.action(),
})
);
});
});
return null;
};