Animated textures !
This commit is contained in:
+54
-11
@@ -2,7 +2,7 @@
|
||||
// docs/modding/gettingstarted/modfiles.md. the same code reads them on the server and in the browser, so it only works on bytes
|
||||
import { unzipSync, zipSync } from "fflate";
|
||||
import type { ModelJson } from "./block_models.ts";
|
||||
import { MAX_TEXTURE_SIZE } from "./constants.ts";
|
||||
import { MAX_ANIMATED_TEXTURE_SIZE, MAX_TEXTURE_SIZE } from "./constants.ts";
|
||||
import {
|
||||
type BlockJson,
|
||||
type EffectJson,
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
validate_tag,
|
||||
} from "./mod_data.ts";
|
||||
import type { ModData } from "./mod_loader.ts";
|
||||
import { texture_animation, type TextureJson, validate_texture_json } from "./texture_animation.ts";
|
||||
import { ENGINE_GLOBAL } from "./hooks.ts";
|
||||
|
||||
export const BMOD_EXTENSION = ".bmod";
|
||||
@@ -48,6 +49,8 @@ export interface Bmod {
|
||||
scripts: Partial<Record<ScriptSide, string>>;
|
||||
// png bytes by texture id, like copper_tools:copper_block
|
||||
textures: Map<string, Uint8Array>;
|
||||
// the .json of the textures that have one, like animated ones, by texture id
|
||||
texture_json?: Map<string, TextureJson>;
|
||||
credits?: string;
|
||||
}
|
||||
|
||||
@@ -63,6 +66,7 @@ export class BmodError extends Error {
|
||||
// data.json every block, model, item, recipe, ore, entity, entity model, effect and tag
|
||||
// scripts/<side>.js
|
||||
// textures/<name>.png the texture <mod id>:<name>
|
||||
// textures/<name>.json its animation, if it has one
|
||||
// credits.md
|
||||
export function write_bmod(bmod: Bmod): Uint8Array<ArrayBuffer> {
|
||||
const text = (value: string) => new TextEncoder().encode(value);
|
||||
@@ -85,6 +89,9 @@ export function write_bmod(bmod: Bmod): Uint8Array<ArrayBuffer> {
|
||||
for (const [id, png] of bmod.textures) {
|
||||
files[`textures/${id.split(":")[1]}.png`] = png;
|
||||
}
|
||||
for (const [id, json] of bmod.texture_json ?? []) {
|
||||
files[`textures/${id.split(":")[1]}.json`] = text(JSON.stringify(json));
|
||||
}
|
||||
files["manifest.json"] = text(JSON.stringify(manifest, null, "\t"));
|
||||
files["data.json"] = text(JSON.stringify(bmod.data));
|
||||
|
||||
@@ -115,7 +122,7 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
|
||||
}
|
||||
|
||||
const problems: string[] = [];
|
||||
const json = (name: string): unknown => {
|
||||
const parse = (name: string): unknown => {
|
||||
const content = files[name];
|
||||
if (!content) {
|
||||
problems.push(`${name} is missing`);
|
||||
@@ -130,8 +137,8 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
|
||||
};
|
||||
const text = (name: string) => files[name] && new TextDecoder().decode(files[name]);
|
||||
|
||||
const manifest = json("manifest.json") as ManifestJson | undefined;
|
||||
const raw_data = json("data.json") as Partial<ModData> | undefined;
|
||||
const manifest = parse("manifest.json") as ManifestJson | undefined;
|
||||
const raw_data = parse("data.json") as Partial<ModData> | undefined;
|
||||
if (!manifest || !raw_data) throw new BmodError(file, problems);
|
||||
|
||||
for (const problem of validate_manifest(manifest, manifest.id)) problems.push(`manifest.json: ${problem}`);
|
||||
@@ -179,7 +186,8 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
|
||||
problems.push(`manifest.json: credits ${manifest.credits} isn't in the file`);
|
||||
}
|
||||
|
||||
const textures = new Map<string, Uint8Array>();
|
||||
// sizes are checked once it's known which textures are animated
|
||||
const pngs = new Map<string, Uint8Array>();
|
||||
for (const [name, content] of Object.entries(files)) {
|
||||
const match = name.match(/^textures\/(.+)\.png$/);
|
||||
if (!match) continue;
|
||||
@@ -189,15 +197,38 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
|
||||
problems.push(`${name}: texture names must be a-z, 0-9 and _`);
|
||||
} else if (!size) {
|
||||
problems.push(`${name}: isn't a png`);
|
||||
} else if (texture_size_problem(size)) {
|
||||
problems.push(`${name}: ${texture_size_problem(size)}`);
|
||||
} else {
|
||||
textures.set(id, content);
|
||||
pngs.set(id, content);
|
||||
}
|
||||
}
|
||||
const texture_json = new Map<string, TextureJson>();
|
||||
for (const name of Object.keys(files)) {
|
||||
const match = name.match(/^textures\/(.+)\.json$/);
|
||||
if (!match) continue;
|
||||
const id = `${mod}:${match[1]}`;
|
||||
const png = pngs.get(id);
|
||||
if (!png) {
|
||||
problems.push(`${name}: there's no textures/${match[1]}.png for it to go with`);
|
||||
continue;
|
||||
}
|
||||
const json = parse(name);
|
||||
if (json === undefined) continue;
|
||||
const json_problems = validate_texture_json(json, png_size(png)!);
|
||||
for (const problem of json_problems) problems.push(`${name}: ${problem}`);
|
||||
if (json_problems.length === 0) texture_json.set(id, json as TextureJson);
|
||||
}
|
||||
const textures = new Map<string, Uint8Array>();
|
||||
for (const [id, png] of pngs) {
|
||||
const problem = texture_size_problem(png_size(png)!, texture_json.get(id));
|
||||
if (problem) {
|
||||
problems.push(`textures/${id.split(":")[1]}.png: ${problem}`);
|
||||
} else {
|
||||
textures.set(id, png);
|
||||
}
|
||||
}
|
||||
|
||||
if (problems.length > 0) throw new BmodError(file, problems);
|
||||
return { manifest, data, scripts, textures, credits };
|
||||
return { manifest, data, scripts, textures, texture_json, credits };
|
||||
}
|
||||
|
||||
// the same mod without its server and worldgen scripts, what players get. the server sends them the world
|
||||
@@ -220,9 +251,21 @@ function checked(list: unknown, name: string, validate: (json: unknown) => strin
|
||||
return list;
|
||||
}
|
||||
|
||||
// textures can be any size up to MAX_TEXTURE_SIZE, like 64×32 for an entity. blocks and items stretch theirs to fit
|
||||
export function texture_size_problem(size: { width: number; height: number }): string | undefined {
|
||||
// textures can be any size up to MAX_TEXTURE_SIZE, like 64×32 for an entity. blocks and items stretch theirs to fit.
|
||||
// for an animated one (json is its .json) that's each frame's size
|
||||
export function texture_size_problem(size: { width: number; height: number }, json?: TextureJson): string | undefined {
|
||||
const { width, height } = size;
|
||||
const animation = json && texture_animation(json, size);
|
||||
if (animation) {
|
||||
const { frame_width, frame_height } = animation;
|
||||
if (width > MAX_ANIMATED_TEXTURE_SIZE || height > MAX_ANIMATED_TEXTURE_SIZE) {
|
||||
return `is ${width}×${height}, animated textures can be at most ${MAX_ANIMATED_TEXTURE_SIZE}×${MAX_ANIMATED_TEXTURE_SIZE}`;
|
||||
}
|
||||
if (frame_width > MAX_TEXTURE_SIZE || frame_height > MAX_TEXTURE_SIZE) {
|
||||
return `has ${frame_width}×${frame_height} frames, textures can be at most ${MAX_TEXTURE_SIZE}×${MAX_TEXTURE_SIZE}`;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
if (width < 1 || height < 1 || width > MAX_TEXTURE_SIZE || height > MAX_TEXTURE_SIZE) {
|
||||
return `is ${width}×${height}, textures can be at most ${MAX_TEXTURE_SIZE}×${MAX_TEXTURE_SIZE}`;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
export const TEXTURE_SIZE = 16;
|
||||
// the most a texture can be on either side, entity sprites are bigger than blocks'
|
||||
export const MAX_TEXTURE_SIZE = 256;
|
||||
// the most an animated texture's png can be on either side, all its frames together. each frame is a texture, at most
|
||||
// MAX_TEXTURE_SIZE
|
||||
export const MAX_ANIMATED_TEXTURE_SIZE = 4096;
|
||||
|
||||
// the game's version, what mods' game_version is checked against. mods with engine_access are tied to its minor version
|
||||
export const GAME_VERSION = "0.1.0";
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
import { PLAYER_PARTS } from "./everything_registry.ts";
|
||||
import { BUILTIN_MODELS, type ModelJson } from "./block_models.ts";
|
||||
import { resolve_tags } from "./tags.ts";
|
||||
import type { TextureJson } from "./texture_animation.ts";
|
||||
import { BIOMES } from "./worldgen/overworld.ts";
|
||||
|
||||
export interface ModReport {
|
||||
@@ -43,6 +44,8 @@ export interface LoadedMod {
|
||||
textures: string[];
|
||||
// absolute paths by texture id
|
||||
texture_files: Map<string, string>;
|
||||
// textures/<name>.json by texture id, for the textures that have one (see texture_animation.ts)
|
||||
texture_json: Map<string, TextureJson>;
|
||||
}
|
||||
|
||||
// dependencies before the mods that need them, otherwise alphabetical
|
||||
|
||||
@@ -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