Player skins !
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
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;
|
||||
// undefined draws the default skin
|
||||
skin: Texture | undefined;
|
||||
#skin_version = 0;
|
||||
|
||||
// how far the limbs have swung and how hard they swing (0 to 1), like minecraft's WalkAnimationState
|
||||
walk_position = 0;
|
||||
@@ -15,6 +20,19 @@ export abstract class Player extends Entity {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
// after moving each tick. full speed is 5 blocks per second, a bit faster than walking
|
||||
update_walk_animation() {
|
||||
const moved = Math.hypot(this.x - this.prev_x, this.z - this.prev_z);
|
||||
|
||||
@@ -20,6 +20,9 @@ export class RemotePlayer extends Player {
|
||||
this.target_z = info.z;
|
||||
this.yaw = info.yaw;
|
||||
this.pitch = info.pitch;
|
||||
if (info.skin) {
|
||||
this.set_skin(info.skin);
|
||||
}
|
||||
}
|
||||
|
||||
lerp_to(x: number, y: number, z: number, yaw: number, pitch: number) {
|
||||
|
||||
Reference in New Issue
Block a user