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
+56 -7
View File
@@ -1,9 +1,12 @@
// mobs mods define (entities/<name>.json), drawn with their entity model and a texture from the atlas. there are no
// animations yet: the model turns to the entity's yaw, and a bone named "head" to where it looks
// entities mods define (entities/<name>.json), drawn with their entity model and a texture from the atlas. there are no
// animations yet: the model turns to the entity's yaw, and a bone named "head" to where it looks. entities without a
// model are their texture facing the camera, like minecraft's fishing hook
import { AssetManager } from "$/client/assets.ts";
import type { Camera } from "$/client/camera.ts";
import type { RemoteEntity } from "$/client/entity/remote_entity.ts";
import type { Texture } from "$/client/renderer/mod.ts";
import { push_vertex, type Texture } from "$/client/renderer/mod.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import type { SpriteRegion } from "$/common/constants.ts";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import type { EntityBoneJson, EntityJson, EntityModelJson } from "$/common/mod_data.ts";
import { push_box, type Vec3 } from "./player_renderer.ts";
@@ -14,13 +17,18 @@ const PIXEL = 1 / 16;
const HURT_TINT: Vec3 = [1, 0.6, 0.6];
// the atlas has to be the current texture
export function render_mod_entity(entity: RemoteEntity, partial_tick: number) {
export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_tick: number) {
const json = EverythingRegistry.get<EntityJson>("entities", entity.type);
const model = json && EverythingRegistry.get<EntityModelJson>("entity_models", json.model);
if (!model) return;
if (!json) return;
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
const region = get_sprite_region(json.texture);
if (json.model === undefined) {
render_sprite(entity, camera, atlas, region, partial_tick);
return;
}
const model = EverythingRegistry.get<EntityModelJson>("entity_models", json.model);
if (!model) return;
const to_uv = (u: number, v: number): [number, number] => [
(region.x + u * region.width / model.texture_width) / atlas.width,
(region.y + v * region.height / model.texture_height) / atlas.height,
@@ -57,6 +65,47 @@ export function render_mod_entity(entity: RemoteEntity, partial_tick: number) {
}
}
// the whole texture, 16 pixels to a block, facing the camera around the middle of the entity's box
function render_sprite(
entity: RemoteEntity,
camera: Camera,
atlas: Texture,
region: SpriteRegion,
partial_tick: number,
) {
const { x, y, z } = entity.render_position(partial_tick);
const cy = y + entity.height / 2;
// the camera's right and up, halved to the sprite's size
const half_width = region.width * PIXEL / 2;
const half_height = region.height * PIXEL / 2;
const [yaw_sin, yaw_cos] = [Math.sin(camera.yaw), Math.cos(camera.yaw)];
const [pitch_sin, pitch_cos] = [Math.sin(camera.pitch), Math.cos(camera.pitch)];
const right: Vec3 = [yaw_cos * half_width, 0, -yaw_sin * half_width];
const up: Vec3 = [yaw_sin * pitch_sin * half_height, pitch_cos * half_height, yaw_cos * pitch_sin * half_height];
const [r, g, b] = entity.hurt_time > 0 || entity.dead ? HURT_TINT : [1, 1, 1];
const u0 = region.x / atlas.width;
const v0 = region.y / atlas.height;
const u1 = (region.x + region.width) / atlas.width;
const v1 = (region.y + region.height) / atlas.height;
// bottom left, bottom right, top right, top left: counter clockwise seen from the camera
const corners = [[-1, -1], [1, -1], [1, 1], [-1, 1]];
for (const i of [0, 1, 2, 0, 2, 3]) {
const [sx, sy] = corners[i];
push_vertex(
x + right[0] * sx + up[0] * sy,
cy + right[1] * sx + up[1] * sy,
z + right[2] * sx + up[2] * sy,
sx < 0 ? u0 : u1,
sy < 0 ? v1 : v0,
r,
g,
b,
1,
);
}
}
// 3 rows of 4, the last column moving: a turn and a move, from a bone's pixels to the model's
type Matrix = number[];
+1 -1
View File
@@ -42,7 +42,7 @@ export class GameRenderer {
begin_mode_3d(camera);
this.level_renderer.render_opaque(client.level, camera);
this.level_renderer.render_destroy_progress(client.game_mode);
this.level_renderer.render_entities(client.level, client.player, partial_tick);
this.level_renderer.render_entities(client.level, camera, client.player, partial_tick);
this.level_renderer.render_translucent(client.level, camera);
end_mode_3d();
this.hand.render(client.player.inventories);
+2 -2
View File
@@ -94,7 +94,7 @@ export class LevelRenderer {
}
// every entity but the one the camera is in
render_entities(level: ClientLevel, camera_entity: Entity, partial_tick: number) {
render_entities(level: ClientLevel, camera: Camera, camera_entity: Entity, partial_tick: number) {
flush_batch();
const default_skin = AssetManager.instance.get<Texture>("bworld:skin");
for (const entity of level.entities.values()) {
@@ -114,7 +114,7 @@ export class LevelRenderer {
if (entity instanceof ItemEntity) {
render_item_entity(entity, partial_tick);
} else if (entity instanceof RemoteEntity) {
render_mod_entity(entity, partial_tick);
render_mod_entity(entity, camera, partial_tick);
}
}
flush_batch();
+4 -4
View File
@@ -98,10 +98,10 @@ function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y:
const sprite_region = get_sprite_region(texture_id);
draw_texture_region(
AssetManager.instance.get("bworld:textures"),
sprite_region.x * 16,
sprite_region.y * 16,
16,
16,
sprite_region.x,
sprite_region.y,
sprite_region.width,
sprite_region.height,
x + 3,
y + 3,
SLOT_SIZE - 6,