Swimming
This commit is contained in:
+45
-1
@@ -1,10 +1,17 @@
|
||||
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_WIDTH } from "$/common/constants.ts";
|
||||
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_SWIM_HEIGHT, PLAYER_WIDTH } from "$/common/constants.ts";
|
||||
import { ItemStack } from "$/common/inventory.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";
|
||||
|
||||
// how much of the way it tips over or stands up each tick, so it takes 10 ticks
|
||||
const SWIM_EASE = 0.1;
|
||||
// how much of the way the body turns toward which way it's going each tick
|
||||
const SWIM_PITCH_EASE = 0.3;
|
||||
// blocks a tick. slower than this the body lies the way they look instead, it's how they moved that's too jittery
|
||||
const SWIM_MOVING = 0.05;
|
||||
|
||||
export abstract class Player extends Entity {
|
||||
name: string;
|
||||
// like minecraft's players: slabs yes, full blocks need a jump
|
||||
@@ -18,6 +25,14 @@ export abstract class Player extends Entity {
|
||||
slim = false;
|
||||
// what they wear that shows, items with a worn model, see the server's player_worn
|
||||
worn: ItemStack[] = [];
|
||||
// lying flat in the short box, see set_swimming
|
||||
swimming = false;
|
||||
// how far over they've tipped, 0 standing to 1 lying flat, see update_swim_animation
|
||||
swim_amount = 0;
|
||||
prev_swim_amount = 0;
|
||||
// which way the body lies while swimming, radians up from flat: the way they're going, or looking when still
|
||||
swim_pitch = 0;
|
||||
prev_swim_pitch = 0;
|
||||
#skin_version = 0;
|
||||
|
||||
constructor(level: ClientLevel, id: string, name: string) {
|
||||
@@ -25,6 +40,35 @@ export abstract class Player extends Entity {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// swimming lies them flat in the short box, and not swimming stands them back up. the local player checks there's
|
||||
// room first, remote players' clients already did
|
||||
set_swimming(swimming: boolean) {
|
||||
this.swimming = swimming;
|
||||
this.height = swimming ? PLAYER_SWIM_HEIGHT : PLAYER_HEIGHT;
|
||||
}
|
||||
|
||||
// after moving each tick, for tipping over and standing up smoothly, local and remote players alike. the pitch comes
|
||||
// from how they moved, so remote players get it without anything more from the server
|
||||
update_swim_animation() {
|
||||
this.prev_swim_amount = this.swim_amount;
|
||||
this.prev_swim_pitch = this.swim_pitch;
|
||||
const amount = this.swimming ? 1 : 0;
|
||||
this.swim_amount += Math.max(-SWIM_EASE, Math.min(SWIM_EASE, amount - this.swim_amount));
|
||||
const [dx, dy, dz] = [this.x - this.prev_x, this.y - this.prev_y, this.z - this.prev_z];
|
||||
const moving = Math.atan2(dy, Math.hypot(dx, dz));
|
||||
const weight = Math.min(Math.hypot(dx, dy, dz) / SWIM_MOVING, 1);
|
||||
const pitch = this.pitch + (moving - this.pitch) * weight;
|
||||
this.swim_pitch += (pitch - this.swim_pitch) * SWIM_PITCH_EASE;
|
||||
}
|
||||
|
||||
// a frame partial_tick of the way to the next tick
|
||||
render_swim(partial_tick: number) {
|
||||
return {
|
||||
amount: this.prev_swim_amount + (this.swim_amount - this.prev_swim_amount) * partial_tick,
|
||||
pitch: this.prev_swim_pitch + (this.swim_pitch - this.prev_swim_pitch) * partial_tick,
|
||||
};
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user