Jester and coilhead sounds

This commit is contained in:
2026-11-22 07:54:31 -03:00
parent 8e587f6446
commit da44685f54
31 changed files with 249 additions and 62 deletions
+35 -2
View File
@@ -1,7 +1,14 @@
// 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 } from "cubi/client";
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";
@@ -29,6 +36,8 @@ 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;
@@ -47,6 +56,7 @@ export function register_jester(ctx: ServerContext) {
state = next;
ticks = 0;
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
@@ -67,6 +77,7 @@ export function register_jester(ctx: ServerContext) {
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);
@@ -148,7 +159,8 @@ function nearest_player(ctx: ServerContext, mob: MobRef) {
}
// 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
// clips did with expressions is here as code. its music box plays while it cranks, and it screams while it's popped,
// for whoever is near then, even if they came after it started
export function register_jester_animator(ctx: ClientContext) {
const clip = (name: string) => ctx.render.clip(`dangerous_business:jester/${name}`);
const idle = clip("idle");
@@ -163,12 +175,24 @@ export function register_jester_animator(ctx: ClientContext) {
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") return;
const state = jester.get_client_data<number>("state");
cranking.animate_when(state === CRANKING, time);
popped.animate_when(state === AGGRO, time);
theme = sound_while(
state === CRANKING,
theme,
() => ctx.sounds.play("dangerous_business:jester/theme", { follow: jester.id }),
);
scream = sound_while(
state === AGGRO,
scream,
() => ctx.sounds.play("dangerous_business:jester/scream", { follow: jester.id, loop: true }),
);
},
pose(pose, time) {
if (cranking.started) {
@@ -200,6 +224,15 @@ export function register_jester_animator(ctx: ClientContext) {
});
}
// 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,