Hold right click to place

This commit is contained in:
2026-10-07 17:30:18 -03:00
parent 7767efab58
commit 4f8fef6ca2
+16 -4
View File
@@ -29,6 +29,8 @@ import type { EntityJson } from "$/common/mod_data.ts";
const ENTITY_REACH = 3;
// how far from the eyes blocks can be broken and used
const BLOCK_REACH = 6;
// holding the use button uses the held item on blocks again this often, like minecraft's 4 ticks
const USE_REPEAT_MS = 200;
// after a long stall (a hidden tab, a debugger) the game skips ahead instead of running every missed tick at once
const MAX_TICKS_PER_FRAME = 10;
@@ -58,6 +60,8 @@ export class Client {
#pending_time = 0;
// whether the attack button is held on a block, breaking it advances every tick
#attacking = false;
// when holding the use button on a block uses it again, see USE_REPEAT_MS
#next_use = 0;
#stopped = false;
// the render distance the server was last told, it sends chunks for it
#sent_view_distance: number;
@@ -314,8 +318,16 @@ export class Client {
return;
}
// the held item swings on every click, and keeps swinging while breaking, like minecraft
if (InputManager.is_mouse_pressed(0) || InputManager.is_mouse_pressed(2) || (attacking && hit)) {
// a click uses right away, holding it on blocks keeps using every USE_REPEAT_MS
const now = performance.now();
const use_pressed = InputManager.is_mouse_pressed(2);
const using = use_pressed || (hit && InputManager.is_mouse_down(2) && now >= this.#next_use);
if (using) {
this.#next_use = now + USE_REPEAT_MS;
}
// the held item swings on every click and use, and keeps swinging while breaking, like minecraft
if (InputManager.is_mouse_pressed(0) || using || (attacking && hit)) {
this.game_renderer.hand.swing();
}
@@ -326,10 +338,10 @@ export class Client {
if (InputManager.is_mouse_pressed(0)) {
this.game_mode.start_destroy_block(hit);
}
if (!attacking && InputManager.is_mouse_pressed(2)) {
if (!attacking && using) {
this.game_mode.use_item_on(hit);
}
} else if (InputManager.is_mouse_pressed(2)) {
} else if (use_pressed) {
this.game_mode.use_item();
} else if (!this.entity_hit && InputManager.is_mouse_pressed(0)) {
this.game_mode.swing();