Fishing hook

This commit is contained in:
2026-09-28 20:52:53 -03:00
parent d606bc4b55
commit ad6adb49dc
16 changed files with 258 additions and 37 deletions
+10
View File
@@ -0,0 +1,10 @@
{
"format_version": 1,
"entity": {
"id": "bworld:fishing_hook",
"texture": "bworld:fishing_hook",
"behavior": "scripted",
"width": 0.25,
"height": 0.25
}
}
+20 -5
View File
@@ -1,5 +1,5 @@
// the base game's block and item behavior, written against the same api as any other mod
import type { BlockRef, Container, Player, ScreenHandle, ServerContext } from "bworld/server";
import type { BlockRef, Container, EntityRef, Player, ScreenHandle, ServerContext } from "bworld/server";
export function setup(ctx: ServerContext) {
register_hoeable(ctx);
@@ -508,12 +508,19 @@ function register_farming(ctx: ServerContext) {
});
}
// fishing, like minecraft's without the bobber: right click water with a rod to cast, wait for a bite, and right click
// again before it gets away to reel it in. there's no hook entity yet, so a chat message says when something bites
// fishing, like minecraft's: right click water with a rod to cast, wait for a bite, and right click again before it
// gets away to reel it in. the hook floats on the water, and goes under when something bites
const FISHING_ROD = "bworld:fishing_rod";
const FISHING_HOOK = "bworld:fishing_hook";
// further than this from where they cast, the line snaps
const LINE_LENGTH = 16;
// the hook's feet under the water's surface, so its red stripe floats right on it (the hook's sprite is 16 pixels
// around the middle of its 0.25 block box, the stripe is 1.5 pixels above that)
const HOOK_DEPTH = 0.125 + 1.5 / 16;
// how far it bobs up and down, and how far under it goes with a bite
const HOOK_BOB = 0.03;
const HOOK_DIP = 0.15;
interface FishingRodParams {
// ticks until something bites, the shortest and longest, like minecraft's 5 to 30 seconds
@@ -532,6 +539,7 @@ interface Cast {
// the hotbar slot the rod is in, switching away reels it in
slot: number;
params: FishingRodParams;
hook: EntityRef;
// ticks, when something bites and when it gets away
bite_at: number;
escape_at?: number;
@@ -559,6 +567,7 @@ function register_fishing(ctx: ServerContext) {
const stop = (player: Player | undefined, id: string) => {
const cast = casts.get(id);
casts.delete(id);
cast?.hook.remove();
const rod = cast && player?.inventory.get(cast.slot);
if (rod?.id === FISHING_ROD) rod.data = undefined;
};
@@ -601,6 +610,7 @@ function register_fishing(ctx: ServerContext) {
block: water.block,
slot: player.selected_slot,
params,
hook: ctx.world.spawn_entity(FISHING_HOOK, water.x + 0.5, water.y + 1 - HOOK_DEPTH, water.z + 0.5),
bite_at: ctx.system.current_tick + between(params.wait),
});
rod.data = { cast: true };
@@ -615,10 +625,13 @@ function register_fishing(ctx: ServerContext) {
const distance = Math.hypot(x - cast.x, y - cast.y, z - cast.z);
if (
!player || player.selected_slot !== cast.slot || player.held_item?.id !== FISHING_ROD ||
distance > LINE_LENGTH || ctx.world.get_block(cast.x, cast.y, cast.z) !== cast.block
distance > LINE_LENGTH || ctx.world.get_block(cast.x, cast.y, cast.z) !== cast.block ||
cast.hook.removed
) {
stop(player, id);
} else if (cast.escape_at === undefined && now >= cast.bite_at) {
continue;
}
if (cast.escape_at === undefined && now >= cast.bite_at) {
cast.escape_at = now + between(cast.params.bite);
player.send_message("Something's biting! Right click to reel it in");
} else if (cast.escape_at !== undefined && now >= cast.escape_at) {
@@ -626,6 +639,8 @@ function register_fishing(ctx: ServerContext) {
cast.escape_at = undefined;
cast.bite_at = now + between(cast.params.wait);
}
const bob = cast.escape_at === undefined ? Math.sin(now / 40 * Math.PI * 2) * HOOK_BOB : -HOOK_DIP;
cast.hook.teleport(cast.x + 0.5, cast.y + 1 - HOOK_DEPTH + bob, cast.z + 0.5);
}
});
}