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,
|
||||
|
||||
@@ -87,6 +87,8 @@ export type ClientMessage =
|
||||
| { type: "break_block"; x: number; y: number; z: number }
|
||||
// the player started hitting a block, for on_click
|
||||
| { type: "hit_block"; x: number; y: number; z: number }
|
||||
// left click on an entity, by id
|
||||
| { type: "attack_entity"; id: string }
|
||||
// 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`
|
||||
|
||||
@@ -41,6 +41,9 @@ and players. Mods define new kinds in their `entities/` folder, and draw them wi
|
||||
**Animals** (the default) are mobs: they wander around, panic when hurt, look at players now and then, swim up in water,
|
||||
and never despawn. They're saved with the world, and players can't place solid blocks where one is standing.
|
||||
|
||||
Players hit animals by left clicking them from up to 3 blocks away, for 1 physical damage (half a heart) whatever they
|
||||
hold. The hit pushes them away, and like any damage it can't hurt them again for half a second unless it's harder.
|
||||
|
||||
**Scripted** entities stay exactly where a script puts them. They don't fall, think or get hurt, and aren't saved with
|
||||
the world, so a script never finds one it doesn't know about after a restart. The fishing hook is one:
|
||||
|
||||
|
||||
@@ -72,12 +72,15 @@ import {
|
||||
type SavedItemEntity,
|
||||
} from "./entity/item_entity.ts";
|
||||
import { Mob } from "./entity/mob.ts";
|
||||
import { LivingEntity } from "./entity/living_entity.ts";
|
||||
import { EntityManager } from "./entity/entity_manager.ts";
|
||||
import { create_entity, entity_type_ids } from "./entity/entity_types.ts";
|
||||
import type { SavedEntity } from "./entity/entity.ts";
|
||||
|
||||
// how far from a player's eyes a block can be changed, a bit more than the client's reach
|
||||
const MAX_REACH = 8;
|
||||
// how hard a player hits with their fist, whatever they're holding. half a heart, like minecraft
|
||||
const PLAYER_ATTACK_DAMAGE = 1;
|
||||
// how far past a player's box items get picked up from, sideways and up or down, like minecraft
|
||||
const PICKUP_REACH_XZ = 1;
|
||||
const PICKUP_REACH_Y = 0.5;
|
||||
@@ -733,6 +736,11 @@ export class GameServer {
|
||||
this.#hit_block(player, message.x, message.y, message.z);
|
||||
}
|
||||
break;
|
||||
case "attack_entity":
|
||||
if (typeof message.id === "string") {
|
||||
this.#attack_entity(player, message.id);
|
||||
}
|
||||
break;
|
||||
case "use_item":
|
||||
this.#use_item(player);
|
||||
break;
|
||||
@@ -805,6 +813,18 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
|
||||
// left click on an entity: hurts it and pushes it away. it can't be hurt again for a moment, see LivingEntity.hurt
|
||||
#attack_entity(player: ServerPlayer, id: string) {
|
||||
const entity = this.entities.get_entity(id);
|
||||
if (!(entity instanceof LivingEntity) || !entity.alive || !this.#entity_in_reach(player, entity)) {
|
||||
return;
|
||||
}
|
||||
const source = { type: "physical", cause: "player_attack", attacker: player, by_player: true } as const;
|
||||
if (entity.hurt(PLAYER_ATTACK_DAMAGE, source)) {
|
||||
player.vitals.add_exhaustion(EXHAUSTION.attack);
|
||||
}
|
||||
}
|
||||
|
||||
// right click at nothing: eating, or the item's on_use
|
||||
#use_item(player: ServerPlayer): boolean {
|
||||
return this.#eat(player) || this.#use_item_components(player);
|
||||
@@ -1585,6 +1605,16 @@ export class GameServer {
|
||||
return dx * dx + dy * dy + dz * dz <= MAX_REACH * MAX_REACH;
|
||||
}
|
||||
|
||||
// from the eyes to the closest point of its box
|
||||
#entity_in_reach(player: ServerPlayer, entity: LivingEntity) {
|
||||
const eye_y = player.y + PLAYER_EYE_HEIGHT;
|
||||
const half = entity.width / 2;
|
||||
const dx = Math.max(entity.x - half - player.x, 0, player.x - entity.x - half);
|
||||
const dy = Math.max(entity.y - eye_y, 0, eye_y - entity.y - entity.height);
|
||||
const dz = Math.max(entity.z - half - player.z, 0, player.z - entity.z - half);
|
||||
return dx * dx + dy * dy + dz * dz <= MAX_REACH * MAX_REACH;
|
||||
}
|
||||
|
||||
#near_any_player(x: number, z: number) {
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
|
||||
@@ -24,6 +24,7 @@ export const EXHAUSTION = {
|
||||
jump: 0.05,
|
||||
sprint_jump: 0.2,
|
||||
break_block: 0.005,
|
||||
attack: 0.1,
|
||||
damage: 0.1,
|
||||
heal_per_health: 6,
|
||||
};
|
||||
|
||||
@@ -144,6 +144,23 @@ Deno.test("hits push away from the attacker", async () => {
|
||||
assert(mob.vz > 0 && mob.vy > 0, "knocked back, away from the attacker at a smaller z");
|
||||
});
|
||||
|
||||
Deno.test("players hit entities they can reach, pushing them away", async () => {
|
||||
const { spawn, ticks, send } = await floor_game();
|
||||
const mob = spawn(0.5, 100, 2.5);
|
||||
const far = spawn(0.5, 100, 20.5);
|
||||
far.tags.add("stays");
|
||||
ticks(2);
|
||||
|
||||
send(1, { type: "attack_entity", id: mob.id });
|
||||
assertEquals(mob.health, 9);
|
||||
assert(mob.vz > 0, "knocked back, away from alice");
|
||||
assert(mob.last_hurt_by_player !== undefined);
|
||||
|
||||
send(1, { type: "attack_entity", id: far.id });
|
||||
assertEquals(far.health, 10, "too far away");
|
||||
send(1, { type: "attack_entity", id: "nothing" });
|
||||
});
|
||||
|
||||
Deno.test("dying: it drops what it had, falls over for a second and is gone", async () => {
|
||||
const { game, spawn, take, ticks } = await floor_game();
|
||||
const mob = spawn(0.5, 100, 2.5);
|
||||
|
||||
Reference in New Issue
Block a user