Make players mobs too !

This commit is contained in:
2026-09-30 10:42:52 -03:00
parent 65f27d6099
commit 4fbcca6851
23 changed files with 314 additions and 132 deletions
+15 -17
View File
@@ -12,12 +12,12 @@ Deno.test("exhaustion eats saturation first, then hunger", () => {
const vitals = new Vitals();
const noop = () => {};
vitals.add_exhaustion(4.1);
vitals.tick(noop, noop);
vitals.tick(20, noop, noop);
assertEquals([vitals.food, vitals.saturation], [20, 4]);
vitals.saturation = 0;
vitals.add_exhaustion(4);
vitals.tick(noop, noop);
vitals.tick(20, noop, noop);
assertEquals([vitals.food, vitals.saturation], [19, 0]);
});
@@ -29,29 +29,27 @@ Deno.test("a full stomach heals, fast with saturation and slowly without, and st
// full hunger and saturation: every 10 ticks, up to a whole heart, paid for with exhaustion
const fast = new Vitals();
fast.health = 10;
for (let i = 0; i < 10; i++) fast.tick(heal, damage);
for (let i = 0; i < 10; i++) fast.tick(10, heal, damage);
assertAlmostEquals(healed, 5 / 6);
assertEquals(fast.exhaustion, 5);
// 18 hunger and no saturation: 1 every 4 seconds
healed = 0;
const slow = new Vitals();
slow.health = 10;
slow.food = 18;
slow.saturation = 0;
for (let i = 0; i < 80; i++) slow.tick(heal, damage);
for (let i = 0; i < 80; i++) slow.tick(10, heal, damage);
assertEquals(healed, 1);
// starving: 1 every 4 seconds, never below 1 health
const starving = new Vitals();
starving.food = 0;
starving.saturation = 0;
starving.health = 2;
for (let i = 0; i < 80; i++) starving.tick(heal, (amount) => starving.health -= amount);
assertEquals(starving.health, 1);
for (let i = 0; i < 400; i++) starving.tick(heal, (amount) => starving.health -= amount);
assertEquals(starving.health, 1);
let health = 2;
for (let i = 0; i < 80; i++) starving.tick(health, heal, (amount) => health -= amount);
assertEquals(health, 1);
for (let i = 0; i < 400; i++) starving.tick(health, heal, (amount) => health -= amount);
assertEquals(health, 1);
assertEquals(hurt, 0);
});
@@ -84,20 +82,20 @@ Deno.test("falling more than 3 blocks hurts, 1 per block after that, and water b
// up 10 blocks, then down in steps like a fall
t.move(110);
for (let y = 109; y >= 100; y--) t.move(y);
assertEquals(t.player.vitals.health, MAX_HEALTH - 7);
assertEquals(t.player.health, MAX_HEALTH - 7);
// the same fall into water
t.player.vitals.health = MAX_HEALTH;
t.player.health = MAX_HEALTH;
t.game.set_block(0, 100, 0, "bworld:water");
t.move(110);
for (let y = 109; y >= 100; y--) t.move(y);
assertEquals(t.player.vitals.health, MAX_HEALTH);
assertEquals(t.player.health, MAX_HEALTH);
});
Deno.test("health and hunger are sent to the player when they change, and saved", async () => {
const t = await setup();
t.take(1);
t.player.vitals.health = 15;
t.player.health = 15;
t.player.vitals.food = 12;
t.game.tick();
const vitals = t.take(1).filter((m) => m.type === "vitals").at(-1);
@@ -108,7 +106,7 @@ Deno.test("health and hunger are sent to the player when they change, and saved"
t.game.on_disconnect(1);
const loaded = await test_game("mods", t.game.save());
loaded.join(1, "alice");
assertEquals(loaded.game.players()[0].vitals.health, 15);
assertEquals(loaded.game.players()[0].health, 15);
assertEquals(loaded.game.players()[0].vitals.food, 12);
});
@@ -144,7 +142,7 @@ Deno.test("dying drops everything, tells everyone, and respawns with full health
assertEquals(deaths, ["alice fall"]);
assert(messages.some((m: Msg) => m.type === "chat" && m.text === "alice hit the ground too hard"));
assert(messages.some((m: Msg) => m.type === "teleport"));
assertEquals([t.player.vitals.health, t.player.vitals.food], [MAX_HEALTH, 20]);
assertEquals([t.player.health, t.player.vitals.food], [MAX_HEALTH, 20]);
assert(t.player.inventory.to_data().every((i) => i === null));
assertEquals(t.game.item_entities().map((e) => e.item.to_data()), [{ id: "bworld:coal", count: 5 }]);
});