From e388d2353c42ffab97ce630bb92a62d4c0e6283d Mon Sep 17 00:00:00 2001 From: redbirdly <155550833+redbirdly@users.noreply.github.com> Date: Wed, 14 Jan 2026 12:46:46 +0800 Subject: [PATCH] Create junction.js --- mods/junction.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 mods/junction.js diff --git a/mods/junction.js b/mods/junction.js new file mode 100644 index 00000000..35cf40a7 --- /dev/null +++ b/mods/junction.js @@ -0,0 +1,50 @@ +// Junction.js + +const connectedPairs = [ + [[-1, 0], [1, 0]], // Horizontal + [[0, -1], [0, 1]] // Vertical +]; + +let isRisingEdge = (current, prev) => current === 2 && prev !== 2; +let isFallingEdge = (current, prev) => current !== 2 && prev === 2; + +elements.junction = { + color: "#9db8b5", + category: "logic", + state: "solid", + behavior: behaviors.WALL, + tick: function (pixel) { + if (pixel.start === pixelTicks) + pixel.prevStateMap = {}; + + for (const pair of connectedPairs) { + let pixelA = pixelMap[pixel.x + pair[0][0]]?.[pixel.y + pair[0][1]]; + let pixelB = pixelMap[pixel.x + pair[1][0]]?.[pixel.y + pair[1][1]]; + + if (!pixelA) continue; + if (!pixelB) continue; + if (pixelA.element !== "logic_wire") continue; + if (pixelB.element !== "logic_wire") continue; + + let stateA = pixelA.lstate; + let stateB = pixelB.lstate; + let prevStateA = pixel.prevStateMap[[pixelA.x, pixelA.y]]; + let prevStateB = pixel.prevStateMap[[pixelB.x, pixelB.y]]; + + // console.log("A:", stateA, "->", prevStateA); + // console.log("B:", stateB, "->", prevStateB); + + // Set previous lstate to current lstate + pixel.prevStateMap[[pixelA.x, pixelA.y]] = pixelA.lstate; + pixel.prevStateMap[[pixelB.x, pixelB.y]] = pixelB.lstate; + + // Rising edge + if (isRisingEdge(stateA, prevStateA)) pixelB.lstate = 2; + if (isRisingEdge(stateB, prevStateB)) pixelA.lstate = 2; + + // Falling edge + if (isFallingEdge(stateA, prevStateA)) pixelB.lstate = -2; + if (isFallingEdge(stateB, prevStateB)) pixelA.lstate = -2; + } + } +};