Breathing !

This commit is contained in:
2026-10-02 11:28:30 -03:00
parent 9ca717d2d0
commit 88d6de13d5
10 changed files with 50 additions and 12 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

+2 -1
View File
@@ -79,11 +79,12 @@ const NO_INPUT: MoveInput = { x: 0, z: 0, jump: false, sprint: false, sneak: fal
export class LocalPlayer extends Player {
client: Client;
inventories = new ClientInventories();
// what the server says, see server/game/vitals.ts. food out of 20, health out of max_health
// what the server says, see server/game/vitals.ts. food and air out of 20, health out of max_health
health = 20;
max_health = 20;
food = 20;
saturation = 5;
air = 20;
// the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator
attack_speed = 4;
last_attack = -Infinity;
+4
View File
@@ -24,6 +24,10 @@ export const GUI_ATLAS = {
saturation_2: { x: 70, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
saturation_3: { x: 80, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
saturation_4: { x: 90, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
// drawn by hand, not from a resource pack
air_empty: { x: 100, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
air_full: { x: 110, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
air_half: { x: 120, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
} as const;
export type GuiSprite = keyof typeof GUI_ATLAS;
+22 -2
View File
@@ -1,4 +1,5 @@
import type { Client } from "$/client/client.ts";
import type { LocalPlayer } from "$/client/entity/local_player.ts";
import type { ClientInventories } from "$/client/inventory.ts";
import { draw_item } from "$/client/rendering/render_utils.ts";
import { canvas, draw_rect, draw_text, measure_text } from "$/client/renderer/mod.ts";
@@ -20,6 +21,9 @@ const ICONS_ABOVE_HOTBAR = 17;
const ICONS_PER_ROW = 10;
const HEART_ROW_SPACING = 10;
const MIN_HEART_ROW_SPACING = 3;
// air: 10 bubbles above the hunger, like minecraft, from when the head goes under water until it's full again
const MAX_AIR = 20;
const AIR_ROW_SPACING = 10;
// how long the held item's name shows above the hotbar after it changes, and the last part of that it fades out in
const HELD_NAME_MS = 2000;
const HELD_NAME_FADE_MS = 500;
@@ -43,7 +47,7 @@ export class Hud {
render(client: Client) {
this.#render_hotbar(client.player.inventories);
this.#render_vitals(client);
const top = icons_y() - heart_rows_height(client.player.max_health);
const top = Math.min(icons_y() - heart_rows_height(client.player.max_health), air_y(client.player));
this.#render_held_name(client.player.inventories, top);
this.#render_crosshair(client);
client.chat.render(false, top - HELD_NAME_GAP);
@@ -91,7 +95,7 @@ export class Hud {
}
// hearts on the left and hunger on the right like minecraft, each icon 2 points. saturation is appleskin's outline
// over the hunger icons, it's never more than the hunger
// over the hunger icons, it's never more than the hunger. air's bubbles go above the hunger, see shows_air
#render_vitals(client: Client) {
const player = client.player;
const { x: left } = hotbar_position();
@@ -126,6 +130,12 @@ export class Hud {
const quarters = saturated >= 1 ? 4 : saturated > 0.5 ? 3 : saturated > 0.25 ? 2 : 1;
draw_gui_sprite(`saturation_${quarters}`, food_x, y);
}
if (shows_air(player)) {
const air_y = y - AIR_ROW_SPACING * GUI_SCALE;
draw_gui_sprite("air_empty", food_x, air_y);
const bubble = icon_fill(player.air, i);
if (bubble) draw_gui_sprite(bubble === "full" ? "air_full" : "air_half", food_x, air_y);
}
}
}
@@ -159,6 +169,16 @@ function icons_y() {
return hotbar_position().y - ICONS_ABOVE_HOTBAR * GUI_SCALE;
}
// as soon as the head is under, so it shows full before it starts going down, and while it fills back up after
function shows_air(player: LocalPlayer) {
return player.eyes_in_water || player.air < MAX_AIR;
}
// the top of the bubbles while they show, the hearts' otherwise
function air_y(player: LocalPlayer) {
return icons_y() - (shows_air(player) ? AIR_ROW_SPACING * GUI_SCALE : 0);
}
// how far apart rows of hearts are, in gui pixels
function heart_row_spacing(max_health: number) {
const rows = Math.ceil(max_health / 2 / ICONS_PER_ROW);
+1
View File
@@ -79,6 +79,7 @@ export class ClientPacketListener {
client.player.max_health = message.max_health;
client.player.food = message.food;
client.player.saturation = message.saturation;
client.player.air = message.air;
break;
case "attack_speed":
client.player.attack_speed = message.value;
+2 -2
View File
@@ -237,8 +237,8 @@ export type ServerMessage =
| { type: "time"; time: number }
// answers suggest: start is where in the text the suggestions replace from, up to the caret
| { type: "suggestions"; id: number; start: number; suggestions: { text: string; tooltip?: string }[] }
// the player's own health, max health, hunger and saturation, when they change
| { type: "vitals"; health: number; max_health: number; food: number; saturation: number }
// the player's own health, max health, hunger, saturation and air (out of 20, 2 a bubble), when they change
| { type: "vitals"; health: number; max_health: number; food: number; saturation: number; air: number }
// the player's own attack speed attribute, when it changes, for the attack indicator under the crosshair
| { type: "attack_speed"; value: number }
// a mod's server script talking to its client script, see ctx.net
+2
View File
@@ -63,6 +63,8 @@ Players follow Minecraft's rules, with 20 health (10 hearts) and 20 hunger:
- **Healing** happens on a full stomach: quickly while there's saturation, slowly with at least 18 hunger.
- **Starving**: at 0 hunger they lose health every 4 seconds, down to half a heart.
- **Falling** hurts 1 per block after the first 3, unless they land in water.
- **Drowning**: with their eyes under water they hold their breath for 15 seconds, shown as bubbles above the hunger,
then lose a heart every second. The `bworld:water_breathing` effect stops it.
- **The void**: below the world, they keep taking damage until they die.
- **Fighting**: other players and mobs can hit them, which also pushes them away.
+4 -2
View File
@@ -1544,10 +1544,12 @@ export class GameServer {
if (player.health > player.max_health) player.health = player.max_health;
const { food, saturation } = player.vitals;
const { health, max_health } = player;
// in bubbles' halves like hunger, so it's only sent when a bubble changes, not every tick under water
const air = Math.max(0, Math.ceil(player.air / player.max_air * 20));
sync(
"vitals",
[health, max_health, food, saturation],
() => ({ type: "vitals", health, max_health, food, saturation }),
[health, max_health, food, saturation, air],
() => ({ type: "vitals", health, max_health, food, saturation, air }),
);
const attack_speed = player.attribute(ATTRIBUTES.attack_speed);
sync("attack_speed", attack_speed, () => ({ type: "attack_speed", value: attack_speed }));
-5
View File
@@ -126,11 +126,6 @@ export class ServerPlayer extends LivingEntity {
return false;
}
// there's no air bar to show yet, so players don't drown
override get can_breathe_underwater() {
return true;
}
// their client moves them, this only burns, falls out of the world and counts effects and hurt time down. while
// knocked back it moves them like a mob that isn't walking anywhere, landing hands them back to their client
override tick() {
+13
View File
@@ -183,3 +183,16 @@ Deno.test("mods can stop or change damage, and hurt, heal and feed players", asy
api.feed(4, 0.5);
assertEquals(api.food, 6);
});
Deno.test("under water they run out of air, shown in bubbles, then drown", async () => {
const t = await setup();
t.game.set_block(0, 100, 0, "bworld:water");
t.game.set_block(0, 101, 0, "bworld:water");
t.take(1);
for (let i = 0; i < 100; i++) t.game.tick();
const air = t.take(1).filter((m: Msg) => m.type === "vitals").at(-1).air;
assert(air > 0 && air < 20, `air ${air}`);
assertEquals(t.player.health, MAX_HEALTH);
for (let i = 0; i < 300; i++) t.game.tick();
assert(t.player.health < MAX_HEALTH);
});