Player api

This commit is contained in:
2026-10-13 22:59:48 -03:00
parent b34e97e487
commit a10b15a3fd
22 changed files with 581 additions and 89 deletions
+31 -1
View File
@@ -12,7 +12,7 @@ type Blocks = (x: number, y: number, z: number) => boolean;
// a player on flat ground at y 0, maybe with a ceiling, and the positions it sends. or in a world of its own: what's
// solid and what's water
function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
const sent: { y: number; swimming?: boolean }[] = [];
const sent: { y: number; swimming?: boolean; sneaking?: boolean }[] = [];
const client = {
level: {
collision_shape: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling),
@@ -123,6 +123,36 @@ Deno.test("walking speeds up until friction evens it out, at minecraft's speeds"
}
});
Deno.test("using an item over time walks at a fifth of the speed, and doesn't sprint", () => {
for (const keys of [["KeyW"], ["KeyW", "ControlLeft"]]) {
const { player } = setup();
run(player, 1, () => false);
player.using_item = true;
for (const key of keys) InputManager.keys_down.add(key);
for (let tick = 0; tick < 40; tick++) player.tick();
const z = player.z;
player.tick();
InputManager.keys_down.clear();
assertAlmostEquals((z - player.z) * 20, 4.317 / 5, 0.01);
assertFalse(player.sprinting);
}
});
Deno.test("holding sneak tells the server right away, flying too", () => {
const { player, sent } = setup();
run(player, 1, () => false);
InputManager.keys_down.add("ShiftLeft");
player.tick();
assertEquals(sent.at(-1)?.sneaking, true);
player.flying = true;
player.tick();
assertFalse(player.sneaking);
assert(player.holding_sneak);
InputManager.keys_down.clear();
player.tick();
assertEquals(sent.at(-1)?.sneaking, undefined);
});
Deno.test("jumping while sprinting is faster, and turning while sprinting is slower", () => {
// average speed over 200 ticks holding w and ctrl, and maybe space, turning this much each tick
const average = (jump: boolean, turn: number) => {