Water phyisics

This commit is contained in:
2026-10-07 19:38:27 -03:00
parent d101fa2ef3
commit 78117f65c9
13 changed files with 338 additions and 13 deletions
+27 -8
View File
@@ -116,15 +116,34 @@ Deno.test("q throws one of an item the way the player looks, ctrl throws the who
assertEquals(game.item_entities().length, 2);
});
Deno.test("water has no collision, items sink through it to the floor", async () => {
Deno.test("items fall into water, float back up and bob at its surface", async () => {
const { game, join } = await test_game("mods");
join(1, "alice");
build_floor(game);
for (let y = 100; y <= 102; y++) {
game.set_block(6, y, 0, "bworld:water");
// a pool 3 deep in a stone basin, so the water stays still
for (let x = 3; x <= 9; x++) {
for (let z = -3; z <= 3; z++) {
for (let y = 99; y <= 103; y++) {
const inside = x >= 4 && x <= 8 && z >= -2 && z <= 2 && y >= 100;
game.set_block(x, y, z, inside ? (y <= 102 ? "bworld:water" : "bworld:air") : "bworld:stone");
}
}
}
const item = game.spawn_item(6.5, 104, 0.5, new ItemStack("bworld:dirt"));
for (let i = 0; i < 40; i++) game.tick();
assert(item.on_ground);
assertEquals(item.y, 100, "on the stone under the water");
const item = game.spawn_item(6.5, 104.5, 0.5, new ItemStack("bworld:dirt"));
let lowest = Infinity;
for (let i = 0; i < 40; i++) {
game.tick();
lowest = Math.min(lowest, item.y);
}
assert(lowest < 102, `it went in, down to ${lowest}`);
for (let i = 0; i < 400; i++) game.tick();
const heights: number[] = [];
for (let i = 0; i < 200; i++) {
game.tick();
heights.push(item.y);
}
// about a tenth of it out of the water at the top, never far under
assert(Math.max(...heights) > 102.85 && Math.max(...heights) < 103, `up to ${Math.max(...heights)}`);
assert(Math.min(...heights) > 102.4, `down to ${Math.min(...heights)}`);
assertEquals([item.x, item.z], [6.5, 0.5]);
});