Player api

This commit is contained in:
2026-10-13 22:59:48 -03:00
parent b34e97e487
commit a10b15a3fd
22 changed files with 581 additions and 89 deletions
+15 -9
View File
@@ -29,10 +29,6 @@ import { EverythingRegistry } from "$/common/everything_registry.ts";
import type { EntityJson } from "$/common/mod_data.ts";
import { tick_texture_animations } from "./texture_animations.ts";
// how far from the eyes entities can be hit, like minecraft
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;
@@ -66,6 +62,8 @@ export class Client {
#attacking = false;
// when holding the use button on a block uses it again, see USE_REPEAT_MS
#next_use = 0;
// a use was sent and the use button hasn't been let go of since, see #handle_block_interaction
#use_held = false;
#stopped = false;
// the render distance the server was last told, it sends chunks for it
#sent_view_distance: number;
@@ -293,7 +291,7 @@ export class Client {
player.z,
player.yaw,
player.pitch,
ENTITY_REACH,
player.entity_interaction_range,
(entity) =>
entity instanceof RemotePlayer ||
(entity instanceof RemoteEntity && !entity.dead && is_hittable(entity)),
@@ -305,7 +303,7 @@ export class Client {
player.z,
player.yaw,
player.pitch,
picked?.distance ?? BLOCK_REACH,
picked?.distance ?? player.block_interaction_range,
this.game_mode.held_item()?.targets_liquids,
);
this.entity_hit = picked && !this.hit_result ? picked.entity : undefined;
@@ -320,16 +318,24 @@ export class Client {
if (!attacking || !hit) {
this.game_mode.stop_destroy_block();
}
// letting go stops using an item over time, like shooting a bow, even with a screen open by then. the server
// may not have said it's using one yet, so it's sent after every use
if (this.#use_held && !InputManager.is_mouse_down(2)) {
this.#use_held = false;
this.connection.send({ type: "release_use" });
}
if (this.screen) {
return;
}
// a click uses right away, holding it on blocks keeps using every USE_REPEAT_MS
// a click uses right away, holding it on blocks keeps using every USE_REPEAT_MS. not while it's using an item
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);
const use_pressed = InputManager.is_mouse_pressed(2) && !this.player.using_item;
const using = use_pressed ||
(hit && InputManager.is_mouse_down(2) && now >= this.#next_use && !this.player.using_item);
if (using) {
this.#next_use = now + USE_REPEAT_MS;
this.#use_held = true;
}
// the held item swings on every click and use, and keeps swinging while breaking, like minecraft