Animated textures !
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
// animated textures, like minecraft's .png.mcmeta and in the same format: textures/<name>.json next to
|
||||
// textures/<name>.png says how to cut the png into frames and how long each one shows. see docs/modding/resources/textures.md
|
||||
export interface TextureJson {
|
||||
animation?: AnimationJson;
|
||||
}
|
||||
|
||||
export interface AnimationJson {
|
||||
// ticks each frame shows unless the frame says otherwise, 1 if not given
|
||||
frametime?: number;
|
||||
// blend each frame into the next a tick at a time instead of switching
|
||||
interpolate?: boolean;
|
||||
// the order to show frames in, by index or { index, time }. every frame in order if not given
|
||||
frames?: (number | { index: number; time: number })[];
|
||||
// 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?: number;
|
||||
height?: number;
|
||||
}
|
||||
|
||||
// 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
|
||||
export function validate_texture_json(json: unknown, size: { width: number; height: number }): string[] {
|
||||
if (!is_object(json)) return [`must be { "animation": { ... } }`];
|
||||
if (json.animation === undefined) return [];
|
||||
const animation = json.animation;
|
||||
if (!is_object(animation)) return ["animation must be an object"];
|
||||
|
||||
const problems: string[] = [];
|
||||
const ticks = (value: unknown) => Number.isInteger(value) && (value as number) >= 1;
|
||||
if (animation.frametime !== undefined && !ticks(animation.frametime)) {
|
||||
problems.push("animation.frametime must be a whole number of ticks, at least 1");
|
||||
}
|
||||
if (animation.interpolate !== undefined && typeof animation.interpolate !== "boolean") {
|
||||
problems.push("animation.interpolate must be true or false");
|
||||
}
|
||||
for (const key of ["width", "height"] as const) {
|
||||
if (animation[key] !== undefined && !ticks(animation[key])) {
|
||||
problems.push(`animation.${key} must be a whole number of pixels, at least 1`);
|
||||
}
|
||||
}
|
||||
if (problems.length > 0) return problems;
|
||||
|
||||
const { frame_width, frame_height } = frame_size(animation as AnimationJson, size);
|
||||
if (size.width % frame_width !== 0 || size.height % frame_height !== 0) {
|
||||
return [
|
||||
`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);
|
||||
|
||||
if (animation.frames !== undefined) {
|
||||
if (!Array.isArray(animation.frames) || animation.frames.length === 0) {
|
||||
problems.push("animation.frames must be a list of frames");
|
||||
} else {
|
||||
animation.frames.forEach((frame, i) => {
|
||||
const index = is_object(frame) ? frame.index : frame;
|
||||
if (!Number.isInteger(index) || index < 0 || index >= count) {
|
||||
problems.push(`animation.frames[${i}]: the png has frames 0 to ${count - 1}`);
|
||||
}
|
||||
if (is_object(frame) && !ticks(frame.time)) {
|
||||
problems.push(`animation.frames[${i}]: time must be a whole number of ticks, at least 1`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
|
||||
// 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 };
|
||||
}
|
||||
|
||||
function is_object(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
Reference in New Issue
Block a user