Make players mobs too !

This commit is contained in:
2026-09-30 10:42:52 -03:00
parent 65f27d6099
commit 4fbcca6851
23 changed files with 314 additions and 132 deletions
+1 -3
View File
@@ -9,12 +9,10 @@ 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";
import { HURT_TINT, push_box, type Vec3 } from "./player_renderer.ts";
// models are in texture pixels, 16 to a block
const PIXEL = 1 / 16;
// how much of each color is left while it flashes red after being hurt, like minecraft's
const HURT_TINT: Vec3 = [1, 0.6, 0.6];
// the atlas has to be the current texture
export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_tick: number) {
+2 -2
View File
@@ -7,7 +7,7 @@ import type { Entity } from "$/client/entity/entity.ts";
import { RemotePlayer } from "$/client/entity/remote_player.ts";
import { ItemEntity } from "$/client/entity/item_entity.ts";
import { render_block_items, render_item_entity } from "./item_renderer.ts";
import { render_player } from "./player_renderer.ts";
import { HURT_TINT, render_player } from "./player_renderer.ts";
import { render_mod_entity } from "./entity_renderer.ts";
import { RemoteEntity } from "$/client/entity/remote_entity.ts";
import {
@@ -104,7 +104,7 @@ export class LevelRenderer {
flush_batch();
set_current_texture(skin.tex);
}
render_player(entity, skin, partial_tick);
render_player(entity, skin, partial_tick, entity.hurt_time > 0 ? HURT_TINT : undefined);
}
}
flush_batch();
+10 -5
View File
@@ -4,6 +4,9 @@ import { push_vertex, type Texture } from "$/client/renderer/mod.ts";
export type Vec3 = [number, number, number];
// how much of each color is left while something flashes red after being hurt, like minecraft's
export const HURT_TINT: Vec3 = [1, 0.6, 0.6];
// minecraft's player model is 32 skin pixels tall, it's scaled to the player's height
const PIXEL = PLAYER_HEIGHT / 32;
// keeps nearest sampling from picking the texel next to a face's edge
@@ -144,8 +147,9 @@ const FACES: { rect: (w: number, h: number, d: number) => number[]; corners: num
},
];
// a player with a minecraft skin, like minecraft's PlayerRenderer. the skin has to be the current texture
export function render_player(player: Player, skin: Texture, partial_tick: number) {
// a player with a minecraft skin, like minecraft's PlayerRenderer. the skin has to be the current texture. tint
// multiplies its colors, for flashing red when hurt
export function render_player(player: Player, skin: Texture, partial_tick: number, tint?: Vec3) {
const { x, y, z } = player.render_position(partial_tick);
const yaw_sin = Math.sin(player.yaw);
const yaw_cos = Math.cos(player.yaw);
@@ -173,8 +177,8 @@ export function render_player(player: Player, skin: Texture, partial_tick: numbe
const mz = (py * sin + pz * cos + pivot_z) * PIXEL;
return [x + mx * yaw_cos + mz * yaw_sin, y + my, z - mx * yaw_sin + mz * yaw_cos];
};
push_cube(skin, part, part.texture, 0, transform);
push_cube(skin, part, part.overlay, part.inflate, transform);
push_cube(skin, part, part.texture, 0, transform, tint);
push_cube(skin, part, part.overlay, part.inflate, transform, tint);
}
}
@@ -184,10 +188,11 @@ function push_cube(
uv: [number, number],
inflate: number,
transform: (x: number, y: number, z: number) => Vec3,
tint?: Vec3,
) {
const low = part.from.map((n) => n - inflate) as Vec3;
const high = part.from.map((n, i) => n + part.size[i] + inflate) as Vec3;
push_box(part.size, low, high, uv, (u, v) => [u / skin.width, v / skin.height], transform);
push_box(part.size, low, high, uv, (u, v) => [u / skin.width, v / skin.height], transform, tint);
}
// a box of a model, from low to high, textured like minecraft's box uv from [u, v] for a box of size (in texture