Allow mobs to be hit
This commit is contained in:
+43
-1
@@ -18,6 +18,13 @@ import { Connection } from "./network.ts";
|
||||
import { PauseScreen } from "./gui/pause_screen.ts";
|
||||
import { back_to_title } from "./gui/title_screen.ts";
|
||||
import { TICK_DELTA } from "$/common/constants.ts";
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { EntityJson } from "$/common/mod_data.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;
|
||||
|
||||
// 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;
|
||||
@@ -39,6 +46,8 @@ export class Client {
|
||||
screens: GuiScreen[] = [];
|
||||
// what the player is looking at, updated every frame
|
||||
hit_result: BlockHitResult | undefined;
|
||||
// the entity being looked at, closer than any block. hit_result is undefined then
|
||||
entity_hit: RemoteEntity | undefined;
|
||||
debugging = false;
|
||||
|
||||
// seconds of game time not ticked yet
|
||||
@@ -169,7 +178,7 @@ export class Client {
|
||||
}
|
||||
InputManager.set_mouse_grabbed(!this.screen);
|
||||
|
||||
this.hit_result = this.level.pick(player.x, player.y + player.eye_height, player.z, player.yaw, player.pitch);
|
||||
this.#pick();
|
||||
this.#handle_block_interaction();
|
||||
|
||||
if (!this.screen && !closed_screen) {
|
||||
@@ -235,6 +244,31 @@ export class Client {
|
||||
}
|
||||
}
|
||||
|
||||
// what's under the crosshair: an entity, or the first block in front of it
|
||||
#pick() {
|
||||
const player = this.player;
|
||||
const eye_y = player.y + player.eye_height;
|
||||
const picked = this.level.pick_entity(
|
||||
player.x,
|
||||
eye_y,
|
||||
player.z,
|
||||
player.yaw,
|
||||
player.pitch,
|
||||
ENTITY_REACH,
|
||||
(entity) => entity instanceof RemoteEntity && !entity.dead && is_hittable(entity),
|
||||
);
|
||||
// a block in front of the entity hides it
|
||||
this.hit_result = this.level.pick(
|
||||
player.x,
|
||||
eye_y,
|
||||
player.z,
|
||||
player.yaw,
|
||||
player.pitch,
|
||||
picked?.distance ?? BLOCK_REACH,
|
||||
);
|
||||
this.entity_hit = picked && !this.hit_result ? picked.entity as RemoteEntity : undefined;
|
||||
}
|
||||
|
||||
// clicks happen right away, holding to break advances in tick()
|
||||
#handle_block_interaction() {
|
||||
const hit = this.hit_result;
|
||||
@@ -253,6 +287,9 @@ export class Client {
|
||||
this.game_renderer.hand.swing();
|
||||
}
|
||||
|
||||
if (this.entity_hit && InputManager.is_mouse_pressed(0)) {
|
||||
this.game_mode.attack(this.entity_hit);
|
||||
}
|
||||
if (hit) {
|
||||
if (InputManager.is_mouse_pressed(0)) {
|
||||
this.game_mode.start_destroy_block(hit);
|
||||
@@ -286,3 +323,8 @@ export class Client {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// mobs can be hit, scripted entities like the fishing hook can't. kinds the client doesn't know are left to the server
|
||||
function is_hittable(entity: RemoteEntity) {
|
||||
return EverythingRegistry.get<EntityJson>("entities", entity.type)?.behavior !== "scripted";
|
||||
}
|
||||
|
||||
@@ -95,6 +95,12 @@ export class MultiPlayerGameMode {
|
||||
container.set_item(index, item.amount > 0 ? item : undefined);
|
||||
}
|
||||
|
||||
// left click on an entity, the server decides whether it's hurt
|
||||
attack(entity: RemoteEntity) {
|
||||
this.client.player.send_position_now();
|
||||
this.client.connection.send({ type: "attack_entity", id: entity.id });
|
||||
}
|
||||
|
||||
// right click on nothing
|
||||
use_item() {
|
||||
this.client.connection.send({ type: "use_item" });
|
||||
|
||||
@@ -547,6 +547,47 @@ export class ClientLevel {
|
||||
chunk.translucent_sort = undefined;
|
||||
}
|
||||
|
||||
// the closest entity whose box is along the view of something at x/y/z looking at yaw/pitch, within max_distance.
|
||||
// only the ones filter allows
|
||||
pick_entity(
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
yaw: number,
|
||||
pitch: number,
|
||||
max_distance: number,
|
||||
filter: (entity: Entity) => boolean,
|
||||
): { entity: Entity; distance: number } | undefined {
|
||||
const cos_pitch = Math.cos(pitch);
|
||||
const direction = [-Math.sin(yaw) * cos_pitch, Math.sin(pitch), -Math.cos(yaw) * cos_pitch];
|
||||
const origin = [x, y, z];
|
||||
|
||||
let closest: { entity: Entity; distance: number } | undefined;
|
||||
for (const entity of this.entities.values()) {
|
||||
if (!filter(entity)) continue;
|
||||
const half = entity.width / 2;
|
||||
const min = [entity.x - half, entity.y, entity.z - half];
|
||||
const max = [entity.x + half, entity.y + entity.height, entity.z + half];
|
||||
// where the ray enters and leaves the box, one axis at a time
|
||||
let enter = 0;
|
||||
let leave = closest?.distance ?? max_distance;
|
||||
for (let axis = 0; axis < 3 && enter <= leave; axis++) {
|
||||
if (Math.abs(direction[axis]) < 1e-9) {
|
||||
if (origin[axis] < min[axis] || origin[axis] > max[axis]) enter = Infinity;
|
||||
continue;
|
||||
}
|
||||
const a = (min[axis] - origin[axis]) / direction[axis];
|
||||
const b = (max[axis] - origin[axis]) / direction[axis];
|
||||
enter = Math.max(enter, Math.min(a, b));
|
||||
leave = Math.min(leave, Math.max(a, b));
|
||||
}
|
||||
if (enter <= leave) {
|
||||
closest = { entity, distance: enter };
|
||||
}
|
||||
}
|
||||
return closest;
|
||||
}
|
||||
|
||||
// the first block along the view of something at x/y/z looking at yaw/pitch, like minecraft's pick
|
||||
pick(
|
||||
x: number,
|
||||
|
||||
Reference in New Issue
Block a user