Move every fishing minigame to their own files
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// the screens of the base game's menus, see scripts/server.ts for their slots. in gui pixels, like minecraft's
|
||||
import type { ClientContext, ClientMenu, Color, Graphics, ItemStack, MenuScreen } from "bworld/client";
|
||||
import type { ClientContext, ClientMenu, Graphics, ItemStack, MenuScreen } from "bworld/client";
|
||||
import { MINIGAMES } from "./minigames/mod.ts";
|
||||
|
||||
const WIDTH = 176;
|
||||
// like minecraft's 176x166 container screens
|
||||
@@ -68,228 +69,10 @@ function bar(
|
||||
}
|
||||
}
|
||||
|
||||
// a fishing minigame's screen, by the minigame's name. see the minigames at the end of scripts/fishing.ts
|
||||
// a fishing minigame's screen, by the minigame's name, see minigames/mod.ts
|
||||
function fishing_minigame_screen(menu: ClientMenu): MenuScreen {
|
||||
const { minigame, fish } = menu.open_data as { minigame: string; fish: ItemStack };
|
||||
const screen = MINIGAME_SCREENS[minigame];
|
||||
const screen = MINIGAMES[minigame]?.screen;
|
||||
if (!screen) throw new Error(`the fishing minigame ${minigame} has no screen`);
|
||||
return screen(menu, fish);
|
||||
}
|
||||
|
||||
const MINIGAME_SCREENS: Record<string, (menu: ClientMenu, fish: ItemStack) => MenuScreen> = {
|
||||
// the fish, and a button in the middle that catches it
|
||||
test: (menu, fish) => {
|
||||
const width = 120;
|
||||
const height = 80;
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
dim_background: false,
|
||||
inventory_overlays: false,
|
||||
on_render_background(g) {
|
||||
g.panel(0, 0, width, height);
|
||||
},
|
||||
on_render_labels(g) {
|
||||
g.text(menu.title, 8, 6);
|
||||
g.item(fish.id, width / 2 - 8, 18);
|
||||
if (g.button("Catch", width / 2 - 25, height / 2, 50, 18)) {
|
||||
menu.click_button(0);
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
// "Hold !" and a bar under it, filling while the mouse is held. the server counts, this sends when the mouse is
|
||||
// pressed (button 1) and let go (button 0)
|
||||
hold: (menu) => {
|
||||
const width = 120;
|
||||
const height = 44;
|
||||
const send_mouse = mouse_sender(menu);
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
dim_background: false,
|
||||
inventory_overlays: false,
|
||||
on_render_background(g) {
|
||||
g.panel(0, 0, width, height);
|
||||
},
|
||||
on_render_labels(g) {
|
||||
send_mouse(g);
|
||||
const text = "Reel !";
|
||||
g.text(text, (width - g.measure_text(text)) / 2, 8);
|
||||
const done = menu.data.progress / menu.data.max || 0;
|
||||
g.rect(10, 26, width - 20, 8, [0.15, 0.1, 0.05, 1]);
|
||||
g.rect(10, 26, (width - 20) * done, 8, [0.3, 0.8, 0.3, 1]);
|
||||
},
|
||||
};
|
||||
},
|
||||
// stardew valley's: a track with the fish and the green bar the mouse lifts, and how close it is to landing on the
|
||||
// right. laid out like ports/stardewfishing's screen, in its gui pixels: positions are from the track's bottom
|
||||
valley: (menu, fish) => {
|
||||
const width = 38;
|
||||
const height = 152;
|
||||
const send_mouse = mouse_sender(menu);
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
dim_background: false,
|
||||
inventory_overlays: false,
|
||||
on_render_background(g) {
|
||||
g.panel(0, 0, width, height);
|
||||
g.rect(14, 4, 16, 144, [0.2, 0.35, 0.6, 1]);
|
||||
},
|
||||
on_render_labels(g) {
|
||||
send_mouse(g);
|
||||
const { bar, bar_size, points, max_points } = menu.data;
|
||||
g.rect(14, 4 + 142 - bar - bar_size, 16, bar_size, [0.4, 0.85, 0.3, 0.8]);
|
||||
g.item(fish.id, 14, 4 + 142 - menu.data.fish - 16);
|
||||
const done = points / max_points || 0;
|
||||
g.rect(32, 4, 4, 144, [0.15, 0.1, 0.05, 1]);
|
||||
g.rect(32, 148 - done * 144, 4, done * 144, [1 - done, done, 0.2, 1]);
|
||||
},
|
||||
};
|
||||
},
|
||||
// the gray rings, green once they're checked, and the green one growing out from the middle, over the world. a
|
||||
// click sends button 1
|
||||
ring: (menu) => {
|
||||
const { rings, ring_radius, ring_spacing, ring_width } = menu.data;
|
||||
const size = 2 * (ring_radius + (rings - 1) * ring_spacing + ring_width);
|
||||
return {
|
||||
width: size,
|
||||
height: size,
|
||||
dim_background: false,
|
||||
inventory_overlays: false,
|
||||
on_render_labels(g) {
|
||||
if (g.mouse.pressed) menu.click_button(1);
|
||||
const { radius, checked } = menu.data;
|
||||
for (let i = 0; i < rings; i++) {
|
||||
const color: Color = i < checked ? [0.3, 0.85, 0.3, 0.9] : [0.8, 0.8, 0.8, 0.9];
|
||||
ring(g, size / 2, size / 2, ring_radius + i * ring_spacing, ring_width, color);
|
||||
}
|
||||
ring(g, size / 2, size / 2, radius, 2, [0.3, 0.85, 0.3, 1]);
|
||||
},
|
||||
};
|
||||
},
|
||||
// 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.
|
||||
// call it every frame
|
||||
function mouse_sender(menu: ClientMenu): (g: Graphics) => void {
|
||||
let holding = false;
|
||||
return (g) => {
|
||||
if (g.mouse.down === holding) return;
|
||||
holding = g.mouse.down;
|
||||
menu.click_button(holding ? 1 : 0);
|
||||
};
|
||||
}
|
||||
|
||||
// a ring around x, y: squares as big as the line is thick, close enough around to look like one
|
||||
function ring(g: Graphics, x: number, y: number, radius: number, thickness: number, color: Color) {
|
||||
const steps = Math.max(8, Math.ceil(2 * Math.PI * radius / (thickness / 2)));
|
||||
for (let i = 0; i < steps; i++) {
|
||||
const angle = i / steps * 2 * Math.PI;
|
||||
const px = x + Math.cos(angle) * radius - thickness / 2;
|
||||
const py = y + Math.sin(angle) * radius - thickness / 2;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
// fishing, like minecraft's: right click with a rod to throw its hook forward. when it lands in a block with
|
||||
// bworld:fishable, like water, it floats there until something bites, and right clicking again before it gets away
|
||||
// reels it in. a hook that lands on anything else lies there until it's reeled in, with nothing on it
|
||||
import type { EntityRef, ItemStack, MenuBuilder, Player, Season, ServerContext } from "bworld/server";
|
||||
import type { EntityRef, ItemStack, Player, Season, ServerContext } from "bworld/server";
|
||||
import type { Position } from "bworld/shared";
|
||||
import { MINIGAMES } from "./minigames/mod.ts";
|
||||
|
||||
const FISHING_ROD = "bworld:fishing_rod";
|
||||
const FISHING_HOOK = "bworld:fishing_hook";
|
||||
const FISHABLE = "bworld:fishable";
|
||||
const FISH = "bworld:fish";
|
||||
// the menu a fish's minigame is played in, with a screen in client.ts
|
||||
// the menu a fish's minigame is played in. client.ts gives it the minigame's screen, see minigames/mod.ts
|
||||
const MINIGAME_MENU = "bworld:fishing_minigame";
|
||||
// built from every item with bworld:fish, see register_fishing
|
||||
const FISHING_TABLE = "bworld:gameplay/fishing";
|
||||
@@ -48,7 +49,7 @@ interface FishParams {
|
||||
time?: { from: number; to: number };
|
||||
// the biomes and #biome tags it's caught in, every one when not given
|
||||
biomes?: string[];
|
||||
// the minigame that lands it, one of MINIGAMES at the end of this file. caught right away when not given
|
||||
// the minigame that lands it, one of MINIGAMES in minigames/mod.ts. caught right away when not given
|
||||
minigame?: string;
|
||||
// how hard its minigame is, 10 when not given. what it changes is up to the minigame
|
||||
difficulty?: number;
|
||||
@@ -175,7 +176,7 @@ export function register_fishing(ctx: ServerContext) {
|
||||
(menu, { minigame, fish, difficulty }, player) => {
|
||||
menu.title = "Reel it in!";
|
||||
menu.open_data = { minigame, fish, difficulty };
|
||||
new MINIGAMES[minigame](menu, fish, player, difficulty);
|
||||
new MINIGAMES[minigame].minigame(menu, fish, player, difficulty);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -280,252 +281,3 @@ export function register_fishing(ctx: ServerContext) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// fishing minigames: what a player does to land a fish whose bworld:fish names one. its menu holds the fish until the
|
||||
// minigame is won, and each has a screen of the same name in client.ts that presses its buttons
|
||||
|
||||
type MinigameArgs = [menu: MenuBuilder, fish: ItemStack, player: Player, difficulty: number];
|
||||
|
||||
// a minigame builds its menu with the menu's own on_button, on_tick and add_data, and calls win or lose once it's over
|
||||
abstract class FishingMinigame {
|
||||
#over = false;
|
||||
|
||||
constructor(
|
||||
protected readonly menu: MenuBuilder,
|
||||
readonly fish: ItemStack,
|
||||
readonly player: Player,
|
||||
// the fish's bworld:fish difficulty, 10 when it has none
|
||||
readonly difficulty: number,
|
||||
) {
|
||||
menu.still_valid(() => !this.#over);
|
||||
}
|
||||
|
||||
// gives the player the fish and closes it
|
||||
protected win() {
|
||||
if (this.#over) return;
|
||||
this.#over = true;
|
||||
this.player.give_item(this.fish.id, this.fish.count, this.fish.data);
|
||||
}
|
||||
|
||||
// the fish gets away, and it closes
|
||||
protected lose() {
|
||||
this.#over = true;
|
||||
}
|
||||
|
||||
// keeps holding up to date with the mouse: the screen presses button 1 when it's pressed and 0 when it's let go
|
||||
protected holding = false;
|
||||
protected listen_for_mouse() {
|
||||
this.menu.on_button((_player, id) => {
|
||||
if (id === 0 || id === 1) {
|
||||
this.holding = id === 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// a "Catch" button in the middle of the screen, button 0
|
||||
class TestMinigame extends FishingMinigame {
|
||||
constructor(...args: MinigameArgs) {
|
||||
super(...args);
|
||||
this.menu.on_button((_player, id) => {
|
||||
if (id === 0) this.win();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// "Hold !" and a bar from 0 to 10 seconds, starting half full. while the mouse is held (button 1, and 0 when it's let
|
||||
// go) it fills a second a second, otherwise it empties faster the harder the fish is: 1 + difficulty / 100 seconds a
|
||||
// second, 1.1 at 10 and 2 at 100. full lands the fish, empty loses it
|
||||
const HOLD_MAX = 10 * 20;
|
||||
class HoldMinigame extends FishingMinigame {
|
||||
#progress = HOLD_MAX / 2;
|
||||
|
||||
constructor(...args: MinigameArgs) {
|
||||
super(...args);
|
||||
this.menu.add_data("progress", () => this.#progress);
|
||||
this.menu.add_data("max", () => HOLD_MAX);
|
||||
this.listen_for_mouse();
|
||||
this.menu.on_tick(() => {
|
||||
this.#progress += this.holding ? 1 : -(1 + this.difficulty / 100);
|
||||
if (this.#progress >= HOLD_MAX) {
|
||||
this.win();
|
||||
} else if (this.#progress <= 0) {
|
||||
this.lose();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// gray rings, and a thin green one growing out from the middle, faster the harder the fish is: like hold's bar
|
||||
// emptying, 1 + difficulty / 100 pixels a tick, 1.1 at 10 and 2 at 100. there's a ring for every 30 difficulty, at
|
||||
// least one: 0 to 30 is 1, 31 to 60 is 2, and so on, each further out than the last. clicking (button 1) while the
|
||||
// green ring is inside the next unchecked one checks it, and checking them all lands the fish. clicking any other time
|
||||
// loses it, and so does letting the green ring grow past one. in gui pixels, radiuses to the middle of each ring's line
|
||||
const RING_RADIUS = 48;
|
||||
const RING_SPACING = 16;
|
||||
const RING_WIDTH = 8;
|
||||
const RING_START = 4;
|
||||
const RING_GROWTH = 1;
|
||||
class RingMinigame extends FishingMinigame {
|
||||
#radius = RING_START;
|
||||
#rings = Math.max(1, Math.ceil(this.difficulty / 30));
|
||||
#checked = 0;
|
||||
|
||||
constructor(...args: MinigameArgs) {
|
||||
super(...args);
|
||||
this.menu.add_data("radius", () => this.#radius);
|
||||
this.menu.add_data("rings", () => this.#rings);
|
||||
this.menu.add_data("checked", () => this.#checked);
|
||||
this.menu.add_data("ring_radius", () => RING_RADIUS);
|
||||
this.menu.add_data("ring_spacing", () => RING_SPACING);
|
||||
this.menu.add_data("ring_width", () => RING_WIDTH);
|
||||
this.menu.on_button((_player, id) => {
|
||||
if (id !== 1) return;
|
||||
if (Math.abs(this.#radius - this.#next_ring()) > RING_WIDTH / 2) return this.lose();
|
||||
if (++this.#checked === this.#rings) this.win();
|
||||
});
|
||||
this.menu.on_tick(() => {
|
||||
this.#radius += RING_GROWTH * (1 + this.difficulty / 100);
|
||||
if (this.#radius > this.#next_ring() + RING_WIDTH / 2) this.lose();
|
||||
});
|
||||
}
|
||||
|
||||
// the radius of the first ring that isn't checked
|
||||
#next_ring() {
|
||||
return RING_RADIUS + this.#checked * RING_SPACING;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// port's gui pixels, from the track's bottom
|
||||
const VALLEY_POINTS = 120;
|
||||
const VALLEY_UP_ACCELERATION = 0.7;
|
||||
const VALLEY_GRAVITY = -0.7;
|
||||
const VALLEY_BAR_SIZE = 36;
|
||||
const VALLEY_MAX_BAR = 142 - VALLEY_BAR_SIZE;
|
||||
const VALLEY_MAX_FISH = 127;
|
||||
// the fish moves like stardew's own BobberBar.update with the "dart" motion type, in its units: a 548 tall track,
|
||||
// from the top, with the fish kept above 532. stardew runs it 60 times a second, so it runs 3 times a tick
|
||||
const STARDEW_TRACK = 548;
|
||||
const STARDEW_MAX_FISH = 532;
|
||||
const STARDEW_STEPS_PER_TICK = 3;
|
||||
class ValleyMinigame extends FishingMinigame {
|
||||
#points = VALLEY_POINTS / 5;
|
||||
#bar = 0;
|
||||
#bar_velocity = 0;
|
||||
// in stardew's units, starting at the bottom
|
||||
#fish = STARDEW_MAX_FISH;
|
||||
#fish_speed = 0;
|
||||
#fish_target = -1;
|
||||
|
||||
constructor(...args: MinigameArgs) {
|
||||
super(...args);
|
||||
this.menu.add_data("bar", () => this.#bar);
|
||||
this.menu.add_data("bar_size", () => VALLEY_BAR_SIZE);
|
||||
this.menu.add_data("fish", () => this.#fish_height());
|
||||
this.menu.add_data("points", () => this.#points);
|
||||
this.menu.add_data("max_points", () => VALLEY_POINTS);
|
||||
this.listen_for_mouse();
|
||||
this.menu.on_tick(() => this.#tick());
|
||||
}
|
||||
|
||||
// the fish in the port's units, from the track's bottom
|
||||
#fish_height() {
|
||||
return (STARDEW_MAX_FISH - this.#fish) / STARDEW_MAX_FISH * VALLEY_MAX_FISH;
|
||||
}
|
||||
|
||||
#tick() {
|
||||
// the bar: up while held, falling otherwise, bouncing a little off the bottom
|
||||
if (this.holding) {
|
||||
if (this.#bar_velocity < 0) this.#bar_velocity *= 0.9;
|
||||
this.#bar_velocity += VALLEY_UP_ACCELERATION;
|
||||
} else if (this.#bar > 0) {
|
||||
this.#bar_velocity += VALLEY_GRAVITY;
|
||||
}
|
||||
this.#bar += this.#bar_velocity;
|
||||
if (this.#bar > VALLEY_MAX_BAR) {
|
||||
this.#bar_velocity = 0;
|
||||
this.#bar = VALLEY_MAX_BAR;
|
||||
} else if (this.#bar <= 0) {
|
||||
this.#bar = 0;
|
||||
this.#bar_velocity = this.#bar_velocity < 2 * VALLEY_GRAVITY ? this.#bar_velocity * -0.4 : 0;
|
||||
}
|
||||
|
||||
for (let i = 0; i < STARDEW_STEPS_PER_TICK; i++) this.#move_fish();
|
||||
|
||||
// the points
|
||||
const fish = this.#fish_height();
|
||||
const on_fish = fish >= Math.floor(this.#bar) - 2 && fish <= Math.ceil(this.#bar) + VALLEY_BAR_SIZE - 12;
|
||||
this.#points += on_fish ? 1 : -1;
|
||||
if (this.#points >= VALLEY_POINTS) this.win();
|
||||
else if (this.#points <= 0) this.lose();
|
||||
}
|
||||
|
||||
// a step of stardew's fish: the harder it is, the more often it picks somewhere new, the further it goes and the
|
||||
// faster it gets there. darters also jump a long way now and then, further the harder they are
|
||||
#move_fish() {
|
||||
const difficulty = this.difficulty;
|
||||
if (Math.random() < difficulty / 4000) {
|
||||
const below = STARDEW_TRACK - this.#fish;
|
||||
const above = this.#fish;
|
||||
const percent = Math.min(99, difficulty + random_int(10, 45)) / 100;
|
||||
this.#fish_target = this.#fish + random_int(Math.min(-above, below), below) * percent;
|
||||
}
|
||||
if (Math.abs(this.#fish - this.#fish_target) > 3 && this.#fish_target !== -1) {
|
||||
const acceleration = (this.#fish_target - this.#fish) /
|
||||
(random_int(10, 30) + (100 - Math.min(100, difficulty)));
|
||||
this.#fish_speed += (acceleration - this.#fish_speed) / 5;
|
||||
} else if (Math.random() < difficulty / 2000) {
|
||||
this.#fish_target = this.#fish + (Math.random() < 0.5 ? random_int(-100, -51) : random_int(50, 101));
|
||||
} else {
|
||||
this.#fish_target = -1;
|
||||
}
|
||||
// darting
|
||||
if (Math.random() < difficulty / 1000) {
|
||||
this.#fish_target = this.#fish +
|
||||
(Math.random() < 0.5 ? random_int(-100 - difficulty * 2, -51) : random_int(50, 101 + difficulty * 2));
|
||||
}
|
||||
this.#fish_target = clamp(this.#fish_target, -1, STARDEW_TRACK);
|
||||
this.#fish = clamp(this.#fish + this.#fish_speed, 0, STARDEW_MAX_FISH);
|
||||
}
|
||||
}
|
||||
|
||||
// from min up to but not including max, like c#'s Random.Next. min when there's nothing between
|
||||
function random_int(min: number, max: number) {
|
||||
return max <= min ? min : Math.floor(min + Math.random() * (max - min));
|
||||
}
|
||||
|
||||
function clamp(value: number, min: number, max: number) {
|
||||
return Math.min(Math.max(value, min), max);
|
||||
}
|
||||
|
||||
const MINIGAMES: Record<string, new (...args: MinigameArgs) => FishingMinigame> = {
|
||||
test: TestMinigame,
|
||||
hold: HoldMinigame,
|
||||
valley: ValleyMinigame,
|
||||
ring: RingMinigame,
|
||||
maze: MazeMinigame,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { FishingMinigame, type MinigameArgs, type MinigameScreen, mouse_sender } from "./minigame.ts";
|
||||
|
||||
// "Hold !" and a bar from 0 to 10 seconds, starting half full. while the mouse is held (button 1, and 0 when it's let
|
||||
// go) it fills a second a second, otherwise it empties faster the harder the fish is: 1 + difficulty / 100 seconds a
|
||||
// second, 1.1 at 10 and 2 at 100. full lands the fish, empty loses it
|
||||
const HOLD_MAX = 10 * 20;
|
||||
export class HoldMinigame extends FishingMinigame {
|
||||
#progress = HOLD_MAX / 2;
|
||||
|
||||
constructor(...args: MinigameArgs) {
|
||||
super(...args);
|
||||
this.menu.add_data("progress", () => this.#progress);
|
||||
this.menu.add_data("max", () => HOLD_MAX);
|
||||
this.listen_for_mouse();
|
||||
this.menu.on_tick(() => {
|
||||
this.#progress += this.holding ? 1 : -(1 + this.difficulty / 100);
|
||||
if (this.#progress >= HOLD_MAX) {
|
||||
this.win();
|
||||
} else if (this.#progress <= 0) {
|
||||
this.lose();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// "Hold !" and a bar under it, filling while the mouse is held. the server counts, this sends when the mouse is
|
||||
// pressed (button 1) and let go (button 0)
|
||||
export const hold_screen: MinigameScreen = (menu) => {
|
||||
const width = 120;
|
||||
const height = 44;
|
||||
const send_mouse = mouse_sender(menu);
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
dim_background: false,
|
||||
inventory_overlays: false,
|
||||
on_render_background(g) {
|
||||
g.panel(0, 0, width, height);
|
||||
},
|
||||
on_render_labels(g) {
|
||||
send_mouse(g);
|
||||
const text = "Reel !";
|
||||
g.text(text, (width - g.measure_text(text)) / 2, 8);
|
||||
const done = menu.data.progress / menu.data.max || 0;
|
||||
g.rect(10, 26, width - 20, 8, [0.15, 0.1, 0.05, 1]);
|
||||
g.rect(10, 26, (width - 20) * done, 8, [0.3, 0.8, 0.3, 1]);
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,124 @@
|
||||
import { FishingMinigame, type MinigameArgs, type MinigameScreen } from "./minigame.ts";
|
||||
|
||||
// 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;
|
||||
export 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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
export const maze_screen: MinigameScreen = (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;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
// 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;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// what every fishing minigame shares. each minigame is a file here with both its halves: a class the server builds
|
||||
// the minigame's menu with, and the screen client.ts draws it with. mod.ts lists them by name
|
||||
import type { ItemStack, MenuBuilder, Player } from "bworld/server";
|
||||
import type { ClientMenu, Graphics, MenuScreen } from "bworld/client";
|
||||
|
||||
export type MinigameArgs = [menu: MenuBuilder, fish: ItemStack, player: Player, difficulty: number];
|
||||
|
||||
// a minigame's screen, with the fish it's for
|
||||
export type MinigameScreen = (menu: ClientMenu, fish: ItemStack) => MenuScreen;
|
||||
|
||||
// a minigame builds its menu with the menu's own on_button, on_tick and add_data, and calls win or lose once it's over
|
||||
export abstract class FishingMinigame {
|
||||
#over = false;
|
||||
|
||||
constructor(
|
||||
protected readonly menu: MenuBuilder,
|
||||
readonly fish: ItemStack,
|
||||
readonly player: Player,
|
||||
// the fish's bworld:fish difficulty, 10 when it has none
|
||||
readonly difficulty: number,
|
||||
) {
|
||||
menu.still_valid(() => !this.#over);
|
||||
}
|
||||
|
||||
// gives the player the fish and closes it
|
||||
protected win() {
|
||||
if (this.#over) return;
|
||||
this.#over = true;
|
||||
this.player.give_item(this.fish.id, this.fish.count, this.fish.data);
|
||||
}
|
||||
|
||||
// the fish gets away, and it closes
|
||||
protected lose() {
|
||||
this.#over = true;
|
||||
}
|
||||
|
||||
// keeps holding up to date with the mouse: the screen presses button 1 when it's pressed and 0 when it's let go
|
||||
protected holding = false;
|
||||
protected listen_for_mouse() {
|
||||
this.menu.on_button((_player, id) => {
|
||||
if (id === 0 || id === 1) {
|
||||
this.holding = id === 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// sends the server button 1 when the mouse is pressed and 0 when it's let go, for minigames you hold the mouse in.
|
||||
// call it every frame
|
||||
export function mouse_sender(menu: ClientMenu): (g: Graphics) => void {
|
||||
let holding = false;
|
||||
return (g) => {
|
||||
if (g.mouse.down === holding) return;
|
||||
holding = g.mouse.down;
|
||||
menu.click_button(holding ? 1 : 0);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// the fishing minigames by name, what a fish's bworld:fish "minigame" says: the class the server builds its menu with
|
||||
// and the screen the client draws it with. see minigame.ts
|
||||
import type { FishingMinigame, MinigameArgs, MinigameScreen } from "./minigame.ts";
|
||||
import { hold_screen, HoldMinigame } from "./hold.ts";
|
||||
import { maze_screen, MazeMinigame } from "./maze.ts";
|
||||
import { ring_screen, RingMinigame } from "./ring.ts";
|
||||
import { test_screen, TestMinigame } from "./test.ts";
|
||||
import { valley_screen, ValleyMinigame } from "./valley.ts";
|
||||
|
||||
export const MINIGAMES: Record<
|
||||
string,
|
||||
{ minigame: new (...args: MinigameArgs) => FishingMinigame; screen: MinigameScreen }
|
||||
> = {
|
||||
test: { minigame: TestMinigame, screen: test_screen },
|
||||
hold: { minigame: HoldMinigame, screen: hold_screen },
|
||||
valley: { minigame: ValleyMinigame, screen: valley_screen },
|
||||
ring: { minigame: RingMinigame, screen: ring_screen },
|
||||
maze: { minigame: MazeMinigame, screen: maze_screen },
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
import type { Color, Graphics } from "bworld/client";
|
||||
import { FishingMinigame, type MinigameArgs, type MinigameScreen } from "./minigame.ts";
|
||||
|
||||
// gray rings, and a thin green one growing out from the middle, faster the harder the fish is: like hold's bar
|
||||
// emptying, 1 + difficulty / 100 pixels a tick, 1.1 at 10 and 2 at 100. there's a ring for every 30 difficulty, at
|
||||
// least one: 0 to 30 is 1, 31 to 60 is 2, and so on, each further out than the last. clicking (button 1) while the
|
||||
// green ring is inside the next unchecked one checks it, and checking them all lands the fish. clicking any other time
|
||||
// loses it, and so does letting the green ring grow past one. in gui pixels, radiuses to the middle of each ring's line
|
||||
const RING_RADIUS = 48;
|
||||
const RING_SPACING = 16;
|
||||
const RING_WIDTH = 8;
|
||||
const RING_START = 4;
|
||||
const RING_GROWTH = 1;
|
||||
export class RingMinigame extends FishingMinigame {
|
||||
#radius = RING_START;
|
||||
#rings = Math.max(1, Math.ceil(this.difficulty / 30));
|
||||
#checked = 0;
|
||||
|
||||
constructor(...args: MinigameArgs) {
|
||||
super(...args);
|
||||
this.menu.add_data("radius", () => this.#radius);
|
||||
this.menu.add_data("rings", () => this.#rings);
|
||||
this.menu.add_data("checked", () => this.#checked);
|
||||
this.menu.add_data("ring_radius", () => RING_RADIUS);
|
||||
this.menu.add_data("ring_spacing", () => RING_SPACING);
|
||||
this.menu.add_data("ring_width", () => RING_WIDTH);
|
||||
this.menu.on_button((_player, id) => {
|
||||
if (id !== 1) return;
|
||||
if (Math.abs(this.#radius - this.#next_ring()) > RING_WIDTH / 2) return this.lose();
|
||||
if (++this.#checked === this.#rings) this.win();
|
||||
});
|
||||
this.menu.on_tick(() => {
|
||||
this.#radius += RING_GROWTH * (1 + this.difficulty / 100);
|
||||
if (this.#radius > this.#next_ring() + RING_WIDTH / 2) this.lose();
|
||||
});
|
||||
}
|
||||
|
||||
// the radius of the first ring that isn't checked
|
||||
#next_ring() {
|
||||
return RING_RADIUS + this.#checked * RING_SPACING;
|
||||
}
|
||||
}
|
||||
|
||||
// the gray rings, green once they're checked, and the green one growing out from the middle, over the world. a
|
||||
// click sends button 1
|
||||
export const ring_screen: MinigameScreen = (menu) => {
|
||||
const { rings, ring_radius, ring_spacing, ring_width } = menu.data;
|
||||
const size = 2 * (ring_radius + (rings - 1) * ring_spacing + ring_width);
|
||||
return {
|
||||
width: size,
|
||||
height: size,
|
||||
dim_background: false,
|
||||
inventory_overlays: false,
|
||||
on_render_labels(g) {
|
||||
if (g.mouse.pressed) menu.click_button(1);
|
||||
const { radius, checked } = menu.data;
|
||||
for (let i = 0; i < rings; i++) {
|
||||
const color: Color = i < checked ? [0.3, 0.85, 0.3, 0.9] : [0.8, 0.8, 0.8, 0.9];
|
||||
ring(g, size / 2, size / 2, ring_radius + i * ring_spacing, ring_width, color);
|
||||
}
|
||||
ring(g, size / 2, size / 2, radius, 2, [0.3, 0.85, 0.3, 1]);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
// a ring around x, y: squares as big as the line is thick, close enough around to look like one
|
||||
function ring(g: Graphics, x: number, y: number, radius: number, thickness: number, color: Color) {
|
||||
const steps = Math.max(8, Math.ceil(2 * Math.PI * radius / (thickness / 2)));
|
||||
for (let i = 0; i < steps; i++) {
|
||||
const angle = i / steps * 2 * Math.PI;
|
||||
const px = x + Math.cos(angle) * radius - thickness / 2;
|
||||
const py = y + Math.sin(angle) * radius - thickness / 2;
|
||||
g.rect(px, py, thickness, thickness, color);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { FishingMinigame, type MinigameArgs, type MinigameScreen } from "./minigame.ts";
|
||||
|
||||
// a "Catch" button in the middle of the screen, button 0
|
||||
export class TestMinigame extends FishingMinigame {
|
||||
constructor(...args: MinigameArgs) {
|
||||
super(...args);
|
||||
this.menu.on_button((_player, id) => {
|
||||
if (id === 0) this.win();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// the fish, and a button in the middle that catches it
|
||||
export const test_screen: MinigameScreen = (menu, fish) => {
|
||||
const width = 120;
|
||||
const height = 80;
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
dim_background: false,
|
||||
inventory_overlays: false,
|
||||
on_render_background(g) {
|
||||
g.panel(0, 0, width, height);
|
||||
},
|
||||
on_render_labels(g) {
|
||||
g.text(menu.title, 8, 6);
|
||||
g.item(fish.id, width / 2 - 8, 18);
|
||||
if (g.button("Catch", width / 2 - 25, height / 2, 50, 18)) {
|
||||
menu.click_button(0);
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,133 @@
|
||||
import { FishingMinigame, type MinigameArgs, type MinigameScreen, mouse_sender } from "./minigame.ts";
|
||||
|
||||
// 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
|
||||
// port's gui pixels, from the track's bottom
|
||||
const VALLEY_POINTS = 120;
|
||||
const VALLEY_UP_ACCELERATION = 0.7;
|
||||
const VALLEY_GRAVITY = -0.7;
|
||||
const VALLEY_BAR_SIZE = 36;
|
||||
const VALLEY_MAX_BAR = 142 - VALLEY_BAR_SIZE;
|
||||
const VALLEY_MAX_FISH = 127;
|
||||
// the fish moves like stardew's own BobberBar.update with the "dart" motion type, in its units: a 548 tall track,
|
||||
// from the top, with the fish kept above 532. stardew runs it 60 times a second, so it runs 3 times a tick
|
||||
const STARDEW_TRACK = 548;
|
||||
const STARDEW_MAX_FISH = 532;
|
||||
const STARDEW_STEPS_PER_TICK = 3;
|
||||
export class ValleyMinigame extends FishingMinigame {
|
||||
#points = VALLEY_POINTS / 5;
|
||||
#bar = 0;
|
||||
#bar_velocity = 0;
|
||||
// in stardew's units, starting at the bottom
|
||||
#fish = STARDEW_MAX_FISH;
|
||||
#fish_speed = 0;
|
||||
#fish_target = -1;
|
||||
|
||||
constructor(...args: MinigameArgs) {
|
||||
super(...args);
|
||||
this.menu.add_data("bar", () => this.#bar);
|
||||
this.menu.add_data("bar_size", () => VALLEY_BAR_SIZE);
|
||||
this.menu.add_data("fish", () => this.#fish_height());
|
||||
this.menu.add_data("points", () => this.#points);
|
||||
this.menu.add_data("max_points", () => VALLEY_POINTS);
|
||||
this.listen_for_mouse();
|
||||
this.menu.on_tick(() => this.#tick());
|
||||
}
|
||||
|
||||
// the fish in the port's units, from the track's bottom
|
||||
#fish_height() {
|
||||
return (STARDEW_MAX_FISH - this.#fish) / STARDEW_MAX_FISH * VALLEY_MAX_FISH;
|
||||
}
|
||||
|
||||
#tick() {
|
||||
// the bar: up while held, falling otherwise, bouncing a little off the bottom
|
||||
if (this.holding) {
|
||||
if (this.#bar_velocity < 0) this.#bar_velocity *= 0.9;
|
||||
this.#bar_velocity += VALLEY_UP_ACCELERATION;
|
||||
} else if (this.#bar > 0) {
|
||||
this.#bar_velocity += VALLEY_GRAVITY;
|
||||
}
|
||||
this.#bar += this.#bar_velocity;
|
||||
if (this.#bar > VALLEY_MAX_BAR) {
|
||||
this.#bar_velocity = 0;
|
||||
this.#bar = VALLEY_MAX_BAR;
|
||||
} else if (this.#bar <= 0) {
|
||||
this.#bar = 0;
|
||||
this.#bar_velocity = this.#bar_velocity < 2 * VALLEY_GRAVITY ? this.#bar_velocity * -0.4 : 0;
|
||||
}
|
||||
|
||||
for (let i = 0; i < STARDEW_STEPS_PER_TICK; i++) this.#move_fish();
|
||||
|
||||
// the points
|
||||
const fish = this.#fish_height();
|
||||
const on_fish = fish >= Math.floor(this.#bar) - 2 && fish <= Math.ceil(this.#bar) + VALLEY_BAR_SIZE - 12;
|
||||
this.#points += on_fish ? 1 : -1;
|
||||
if (this.#points >= VALLEY_POINTS) this.win();
|
||||
else if (this.#points <= 0) this.lose();
|
||||
}
|
||||
|
||||
// a step of stardew's fish: the harder it is, the more often it picks somewhere new, the further it goes and the
|
||||
// faster it gets there. darters also jump a long way now and then, further the harder they are
|
||||
#move_fish() {
|
||||
const difficulty = this.difficulty;
|
||||
if (Math.random() < difficulty / 4000) {
|
||||
const below = STARDEW_TRACK - this.#fish;
|
||||
const above = this.#fish;
|
||||
const percent = Math.min(99, difficulty + random_int(10, 45)) / 100;
|
||||
this.#fish_target = this.#fish + random_int(Math.min(-above, below), below) * percent;
|
||||
}
|
||||
if (Math.abs(this.#fish - this.#fish_target) > 3 && this.#fish_target !== -1) {
|
||||
const acceleration = (this.#fish_target - this.#fish) /
|
||||
(random_int(10, 30) + (100 - Math.min(100, difficulty)));
|
||||
this.#fish_speed += (acceleration - this.#fish_speed) / 5;
|
||||
} else if (Math.random() < difficulty / 2000) {
|
||||
this.#fish_target = this.#fish + (Math.random() < 0.5 ? random_int(-100, -51) : random_int(50, 101));
|
||||
} else {
|
||||
this.#fish_target = -1;
|
||||
}
|
||||
// darting
|
||||
if (Math.random() < difficulty / 1000) {
|
||||
this.#fish_target = this.#fish +
|
||||
(Math.random() < 0.5 ? random_int(-100 - difficulty * 2, -51) : random_int(50, 101 + difficulty * 2));
|
||||
}
|
||||
this.#fish_target = clamp(this.#fish_target, -1, STARDEW_TRACK);
|
||||
this.#fish = clamp(this.#fish + this.#fish_speed, 0, STARDEW_MAX_FISH);
|
||||
}
|
||||
}
|
||||
|
||||
// from min up to but not including max, like c#'s Random.Next. min when there's nothing between
|
||||
function random_int(min: number, max: number) {
|
||||
return max <= min ? min : Math.floor(min + Math.random() * (max - min));
|
||||
}
|
||||
|
||||
function clamp(value: number, min: number, max: number) {
|
||||
return Math.min(Math.max(value, min), max);
|
||||
}
|
||||
|
||||
// stardew valley's: a track with the fish and the green bar the mouse lifts, and how close it is to landing on the
|
||||
// right. laid out like ports/stardewfishing's screen, in its gui pixels: positions are from the track's bottom
|
||||
export const valley_screen: MinigameScreen = (menu, fish) => {
|
||||
const width = 38;
|
||||
const height = 152;
|
||||
const send_mouse = mouse_sender(menu);
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
dim_background: false,
|
||||
inventory_overlays: false,
|
||||
on_render_background(g) {
|
||||
g.panel(0, 0, width, height);
|
||||
g.rect(14, 4, 16, 144, [0.2, 0.35, 0.6, 1]);
|
||||
},
|
||||
on_render_labels(g) {
|
||||
send_mouse(g);
|
||||
const { bar, bar_size, points, max_points } = menu.data;
|
||||
g.rect(14, 4 + 142 - bar - bar_size, 16, bar_size, [0.4, 0.85, 0.3, 0.8]);
|
||||
g.item(fish.id, 14, 4 + 142 - menu.data.fish - 16);
|
||||
const done = points / max_points || 0;
|
||||
g.rect(32, 4, 4, 144, [0.15, 0.1, 0.05, 1]);
|
||||
g.rect(32, 148 - done * 144, 4, done * 144, [1 - done, done, 0.2, 1]);
|
||||
},
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user