Improve jumping

This commit is contained in:
2026-09-27 15:06:07 -03:00
parent 29e61a235b
commit 5298ff7cf3
2 changed files with 111 additions and 5 deletions
+18 -5
View File
@@ -3,8 +3,12 @@ import { InputManager } from "../input_manager.ts";
import { ClientInventories } from "../inventory.ts";
import { Player } from "./player.ts";
// the position goes to the server every other tick
// the position goes to the server every other tick, and on the tick it lands so the server always sees it touch the
// ground, which is what resets its fall distance
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;
// the player this client controls, like minecraft's LocalPlayer
export class LocalPlayer extends Player {
@@ -20,6 +24,8 @@ export class LocalPlayer extends Player {
jump_force = 9.23;
#ticks_since_sent = 0;
// counts down every tick, letting go of jump clears it
#jump_delay = 0;
constructor(client: Client, id: string, name: string) {
super(client.level, id, name);
@@ -29,9 +35,12 @@ export class LocalPlayer extends Player {
tick() {
if (!this.client.screen) {
this.#apply_input();
} else {
this.#jump_delay = 0;
}
const was_on_ground = this.on_ground;
this.move();
this.#send_position();
this.#send_position(this.on_ground && !was_on_ground);
}
// turns with the mouse every frame, not every tick, like minecraft's MouseHandler.turnPlayer
@@ -75,14 +84,18 @@ export class LocalPlayer extends Player {
this.vx = (sin * input_z + cos * input_x) * speed;
this.vz = (cos * input_z - sin * input_x) * speed;
if (InputManager.is_key_down(options.key_jump) && this.on_ground) {
this.#jump_delay = Math.max(0, this.#jump_delay - 1);
if (!InputManager.is_key_down(options.key_jump)) {
this.#jump_delay = 0;
} else if (this.on_ground && this.#jump_delay === 0) {
this.vy += this.jump_force;
this.#jump_delay = JUMP_DELAY_TICKS;
}
}
#send_position() {
#send_position(now: boolean) {
this.#ticks_since_sent += 1;
if (this.#ticks_since_sent < SEND_POSITION_TICKS) {
if (!now && this.#ticks_since_sent < SEND_POSITION_TICKS) {
return;
}
this.#ticks_since_sent = 0;
+93
View File
@@ -0,0 +1,93 @@
import { assert, assertEquals } from "@std/assert";
import { LocalPlayer } from "$/client/entity/local_player.ts";
import { InputManager } from "$/client/input_manager.ts";
import { Options } from "$/client/options.ts";
import type { Client } from "$/client/client.ts";
// a player on flat ground at y 0, maybe with a ceiling, and the positions it sends
function setup(ceiling = Infinity) {
const sent: { y: number }[] = [];
const client = {
level: { has_collision: (_x: number, y: number) => y < 0 || y >= ceiling },
screen: undefined,
options: new Options(),
connection: { send: (message: { type: string; y: number }) => message.type === "move" && sent.push(message) },
};
const player = new LocalPlayer(client as unknown as Client, "id", "alice");
player.set_position(0.5, 0, 0.5);
return { player, sent };
}
// the ticks it jumped on
function run(player: LocalPlayer, ticks: number, space: (tick: number) => boolean) {
const jumps: number[] = [];
for (let tick = 0; tick < ticks; tick++) {
if (space(tick)) InputManager.keys_down.add("Space");
else InputManager.keys_down.delete("Space");
const was_on_ground = player.on_ground;
player.tick();
if (was_on_ground && !player.on_ground) jumps.push(tick);
}
InputManager.keys_down.clear();
return jumps;
}
Deno.test("holding jump jumps again as soon as it lands, but not sooner than minecraft's jump delay", () => {
const open = setup();
run(open.player, 1, () => false);
const landings: number[] = [];
const jumps = run(open.player, 100, (tick) => {
if (tick > 0 && open.player.on_ground) landings.push(tick);
return true;
});
assert(jumps.length >= 5, `jumped ${jumps.length} times`);
// a jump is longer in the air than the delay, so it's never waiting on the ground
for (const jump of jumps.slice(1)) {
assert(landings.includes(jump), `waited on the ground before jumping on tick ${jump}`);
}
// with a block right above its head it lands sooner, and waits out the delay on the ground
const low = setup(2);
run(low.player, 1, () => false);
let standing = 0;
const waited: number[] = [];
const bumped = run(low.player, 100, () => {
standing = low.player.on_ground ? standing + 1 : 0;
waited.push(standing);
return true;
});
assert(bumped.length >= 5, `jumped ${bumped.length} times`);
for (let i = 1; i < bumped.length; i++) {
assert(bumped[i] - bumped[i - 1] >= 10, `jumped again after ${bumped[i] - bumped[i - 1]} ticks`);
}
assert(bumped.slice(1).some((jump) => waited[jump] > 1), "never waited on the ground");
// letting go clears the delay
const { player } = setup(2);
run(player, 1, () => false);
run(player, 1, () => true);
while (!player.on_ground) run(player, 1, () => true);
run(player, 1, () => false);
assertEquals(run(player, 1, () => true), [0]);
});
Deno.test("the position is sent on the tick it lands, so the server sees it touch the ground between jumps", () => {
const { player, sent } = setup();
run(player, 1, () => false);
// jump, and press again the moment it lands, for the shortest time on the ground
let landings = 0;
for (let tick = 0; tick < 200; tick++) {
InputManager.keys_down.clear();
if (player.on_ground && tick % 2 === 0) InputManager.keys_down.add("Space");
const in_air = !player.on_ground;
const before = sent.length;
player.tick();
if (in_air && player.on_ground) {
landings++;
assertEquals(sent.length, before + 1, "sent when it landed");
assertEquals(sent.at(-1)!.y, 0);
}
}
InputManager.keys_down.clear();
assert(landings >= 5, `landed ${landings} times`);
});