Improve moviment !

This commit is contained in:
2026-09-27 15:37:09 -03:00
parent 5298ff7cf3
commit 9f47c709f1
10 changed files with 285 additions and 37 deletions
+53
View File
@@ -91,3 +91,56 @@ Deno.test("the position is sent on the tick it lands, so the server sees it touc
InputManager.keys_down.clear();
assert(landings >= 5, `landed ${landings} times`);
});
Deno.test("opening a screen while walking stops the player", () => {
const { player } = setup();
const client = player.client as unknown as { screen: unknown };
InputManager.keys_down.add("KeyW");
for (let tick = 0; tick < 5; tick++) player.tick();
client.screen = {};
player.tick();
const z = player.z;
for (let tick = 0; tick < 20; tick++) player.tick();
InputManager.keys_down.clear();
assertEquals(player.z, z);
assertEquals([player.vx, player.vz], [0, 0]);
});
Deno.test("sneaking doesn't walk off edges, from any direction, but still jumps off them", () => {
for (const [key, yaw] of [["KeyW", 0], ["KeyW", Math.PI / 4], ["KeyD", 0], ["KeyS", 1], ["KeyA", 2.5]] as const) {
// a 3x3 platform with nothing around it
const { player } = setup();
const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean };
level.has_collision = (x, y, z) => y === -1 && x >= 0 && x < 3 && z >= 0 && z < 3;
player.set_position(1.5, 0, 1.5);
player.yaw = yaw;
run(player, 1, () => false);
InputManager.keys_down.add("KeyC");
InputManager.keys_down.add(key);
for (let tick = 0; tick < 60; tick++) player.tick();
assertEquals(player.y, 0, `fell off with ${key} at yaw ${yaw}`);
assert(player.on_ground);
InputManager.keys_down.add("Space");
for (let tick = 0; tick < 60; tick++) player.tick();
InputManager.keys_down.clear();
assert(player.y < -1, "jumped off");
}
});
Deno.test("stepping from the edge of a block onto the one diagonal to it doesn't fall between them", () => {
// floor only at (0, 0) and (1, 1)
const { player } = setup();
const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean };
level.has_collision = (x, y, z) => y === -1 && ((x === 0 && z === 0) || (x === 1 && z === 1));
// on the +x edge of the first block, hanging over it, then walking diagonally to the other
player.set_position(1, 0, 0.3);
run(player, 1, () => false);
player.yaw = -3 * Math.PI / 4;
InputManager.keys_down.add("KeyW");
for (let tick = 0; tick < 20 && !(player.x > 1.3 && player.z > 1.3); tick++) player.tick();
InputManager.keys_down.clear();
assert(player.x > 1.3 && player.z > 1.3, "got over the second block");
assertEquals(player.y, 0);
assert(player.on_ground);
});
+43
View File
@@ -0,0 +1,43 @@
import { assert, assertAlmostEquals } from "@std/assert";
import { move_body } from "$/common/physics.ts";
const body = (x: number, z: number, vx: number, vz: number) => ({ x, y: 0, z, vx, vy: 0, vz, width: 0.6, height: 1.8 });
// a block at x 0 to 1, z 1 to 2, on a floor
const solid = (x: number, y: number, z: number) => y === -1 || (x === 0 && z === 1 && y === 0);
Deno.test("moving diagonally into a corner keeps going the way it's moving most, like minecraft", () => {
// mostly +z, from just beside the corner: z goes first, so the small x move is what the block stops
const mostly_z = body(-0.33, 0.65, 1, 4);
move_body(mostly_z, 0, solid);
assertAlmostEquals(mostly_z.z, 0.85);
assertAlmostEquals(mostly_z.x, -0.3);
// mostly +x: x goes first, and the small z move is stopped at the block's face
const mostly_x = body(-0.33, 0.65, 4, 1.2);
move_body(mostly_x, 0, solid);
assertAlmostEquals(mostly_x.x, -0.13);
assertAlmostEquals(mostly_x.z, 0.7);
});
Deno.test("a body on the ground steps up onto something low, not onto a full block, and only with a step height", () => {
// just walked off something, 0.1 below the top of the block at x 1 that it's walking into
const next_block = (x: number, y: number, z: number) => x === 1 && y === -1 && z === 0;
const dipped = () => ({ ...body(0.65, 0.5, 4, 0), y: -0.1 });
const stepping = dipped();
move_body(stepping, -32, next_block, 0.6, true);
assertAlmostEquals(stepping.y, 0, 1e-9);
assertAlmostEquals(stepping.x, 0.85);
// without a step height it's a wall, and it slides down it
const item = dipped();
move_body(item, -32, next_block, 0, true);
assertAlmostEquals(item.x, 0.7);
assert(item.y < -0.1);
// a full block is too tall to step onto
const wall = { ...body(0.5, 0.5, 4, 0), y: 0 };
move_body(wall, -32, (x, y, z) => y === -1 || (x === 1 && y === 0 && z === 0), 0.6, true);
assertAlmostEquals(wall.x, 0.7);
assertAlmostEquals(wall.y, 0);
});