Files
bworld/mods/bworld/scripts/minigames/minigame.ts
T

58 lines
1.9 KiB
TypeScript

// 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);
};
}