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) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import { GuiContainer } from "./gui/gui_container.ts";
|
||||
import { GuiChat } from "./gui/gui_chat.ts";
|
||||
import { RemotePlayer } from "./entity/remote_player.ts";
|
||||
import { ItemEntity } from "./entity/item_entity.ts";
|
||||
import { Player } from "./entity/player.ts";
|
||||
|
||||
// applies what the server sends, like minecraft's ClientPacketListener. messages queue up on the connection and
|
||||
// get handled here once per frame, inside the game loop
|
||||
@@ -50,6 +51,13 @@ export class ClientPacketListener {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "player_skin": {
|
||||
const player = level.entities.get(message.id);
|
||||
if (player instanceof Player) {
|
||||
player.set_skin(message.skin);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "add_entity":
|
||||
level.add_entity(new ItemEntity(level, message.entity));
|
||||
break;
|
||||
|
||||
@@ -12,6 +12,7 @@ import { render_player } from "./player_renderer.ts";
|
||||
import {
|
||||
draw_terrain,
|
||||
flush_batch,
|
||||
get_current_texture,
|
||||
push_back_face,
|
||||
push_bottom_face,
|
||||
push_front_face,
|
||||
@@ -94,11 +95,14 @@ export class LevelRenderer {
|
||||
// every entity but the one the camera is in
|
||||
render_entities(level: ClientLevel, camera_entity: Entity, partial_tick: number) {
|
||||
flush_batch();
|
||||
const skin = AssetManager.instance.get<Texture>("bworld:skin");
|
||||
set_current_texture(skin.tex);
|
||||
|
||||
const default_skin = AssetManager.instance.get<Texture>("bworld:skin");
|
||||
for (const entity of level.entities.values()) {
|
||||
if (entity !== camera_entity && entity instanceof RemotePlayer) {
|
||||
const skin = entity.skin ?? default_skin;
|
||||
if (get_current_texture() !== skin.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(skin.tex);
|
||||
}
|
||||
render_player(entity, skin, partial_tick);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { load_texture, type Texture } from "./renderer/mod.ts";
|
||||
|
||||
// a player's skin as the server sends it (a base64 64x64 png), undefined when it can't be decoded
|
||||
export async function decode_skin(skin: string): Promise<Texture | undefined> {
|
||||
try {
|
||||
const bytes = Uint8Array.from(atob(skin), (char) => char.charCodeAt(0));
|
||||
// as stored, so pngs with a color profile look the same as in an editor that ignores it, like minecraft
|
||||
const image = await createImageBitmap(new Blob([bytes], { type: "image/png" }), {
|
||||
colorSpaceConversion: "none",
|
||||
premultiplyAlpha: "none",
|
||||
});
|
||||
const texture = image.width === 64 && image.height === 64 ? load_texture(image) : undefined;
|
||||
image.close();
|
||||
return texture;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user