Improve moviment !
This commit is contained in:
+1
-1
@@ -17,7 +17,7 @@ export class Camera {
|
||||
setup(entity: Entity, partial_tick: number) {
|
||||
const { x, y, z } = entity.render_position(partial_tick);
|
||||
this.x = x;
|
||||
this.y = y + entity.eye_height;
|
||||
this.y = y + entity.render_eye_height(partial_tick);
|
||||
this.z = z;
|
||||
this.yaw = entity.yaw;
|
||||
this.pitch = entity.pitch;
|
||||
|
||||
+15
-1
@@ -30,6 +30,12 @@ export abstract class Entity {
|
||||
// what's left of its speed after each tick in the air, like minecraft's 0.98. it caps falling speed
|
||||
// at 49 * -gravity * TICK_DELTA, 78 blocks per second with minecraft's gravity
|
||||
drag = 0.98;
|
||||
// what it walks up onto without jumping, see move_body
|
||||
step_height = 0;
|
||||
// it can still step up this many ticks after leaving the ground
|
||||
step_grace_ticks = 0;
|
||||
// ticks since it was last on the ground
|
||||
#air_ticks = 0;
|
||||
|
||||
// which way it hit something on each axis in the last move: 1 or -1, 0 for nothing.
|
||||
// 1 on y means it's standing on something
|
||||
@@ -71,15 +77,23 @@ export abstract class Entity {
|
||||
};
|
||||
}
|
||||
|
||||
// how high the eyes are, for the camera. a frame is partial_tick of the way to the next tick
|
||||
render_eye_height(_partial_tick: number) {
|
||||
return this.eye_height;
|
||||
}
|
||||
|
||||
// one step of the game, TICK_DELTA seconds
|
||||
abstract tick(): void;
|
||||
|
||||
// falls and moves by its velocity for one tick (see move_body), then slows down from drag
|
||||
move() {
|
||||
const collisions = move_body(this, this.gravity, (x, y, z) => this.level.has_collision(x, y, z));
|
||||
const is_solid = (x: number, y: number, z: number) => this.level.has_collision(x, y, z);
|
||||
const can_step = this.#air_ticks <= this.step_grace_ticks && this.vy <= 0;
|
||||
const collisions = move_body(this, this.gravity, is_solid, this.step_height, can_step);
|
||||
this.colliding_x = collisions.x;
|
||||
this.colliding_y = collisions.y;
|
||||
this.colliding_z = collisions.z;
|
||||
this.#air_ticks = this.on_ground ? 0 : this.#air_ticks + 1;
|
||||
this.vx *= this.drag;
|
||||
this.vy *= this.drag;
|
||||
this.vz *= this.drag;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { PLAYER_EYE_HEIGHT, TICK_DELTA } from "$/common/constants.ts";
|
||||
import { back_off_from_edge } from "$/common/physics.ts";
|
||||
import type { Client } from "../client.ts";
|
||||
import { InputManager } from "../input_manager.ts";
|
||||
import { ClientInventories } from "../inventory.ts";
|
||||
@@ -9,6 +11,24 @@ const SEND_POSITION_TICKS = 2;
|
||||
// ticks after a jump before holding jump can jump again, like minecraft's noJumpDelay. a jump is longer than this in
|
||||
// the air, so holding it jumps again as soon as it lands
|
||||
const JUMP_DELAY_TICKS = 10;
|
||||
// like minecraft: sneaking is 30% of walking speed and lowers the eyes 0.35, which ease there by half each tick
|
||||
const SNEAK_SPEED = 0.3;
|
||||
const SNEAK_EYE_HEIGHT = PLAYER_EYE_HEIGHT - 0.35;
|
||||
const EYE_EASE = 0.5;
|
||||
// sneaking keeps it from walking off anything taller than this
|
||||
const SNEAK_MAX_DROP = 0.6;
|
||||
|
||||
// what the keys ask for this tick. x is right, z is back, as -1 to 1
|
||||
interface MoveInput {
|
||||
x: number;
|
||||
z: number;
|
||||
jump: boolean;
|
||||
sprint: boolean;
|
||||
sneak: boolean;
|
||||
}
|
||||
|
||||
// while a screen is open, like minecraft ignoring movement keys then
|
||||
const NO_INPUT: MoveInput = { x: 0, z: 0, jump: false, sprint: false, sneak: false };
|
||||
|
||||
// the player this client controls, like minecraft's LocalPlayer
|
||||
export class LocalPlayer extends Player {
|
||||
@@ -26,6 +46,8 @@ export class LocalPlayer extends Player {
|
||||
#ticks_since_sent = 0;
|
||||
// counts down every tick, letting go of jump clears it
|
||||
#jump_delay = 0;
|
||||
sneaking = false;
|
||||
#prev_eye_height = PLAYER_EYE_HEIGHT;
|
||||
|
||||
constructor(client: Client, id: string, name: string) {
|
||||
super(client.level, id, name);
|
||||
@@ -33,12 +55,25 @@ export class LocalPlayer extends Player {
|
||||
}
|
||||
|
||||
tick() {
|
||||
if (!this.client.screen) {
|
||||
this.#apply_input();
|
||||
} else {
|
||||
this.#jump_delay = 0;
|
||||
}
|
||||
this.#apply_input(this.client.screen ? NO_INPUT : this.#read_input());
|
||||
this.#prev_eye_height = this.eye_height;
|
||||
const eye_height = this.sneaking ? SNEAK_EYE_HEIGHT : PLAYER_EYE_HEIGHT;
|
||||
this.eye_height += (eye_height - this.eye_height) * EYE_EASE;
|
||||
|
||||
const was_on_ground = this.on_ground;
|
||||
// not while jumping, so it can still jump off a ledge
|
||||
if (this.sneaking && this.on_ground && this.vy <= 0) {
|
||||
const is_solid = (x: number, y: number, z: number) => this.level.has_collision(x, y, z);
|
||||
const [dx, dz] = back_off_from_edge(
|
||||
this,
|
||||
this.vx * TICK_DELTA,
|
||||
this.vz * TICK_DELTA,
|
||||
SNEAK_MAX_DROP,
|
||||
is_solid,
|
||||
);
|
||||
this.vx = dx / TICK_DELTA;
|
||||
this.vz = dz / TICK_DELTA;
|
||||
}
|
||||
this.move();
|
||||
this.#send_position(this.on_ground && !was_on_ground);
|
||||
}
|
||||
@@ -52,40 +87,42 @@ export class LocalPlayer extends Player {
|
||||
this.pitch = Math.max(-limit, Math.min(limit, this.pitch));
|
||||
}
|
||||
|
||||
// walking relative to where it's looking
|
||||
#apply_input() {
|
||||
// the eyes between the last tick and this one, for the camera
|
||||
override render_eye_height(partial_tick: number) {
|
||||
return this.#prev_eye_height + (this.eye_height - this.#prev_eye_height) * partial_tick;
|
||||
}
|
||||
|
||||
#read_input(): MoveInput {
|
||||
const options = this.client.options;
|
||||
let input_x = 0;
|
||||
let input_z = 0;
|
||||
|
||||
if (InputManager.is_key_down(options.key_left)) {
|
||||
input_x -= 1;
|
||||
}
|
||||
if (InputManager.is_key_down(options.key_right)) {
|
||||
input_x += 1;
|
||||
}
|
||||
if (InputManager.is_key_down(options.key_forward)) {
|
||||
input_z -= 1;
|
||||
}
|
||||
if (InputManager.is_key_down(options.key_back)) {
|
||||
input_z += 1;
|
||||
}
|
||||
const down = (key: typeof options.key_jump) => InputManager.is_key_down(key);
|
||||
return {
|
||||
x: Number(down(options.key_right)) - Number(down(options.key_left)),
|
||||
z: Number(down(options.key_back)) - Number(down(options.key_forward)),
|
||||
jump: down(options.key_jump),
|
||||
sprint: down(options.key_sprint),
|
||||
sneak: down(options.key_sneak),
|
||||
};
|
||||
}
|
||||
|
||||
// walking relative to where it's looking
|
||||
#apply_input(input: MoveInput) {
|
||||
let { x: input_x, z: input_z } = input;
|
||||
const size = Math.hypot(input_x, input_z);
|
||||
if (size > 0) {
|
||||
input_x /= size;
|
||||
input_z /= size;
|
||||
}
|
||||
|
||||
this.sneaking = input.sneak;
|
||||
const sin = Math.sin(this.yaw);
|
||||
const cos = Math.cos(this.yaw);
|
||||
const speed = this.move_speed * (InputManager.is_key_down(options.key_sprint) ? 1.75 : 1);
|
||||
const speed = this.move_speed * (input.sneak ? SNEAK_SPEED : input.sprint ? 1.75 : 1);
|
||||
|
||||
this.vx = (sin * input_z + cos * input_x) * speed;
|
||||
this.vz = (cos * input_z - sin * input_x) * speed;
|
||||
|
||||
this.#jump_delay = Math.max(0, this.#jump_delay - 1);
|
||||
if (!InputManager.is_key_down(options.key_jump)) {
|
||||
if (!input.jump) {
|
||||
this.#jump_delay = 0;
|
||||
} else if (this.on_ground && this.#jump_delay === 0) {
|
||||
this.vy += this.jump_force;
|
||||
|
||||
@@ -6,6 +6,11 @@ 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
|
||||
|
||||
@@ -122,6 +122,12 @@ export class InputManager {
|
||||
}
|
||||
});
|
||||
|
||||
// keys let go while the window isn't focused never send keyup, so they'd stay held
|
||||
self.addEventListener("blur", () => {
|
||||
this.keys_down.clear();
|
||||
this.mouse_buttons_down.clear();
|
||||
});
|
||||
|
||||
self.addEventListener("keyup", (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
@@ -10,6 +10,8 @@ export class Options {
|
||||
key_right: KeyCode = "KeyD";
|
||||
key_jump: KeyCode = "Space";
|
||||
key_sprint: KeyCode = "ShiftLeft";
|
||||
// not shift like minecraft, that's sprint. ctrl isn't either: ctrl+w closes the tab and a page can't stop it
|
||||
key_sneak: KeyCode = "KeyC";
|
||||
key_inventory: KeyCode = "KeyE";
|
||||
// with ctrl it drops the whole stack
|
||||
key_drop: KeyCode = "KeyQ";
|
||||
|
||||
Reference in New Issue
Block a user