273 lines
11 KiB
TypeScript
273 lines
11 KiB
TypeScript
// the jester: a box on legs that wanders until it sees a player. it follows them for a while, stops to wind its crank,
|
|
// and pops open, then runs down every player in the facility, faster and faster, until there's nobody left inside
|
|
import type { MobRef, Player, ScriptGoal, ServerContext } from "cubi/server";
|
|
import {
|
|
type AnimatedEntity,
|
|
AnimationState,
|
|
type ClientContext,
|
|
type Clip,
|
|
type EntityPose,
|
|
type PlayingSound,
|
|
} from "cubi/client";
|
|
import { opens_doors } from "./coilhead.ts";
|
|
import { in_facility } from "./interior.ts";
|
|
|
|
const JESTER = "dangerous_business:jester";
|
|
|
|
// its "state" client data, what its animator plays
|
|
const ROAMING = 1;
|
|
const CRANKING = 2;
|
|
const AGGRO = 3;
|
|
|
|
// how far away it notices a player it can see
|
|
const SEE_RANGE = 24;
|
|
// how close it comes to the player it's following
|
|
const FOLLOW_DISTANCE = 3;
|
|
// seconds after it first notices someone before it starts cranking, shorter with more players around
|
|
const COUNTDOWN_ALONE = [25, 42];
|
|
const COUNTDOWN_TOGETHER = [13, 21];
|
|
const CRANK_TICKS = 40 * 20;
|
|
// how long it stands after popping before it goes
|
|
const POP_TICKS = 2 * 20;
|
|
// it chases a fifth faster than it walks, and another fifth every SPEED_UP_TICKS, up to twice as fast
|
|
const SPEED_STEP = 0.2;
|
|
const SPEED_UP_TICKS = 120;
|
|
const MAX_SPEED_STEPS = 8;
|
|
const CHASE_SPEED = "dangerous_business:jester_chase";
|
|
// it only waits a moment at a closed door
|
|
const OPEN_DOOR_TICKS = 20;
|
|
// the crank turns once a second while it cranks, clicking this many ticks into each, where its crank clip has it
|
|
const CRANK_CLICK_TICK = 12;
|
|
|
|
export function register_jester(ctx: ServerContext) {
|
|
const builtin = ctx.entities.goals;
|
|
const chase = builtin.melee_attack();
|
|
const stroll = builtin.random_stroll();
|
|
const look_around = builtin.random_look_around();
|
|
|
|
ctx.entities.register_behavior(JESTER, (mob, goals) => {
|
|
let state = mob.get_client_data<number>("state") ?? ROAMING;
|
|
// ticks in its state, going on from where one that was saved had got to
|
|
let ticks = Math.max(0, ctx.world.time - (mob.get_client_data<number>("since") ?? ctx.world.time));
|
|
// ticks left until it cranks, from when it first noticed someone
|
|
let countdown: number | undefined;
|
|
let followed: Player | undefined;
|
|
const set_state = (next: number) => {
|
|
state = next;
|
|
ticks = 0;
|
|
// when it started, by the world's time, so a client that comes upon it partway knows how far it's got
|
|
mob.set_client_data("since", ctx.world.time);
|
|
mob.set_client_data("state", next);
|
|
if (next === AGGRO) mob.play_sound("dangerous_business:jester/pop");
|
|
};
|
|
|
|
// what it's doing, which the goals after it follow
|
|
goals.add(0, {
|
|
can_use: () => true,
|
|
tick: () => {
|
|
ticks++;
|
|
if (state === ROAMING) {
|
|
if (followed && (followed.health <= 0 || !in_facility(followed.position))) followed = undefined;
|
|
followed ??= ctx.players.all().find((player) => can_see(ctx, mob, player));
|
|
if (followed && countdown === undefined) {
|
|
const [low, high] = ctx.players.all().length > 1 ? COUNTDOWN_TOGETHER : COUNTDOWN_ALONE;
|
|
countdown = Math.round((low + (high - low) * mob.random()) * 20);
|
|
}
|
|
if (countdown !== undefined && --countdown <= 0) {
|
|
countdown = undefined;
|
|
followed = undefined;
|
|
set_state(CRANKING);
|
|
}
|
|
} else if (state === CRANKING) {
|
|
if (ticks % 20 === CRANK_CLICK_TICK) mob.play_sound("dangerous_business:jester/turn_crank");
|
|
if (ticks >= CRANK_TICKS) set_state(AGGRO);
|
|
} else {
|
|
const target = nearest_player(ctx, mob);
|
|
if (!target) {
|
|
mob.target = undefined;
|
|
mob.remove_modifier("cubi:movement_speed", CHASE_SPEED);
|
|
set_state(ROAMING);
|
|
return;
|
|
}
|
|
if (ticks < POP_TICKS) return;
|
|
mob.target = target;
|
|
const steps = Math.min(MAX_SPEED_STEPS, 1 + Math.floor((ticks - POP_TICKS) / SPEED_UP_TICKS));
|
|
mob.add_modifier("cubi:movement_speed", {
|
|
id: CHASE_SPEED,
|
|
amount: steps * SPEED_STEP,
|
|
operation: "add_multiplied_total",
|
|
});
|
|
}
|
|
},
|
|
});
|
|
// stands still while it cranks and while it pops
|
|
goals.add(1, {
|
|
flags: ["move", "look", "jump"],
|
|
can_use: () => state === CRANKING || (state === AGGRO && ticks < POP_TICKS),
|
|
start: () => mob.stop(),
|
|
});
|
|
goals.add(2, chase);
|
|
goals.add(3, follow(mob, () => state === ROAMING ? followed : undefined));
|
|
goals.add(4, stroll);
|
|
goals.add(5, look_around);
|
|
goals.add(6, opens_doors(ctx, mob, OPEN_DOOR_TICKS));
|
|
});
|
|
}
|
|
|
|
// keeps close to who(), looking at them, and stands by them once it's there instead of wandering off. it goes a new
|
|
// way only once they've moved to another block
|
|
function follow(mob: MobRef, who: () => Player | undefined): ScriptGoal {
|
|
let block = "";
|
|
return {
|
|
flags: ["move", "look"],
|
|
can_use: () => who() !== undefined,
|
|
start: () => block = "",
|
|
stop: () => mob.stop(),
|
|
tick: () => {
|
|
// the goal deciding what the jester does can let them go this tick
|
|
const player = who();
|
|
if (!player) return;
|
|
const { x, y, z } = player.position;
|
|
mob.look_at(x, y + 1.5, z);
|
|
const at = mob.position;
|
|
if (Math.hypot(x - at.x, y - at.y, z - at.z) <= FOLLOW_DISTANCE) {
|
|
if (block !== "") mob.stop();
|
|
block = "";
|
|
return;
|
|
}
|
|
const now = `${Math.floor(x)},${Math.floor(y)},${Math.floor(z)}`;
|
|
if (now === block) return;
|
|
block = now;
|
|
mob.move_to(x, y, z);
|
|
},
|
|
};
|
|
}
|
|
|
|
// whether it can see the player's eyes from its own, close enough to notice them, in the facility
|
|
function can_see(ctx: ServerContext, mob: MobRef, player: Player) {
|
|
if (player.health <= 0 || !in_facility(player.position)) return false;
|
|
const from = mob.eye_position;
|
|
const to = player.eye_position;
|
|
if (Math.hypot(to.x - from.x, to.y - from.y, to.z - from.z) > SEE_RANGE) return false;
|
|
return ctx.world.raycast(from, to) === undefined;
|
|
}
|
|
|
|
// the closest living player in the facility, however far
|
|
function nearest_player(ctx: ServerContext, mob: MobRef) {
|
|
const at = mob.position;
|
|
const distance = ({ position: { x, y, z } }: Player) => Math.hypot(x - at.x, y - at.y, z - at.z);
|
|
const inside = ctx.players.all().filter((player) => player.health > 0 && in_facility(player.position));
|
|
return inside.sort((a, b) => distance(a) - distance(b))[0] as Player | undefined;
|
|
}
|
|
|
|
// what bedrock's animation controller did: idle and walk while it roams, crank, then pop, then its chase. what its
|
|
// clips did with expressions is here as code. its music box plays while it cranks, and it screams and stomps while
|
|
// it's popped. a player who comes upon it partway sees and hears it from as far as it's got, by when its state started
|
|
export function register_jester_animator(ctx: ClientContext) {
|
|
const clip = (name: string) => ctx.render.clip(`dangerous_business:jester/${name}`);
|
|
const idle = clip("idle");
|
|
const walk = clip("walk");
|
|
const crank = clip("crank");
|
|
const pop = clip("pop");
|
|
const aggro_idle = clip("aggro_idle");
|
|
const aggro_walk = clip("aggro_walk");
|
|
const attack = clip("attack");
|
|
|
|
ctx.render.register_entity_animator(JESTER, (jester) => {
|
|
const cranking = new AnimationState();
|
|
const popped = new AnimationState();
|
|
const attacking = new AnimationState();
|
|
let theme: PlayingSound | undefined;
|
|
let scream: PlayingSound | undefined;
|
|
return {
|
|
on_data(field, time) {
|
|
if (field !== "state" && field !== "since") return;
|
|
const state = jester.get_client_data<number>("state");
|
|
const since = jester.get_client_data<number>("since");
|
|
// ticks it's been in its state
|
|
const ticks = since === undefined ? 0 : Math.max(0, ctx.world.time - since);
|
|
cranking.animate_when(state === CRANKING, time - ticks);
|
|
popped.animate_when(state === AGGRO, time - ticks);
|
|
// loops, so one played out of range comes in where it would be when the player comes near. the music box
|
|
// is longer than the cranking, so it never goes around
|
|
const play = (sound: string) =>
|
|
ctx.sounds.play(sound, { follow: jester.id, loop: true, offset: ticks / 20 });
|
|
theme = sound_while(state === CRANKING, theme, () => play("dangerous_business:jester/theme"));
|
|
scream = sound_while(state === AGGRO, scream, () => play("dangerous_business:jester/scream"));
|
|
},
|
|
step_sound: () =>
|
|
jester.get_client_data<number>("state") === AGGRO ? "dangerous_business:jester/stomp" : undefined,
|
|
pose(pose, time) {
|
|
if (cranking.started) {
|
|
pose.play_state(crank, cranking, time);
|
|
} else if (popped.started && !popped.finished(pop, time)) {
|
|
pose.play_state(pop, popped, time);
|
|
} else if (popped.started) {
|
|
const swinging = jester.attack_progress(time) > 0;
|
|
if (swinging && (!attacking.started || attacking.finished(attack, time))) attacking.start(time);
|
|
if (attacking.started && !attacking.finished(attack, time)) {
|
|
pose.play_state(attack, attacking, time);
|
|
} else {
|
|
// its walk goes half as fast as when it roams
|
|
walking(pose, jester, time, aggro_walk, 40, (weight) => {
|
|
pose.play(aggro_idle, time / 20, weight);
|
|
sway(pose, time, weight, true);
|
|
});
|
|
}
|
|
} else {
|
|
walking(pose, jester, time, walk, 20, (weight) => {
|
|
pose.play(idle, time / 20, weight);
|
|
sway(pose, time, weight, false);
|
|
});
|
|
// both clips hide them, which blended halfway would only shrink them
|
|
pose.scale("tendrils", 0, 0, 0);
|
|
}
|
|
},
|
|
};
|
|
});
|
|
}
|
|
|
|
// a sound that goes with a state: played once it holds, stopped once it doesn't
|
|
function sound_while(holds: boolean, sound: PlayingSound | undefined, play: () => PlayingSound) {
|
|
if (!holds) {
|
|
sound?.stop();
|
|
return undefined;
|
|
}
|
|
return sound ?? play();
|
|
}
|
|
|
|
// clip as it walks, a second of it for ticks of walk_position, and standing as strongly as it isn't walking
|
|
function walking(
|
|
pose: EntityPose,
|
|
jester: AnimatedEntity,
|
|
time: number,
|
|
clip: Clip,
|
|
ticks: number,
|
|
standing: (weight: number) => void,
|
|
) {
|
|
const speed = Math.min(jester.walk_speed(time), 1);
|
|
pose.play(clip, jester.walk_position(time) / ticks, speed);
|
|
standing(1 - speed);
|
|
}
|
|
|
|
// the box bobbing and the arm swaying as it stands, and once it's popped the lid, skull, jaw and crank too
|
|
function sway(pose: EntityPose, time: number, weight: number, popped: boolean) {
|
|
// bedrock's math.cos and math.sin of life_time (seconds) times degrees a second
|
|
const seconds = time / 20;
|
|
const cos = (degrees_a_second: number) => Math.cos(seconds * degrees_a_second * Math.PI / 180) * weight;
|
|
const sin = (degrees_a_second: number) => Math.sin(seconds * degrees_a_second * Math.PI / 180) * weight;
|
|
pose.rotate("box", cos(180) * 2.5, 0, 0);
|
|
pose.rotate("left_arm", (-1.6248 * weight) + cos(90) * 5, -22.22485 * weight, 70.78761 * weight);
|
|
pose.rotate("left_forearm", cos(180) * 5, 62.5 * weight, 0);
|
|
if (!popped) {
|
|
pose.move("box", 0, -weight + cos(180), 0);
|
|
pose.move("left_arm", 0, 3 * weight - cos(180), 0);
|
|
return;
|
|
}
|
|
pose.rotate("tendrils", sin(180) * 2.5, 0, 0);
|
|
pose.rotate("box_lid", -127.5 * weight + sin(180) * 5, 0, 0);
|
|
pose.rotate("skull", cos(180) * -2.5, 0, 0);
|
|
pose.rotate("jaw", 10 * weight + sin(180) * 5, 0, 0);
|
|
pose.rotate("crank", sin(180) * 2.5, 0, 0);
|
|
}
|