UNPKG

prendy

Version:

Make games with prerendered backdrops using babylonjs and repond

37 lines (36 loc) 1.69 kB
import { forEach } from "chootils/dist/loops"; import { makeEffects } from "repond"; import { meta } from "../../meta"; import { getUsefulStoryStuff } from "./prendyRuleMakers"; export function makeTriggerEffects(callbacksMap, options) { // TODO make dynamic rule? // this won't update the playerCharacter at the moment const { whenLeave = false } = options ?? {}; const charactersWithTriggers = Object.keys(callbacksMap); return makeEffects(({ itemEffect }) => ({ whenAtTriggersChanges: itemEffect({ run({ newValue: atTriggers, prevValue: prevAtTriggers, itemId: characterName }) { const { placeInfoByName } = meta.assets; const usefulStoryStuff = getUsefulStoryStuff(); const { nowPlaceName } = usefulStoryStuff; if (!callbacksMap[characterName]) { return; } const triggerNames = placeInfoByName[nowPlaceName].triggerNames; forEach(triggerNames, (triggerName) => { const justEntered = atTriggers[triggerName] && !prevAtTriggers[triggerName]; const justLeft = !atTriggers[triggerName] && prevAtTriggers[triggerName]; if ((whenLeave && justLeft) || (!whenLeave && justEntered)) { callbacksMap[characterName]?.[nowPlaceName]?.[triggerName]?.(usefulStoryStuff); } }); }, check: { prop: ["atTriggers"], type: "characters", id: charactersWithTriggers, }, step: "collisionReaction", }), })); }