From 88d6de13d519f2acf9be35bcb3fec102f9e30c0d Mon Sep 17 00:00:00 2001 From: Paula Date: Fri, 2 Oct 2026 11:28:30 -0300 Subject: [PATCH] Breathing ! --- assets/sprites/gui.png | Bin 2306 -> 2333 bytes client/entity/local_player.ts | 3 ++- client/gui/gui_atlas.ts | 4 ++++ client/gui/hud.ts | 24 ++++++++++++++++++++++-- client/packet_listener.ts | 1 + common/protocol.ts | 4 ++-- docs/modding/entities/players.md | 2 ++ server/game/game_server.ts | 6 ++++-- server/game/player.ts | 5 ----- tests/vitals_test.ts | 13 +++++++++++++ 10 files changed, 50 insertions(+), 12 deletions(-) diff --git a/assets/sprites/gui.png b/assets/sprites/gui.png index 41f0fdb39177f314370917b611136a3ebe3553d0..95cd44c7c9d18cd1740f92d579b6901585e6e225 100644 GIT binary patch delta 1818 zcmZ{keOyxa9>)&|qWJ*Wqhyw5=F-qvXuCdvVyk7@yw!9T>9RCkTB)Fjf@bP{87i@| zWkbtK@3O>NGI61`%IZ4HuD{(Aj9KlBo?THIRC^$bxsdtL)6<0N(i9RiD{BO?YW`93SIGaakL{ z+X`*;nkCSo)rlokT{^s4idy-3h&SmVI%CrIdO_S(+>0X8+0LuD{~3^NVJ->Uo>)>v zgwC#o&T>P%FP_1~)z-Wkcm5~4yHAI_Sx>%U$a5ufMt*qxvVE_PZ?l-9xL^z~%UH#2 zJpTKWHqjk#B;;16y$)c`{*o2NyjMUPOP1OZMX`6Q3S8pW>|UF=ZR;3n^Ob_QIB%pN zqu|abEfGIk0FQXe1=3kTD3=EN^|r5t4K28Pi9~v`kk20r=X*k}g2P&465(P$l6(q4 z1o21|cl6yW1ujF5)W7s~I}R6r?JF3t?S*l;06y6y{lA^;?Jc-n(1IWf;MY3YXD))x z2~Hpde;6Iv@jZ&-Qn?-9TET+Z7U%guc|J5@Bd3}WzJeOyms%(Nj|+u>2aA1cg^2Ya zMbsZIDSrxx&@dhD!h{65RQOU821%QOOAfW!{vmWP=4&|g0*=3hUcmK%9Xw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4G4b26b0Mk^o5c9GgBcq`1Pll`^*Fvu zM!AZGT?Z=GrNaSY$HoN>h;17Ta~100$B%E%MZbEGD9Tgc0))EFvxI`)3dt!oAVnd7 z4}$~ebFIc%6JyTA6YysSCKqN-h+dYIg9lRWMqX&Tn5hB$u)~Hk-eiVn0@M!maiK_1 zRsd2+B&&K!6@$aFkYN|vJ_tl_-v{s(aOIz%Z?vpodPA0fKB%<(!WK$B$5YfiQ<;hn^LnxiaEm+IBW{M5c58H46kGJ!9PV>$MxLF5c zn(+YS@*TWr%GV5lBQ+~^0Poy$z_bk4WTnMnEGCCI^f4#mM`0CoDMC^Z<#XTHp&fwM NzCB*9W#5J-{S9%JMzR0^ diff --git a/client/entity/local_player.ts b/client/entity/local_player.ts index cac0159..a22ccf1 100644 --- a/client/entity/local_player.ts +++ b/client/entity/local_player.ts @@ -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; diff --git a/client/gui/gui_atlas.ts b/client/gui/gui_atlas.ts index c6098ee..20a2783 100644 --- a/client/gui/gui_atlas.ts +++ b/client/gui/gui_atlas.ts @@ -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; diff --git a/client/gui/hud.ts b/client/gui/hud.ts index ca69abb..08dc26a 100644 --- a/client/gui/hud.ts +++ b/client/gui/hud.ts @@ -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); diff --git a/client/packet_listener.ts b/client/packet_listener.ts index 51bfb1f..b91c404 100644 --- a/client/packet_listener.ts +++ b/client/packet_listener.ts @@ -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; diff --git a/common/protocol.ts b/common/protocol.ts index 4af5a8e..8a94a0b 100644 --- a/common/protocol.ts +++ b/common/protocol.ts @@ -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 diff --git a/docs/modding/entities/players.md b/docs/modding/entities/players.md index 493c274..2b62367 100644 --- a/docs/modding/entities/players.md +++ b/docs/modding/entities/players.md @@ -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. diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 0588354..be91a78 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -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 })); diff --git a/server/game/player.ts b/server/game/player.ts index cd757f5..259d41a 100644 --- a/server/game/player.ts +++ b/server/game/player.ts @@ -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() { diff --git a/tests/vitals_test.ts b/tests/vitals_test.ts index d151533..4a4bf11 100644 --- a/tests/vitals_test.ts +++ b/tests/vitals_test.ts @@ -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); +});