Allow mobs to be hit
This commit is contained in:
@@ -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