From 5e2b415f5c2414dc65a87677ee29cafacd444192 Mon Sep 17 00:00:00 2001 From: paulaboks Date: Wed, 30 Sep 2026 15:41:30 -0300 Subject: [PATCH] Hitting !! --- client/client.ts | 4 ++++ client/entity/local_player.ts | 10 ++++++++++ client/game_mode.ts | 7 +++++++ client/gui/hud.ts | 21 ++++++++++++++++++--- client/packet_listener.ts | 3 +++ common/protocol.ts | 6 +++++- docs/modding/entities/players.md | 3 ++- server/game/game_server.ts | 10 ++++++++-- tests/entity_test.ts | 25 +++++++++++++++++++++++++ 9 files changed, 82 insertions(+), 7 deletions(-) diff --git a/client/client.ts b/client/client.ts index 6fdba2b..2f50b10 100644 --- a/client/client.ts +++ b/client/client.ts @@ -302,6 +302,8 @@ export class Client { } } else if (InputManager.is_mouse_pressed(2)) { this.game_mode.use_item(); + } else if (!this.entity_hit && InputManager.is_mouse_pressed(0)) { + this.game_mode.swing(); } } @@ -323,6 +325,8 @@ export class Client { if (inventories.hotbar_selected !== previous) { this.connection.send({ type: "select_slot", slot: inventories.hotbar_selected }); + // the server starts the attack cooldown over too + this.player.last_attack = performance.now(); } } } diff --git a/client/entity/local_player.ts b/client/entity/local_player.ts index f9de6f7..47ed866 100644 --- a/client/entity/local_player.ts +++ b/client/entity/local_player.ts @@ -60,6 +60,9 @@ export class LocalPlayer extends Player { health = 20; food = 20; saturation = 5; + // the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator + attack_speed = 4; + last_attack = -Infinity; // blocks per second, peaks about 1.25 blocks up with the entity's gravity and drag jump_force = 9.23; @@ -81,6 +84,13 @@ export class LocalPlayer extends Player { this.client = client; } + // like the server's ServerPlayer.attack_strength, from real time so the indicator fills smoothly: 0 right after + // attacking, 1 after 20 / attack_speed ticks + attack_strength(): number { + const ticks = (performance.now() - this.last_attack) / 50; + return Math.min(1, Math.max(0, (ticks + 0.5) / (20 / this.attack_speed))); + } + tick() { this.#apply_input(this.client.screen ? NO_INPUT : this.#read_input()); this.#prev_eye_height = this.eye_height; diff --git a/client/game_mode.ts b/client/game_mode.ts index 63bb54b..b65f69e 100644 --- a/client/game_mode.ts +++ b/client/game_mode.ts @@ -100,6 +100,13 @@ export class MultiPlayerGameMode { attack(entity: Entity) { this.client.player.send_position_now(); this.client.connection.send({ type: "attack_entity", id: entity.id }); + this.client.player.last_attack = performance.now(); + } + + // left click on nothing: a swing at the air, which uses up the attack cooldown like an attack + swing() { + this.client.connection.send({ type: "swing" }); + this.client.player.last_attack = performance.now(); } // right click on nothing diff --git a/client/gui/hud.ts b/client/gui/hud.ts index 7d70044..3a32ddc 100644 --- a/client/gui/hud.ts +++ b/client/gui/hud.ts @@ -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); } } diff --git a/client/packet_listener.ts b/client/packet_listener.ts index ab9319d..a81faa9 100644 --- a/client/packet_listener.ts +++ b/client/packet_listener.ts @@ -78,6 +78,9 @@ export class ClientPacketListener { client.player.food = message.food; client.player.saturation = message.saturation; break; + case "attack_speed": + client.player.attack_speed = message.value; + break; case "player_leave": level.remove_entity(message.id); break; diff --git a/common/protocol.ts b/common/protocol.ts index cf3bfb5..68e1c0e 100644 --- a/common/protocol.ts +++ b/common/protocol.ts @@ -7,7 +7,7 @@ export const AIR_ID = "bworld:air"; // bump when a client and server of different versions can't play together. // the server rejects a different version before the client downloads anything -export const PROTOCOL_VERSION = 10; +export const PROTOCOL_VERSION = 12; export interface PlayerInfo { id: string; @@ -90,6 +90,8 @@ export type ClientMessage = | { type: "hit_block"; x: number; y: number; z: number } // left click on an entity, by id | { type: "attack_entity"; id: string } + // left click at nothing, a swing at the air. it starts the attack cooldown over, like an attack + | { type: "swing" } // right click without looking at a block, for items' on_use | { type: "use_item" } // right click on a block: interact with it, or place the held block against `face` @@ -190,6 +192,8 @@ export type ServerMessage = | { type: "suggestions"; id: number; start: number; suggestions: { text: string; tooltip?: string }[] } // the player's own health, hunger and saturation, when they change | { type: "vitals"; health: number; food: number; saturation: 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 | { type: "mod"; channel: string; data: unknown }; diff --git a/docs/modding/entities/players.md b/docs/modding/entities/players.md index 81b66df..36c2fac 100644 --- a/docs/modding/entities/players.md +++ b/docs/modding/entities/players.md @@ -88,7 +88,8 @@ plus the amount of each `add_multiplied_total`. A modifier with the same id repl saved: a mod that gives them should give them again when the player joins, the way Curios does for worn items. Like Minecraft, an attack right after the last one, or right after changing the held item, does only a fifth of the -damage, rising to full after `20 / attack_speed` ticks. +damage, rising to full after `20 / attack_speed` ticks. A bar under the crosshair shows it filling up, and the server +sends players their `attack_speed` whenever it changes so the bar matches. ## Death diff --git a/server/game/game_server.ts b/server/game/game_server.ts index cd2ef69..b96744e 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -760,6 +760,9 @@ export class GameServer { this.#attack_entity(player, message.id); } break; + case "swing": + player.attack_strength_ticker = 0; + break; case "use_item": this.#use_item(player); break; @@ -841,6 +844,9 @@ export class GameServer { // left click on an entity or another player: hurts them with the player's attack_damage and pushes them away. // they can't be hurt again for a moment, see LivingEntity.hurt #attack_entity(player: ServerPlayer, id: string) { + // like minecraft, any swing starts the cooldown over, hit or not. their client does the same for its indicator + const strength = player.attack_strength(); + player.attack_strength_ticker = 0; const entity = this.entities.get_entity(id) ?? this.players().find((other) => other.id === id); if ( !(entity instanceof LivingEntity) || entity === player || !entity.alive || !player.alive || @@ -850,8 +856,6 @@ export class GameServer { } const source = { type: "physical", cause: "player_attack", attacker: player, by_player: true } as const; // like minecraft, attacking again before the attack speed allows hits weaker - const strength = player.attack_strength(); - player.attack_strength_ticker = 0; const damage = player.attribute(ATTRIBUTES.attack_damage) * (0.2 + strength * strength * 0.8); if (entity.hurt(damage, source)) { player.vitals.add_exhaustion(EXHAUSTION.attack); @@ -1576,6 +1580,8 @@ export class GameServer { const { food, saturation } = player.vitals; const health = player.health; sync("vitals", [health, food, saturation], () => ({ type: "vitals", health, food, saturation })); + const attack_speed = player.attribute(ATTRIBUTES.attack_speed); + sync("attack_speed", attack_speed, () => ({ type: "attack_speed", value: attack_speed })); if (player.screen) { const items = player.screen.container.to_synced_data(); diff --git a/tests/entity_test.ts b/tests/entity_test.ts index ef613bf..897cc50 100644 --- a/tests/entity_test.ts +++ b/tests/entity_test.ts @@ -184,6 +184,31 @@ Deno.test("attacking again before the attack speed allows hits weaker, like mine assertEquals(alice.attack_strength(), 1); }); +Deno.test("players hear their attack speed for the indicator, and any swing, even at the air, starts the cooldown over", async () => { + const { game, spawn, ticks, send, take } = await floor_game(); + const [alice] = game.players(); + ticks(1); + take(1); + alice.attributes.get(ATTRIBUTES.attack_speed)!.add_modifier({ + id: "claws", + amount: 0.3, + operation: "add_multiplied_base", + }); + ticks(1); + assertEquals(take(1).filter((m) => m.type === "attack_speed").map((m) => m.value), [4 * 1.3]); + + // too far to hit, but the swing still counts, like the client's indicator shows + const far = spawn(0.5, 100, 20.5); + ticks(10); + send(1, { type: "attack_entity", id: far.id }); + assertEquals(alice.attack_strength_ticker, 0); + + // and so does a swing at the air + ticks(10); + send(1, { type: "swing" }); + assertEquals(alice.attack_strength_ticker, 0); +}); + Deno.test("players hit each other, knocking them back and flashing red for everyone else", async () => { const { game, ticks, send, join, take } = await floor_game(); join(2, "bob");