Files
bworld/client/entity/player.ts
T
2026-09-30 17:30:14 -03:00

39 lines
1.4 KiB
TypeScript

import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_WIDTH } from "$/common/constants.ts";
import type { ClientLevel } from "../level/client_level.ts";
import type { Texture } from "../renderer/mod.ts";
import { decode_skin } from "../skins.ts";
import { Entity } from "./entity.ts";
export abstract class Player extends Entity {
name: string;
// like minecraft's players: slabs yes, full blocks need a jump
override step_height = 0.6;
// more than minecraft, where it's only the tick it leaves the ground: stepping from the edge of a block onto one
// diagonal to it drops it a little first, this lets it still step up onto that one
override step_grace_ticks = 3;
// undefined draws the default skin
skin: Texture | undefined;
// 3 pixel wide arms, for skins made for them
slim = false;
#skin_version = 0;
constructor(level: ClientLevel, id: string, name: string) {
super(level, id, PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_EYE_HEIGHT);
this.name = name;
}
// a base64 png from the server, or undefined for the default. the old skin stays until the new one is decoded
async set_skin(skin: string | undefined, slim = false) {
const version = ++this.#skin_version;
const texture = skin ? await decode_skin(skin) : undefined;
if (version !== this.#skin_version) {
// a newer skin came while this one was decoding
texture?.tex.destroy();
return;
}
this.skin?.tex.destroy();
this.skin = texture;
this.slim = slim;
}
}