Files
2026-11-22 07:54:31 -03:00

112 lines
4.5 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, ScriptGoal, ServerContext } from "cubi/server";
import { AnimationState, type ClientContext } from "cubi/client";
import { DOOR, type DoorParams, set_door_open } from "./shared.ts";
// 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 = 2;
// how long it stands at a closed door on its way before opening it, in ticks
const OPEN_DOOR_TICKS = 20 * 20;
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. clients see it
// frozen, for its animator
goals.add(0, {
flags: ["move", "look", "jump"],
can_use: () => ctx.players.all().some((player) => sees(ctx, player, mob)),
start: () => {
mob.stop();
mob.set_client_data("frozen", true);
mob.play_sound("dangerous_business:coilhead/stop");
mob.play_sound("dangerous_business:coilhead/seen");
},
stop: () => mob.set_client_data("frozen", undefined),
});
goals.add(1, chase);
goals.add(2, stroll);
goals.add(3, look_around);
goals.add(4, opens_doors(ctx, mob, OPEN_DOOR_TICKS));
goals.add_target(0, hurt_by);
goals.add_target(1, nearest_player);
});
}
// bobs as it walks and stands still otherwise. when it freezes its head springs back and forth once, and it looks
// where it's looking
export function register_coilhead_animator(ctx: ClientContext) {
const idle = ctx.render.clip("dangerous_business:coilhead/idle");
const walk = ctx.render.clip("dangerous_business:coilhead/walk");
const stop = ctx.render.clip("dangerous_business:coilhead/stop");
ctx.render.register_entity_animator("dangerous_business:coilhead", (coilhead) => {
const frozen = new AnimationState();
return {
on_data(field, time) {
if (field === "frozen") frozen.animate_when(coilhead.get_client_data("frozen") === true, time);
},
pose(pose, time) {
if (frozen.started && !frozen.finished(stop, time)) {
pose.play_state(stop, frozen, time);
} else {
pose.play(idle, time / 20);
pose.play_walk(walk, coilhead, time);
}
pose.look("head");
},
};
});
}
// opens a door on the mob's way after it's stood at it for ticks. without flags, so the goal going after its target
// keeps its way and the mob stays at the door. the door's in the mobs_path_through tag, so its way goes through it
export function opens_doors(ctx: ServerContext, mob: MobRef, ticks: number): ScriptGoal {
let waited = 0;
const door = () => {
const blocked = mob.blocked_by;
return blocked && DOOR in ctx.blocks.components(blocked.id) ? blocked : undefined;
};
return {
can_use: () => door() !== undefined,
start: () => waited = 0,
tick: () => {
// a goal that started this tick, like being seen, can stop the mob's way and with it what blocks it
const blocked = door();
if (!blocked) return;
const { x, y, z, id } = blocked;
if (++waited !== ticks) return;
set_door_open(ctx, x, y, z, 1, ctx.blocks.components(id)[DOOR] as DoorParams);
},
};
}
// 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;
});
}