Movement copied from the minecraft wiki :3
This commit is contained in:
+106
-2
@@ -1,4 +1,4 @@
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { assert, assertAlmostEquals, 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";
|
||||
@@ -92,13 +92,117 @@ Deno.test("the position is sent on the tick it lands, so the server sees it touc
|
||||
assert(landings >= 5, `landed ${landings} times`);
|
||||
});
|
||||
|
||||
Deno.test("walking speeds up until friction evens it out, at minecraft's speeds", () => {
|
||||
for (const [keys, speed] of [[[], 4.317], [["ControlLeft"], 5.612], [["ShiftLeft"], 1.295]] as const) {
|
||||
const { player } = setup();
|
||||
run(player, 1, () => false);
|
||||
InputManager.keys_down.add("KeyW");
|
||||
for (const key of keys) InputManager.keys_down.add(key);
|
||||
player.tick();
|
||||
assert(-player.vz < speed / 2, "doesn't start at full speed");
|
||||
for (let tick = 0; tick < 40; tick++) player.tick();
|
||||
const z = player.z;
|
||||
player.tick();
|
||||
InputManager.keys_down.clear();
|
||||
assertAlmostEquals((z - player.z) * 20, speed, 0.01);
|
||||
}
|
||||
});
|
||||
|
||||
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) => {
|
||||
const { player } = setup();
|
||||
run(player, 1, () => false);
|
||||
InputManager.keys_down.add("KeyW");
|
||||
InputManager.keys_down.add("ControlLeft");
|
||||
if (jump) InputManager.keys_down.add("Space");
|
||||
let distance = 0;
|
||||
for (let tick = 0; tick < 240; tick++) {
|
||||
player.yaw += turn;
|
||||
const { x, z } = player;
|
||||
player.tick();
|
||||
if (tick >= 40) distance += Math.hypot(player.x - x, player.z - z);
|
||||
}
|
||||
InputManager.keys_down.clear();
|
||||
return distance / 10;
|
||||
};
|
||||
const sprinting = average(false, 0);
|
||||
const jumping = average(true, 0);
|
||||
assertAlmostEquals(sprinting, 5.612, 0.01);
|
||||
assertAlmostEquals(jumping, 7.127, 0.05);
|
||||
assert(average(false, 0.1) < sprinting - 0.05, "turning slows it down");
|
||||
});
|
||||
|
||||
Deno.test("sprinting starts going forward and keeps going until it stops, sneaks, gets hungry or hits a wall", () => {
|
||||
const keys = (...keys: string[]) => {
|
||||
InputManager.keys_down.clear();
|
||||
for (const key of keys) InputManager.keys_down.add(key);
|
||||
};
|
||||
const { player } = setup();
|
||||
run(player, 1, () => false);
|
||||
|
||||
// not backwards or sideways
|
||||
keys("KeyS", "ControlLeft");
|
||||
player.tick();
|
||||
keys("KeyA", "ControlLeft");
|
||||
player.tick();
|
||||
assert(!player.sprinting, "sprinting without going forward");
|
||||
|
||||
// letting go of the key keeps it, letting go of forward stops it
|
||||
keys("KeyW", "ControlLeft");
|
||||
player.tick();
|
||||
keys("KeyW");
|
||||
player.tick();
|
||||
assert(player.sprinting, "letting go of sprint stopped it");
|
||||
keys();
|
||||
player.tick();
|
||||
assert(!player.sprinting, "still sprinting after stopping");
|
||||
|
||||
// sneaking stops it
|
||||
keys("KeyW", "ControlLeft");
|
||||
player.tick();
|
||||
keys("KeyW", "ControlLeft", "ShiftLeft");
|
||||
player.tick();
|
||||
assert(!player.sprinting, "sprinting while sneaking");
|
||||
|
||||
// too hungry to start, and getting hungry stops it
|
||||
player.food = 6;
|
||||
keys("KeyW", "ControlLeft");
|
||||
player.tick();
|
||||
assert(!player.sprinting, "sprinting while hungry");
|
||||
player.food = 20;
|
||||
player.tick();
|
||||
assert(player.sprinting);
|
||||
player.food = 6;
|
||||
player.tick();
|
||||
assert(!player.sprinting, "kept sprinting after getting hungry");
|
||||
InputManager.keys_down.clear();
|
||||
|
||||
// running into a wall stops it, running along one doesn't
|
||||
for (const [yaw, stops] of [[0, true], [Math.PI / 2, false]] as const) {
|
||||
const { player } = setup();
|
||||
const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean };
|
||||
// a wall on the -z side
|
||||
level.has_collision = (_x, y, z) => y < 0 || z < -2;
|
||||
player.set_position(0.5, 0, -1);
|
||||
player.yaw = yaw;
|
||||
run(player, 1, () => false);
|
||||
keys("KeyW", "ControlLeft");
|
||||
if (yaw !== 0) keys("KeyW", "ControlLeft", "KeyD");
|
||||
for (let tick = 0; tick < 20; tick++) player.tick();
|
||||
InputManager.keys_down.clear();
|
||||
assertEquals(player.sprinting, !stops, `yaw ${yaw}`);
|
||||
}
|
||||
});
|
||||
|
||||
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();
|
||||
// it slides a little before friction stops it
|
||||
for (let tick = 0; tick < 20; tick++) player.tick();
|
||||
const z = player.z;
|
||||
for (let tick = 0; tick < 20; tick++) player.tick();
|
||||
InputManager.keys_down.clear();
|
||||
|
||||
Reference in New Issue
Block a user