Maze minigame

This commit is contained in:
2026-10-11 15:22:42 -03:00
parent ec34603b0a
commit a63094213e
5 changed files with 169 additions and 9 deletions
+1 -1
View File
@@ -12,7 +12,7 @@
"weight": 25,
"seasons": ["spring", "summer", "autumn", "winter"],
"biomes": ["#bworld:is_ocean"],
"minigame": "ring",
"minigame": "maze",
"difficulty": 100
}
},
+101
View File
@@ -169,6 +169,58 @@ const MINIGAME_SCREENS: Record<string, (menu: ClientMenu, fish: ItemStack) => Me
},
};
},
// a maze over the world. the mouse starts it by going onto the yellow start, wherever it was when it opened, then
// touching a wall or leaving the maze sends button 0, and reaching the green end sends button 1. it's a grid of
// 4 cells a side, and 1 more for every 20 difficulty. the time left shrinks along a bar under it
maze: (menu) => {
const { difficulty } = menu.open_data as { difficulty: number };
const cells = 4 + Math.floor(difficulty / 20);
const size = cells * MAZE_CELL + MAZE_WALL;
const walls = maze_walls(cells);
const cell = (
x: number,
y: number,
) => [x * MAZE_CELL + MAZE_WALL, y * MAZE_CELL + MAZE_WALL, MAZE_CELL - MAZE_WALL];
const [start_x, start_y, pad] = cell(0, 0);
const [end_x, end_y] = cell(cells - 1, cells - 1);
const inside = (x: number, y: number, rx: number, ry: number, w: number, h: number) =>
x >= rx && x < rx + w && y >= ry && y < ry + h;
let started = false;
let over = false;
let last = { x: 0, y: 0 };
return {
width: size,
height: size + 2 * MAZE_WALL,
dim_background: false,
inventory_overlays: false,
on_render_labels(g) {
g.rect(0, 0, size, size, [0.05, 0.1, 0.2, 0.6]);
const time_left = menu.data.ticks_left / menu.data.time || 0;
g.rect(0, size + MAZE_WALL, size * time_left, MAZE_WALL, [1 - time_left, time_left, 0.2, 1]);
g.rect(start_x, start_y, pad, pad, started ? [0.5, 0.5, 0.2, 0.6] : [0.9, 0.8, 0.2, 1]);
g.rect(end_x, end_y, pad, pad, [0.3, 0.85, 0.3, 1]);
for (const [x, y, w, h] of walls) g.rect(x, y, w, h, [0.8, 0.8, 0.8, 1]);
const mouse = { x: g.mouse.x, y: g.mouse.y };
if (!over && !started && inside(mouse.x, mouse.y, start_x, start_y, pad, pad)) started = true;
else if (!over && started) {
// every pixel along the way since last frame, so moving fast doesn't skip a wall
const steps = Math.max(1, Math.ceil(Math.hypot(mouse.x - last.x, mouse.y - last.y)));
for (let i = 1; i <= steps && !over; i++) {
const x = last.x + (mouse.x - last.x) * i / steps;
const y = last.y + (mouse.y - last.y) * i / steps;
const hit = !inside(x, y, 0, 0, size, size) ||
walls.some(([wx, wy, ww, wh]) => inside(x, y, wx, wy, ww, wh));
if (hit || inside(x, y, end_x, end_y, pad, pad)) {
over = true;
menu.click_button(hit ? 0 : 1);
}
}
}
last = mouse;
},
};
},
};
// sends the server button 1 when the mouse is pressed and 0 when it's let go, for minigames you hold the mouse in.
@@ -192,3 +244,52 @@ function ring(g: Graphics, x: number, y: number, radius: number, thickness: numb
g.rect(px, py, thickness, thickness, color);
}
}
// the maze minigame's cells and walls, in gui pixels
const MAZE_CELL = 20;
const MAZE_WALL = 4;
// a maze of cells by cells, as its walls' [x, y, width, height]: a random depth first one, like a recursive
// backtracker, so there's exactly one way between any two cells
function maze_walls(cells: number): [number, number, number, number][] {
// whether each cell has a wall on its right and below it
const right = new Array(cells * cells).fill(true);
const below = new Array(cells * cells).fill(true);
const visited = new Array(cells * cells).fill(false);
const stack = [0];
visited[0] = true;
while (stack.length > 0) {
const at = stack.at(-1)!;
const [x, y] = [at % cells, Math.floor(at / cells)];
const next = [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]
.filter(([nx, ny]) => nx >= 0 && ny >= 0 && nx < cells && ny < cells && !visited[ny * cells + nx])
.map(([nx, ny]) => ny * cells + nx);
if (next.length === 0) {
stack.pop();
continue;
}
const to = next[Math.floor(Math.random() * next.length)];
if (to === at + 1) right[at] = false;
else if (to === at - 1) right[to] = false;
else if (to > at) below[at] = false;
else below[to] = false;
visited[to] = true;
stack.push(to);
}
const size = cells * MAZE_CELL + MAZE_WALL;
const walls: [number, number, number, number][] = [
[0, 0, size, MAZE_WALL],
[0, size - MAZE_WALL, size, MAZE_WALL],
[0, 0, MAZE_WALL, size],
[size - MAZE_WALL, 0, MAZE_WALL, size],
];
for (let i = 0; i < cells * cells; i++) {
const [x, y] = [(i % cells) * MAZE_CELL, Math.floor(i / cells) * MAZE_CELL];
if (right[i] && i % cells < cells - 1) walls.push([x + MAZE_CELL, y, MAZE_WALL, MAZE_CELL + MAZE_WALL]);
if (below[i] && Math.floor(i / cells) < cells - 1) {
walls.push([x, y + MAZE_CELL, MAZE_CELL + MAZE_WALL, MAZE_WALL]);
}
}
return walls;
}
+22
View File
@@ -396,6 +396,27 @@ class RingMinigame extends FishingMinigame {
}
}
// a maze to take the mouse through without touching its walls, bigger the harder the fish is, in 8 seconds from when
// it opens whatever the difficulty. it's all played on the screen, which says how it went: button 1 when the mouse
// reaches the end, 0 when it touches a wall
const MAZE_TIME = 8 * 20;
class MazeMinigame extends FishingMinigame {
#ticks_left = MAZE_TIME;
constructor(...args: MinigameArgs) {
super(...args);
this.menu.add_data("ticks_left", () => this.#ticks_left);
this.menu.add_data("time", () => MAZE_TIME);
this.menu.on_button((_player, id) => {
if (id === 1) this.win();
else if (id === 0) this.lose();
});
this.menu.on_tick(() => {
if (--this.#ticks_left <= 0) this.lose();
});
}
}
// stardew valley's, ported from ports/stardewfishing's FishingMinigame: holding the mouse lifts a green bar up a track
// and letting go drops it, while the fish darts up and down. points go up 1 a tick while the bar is on the fish and
// down 1 while it isn't, starting at a fifth: all of them lands it, none loses it. the bar and points are in the
@@ -506,4 +527,5 @@ const MINIGAMES: Record<string, new (...args: MinigameArgs) => FishingMinigame>
hold: HoldMinigame,
valley: ValleyMinigame,
ring: RingMinigame,
maze: MazeMinigame,
};