53 lines
2.1 KiB
TypeScript
53 lines
2.1 KiB
TypeScript
// the coilhead: it goes after players like any monster, but can't move while one of them is looking at it
|
|
import type { MobRef, Player, Position, ServerContext } from "cubi/server";
|
|
|
|
// how far away a player still freezes it
|
|
const SEEN_RANGE = 48;
|
|
// how far from where a player looks it can be and still be seen, as the cosine of the angle: 0.5 is 60 degrees each
|
|
// way, about what fits on the screen
|
|
const VIEW_COS = 0.5;
|
|
// how much faster than its movement_speed it runs at its target
|
|
const CHASE_SPEED = 1.25;
|
|
|
|
export function register_coilhead(ctx: ServerContext) {
|
|
const builtin = ctx.entities.goals;
|
|
const chase = builtin.melee_attack({ speed: CHASE_SPEED });
|
|
const stroll = builtin.random_stroll({ speed: 0.6 });
|
|
const look_around = builtin.random_look_around();
|
|
const hurt_by = builtin.hurt_by_target();
|
|
const nearest_player = builtin.nearest_attackable_player();
|
|
|
|
ctx.entities.register_behavior("dangerous_business:coilhead", (mob, goals) => {
|
|
// takes every control from the goals after it, so it doesn't walk, turn, jump or hit while seen
|
|
goals.add(0, {
|
|
flags: ["move", "look", "jump"],
|
|
can_use: () => ctx.players.all().some((player) => sees(ctx, player, mob)),
|
|
start: () => mob.stop(),
|
|
});
|
|
goals.add(1, chase);
|
|
goals.add(2, stroll);
|
|
goals.add(3, look_around);
|
|
goals.add_target(0, hurt_by);
|
|
goals.add_target(1, nearest_player);
|
|
});
|
|
}
|
|
|
|
// whether the player is looking its way with nothing in between, at its head or its middle
|
|
function sees(ctx: ServerContext, player: Player, mob: MobRef) {
|
|
if (player.health <= 0) return false;
|
|
const eye = player.eye_position;
|
|
const look = player.look_direction;
|
|
const head = mob.eye_position;
|
|
const feet = mob.position;
|
|
const middle = { x: feet.x, y: (feet.y + head.y) / 2, z: feet.z };
|
|
return [head, middle].some((point: Position) => {
|
|
const dx = point.x - eye.x;
|
|
const dy = point.y - eye.y;
|
|
const dz = point.z - eye.z;
|
|
const distance = Math.hypot(dx, dy, dz);
|
|
if (distance > SEEN_RANGE) return false;
|
|
if (distance > 0 && (dx * look.x + dy * look.y + dz * look.z) / distance < VIEW_COS) return false;
|
|
return ctx.world.raycast(eye, point) === undefined;
|
|
});
|
|
}
|