Make fishing rods not actually target water

This commit is contained in:
2026-10-07 18:00:37 -03:00
parent b128da0631
commit b6b10e35e2
10 changed files with 374 additions and 70 deletions
+1 -2
View File
@@ -6,9 +6,8 @@
"data_textures": {
"cast": "bworld:fishing_rod_cast"
},
"targets_liquids": true,
"max_stack": 1,
"lore": "Right click water to cast, and again when something bites.",
"lore": "Right click to cast into water, and again when something bites.",
"components": {
"bworld:fishing_rod": {
"wait": [100, 600],
+79 -42
View File
@@ -1,5 +1,6 @@
// the base game's block and item behavior, written against the same api as any other mod
import type { BlockRef, Container, EntityRef, Player, ServerContext } from "bworld/server";
import type { Position } from "bworld/shared";
import { register_curios } from "./curios.ts";
import { register_effects } from "./effects.ts";
@@ -622,13 +623,23 @@ function register_buckets(ctx: ServerContext) {
});
}
// 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
// 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
const FISHING_ROD = "bworld:fishing_rod";
const FISHING_HOOK = "bworld:fishing_hook";
// further than this from where they cast, the line snaps
const FISHABLE = "bworld:fishable";
// further than this from the hook, the line snaps
const LINE_LENGTH = 16;
// blocks per tick it's thrown at, and how it falls and slows down in the air each tick, like minecraft's hook
const THROW_SPEED = 1;
const GRAVITY = 0.03;
const DRAG = 0.92;
// it's reeled in if it's still flying after this many ticks
const MAX_FLIGHT = 100;
// half the hook's 0.25 block box: the hook's middle flies along the line, its feet are this much under it
const HOOK_HALF = 0.125;
// 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;
@@ -645,25 +656,25 @@ interface FishingRodParams {
}
interface Cast {
x: number;
y: number;
z: number;
// the block they cast into, the line comes out when it's gone
block: string;
// the hotbar slot the rod is in, switching away reels it in
slot: number;
params: FishingRodParams;
hook: EntityRef;
// the hook's middle
position: Position;
// blocks per tick while it's flying, undefined once it has landed
velocity?: Position;
thrown_at: number;
// the fishable block it floats on, once it lands in one. the line comes out when it's gone
water?: { x: number; y: number; z: number; block: string };
// ticks, when something bites and when it gets away
bite_at: number;
bite_at?: number;
escape_at?: number;
}
function register_fishing(ctx: ServerContext) {
// by player id
const casts = new Map<string, Cast>();
// the water each player right clicked this tick, for their rod's on_use, which doesn't get the block
const aimed = new Map<string, { x: number; y: number; z: number; block: string; tick: number }>();
const between = ([min, max]: [number, number]) => min + Math.floor(Math.random() * (max - min + 1));
@@ -677,7 +688,14 @@ function register_fishing(ctx: ServerContext) {
return loot.at(-1)?.item;
};
// takes the line out of the water, the rod in the slot looks uncast again
const fishable = new Map<string, boolean>();
const is_fishable = (id: string) => {
let result = fishable.get(id);
if (result === undefined) fishable.set(id, result = FISHABLE in ctx.blocks.components(id));
return result;
};
// takes the line in, the rod in the slot looks uncast again
const stop = (player: Player | undefined, id: string) => {
const cast = casts.get(id);
casts.delete(id);
@@ -686,18 +704,35 @@ function register_fishing(ctx: ServerContext) {
if (rod?.id === FISHING_ROD) rod.data = undefined;
};
ctx.components.register_block("bworld:fishable", {
on_interact(block, _params, player) {
aimed.set(player.id, {
x: block.x,
y: block.y,
z: block.z,
block: block.id,
tick: ctx.system.current_tick,
});
return false;
},
});
// moves the hook along its flight for a tick, until it hits something
const fly = (cast: Cast, velocity: Position, now: number) => {
const from = cast.position;
const to = { x: from.x + velocity.x, y: from.y + velocity.y, z: from.z + velocity.z };
const hit = ctx.world.raycast(from, to, { liquids: "any" });
if (!hit) {
cast.position = to;
velocity.x *= DRAG;
velocity.y = velocity.y * DRAG - GRAVITY;
velocity.z *= DRAG;
cast.hook.teleport(to.x, to.y - HOOK_HALF, to.z);
return;
}
cast.velocity = undefined;
cast.position = { ...hit.position };
if (is_fishable(hit.id)) {
// it floats on top, even when it went in by a side under the surface
let y = hit.y;
while (ctx.world.get_block(hit.x, y + 1, hit.z) === hit.id) y++;
cast.water = { x: hit.x, y, z: hit.z, block: hit.id };
cast.position.y = y + 1;
cast.bite_at = now + between(cast.params.wait);
} else {
cast.hook.teleport(hit.position.x, hit.position.y - HOOK_HALF, hit.position.z);
}
};
// fishable blocks are found by their components, see is_fishable
ctx.components.register_block(FISHABLE, {});
ctx.components.register_item<FishingRodParams>(FISHING_ROD, {
on_use(rod, params, player) {
@@ -710,22 +745,16 @@ function register_fishing(ctx: ServerContext) {
stop(player, player.id);
return;
}
const water = aimed.get(player.id);
aimed.delete(player.id);
if (water?.tick !== ctx.system.current_tick) {
// cast before the server restarted
rod.data = undefined;
return;
}
const eye = player.eye_position;
const look = player.look_direction;
const position = { x: eye.x + look.x * 0.5, y: eye.y + look.y * 0.5, z: eye.z + look.z * 0.5 };
casts.set(player.id, {
x: water.x,
y: water.y,
z: water.z,
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),
hook: ctx.world.spawn_entity(FISHING_HOOK, position.x, position.y - HOOK_HALF, position.z),
position,
velocity: { x: look.x * THROW_SPEED, y: look.y * THROW_SPEED, z: look.z * THROW_SPEED },
thrown_at: ctx.system.current_tick,
});
rod.data = { cast: true };
},
@@ -735,16 +764,24 @@ function register_fishing(ctx: ServerContext) {
const now = ctx.system.current_tick;
for (const [id, cast] of casts) {
const player = ctx.players.get(id);
const { x, y, z } = player?.position ?? cast;
const distance = Math.hypot(x - cast.x, y - cast.y, z - cast.z);
const { x, y, z } = player?.position ?? cast.position;
const distance = Math.hypot(x - cast.position.x, y - cast.position.y, z - cast.position.z);
const water = cast.water;
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 ||
cast.hook.removed
distance > LINE_LENGTH || cast.hook.removed ||
(water && ctx.world.get_block(water.x, water.y, water.z) !== water.block) ||
(cast.velocity && (now - cast.thrown_at > MAX_FLIGHT || cast.position.y < 0))
) {
stop(player, id);
continue;
}
if (cast.velocity) {
fly(cast, cast.velocity, now);
}
if (!cast.water || cast.bite_at === undefined) {
continue;
}
if (cast.escape_at === undefined && now >= cast.bite_at) {
cast.escape_at = now + between(cast.params.bite);
} else if (cast.escape_at !== undefined && now >= cast.escape_at) {
@@ -753,7 +790,7 @@ function register_fishing(ctx: ServerContext) {
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);
cast.hook.teleport(cast.position.x, cast.position.y - HOOK_DEPTH + bob, cast.position.z);
}
});
}