// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // animated textures, like minecraft's .png.mcmeta and in the same format: textures/.json next to // textures/.png says how to cut the png into frames and how long each one shows. see docs/modding/resources/textures.md import * as z from "zod"; import { find_problems, type Problems } from "./schema.ts"; const animation_schema = z.object({ // ticks each frame shows unless the frame says otherwise, 1 if not given frametime: z.int().min(1).optional(), // blend each frame into the next a tick at a time instead of switching interpolate: z.boolean().optional(), // the order to show frames in, by index or { index, time }. every frame in order if not given frames: z.array(z.union([z.int().min(0), z.object({ index: z.int().min(0), time: z.int().min(1) })])).min(1) .optional(), // a frame's size in pixels. frames are square, as wide as the png is or as tall if that's less, if not given width: z.int().min(1).optional(), height: z.int().min(1).optional(), }); export type AnimationJson = z.infer; const texture_schema = z.object({ animation: animation_schema.optional() }, 'must be { "animation": { ... } }'); export type TextureJson = z.infer; // an animation ready to play export interface Animation { frame_width: number; frame_height: number; interpolate: boolean; frames: { x: number; y: number; time: number }[]; } // what's wrong with a texture's .json, for the png it goes with: its frames have to be in it export function validate_texture_json(json: unknown, size: { width: number; height: number }): Problems { const fits_png = texture_schema.superRefine(({ animation }, ctx) => { if (!animation) return; const { frame_width, frame_height } = frame_size(animation, size); if (size.width % frame_width !== 0 || size.height % frame_height !== 0) { return ctx.addIssue({ code: "custom", message: `the png is ${size.width}×${size.height}, which doesn't split into ${frame_width}×${frame_height} frames`, }); } const count = (size.width / frame_width) * (size.height / frame_height); animation.frames?.forEach((frame, i) => { const index = typeof frame === "number" ? frame : frame.index; if (index >= count) { const path = ["animation", "frames", i, ...(typeof frame === "number" ? [] : ["index"])]; ctx.addIssue({ code: "custom", path, message: `isn't one of the png's frames, 0 to ${count - 1}` }); } }); }); return find_problems(fits_png, json); } // the animation in a texture's .json, which validate_texture_json passed. undefined if it isn't animated export function texture_animation(json: TextureJson, size: { width: number; height: number }): Animation | undefined { const animation = json.animation; if (!animation) return undefined; const { frame_width, frame_height } = frame_size(animation, size); const columns = size.width / frame_width; const frametime = animation.frametime ?? 1; const order = animation.frames ?? Array.from({ length: columns * (size.height / frame_height) }, (_, i) => i); // frames are read from the png like text, left to right then top to bottom const frames = order.map((frame) => { const index = typeof frame === "number" ? frame : frame.index; return { x: (index % columns) * frame_width, y: Math.floor(index / columns) * frame_height, time: typeof frame === "number" ? frametime : frame.time, }; }); return { frame_width, frame_height, interpolate: animation.interpolate ?? false, frames }; } function frame_size(animation: AnimationJson, size: { width: number; height: number }) { const side = Math.min(size.width, size.height); return { frame_width: animation.width ?? side, frame_height: animation.height ?? side }; }