Hold and valley minigames
This commit is contained in:
@@ -11,7 +11,9 @@
|
||||
"bworld:fish": {
|
||||
"weight": 25,
|
||||
"seasons": ["spring", "summer", "autumn", "winter"],
|
||||
"biomes": ["#bworld:is_ocean"]
|
||||
"biomes": ["#bworld:is_ocean"],
|
||||
"minigame": "valley",
|
||||
"difficulty": 100
|
||||
}
|
||||
},
|
||||
"base_sell_value": 30
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// 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, Graphics, MenuScreen } from "bworld/client";
|
||||
import type { ClientContext, ClientMenu, Graphics, ItemStack, MenuScreen } from "bworld/client";
|
||||
|
||||
const WIDTH = 176;
|
||||
// like minecraft's 176x166 container screens
|
||||
@@ -9,6 +9,7 @@ export function setup(ctx: ClientContext) {
|
||||
ctx.ui.register_menu_screen("bworld:storage", chest_screen);
|
||||
ctx.ui.register_menu_screen("bworld:furnace", furnace_screen);
|
||||
ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen);
|
||||
ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen);
|
||||
}
|
||||
|
||||
// a panel with the menu's title and the inventory's label over its rows, drawing anything else over it
|
||||
@@ -66,3 +67,96 @@ function bar(
|
||||
g.texture(full, x, y + size * (1 - done), size, size * done, [0, 1 - done, 1, done]);
|
||||
}
|
||||
}
|
||||
|
||||
// a fishing minigame's screen, by the minigame's name. see the minigames at the end of scripts/fishing.ts
|
||||
function fishing_minigame_screen(menu: ClientMenu): MenuScreen {
|
||||
const { minigame, fish } = menu.open_data as { minigame: string; fish: ItemStack };
|
||||
const screen = MINIGAME_SCREENS[minigame];
|
||||
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]);
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
// 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);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,13 +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, Player, Season, ServerContext } from "bworld/server";
|
||||
import type { EntityRef, ItemStack, MenuBuilder, Player, Season, ServerContext } from "bworld/server";
|
||||
import type { Position } from "bworld/shared";
|
||||
|
||||
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
|
||||
const MINIGAME_MENU = "bworld:fishing_minigame";
|
||||
// built from every item with bworld:fish, see register_fishing
|
||||
const FISHING_TABLE = "bworld:gameplay/fishing";
|
||||
// further than this from the hook, the line snaps
|
||||
@@ -46,6 +48,10 @@ 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
|
||||
minigame?: string;
|
||||
// how hard its minigame is, 10 when not given. what it changes is up to the minigame
|
||||
difficulty?: number;
|
||||
}
|
||||
|
||||
interface Cast {
|
||||
@@ -149,6 +155,12 @@ export function register_fishing(ctx: ServerContext) {
|
||||
const entries = ctx.items.ids().flatMap((id) => {
|
||||
const fish = ctx.items.components(id)[FISH] as FishParams | undefined;
|
||||
if (!fish) return [];
|
||||
if (fish.difficulty !== undefined && !(typeof fish.difficulty === "number" && fish.difficulty >= 0)) {
|
||||
throw new Error(`${id}'s fishing difficulty must be a number, 0 or more`);
|
||||
}
|
||||
if (fish.minigame !== undefined && !(fish.minigame in MINIGAMES)) {
|
||||
throw new Error(`${id}'s fishing minigame ${fish.minigame} doesn't exist`);
|
||||
}
|
||||
const conditions = [];
|
||||
if (fish.seasons) conditions.push({ condition: "bworld:season", seasons: fish.seasons });
|
||||
if (fish.time) conditions.push({ condition: "bworld:time", ...fish.time });
|
||||
@@ -157,13 +169,64 @@ export function register_fishing(ctx: ServerContext) {
|
||||
});
|
||||
ctx.loot.register_table(FISHING_TABLE, { pools: entries.length > 0 ? [{ entries }] : [] });
|
||||
|
||||
// holds the fish until its minigame is won, and closes then. closing it before lets the fish get away
|
||||
const minigame_menu = ctx.menus.register<{ minigame: string; fish: ItemStack; difficulty: number }>(
|
||||
MINIGAME_MENU,
|
||||
(menu, { minigame, fish, difficulty }, player) => {
|
||||
menu.title = "Reel it in!";
|
||||
menu.open_data = { minigame, fish, difficulty };
|
||||
new MINIGAMES[minigame](menu, fish, player, difficulty);
|
||||
},
|
||||
);
|
||||
|
||||
// what's reeled in: a fish with a minigame opens it, anything else is given right away
|
||||
const land = (player: Player, item: ItemStack) => {
|
||||
const fish = ctx.items.components(item.id)[FISH] as FishParams | undefined;
|
||||
if (fish?.minigame) {
|
||||
minigame_menu.open(player, { minigame: fish.minigame, fish: item, difficulty: fish.difficulty ?? 10 });
|
||||
} else {
|
||||
player.give_item(item.id, item.count, item.data);
|
||||
}
|
||||
};
|
||||
|
||||
// /fish reels in what the fishing table gives where you stand, /fish <fish> reels in that fish, its minigame and
|
||||
// all, for trying minigames out
|
||||
const fishes = entries.map((entry) => entry.item);
|
||||
const { literal, argument, types } = ctx.commands;
|
||||
ctx.commands.register(
|
||||
literal("fish")
|
||||
.describe("Reels in a fish, like a bite on your rod")
|
||||
.executes(({ source }) => {
|
||||
const { player } = source;
|
||||
for (const item of ctx.loot.roll(FISHING_TABLE, { position: player.position, player })) {
|
||||
land(player, item);
|
||||
}
|
||||
})
|
||||
.then(
|
||||
argument("fish", types.item())
|
||||
.suggests((partial) =>
|
||||
fishes
|
||||
.filter((id) => id.startsWith(partial) || id.split(":")[1].startsWith(partial))
|
||||
.map((id) => ({ text: id }))
|
||||
)
|
||||
.executes((context) => {
|
||||
const id = context.get<string>("fish");
|
||||
if (!fishes.includes(id)) {
|
||||
context.source.send_message(`${id} isn't a fish`);
|
||||
return;
|
||||
}
|
||||
land(context.source.player, { id, count: 1 });
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
ctx.components.register_item<FishingRodParams>(FISHING_ROD, {
|
||||
on_use(rod, params, player) {
|
||||
const cast = casts.get(player.id);
|
||||
if (cast) {
|
||||
if (cast.escape_at !== undefined) {
|
||||
const loot = ctx.loot.roll(params.loot_table, { position: cast.position, player, tool: rod });
|
||||
for (const item of loot) player.give_item(item.id, item.count, item.data);
|
||||
for (const item of loot) land(player, item);
|
||||
}
|
||||
stop(player, player.id);
|
||||
return;
|
||||
@@ -217,3 +280,189 @@ 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user