prendy
Version:
Make games with prerendered backdrops using babylonjs and repond
43 lines (42 loc) • 2.3 kB
JavaScript
import { forEach } from "chootils/dist/loops";
import { getState, makeEffects } from "repond";
import { meta } from "../../meta";
import { getCharDollStuff } from "../prendyUtils/characters";
import { getUsefulStoryStuff } from "./prendyRuleMakers";
export function makeTouchEffects(callbacksMap, options) {
const { characterName, distanceType = "touch", whenLeave = false } = options ?? {};
return makeEffects(({ itemEffect }) => ({
whenInRangeChangesToCheckTouch: itemEffect({
run({ newValue: inRange, prevValue: prevInRange, itemId: changedDollName, itemState: dollState }) {
const { dollNames } = meta.assets;
const { playerCharacter } = getState().global.main;
const charName = characterName || playerCharacter;
const { dollName: charDollName } = getCharDollStuff(charName) ?? {};
// at the moment runs for every doll instead of just the main character,
// could maybe fix with dynamic rule for character that checks for doll changes (and runs at start)
if (!charDollName || changedDollName !== charDollName)
return;
// || !dollState.isVisible
const usefulStoryStuff = getUsefulStoryStuff();
forEach(dollNames, (dollName) => {
const otherDollState = getState().dolls[dollName];
// if (!otherDollState.isVisible) return;
const justEntered = inRange[dollName][distanceType] && !prevInRange[dollName][distanceType];
const justLeft = !inRange[dollName][distanceType] && prevInRange[dollName][distanceType];
const whatToRun = callbacksMap[dollName];
if (dollName !== charDollName) {
if ((whenLeave && justLeft) || (!whenLeave && justEntered))
whatToRun?.(usefulStoryStuff);
}
});
},
check: {
prop: ["inRange"],
type: "dolls",
},
id: `inRangeStoryEffects_${characterName ?? "player"}_${distanceType}_${whenLeave}`,
step: "collisionReaction",
atStepEnd: true,
}),
}));
}