commit 34efe7c35ad66fbfc125759738ab5fa7e1faa91e Author: commodore Date: Wed Mar 11 09:56:10 2026 -0400 Add mod.js diff --git a/mod.js b/mod.js new file mode 100644 index 0000000..968342c --- /dev/null +++ b/mod.js @@ -0,0 +1,114 @@ +// my1stmod.js - A configurable delay element for Sandboxels +// When powered from the bottom, it waits for a delay then outputs power to the top +// When powered from the sides, it changes the delay duration + +elements.delay_gate = { + color: "#8B4513", // Saddle brown color + behavior: [ + ["XX","SH:delay_gate","XX"], // Top: outputs shock to above + ["SH:delay_gate","XX","SH:delay_gate"], // Sides: accept input to change delay + ["XX","SH","XX"] // Bottom: accept power input + ], + category: "machines", + state: "solid", + conduct: 1, // Conducts electricity + properties: { + delay: 30, // Default delay in ticks (0.5 seconds at 60fps) + timer: 0, // Current timer count + powered: false // Is currently receiving power? + }, + tick: function(pixel) { + // Initialize properties if not set + if (pixel.delay === undefined) pixel.delay = 30; + if (pixel.timer === undefined) pixel.timer = 0; + if (pixel.powered === undefined) pixel.powered = false; + + // Check for power input from below (pixel.y + 1) + let below = getPixel(pixel.x, pixel.y + 1); + let receivingPower = false; + + if (below && (below.charge || below.chargeCD)) { + receivingPower = true; + } + + // Check for side inputs to adjust delay (left = decrease, right = increase) + let left = getPixel(pixel.x - 1, pixel.y); + let right = getPixel(pixel.x + 1, pixel.y); + + // Left side powered = decrease delay (faster) + if (left && (left.charge || left.chargeCD) && pixel.delay > 5) { + pixel.delay--; + // Visual feedback - flash lighter + pixel.color = "#A0522D"; + setTimeout(() => { + if (pixel) pixel.color = "#8B4513"; + }, 100); + } + + // Right side powered = increase delay (slower) + if (right && (right.charge || right.chargeCD) && pixel.delay < 300) { + pixel.delay++; + // Visual feedback - flash darker + pixel.color = "#654321"; + setTimeout(() => { + if (pixel) pixel.color = "#8B4513"; + }, 100); + } + + // Handle power delay logic + if (receivingPower) { + if (!pixel.powered) { + // Just started receiving power + pixel.powered = true; + pixel.timer = 0; + } + + // Increment timer while powered + pixel.timer++; + + // Check if delay reached + if (pixel.timer >= pixel.delay) { + // Output power to above + let above = getPixel(pixel.x, pixel.y - 1); + if (above && above.conduct) { + above.charge = 1; + above.chargeCD = 5; // Charge cooldown + } + // Visual indicator - turn yellow when outputting + pixel.color = "#FFD700"; + } else { + // Waiting state - pulsing color based on progress + let progress = pixel.timer / pixel.delay; + let r = Math.floor(139 + (255 - 139) * progress); + let g = Math.floor(69 + (215 - 69) * progress); + let b = Math.floor(19 + (0 - 19) * progress); + pixel.color = `rgb(${r},${g},${b})`; + } + } else { + // No power input + if (pixel.powered) { + // Just lost power, reset + pixel.powered = false; + pixel.timer = 0; + pixel.color = "#8B4513"; + } + } + } +}; + +// Optional: Add a tool to manually set delay values +elements.delay_setter = { + color: "#4B0082", + tool: function(pixel) { + if (pixel.element === "delay_gate") { + // Cycle through preset delays: 10, 30, 60, 120, 300 ticks + const delays = [10, 30, 60, 120, 300]; + let currentIdx = delays.indexOf(pixel.delay); + let nextIdx = (currentIdx + 1) % delays.length; + pixel.delay = delays[nextIdx]; + logMessage(`Delay set to ${pixel.delay} ticks`); + } + }, + category: "tools", + desc: "Click on a Delay Gate to cycle through delay presets" +}; \ No newline at end of file