105 lines
5.0 KiB
TypeScript
105 lines
5.0 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// swimming as the server sees it: each player's client says when they're swimming, which gives them the short box
|
|
// and low eyes, and tells everyone else. how they swim is the client's, see local_player_test.ts
|
|
import { assert, assertAlmostEquals, assertEquals, assertNotEquals } from "@std/assert";
|
|
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_SWIM_EYE_HEIGHT, PLAYER_SWIM_HEIGHT } from "@/common/constants.ts";
|
|
import { part_point, PARTS, type PlayerPose, pose_at, pose_to_world } from "@/client/rendering/player_model.ts";
|
|
import { test_game } from "./helpers.ts";
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
type Msg = any;
|
|
|
|
Deno.test("a player's client says when they swim, which shortens their box and tells everyone else", async () => {
|
|
const t = await test_game("mods");
|
|
t.join(1, "alice");
|
|
t.join(2, "bob");
|
|
const alice = t.game.players().find((player) => player.name === "alice")!;
|
|
t.take(2);
|
|
const api = t.game.mods.player(alice);
|
|
assertEquals(api.is_swimming(), false);
|
|
|
|
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0, swimming: true });
|
|
assert(alice.swimming);
|
|
assert(api.is_swimming());
|
|
assertEquals(alice.height, PLAYER_SWIM_HEIGHT);
|
|
assertEquals(alice.eye_height, PLAYER_SWIM_EYE_HEIGHT);
|
|
const moved = t.take(2).findLast((m: Msg) => m.type === "player_move" && m.id === alice.id);
|
|
assertEquals(moved.swimming, true);
|
|
|
|
// someone joining now sees her swimming
|
|
const { players } = t.join(3, "carol");
|
|
assertEquals(players.find((player: Msg) => player.id === alice.id)?.swimming, true);
|
|
|
|
// a move without it stands her back up
|
|
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
|
assertEquals(api.is_swimming(), false);
|
|
assertEquals(alice.height, PLAYER_HEIGHT);
|
|
assertEquals(alice.eye_height, PLAYER_EYE_HEIGHT);
|
|
const stood = t.take(2).findLast((m: Msg) => m.type === "player_move" && m.id === alice.id);
|
|
assertEquals(stood.swimming, undefined);
|
|
});
|
|
|
|
// how a swimming player is drawn, see client/rendering/player_model.ts
|
|
const pose = (swim?: { amount: number; pitch: number }, pitch = 0, position = 0) =>
|
|
pose_at({ x: 0, y: 0, z: 0, yaw: 0, pitch, slim: false, walk: { position, speed: 1 }, swim });
|
|
const world = (p: PlayerPose, name: string, x: number, y: number, z: number) => {
|
|
const part = PARTS.find((part) => part.name === name)!;
|
|
return pose_to_world(p)(...part_point(p, part, x, y, z));
|
|
};
|
|
|
|
Deno.test("not swimming at all, the pose is the walking one", () => {
|
|
for (const position of [0, 1, 2.5]) {
|
|
const walking = pose(undefined, 0.3, position);
|
|
const still = pose({ amount: 0, pitch: -1 }, 0.3, position);
|
|
assertEquals(still, walking);
|
|
for (const part of PARTS) {
|
|
assertEquals(world(still, part.name, 1, 2, 3), world(walking, part.name, 1, 2, 3));
|
|
}
|
|
}
|
|
});
|
|
|
|
Deno.test("swimming, the body lies flat in the short box, its head forward and facing where they look", () => {
|
|
const flat = pose({ amount: 1, pitch: 0 });
|
|
// the neck is ahead (the model faces -z) and the feet behind
|
|
const [, head_y, head_z] = world(flat, "head", 0, 0, 0);
|
|
const [, feet_y, feet_z] = world(flat, "right_leg", 0, -12, 0);
|
|
assert(head_z < -0.4 && feet_z > 0.8, `head at ${head_z}, feet at ${feet_z}`);
|
|
// the body is inside the box
|
|
for (const [x, y, z] of [[-4, 0, -2], [4, 12, 2], [-4, 12, -2], [4, 0, 2]]) {
|
|
const [, by] = world(flat, "body", x, y, z);
|
|
assert(by >= 0 && by <= PLAYER_SWIM_HEIGHT, `body at ${by}`);
|
|
}
|
|
assert(Math.abs(head_y - PLAYER_SWIM_HEIGHT / 2) < 0.2 && Math.abs(feet_y - PLAYER_SWIM_HEIGHT / 2) < 0.2);
|
|
|
|
// the face points where they look, whichever way the body lies
|
|
for (const [moving, look] of [[0, 0], [0, -0.5], [-0.8, -0.8], [0.4, 0.2]]) {
|
|
const p = pose({ amount: 1, pitch: moving }, look);
|
|
const [, back_y, back_z] = world(p, "head", 0, 4, 4);
|
|
const [, front_y, front_z] = world(p, "head", 0, 4, -4);
|
|
assertAlmostEquals(Math.atan2(front_y - back_y, back_z - front_z), look, 1e-9);
|
|
}
|
|
|
|
// diving, the head goes down
|
|
const [, diving_y] = world(pose({ amount: 1, pitch: -1 }), "head", 0, 8, 0);
|
|
assert(diving_y < head_y);
|
|
});
|
|
|
|
Deno.test("swimming, the arms go around half a turn apart and the legs flutter", () => {
|
|
const arm_tip = (p: PlayerPose, name: string) => world(p, name, 1, -10, 0);
|
|
// a whole stroke
|
|
const strokes = [0, 3, 6, 9, 12, 15, 18].map((position) => pose({ amount: 1, pitch: 0 }, 0, position));
|
|
const right = strokes.map((p) => arm_tip(p, "right_arm"));
|
|
// it goes both above and below the body over a stroke
|
|
assert(right.some(([, y]) => y > PLAYER_SWIM_HEIGHT) && right.some(([, y]) => y < 0.2));
|
|
// the other arm is on the other side
|
|
const [, right_y] = arm_tip(strokes[1], "right_arm");
|
|
const [, left_y] = arm_tip(strokes[1], "left_arm");
|
|
assert(Math.sign(right_y - 0.4) !== Math.sign(left_y - 0.4), `arms at ${right_y}, ${left_y}`);
|
|
// legs opposite each other
|
|
const [, right_leg] = world(strokes[0], "right_leg", 0, -12, 0);
|
|
const [, left_leg] = world(strokes[0], "left_leg", 0, -12, 0);
|
|
assertNotEquals(right_leg, left_leg);
|
|
});
|