140 lines
6.3 KiB
TypeScript
140 lines
6.3 KiB
TypeScript
// animated textures: textures/<name>.json, carrying it through .bmod files, and playing it a tick at a time
|
||
import { assertEquals, assertThrows } from "@std/assert";
|
||
import { zipSync } from "fflate";
|
||
import { read_bmod, texture_size_problem } from "$/common/bmod.ts";
|
||
import { texture_animation, validate_texture_json } from "$/common/texture_animation.ts";
|
||
import { AnimatedTexture } from "$/client/texture_animations.ts";
|
||
import { load_and_check } from "$/tools/check_mods.ts";
|
||
import { pack_mod } from "$/tools/pack_mod.ts";
|
||
import { create_mod } from "$/tools/new_mod.ts";
|
||
import { copy_dir } from "./helpers.ts";
|
||
|
||
// a png's header and nothing else, enough for everything that only reads its size
|
||
function png_header(width: number, height: number) {
|
||
const bytes = new Uint8Array(24);
|
||
bytes.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||
new DataView(bytes.buffer).setUint32(16, width);
|
||
new DataView(bytes.buffer).setUint32(20, height);
|
||
return bytes;
|
||
}
|
||
|
||
Deno.test("an animation is cut into square frames down the png unless it says otherwise", () => {
|
||
const strip = texture_animation({ animation: { frametime: 2 } }, { width: 16, height: 48 })!;
|
||
assertEquals(strip.frame_width, 16);
|
||
assertEquals(strip.frame_height, 16);
|
||
assertEquals(strip.interpolate, false);
|
||
assertEquals(strip.frames, [{ x: 0, y: 0, time: 2 }, { x: 0, y: 16, time: 2 }, { x: 0, y: 32, time: 2 }]);
|
||
|
||
// a grid is read left to right then top to bottom, and frames can repeat with their own times
|
||
const grid = texture_animation(
|
||
{ animation: { width: 8, height: 8, frames: [3, { index: 1, time: 5 }, 3] } },
|
||
{ width: 16, height: 16 },
|
||
)!;
|
||
assertEquals(grid.frames, [{ x: 8, y: 8, time: 1 }, { x: 8, y: 0, time: 5 }, { x: 8, y: 8, time: 1 }]);
|
||
|
||
assertEquals(texture_animation({}, { width: 16, height: 16 }), undefined);
|
||
});
|
||
|
||
Deno.test("a texture's .json that doesn't fit its png says why", () => {
|
||
const size = { width: 16, height: 48 };
|
||
assertEquals(validate_texture_json({ animation: {} }, size), []);
|
||
assertEquals(validate_texture_json([], size), ['must be { "animation": { ... } }']);
|
||
assertEquals(validate_texture_json({ animation: { frametime: 0 } }, size), [
|
||
"animation.frametime must be a whole number of ticks, at least 1",
|
||
]);
|
||
assertEquals(validate_texture_json({ animation: { height: 20 } }, size), [
|
||
"the png is 16×48, which doesn't split into 16×20 frames",
|
||
]);
|
||
assertEquals(validate_texture_json({ animation: { frames: [0, 3, { index: 1 }] } }, size), [
|
||
"animation.frames[1]: the png has frames 0 to 2",
|
||
"animation.frames[2]: time must be a whole number of ticks, at least 1",
|
||
]);
|
||
});
|
||
|
||
Deno.test("an animated texture's frames have to fit the size limit, not its whole strip", () => {
|
||
const animated = { animation: {} };
|
||
assertEquals(texture_size_problem({ width: 16, height: 512 }), "is 16×512, textures can be at most 256×256");
|
||
assertEquals(texture_size_problem({ width: 16, height: 512 }, animated), undefined);
|
||
assertEquals(
|
||
texture_size_problem({ width: 512, height: 1024 }, animated),
|
||
"has 512×512 frames, textures can be at most 256×256",
|
||
);
|
||
assertEquals(
|
||
texture_size_problem({ width: 16, height: 8192 }, animated),
|
||
"is 16×8192, animated textures can be at most 4096×4096",
|
||
);
|
||
});
|
||
|
||
Deno.test("a texture's .json is checked, packed into the .bmod and read back", async () => {
|
||
const sources = Deno.makeTempDirSync({ prefix: "bworld_anim_src_" });
|
||
copy_dir("mods/bworld", `${sources}/bworld`);
|
||
create_mod("lava_lamps", "Lava Lamps", sources);
|
||
const textures = `${sources}/lava_lamps/textures`;
|
||
Deno.mkdirSync(`${textures}/lamps`, { recursive: true });
|
||
Deno.writeFileSync(`${textures}/lamps/goo.png`, png_header(16, 64));
|
||
const json = { animation: { frametime: 3, interpolate: true } };
|
||
Deno.writeTextFileSync(`${textures}/lamps/goo.json`, JSON.stringify(json));
|
||
|
||
const lamps = () => load_and_check(sources).find((m) => m.id === "lava_lamps")!;
|
||
const mod = lamps();
|
||
assertEquals(mod.report.errors, []);
|
||
const bmod = read_bmod(await pack_mod(mod), "lava_lamps.bmod");
|
||
assertEquals([...bmod.texture_json!], [["lava_lamps:lamps_goo", json]]);
|
||
|
||
// a .json without its png, or one that doesn't fit it, is an error
|
||
Deno.writeTextFileSync(`${textures}/stray.json`, JSON.stringify(json));
|
||
Deno.writeTextFileSync(
|
||
`${textures}/lamps/goo.json`,
|
||
JSON.stringify({ animation: { width: 5 } }),
|
||
);
|
||
const broken = lamps();
|
||
assertEquals(broken.report.errors, [
|
||
"textures/lamps/goo.json: the png is 16×64, which doesn't split into 5×16 frames",
|
||
"textures/stray.json: there's no stray.png for it to go with",
|
||
]);
|
||
Deno.removeSync(sources, { recursive: true });
|
||
|
||
const text = (value: unknown) => new TextEncoder().encode(JSON.stringify(value));
|
||
const manifest = { format_version: 1, id: "broken", name: "Broken", version: "1.0.0" };
|
||
assertThrows(
|
||
() =>
|
||
read_bmod(
|
||
zipSync({
|
||
"manifest.json": text(manifest),
|
||
"data.json": text({}),
|
||
"textures/goo.png": png_header(16, 32),
|
||
"textures/goo.json": text({ animation: { frames: [2] } }),
|
||
}),
|
||
"broken.bmod",
|
||
),
|
||
Error,
|
||
"textures/goo.json: animation.frames[0]: the png has frames 0 to 1",
|
||
);
|
||
});
|
||
|
||
// a 1 pixel wide strip of frames, each frame's red being its index × 100
|
||
function strip(animation: { frametime?: number; interpolate?: boolean; frames?: number[] }, count: number) {
|
||
const pixels = new Uint8ClampedArray(count * 4);
|
||
for (let i = 0; i < count; i++) pixels.set([i * 100, 0, 0, 255], i * 4);
|
||
const parsed = texture_animation({ animation }, { width: 1, height: count })!;
|
||
return new AnimatedTexture({ x: 0, y: 0, width: 1, height: 1 }, parsed, pixels, 1);
|
||
}
|
||
|
||
// what's in the atlas after each of ticks ticks, the red of it, or undefined for ticks that change nothing
|
||
function play(texture: AnimatedTexture, ticks: number) {
|
||
return Array.from({ length: ticks }, () => {
|
||
const upload = texture.tick();
|
||
return upload && upload.data[upload.offset];
|
||
});
|
||
}
|
||
|
||
Deno.test("an animation changes the atlas only on the ticks its frame changes", () => {
|
||
assertEquals(play(strip({ frametime: 2 }, 3), 7), [undefined, 100, undefined, 200, undefined, 0, undefined]);
|
||
// a frame shown twice in a row doesn't copy itself over itself
|
||
assertEquals(play(strip({ frames: [0, 0, 1] }, 2), 4), [undefined, 100, 0, undefined]);
|
||
});
|
||
|
||
Deno.test("an interpolated animation blends towards the next frame every tick", () => {
|
||
assertEquals(play(strip({ frametime: 4, interpolate: true }, 2), 8), [25, 50, 75, 100, 75, 50, 25, 0]);
|
||
});
|