Track time on jester windup
This commit is contained in:
@@ -47,14 +47,16 @@ export function register_jester(ctx: ServerContext) {
|
||||
|
||||
ctx.entities.register_behavior(JESTER, (mob, goals) => {
|
||||
let state = mob.get_client_data<number>("state") ?? ROAMING;
|
||||
// ticks in its state
|
||||
let ticks = 0;
|
||||
// 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");
|
||||
};
|
||||
@@ -159,8 +161,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. 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
|
||||
// clips did with expressions is here as code. its music box plays while it cranks, and it screams 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");
|
||||
@@ -179,20 +181,19 @@ export function register_jester_animator(ctx: ClientContext) {
|
||||
let scream: PlayingSound | undefined;
|
||||
return {
|
||||
on_data(field, time) {
|
||||
if (field !== "state") return;
|
||||
if (field !== "state" && field !== "since") 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 }),
|
||||
);
|
||||
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"));
|
||||
},
|
||||
pose(pose, time) {
|
||||
if (cranking.started) {
|
||||
|
||||
@@ -12,6 +12,8 @@ import type { Mob } from "@/server/game/entity/mob.ts";
|
||||
import { copy_dir, errors_during, ListOutput, load_as_player, test_game, test_mob, VOLUMES } from "./helpers.ts";
|
||||
import { animation_for } from "@/client/rendering/entity_animators.ts";
|
||||
import { sounds } from "@/client/sounds.ts";
|
||||
import { set_mods_client } from "@/client/mods.ts";
|
||||
import type { Client } from "@/client/client.ts";
|
||||
import type { EntityData } from "@/common/protocol.ts";
|
||||
|
||||
const HALF = "dangerous_business:concrete_wall_slab";
|
||||
@@ -342,6 +344,17 @@ Deno.test("a popped jester runs down the nearest player faster and faster, and b
|
||||
assertEquals(speed(), walking);
|
||||
});
|
||||
|
||||
Deno.test("a jester saved while it winds goes on from where it got to", async () => {
|
||||
const { t } = await setup();
|
||||
const jester = spawn_jester(t, 2);
|
||||
// 39.5 seconds in
|
||||
jester.client_data = { state: 2, since: t.game.world.time - 790 };
|
||||
for (let i = 0; i < 5; i++) t.game.tick();
|
||||
assertEquals(jester.client_data.state, 2);
|
||||
for (let i = 0; i < 15; i++) t.game.tick();
|
||||
assertEquals(jester.client_data.state, 3);
|
||||
});
|
||||
|
||||
Deno.test("a jester's crank clicks once a second while it winds, and it pops once", async () => {
|
||||
const { t } = await setup();
|
||||
const jester = spawn_jester(t, 2);
|
||||
@@ -404,16 +417,20 @@ Deno.test("a jester keeps its tendrils in the box until it pops, and turns its c
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("a jester's music box plays while it winds, and it screams while it's popped", async () => {
|
||||
Deno.test("a jester's music box and scream play from as far as it's got, for a player who comes upon it partway", async () => {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
|
||||
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
|
||||
await load_as_player(dir);
|
||||
// the world's time on this client, in ticks
|
||||
const level = { time: 10_000 };
|
||||
set_mods_client({ level } as unknown as Client);
|
||||
const output = new ListOutput();
|
||||
sounds.set_output(output);
|
||||
const jester = {
|
||||
id: "e1",
|
||||
type: JESTER,
|
||||
data: { client_data: { state: 2 } } as EntityData,
|
||||
// winding for 30 seconds already
|
||||
data: { client_data: { state: 2, since: 10_000 - 600 } } as EntityData,
|
||||
age: 0,
|
||||
yaw: 0,
|
||||
head_yaw: 0,
|
||||
@@ -429,23 +446,29 @@ Deno.test("a jester's music box plays while it winds, and it screams while it's
|
||||
);
|
||||
const set_state = (state: number) => {
|
||||
const before = jester.data;
|
||||
jester.data = { client_data: { state } };
|
||||
jester.data = { client_data: { state, since: level.time } };
|
||||
animation.data_changed(before);
|
||||
frame();
|
||||
};
|
||||
// a player coming near while it already winds hears it too
|
||||
const animation = animation_for(jester)!;
|
||||
frame();
|
||||
const [theme] = output.playing;
|
||||
assertEquals([theme.file.id, theme.settings.loop], ["dangerous_business:jester/theme", false]);
|
||||
assertEquals(theme.file.id, "dangerous_business:jester/theme");
|
||||
assert(theme.settings.loop);
|
||||
// and a moment of the real clock since it was played
|
||||
assertAlmostEquals(theme.settings.offset, 30, 0.1);
|
||||
|
||||
level.time += 200;
|
||||
set_state(3);
|
||||
assert(theme.stopped);
|
||||
const [scream] = output.playing;
|
||||
assertEquals([scream.file.id, scream.settings.loop], ["dangerous_business:jester/scream", true]);
|
||||
assertEquals(scream.file.id, "dangerous_business:jester/scream");
|
||||
assert(scream.settings.loop);
|
||||
assertAlmostEquals(scream.settings.offset, 0, 0.1);
|
||||
|
||||
set_state(1);
|
||||
assertEquals(output.playing, []);
|
||||
sounds.stop_all();
|
||||
set_mods_client(undefined as unknown as Client);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user