Merge pull request #391 from GGodPL/main

This commit is contained in:
slweeb 2024-03-04 09:00:25 -05:00 committed by GitHub
commit 35100874b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 81 additions and 0 deletions

81
mods/maze.js Normal file
View File

@ -0,0 +1,81 @@
{
const inBounds = (x, y, width, height) => x >= 0 && x < width && y >= 0 && y < height;
const getRandomValue = (min, max) => Math.floor(Math.random() * (max - min) + min);
const generateMaze = (w, h, originX, originY) => {
const map = [];
for (let i = 0; i < w; i++) {
map[i] = [];
for (let j = 0; j < w; j++) {
map[i][j] = 0;
}
}
const stack = [];
let current = [];
const start = [w - 2, h - 2];
stack.push(start);
current = start;
while (stack.length > 0) {
const [x, y] = current;
const neighbors = [
inBounds(x, y + 2, w, h) ? [x, y + 2, 0, 1] : null,
inBounds(x, y - 2, w, h) ? [x, y - 2, 0, -1] : null,
inBounds(x + 2, y, w, h) ? [x + 2, y, 1, 0] : null,
inBounds(x - 2, y, w, h) ? [x - 2, y, -1, 0] : null
].filter(a => a != null && map[a[0]][a[1]] == 0);
if (neighbors.length == 0) {
const cell = stack.pop();
current = cell;
continue;
}
const next = neighbors[getRandomValue(0, neighbors.length)];
const [offsetX, offsetY] = next.slice(2);
map[x + offsetX][y + offsetY] = 1;
map[x + offsetX * 2][y + offsetY * 2] = 1;
stack.push([x, y]);
current = [next[0], next[1]];
}
for (let i = 0; i <= w; i++) {
for (let j = 0; j <= h; j++) {
const x = i + originX;
const y = j + originY;
if (pixelMap[x][y]) deletePixel(x, y);
if (i == 0 || j == 0) createPixel("wall", x, y);
else if (map[i - 1][j - 1] == 0) createPixel("wall", x, y);
if ((i == 1 && j == 0) || (i == w - 1 && j == h)) deletePixel(x, y);
}
}
}
elements.maze = {
color: "#760144",
category: "special",
tool: (_) => {},
onMouseDown: () => {
if (outOfBounds(Math.floor(mousePos.x - mouseSize / 2) + 1, Math.floor(mousePos.y - mouseSize / 2) + 1) || outOfBounds(Math.floor(mousePos.x - mouseSize / 2) + mouseSize, Math.floor(mousePos.y - mouseSize / 2) + mouseSize)) return;
generateMaze(mouseSize - 1, mouseSize - 1, Math.floor(mousePos.x - mouseSize / 2) + 1, Math.floor(mousePos.y - mouseSize / 2) + 1)
}
}
runAfterLoadList.push(() => {
if (!enabledMods.includes("mods/editTools.js")) {
document.addEventListener("mousedown", (ev) => {
if (elements[currentElement].onMouseDown) {
elements[currentElement].onMouseDown();
}
})
}
})
}