Hitting !!

This commit is contained in:
2026-09-30 15:41:30 -03:00
parent 3af5f6e54b
commit 5e2b415f5c
9 changed files with 82 additions and 7 deletions
+18 -3
View File
@@ -1,7 +1,7 @@
import type { Client } from "$/client/client.ts";
import type { ClientInventories } from "$/client/inventory.ts";
import { draw_item } from "$/client/rendering/render_utils.ts";
import { canvas, draw_text, measure_text } from "$/client/renderer/mod.ts";
import { canvas, draw_rect, draw_text, measure_text } from "$/client/renderer/mod.ts";
import { display_name } from "$/common/utils.ts";
import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts";
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
@@ -24,6 +24,12 @@ const HELD_NAME_GAP = 12;
const CROSSHAIR_SIZE = 15;
// under the crosshair so it shows on bright sky and snow, minecraft inverts the colors behind it instead
const CROSSHAIR_SHADOW: [number, number, number, number] = [0, 0, 0, 0.4];
// the attack indicator, like minecraft's: a bar under the crosshair filling up while the attack cooldown runs
const ATTACK_BAR_WIDTH = 16;
const ATTACK_BAR_HEIGHT = 2;
const ATTACK_BAR_GAP = 2;
const ATTACK_BAR_BACK: [number, number, number, number] = [0, 0, 0, 0.4];
const ATTACK_BAR_FILL: [number, number, number, number] = [1, 1, 1, 0.9];
// what's drawn over the world while playing, like minecraft's Gui: hotbar, crosshair and chat
export class Hud {
@@ -35,7 +41,7 @@ export class Hud {
this.#render_hotbar(client.player.inventories);
this.#render_vitals(client);
this.#render_held_name(client.player.inventories);
this.#render_crosshair();
this.#render_crosshair(client);
client.chat.render(false, icons_y() - HELD_NAME_GAP);
}
@@ -113,12 +119,21 @@ export class Hud {
}
}
#render_crosshair() {
#render_crosshair(client: Client) {
const size = CROSSHAIR_SIZE * GUI_SCALE;
const x = Math.round((canvas.width - size) / 2);
const y = Math.round((canvas.height - size) / 2);
draw_gui_sprite("crosshair", x + GUI_SCALE, y + GUI_SCALE, undefined, undefined, CROSSHAIR_SHADOW);
draw_gui_sprite("crosshair", x, y);
// only while it's charging, like minecraft
const strength = client.player.attack_strength();
if (strength >= 1) return;
const width = ATTACK_BAR_WIDTH * GUI_SCALE;
const bar_x = Math.round((canvas.width - width) / 2);
const bar_y = y + size + ATTACK_BAR_GAP * GUI_SCALE;
draw_rect(bar_x, bar_y, width, ATTACK_BAR_HEIGHT * GUI_SCALE, ATTACK_BAR_BACK);
draw_rect(bar_x, bar_y, Math.round(width * strength), ATTACK_BAR_HEIGHT * GUI_SCALE, ATTACK_BAR_FILL);
}
}