Player model
This commit is contained in:
@@ -5,8 +5,28 @@ import { Entity } from "./entity.ts";
|
||||
export abstract class Player extends Entity {
|
||||
name: string;
|
||||
|
||||
// how far the limbs have swung and how hard they swing (0 to 1), like minecraft's WalkAnimationState
|
||||
walk_position = 0;
|
||||
walk_speed = 0;
|
||||
prev_walk_speed = 0;
|
||||
|
||||
constructor(level: ClientLevel, id: string, name: string) {
|
||||
super(level, id, PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_EYE_HEIGHT);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// 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);
|
||||
this.prev_walk_speed = this.walk_speed;
|
||||
this.walk_speed += (Math.min(moved * 4, 1) - this.walk_speed) * 0.4;
|
||||
this.walk_position += this.walk_speed;
|
||||
}
|
||||
|
||||
walk_animation(partial_tick: number) {
|
||||
return {
|
||||
position: this.walk_position - this.walk_speed * (1 - partial_tick),
|
||||
speed: this.prev_walk_speed + (this.walk_speed - this.prev_walk_speed) * partial_tick,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ const SMOOTHING = 12;
|
||||
|
||||
// another player on the server, it moves where the server says instead of simulating anything
|
||||
export class RemotePlayer extends Player {
|
||||
color: [number, number, number];
|
||||
|
||||
// where the server last said it is, the drawn position eases towards it so movement isn't choppy
|
||||
target_x: number;
|
||||
target_y: number;
|
||||
@@ -22,7 +20,6 @@ export class RemotePlayer extends Player {
|
||||
this.target_z = info.z;
|
||||
this.yaw = info.yaw;
|
||||
this.pitch = info.pitch;
|
||||
this.color = color_from_name(info.name);
|
||||
}
|
||||
|
||||
lerp_to(x: number, y: number, z: number, yaw: number, pitch: number) {
|
||||
@@ -38,29 +35,6 @@ export class RemotePlayer extends Player {
|
||||
this.x += (this.target_x - this.x) * t;
|
||||
this.y += (this.target_y - this.y) * t;
|
||||
this.z += (this.target_z - this.z) * t;
|
||||
this.update_walk_animation();
|
||||
}
|
||||
}
|
||||
|
||||
function color_from_name(name: string): [number, number, number] {
|
||||
let hash = 0;
|
||||
for (const ch of name) {
|
||||
hash = (hash * 31 + ch.charCodeAt(0)) | 0;
|
||||
}
|
||||
const hue = ((hash % 360) + 360) % 360;
|
||||
// hsl with s=0.6 l=0.6 to rgb
|
||||
const c = 0.48;
|
||||
const x = c * (1 - Math.abs(((hue / 60) % 2) - 1));
|
||||
const m = 0.36;
|
||||
const [r, g, b] = hue < 60
|
||||
? [c, x, 0]
|
||||
: hue < 120
|
||||
? [x, c, 0]
|
||||
: hue < 180
|
||||
? [0, c, x]
|
||||
: hue < 240
|
||||
? [0, x, c]
|
||||
: hue < 300
|
||||
? [x, 0, c]
|
||||
: [c, 0, x];
|
||||
return [r + m, g + m, b + m];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user