88 lines
3.0 KiB
TypeScript
88 lines
3.0 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// 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 "cubi/server";
|
|
import type { ClientMenu, Graphics, MenuScreen } from "cubi/client";
|
|
|
|
// how long the puzzle minigames give the player, the maze and chess, whatever the difficulty
|
|
export const SOLVE_TIME = 8 * 20;
|
|
|
|
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;
|
|
}
|
|
|
|
// for minigames played all on the screen: its button 1 lands the fish, 0 loses it
|
|
protected listen_for_result() {
|
|
this.menu.on_button((_player, id) => {
|
|
if (id === 1) this.win();
|
|
else if (id === 0) this.lose();
|
|
});
|
|
}
|
|
|
|
// loses the fish after this many ticks. the screen gets ticks_left and time, for time_bar
|
|
protected time_limit(ticks: number) {
|
|
let ticks_left = ticks;
|
|
this.menu.add_data("ticks_left", () => ticks_left);
|
|
this.menu.add_data("time", () => ticks);
|
|
this.menu.on_tick(() => {
|
|
if (--ticks_left <= 0) this.lose();
|
|
});
|
|
}
|
|
|
|
// 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);
|
|
};
|
|
}
|
|
|
|
// a time_limit's time left, as a bar that shrinks and goes from green to red
|
|
export function time_bar(g: Graphics, menu: ClientMenu, x: number, y: number, width: number, height: number) {
|
|
const left = menu.data.ticks_left / menu.data.time || 0;
|
|
g.rect(x, y, width * left, height, [1 - left, left, 0.2, 1]);
|
|
}
|