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) => {
|
ctx.entities.register_behavior(JESTER, (mob, goals) => {
|
||||||
let state = mob.get_client_data<number>("state") ?? ROAMING;
|
let state = mob.get_client_data<number>("state") ?? ROAMING;
|
||||||
// ticks in its state
|
// ticks in its state, going on from where one that was saved had got to
|
||||||
let ticks = 0;
|
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
|
// ticks left until it cranks, from when it first noticed someone
|
||||||
let countdown: number | undefined;
|
let countdown: number | undefined;
|
||||||
let followed: Player | undefined;
|
let followed: Player | undefined;
|
||||||
const set_state = (next: number) => {
|
const set_state = (next: number) => {
|
||||||
state = next;
|
state = next;
|
||||||
ticks = 0;
|
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);
|
mob.set_client_data("state", next);
|
||||||
if (next === AGGRO) mob.play_sound("dangerous_business:jester/pop");
|
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
|
// 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,
|
// 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
|
// 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) {
|
export function register_jester_animator(ctx: ClientContext) {
|
||||||
const clip = (name: string) => ctx.render.clip(`dangerous_business:jester/${name}`);
|
const clip = (name: string) => ctx.render.clip(`dangerous_business:jester/${name}`);
|
||||||
const idle = clip("idle");
|
const idle = clip("idle");
|
||||||
@@ -179,20 +181,19 @@ export function register_jester_animator(ctx: ClientContext) {
|
|||||||
let scream: PlayingSound | undefined;
|
let scream: PlayingSound | undefined;
|
||||||
return {
|
return {
|
||||||
on_data(field, time) {
|
on_data(field, time) {
|
||||||
if (field !== "state") return;
|
if (field !== "state" && field !== "since") return;
|
||||||
const state = jester.get_client_data<number>("state");
|
const state = jester.get_client_data<number>("state");
|
||||||
cranking.animate_when(state === CRANKING, time);
|
const since = jester.get_client_data<number>("since");
|
||||||
popped.animate_when(state === AGGRO, time);
|
// ticks it's been in its state
|
||||||
theme = sound_while(
|
const ticks = since === undefined ? 0 : Math.max(0, ctx.world.time - since);
|
||||||
state === CRANKING,
|
cranking.animate_when(state === CRANKING, time - ticks);
|
||||||
theme,
|
popped.animate_when(state === AGGRO, time - ticks);
|
||||||
() => ctx.sounds.play("dangerous_business:jester/theme", { follow: jester.id }),
|
// 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
|
||||||
scream = sound_while(
|
const play = (sound: string) =>
|
||||||
state === AGGRO,
|
ctx.sounds.play(sound, { follow: jester.id, loop: true, offset: ticks / 20 });
|
||||||
scream,
|
theme = sound_while(state === CRANKING, theme, () => play("dangerous_business:jester/theme"));
|
||||||
() => ctx.sounds.play("dangerous_business:jester/scream", { follow: jester.id, loop: true }),
|
scream = sound_while(state === AGGRO, scream, () => play("dangerous_business:jester/scream"));
|
||||||
);
|
|
||||||
},
|
},
|
||||||
pose(pose, time) {
|
pose(pose, time) {
|
||||||
if (cranking.started) {
|
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 { 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 { animation_for } from "@/client/rendering/entity_animators.ts";
|
||||||
import { sounds } from "@/client/sounds.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";
|
import type { EntityData } from "@/common/protocol.ts";
|
||||||
|
|
||||||
const HALF = "dangerous_business:concrete_wall_slab";
|
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);
|
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 () => {
|
Deno.test("a jester's crank clicks once a second while it winds, and it pops once", async () => {
|
||||||
const { t } = await setup();
|
const { t } = await setup();
|
||||||
const jester = spawn_jester(t, 2);
|
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.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_" });
|
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
|
||||||
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
|
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
|
||||||
await load_as_player(dir);
|
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();
|
const output = new ListOutput();
|
||||||
sounds.set_output(output);
|
sounds.set_output(output);
|
||||||
const jester = {
|
const jester = {
|
||||||
id: "e1",
|
id: "e1",
|
||||||
type: JESTER,
|
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,
|
age: 0,
|
||||||
yaw: 0,
|
yaw: 0,
|
||||||
head_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 set_state = (state: number) => {
|
||||||
const before = jester.data;
|
const before = jester.data;
|
||||||
jester.data = { client_data: { state } };
|
jester.data = { client_data: { state, since: level.time } };
|
||||||
animation.data_changed(before);
|
animation.data_changed(before);
|
||||||
frame();
|
frame();
|
||||||
};
|
};
|
||||||
// a player coming near while it already winds hears it too
|
|
||||||
const animation = animation_for(jester)!;
|
const animation = animation_for(jester)!;
|
||||||
frame();
|
frame();
|
||||||
const [theme] = output.playing;
|
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);
|
set_state(3);
|
||||||
assert(theme.stopped);
|
assert(theme.stopped);
|
||||||
const [scream] = output.playing;
|
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);
|
set_state(1);
|
||||||
assertEquals(output.playing, []);
|
assertEquals(output.playing, []);
|
||||||
sounds.stop_all();
|
sounds.stop_all();
|
||||||
|
set_mods_client(undefined as unknown as Client);
|
||||||
Deno.removeSync(dir, { recursive: true });
|
Deno.removeSync(dir, { recursive: true });
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user