UNPKG

prendy

Version:

Make games with prerendered backdrops using babylonjs and repond

92 lines (91 loc) 4.41 kB
import { breakableForEach, forEach } from "chootils/dist/loops"; import { getRefs, getState, onNextTick } from "repond"; import { meta } from "../../meta"; import { getCharDollStuff } from "../prendyUtils/characters"; import { getUsefulStoryStuff } from "./prendyRuleMakers"; export function initPickupsEffects({ onUsePickupAtTrigger, onUsePickupToTalk, onUsePickupGenerally, }) { const onPickupButtonClick = (pickupName) => { const didUsePickupAtTrigger = onUsePickupAtTrigger(pickupName); const didUsePickupWithDoll = onUsePickupToTalk(pickupName); // console.log("didUsePickupAtTrigger", didUsePickupAtTrigger); // console.log("didUsePickupWithDoll", didUsePickupWithDoll); // NOTE the top two functions can return true if they ran, // and if neither returned true, it runs the general one if (!didUsePickupAtTrigger && !didUsePickupWithDoll) { onUsePickupGenerally(pickupName); } }; onNextTick(() => { // This sets an onClick callback in global refs that gets called when clicking the pickup button, // so no rules are actually started here, but it uses the same format as the other rule makers getRefs().global.main.onPickupButtonClick = onPickupButtonClick; }); } // the returned function gets run onClick in the pickup picture button gui export function makeOnUsePickupAtTrigger(callBacksObject, characterNameParam) { const onClickPickupButton = (pickupName) => { const characterName = characterNameParam || meta.assets.characterNames[0]; const { placeInfoByName } = meta.assets; const usefulStoryStuff = getUsefulStoryStuff(); let didInteractWithSomething = false; const { aConvoIsHappening, nowPlaceName } = usefulStoryStuff.globalState; const { atTriggers } = getState().characters[characterName]; console.log("makeOnUsePickupAtTrigger, aConvoIsHappening", aConvoIsHappening); if (aConvoIsHappening) return; const triggerNames = placeInfoByName[nowPlaceName].triggerNames; // NOTE Could b breakable if only checking one trigger forEach(triggerNames, (triggerName) => { if (atTriggers[triggerName]) { const whatToDo = callBacksObject?.[nowPlaceName]?.[triggerName]?.[pickupName]; if (whatToDo) { whatToDo(usefulStoryStuff); didInteractWithSomething = true; } } }); return didInteractWithSomething; }; return onClickPickupButton; } // the returned function gets run onClick in the pickup picture button gui export function makeOnUsePickupGenerally(callBacksObject) { const onClickPickupButton = (pickupName) => { const usefulStoryStuff = getUsefulStoryStuff(); const { aConvoIsHappening } = usefulStoryStuff.globalState; if (aConvoIsHappening) return; // NOTE this should only run if an item wasn't just used with a trigger or a doll callBacksObject?.[pickupName]?.(usefulStoryStuff); }; return onClickPickupButton; } // the returned function gets run onClick in the pickup picture button gui export function makeOnUsePickupToTalk(callBacksObject, characterNameParam) { const onClickPickupButton = (pickupName) => { const characterName = characterNameParam || meta.assets.characterNames[0]; const { dollNames } = meta.assets; let didInteractWithSomething = false; const usefulStoryStuff = getUsefulStoryStuff(); const { aConvoIsHappening } = usefulStoryStuff.globalState; if (aConvoIsHappening) return; const { dollState, dollName: charDollName } = getCharDollStuff(characterName) ?? {}; if (!dollState) return; const { inRange } = dollState; breakableForEach(dollNames, (dollName) => { const whatToDo = callBacksObject[dollName]?.[pickupName]; const isInTalkRange = inRange[dollName].talk; if (dollName !== charDollName && isInTalkRange) { if (whatToDo) { whatToDo(usefulStoryStuff); didInteractWithSomething = true; } return true; // break } }); return didInteractWithSomething; }; return onClickPickupButton; }