Messy but working chicken
This commit is contained in:
@@ -80,6 +80,52 @@ export interface ItemJson {
|
||||
components?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
// a kind of mob, drawn with an entity model and one texture. it wanders around, panics when hurt and looks at players
|
||||
export interface EntityJson {
|
||||
id: string;
|
||||
// an entity model, see EntityModelJson
|
||||
model: string;
|
||||
texture: string;
|
||||
// its collision box in blocks, width is for both x and z
|
||||
width: number;
|
||||
height: number;
|
||||
max_health?: number;
|
||||
// minecraft's per tick speed, 0.25 for most animals
|
||||
movement_speed?: number;
|
||||
}
|
||||
|
||||
type Vec3 = [number, number, number];
|
||||
|
||||
// a bedrock entity model (models/<name>.geo.json) with only what the engine uses. positions and sizes are in
|
||||
// texture pixels, 16 to a block, and the model faces -z with its feet at 0
|
||||
export interface EntityModelJson {
|
||||
id: string;
|
||||
// the size its uvs are laid out for, the texture is stretched to it
|
||||
texture_width: number;
|
||||
texture_height: number;
|
||||
// parents come before their children
|
||||
bones: EntityBoneJson[];
|
||||
}
|
||||
|
||||
export interface EntityBoneJson {
|
||||
name: string;
|
||||
parent?: string;
|
||||
// what it turns around
|
||||
pivot: Vec3;
|
||||
// degrees around x, y then z
|
||||
rotation?: Vec3;
|
||||
cubes: EntityCubeJson[];
|
||||
}
|
||||
|
||||
// a box from origin to origin + size, textured like minecraft's box uv: see the renderer's FACES
|
||||
export interface EntityCubeJson {
|
||||
origin: Vec3;
|
||||
size: Vec3;
|
||||
uv: [number, number];
|
||||
// grows the box this many pixels each way without moving its texture
|
||||
inflate?: number;
|
||||
}
|
||||
|
||||
// an item a recipe takes: its id, or a list of ids any of which fits, like every kind of planks
|
||||
export type ItemChoice = string | string[];
|
||||
|
||||
@@ -579,6 +625,112 @@ export function validate_ore(json: unknown): Problems {
|
||||
return problems;
|
||||
}
|
||||
|
||||
export function validate_entity(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["entity must be an object"];
|
||||
const problems = [
|
||||
...validate_id(json.id, "id"),
|
||||
...validate_id(json.model, "model"),
|
||||
...validate_id(json.texture, "texture"),
|
||||
];
|
||||
for (const key of ["width", "height"]) {
|
||||
if (typeof json[key] !== "number" || json[key] <= 0 || json[key] > 16) {
|
||||
problems.push(`${key} must be a number of blocks above 0, at most 16`);
|
||||
}
|
||||
}
|
||||
for (const key of ["max_health", "movement_speed"]) {
|
||||
if (json[key] !== undefined && (typeof json[key] !== "number" || json[key] < 0)) {
|
||||
problems.push(`${key} must be a number, 0 or more`);
|
||||
}
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
|
||||
export function validate_entity_model(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["entity model must be an object"];
|
||||
const problems = validate_id(json.id, "id");
|
||||
for (const key of ["texture_width", "texture_height"]) {
|
||||
if (!Number.isInteger(json[key]) || (json[key] as number) < 1) problems.push(`${key} must be a whole number`);
|
||||
}
|
||||
if (!Array.isArray(json.bones)) return [...problems, "bones must be a list"];
|
||||
const names = new Set<string>();
|
||||
json.bones.forEach((bone, i) => {
|
||||
const field = `bones[${i}]`;
|
||||
if (!is_object(bone) || typeof bone.name !== "string") {
|
||||
problems.push(`${field} must be { name, pivot, cubes }`);
|
||||
return;
|
||||
}
|
||||
if (names.has(bone.name)) problems.push(`${field}: there are two bones named ${bone.name}`);
|
||||
if (bone.parent !== undefined && !names.has(bone.parent as string)) {
|
||||
problems.push(`${field}: parent ${bone.parent} must be a bone before it`);
|
||||
}
|
||||
names.add(bone.name);
|
||||
problems.push(...validate_vec3(bone.pivot, `${field}.pivot`));
|
||||
if (bone.rotation !== undefined) problems.push(...validate_vec3(bone.rotation, `${field}.rotation`));
|
||||
if (!Array.isArray(bone.cubes)) {
|
||||
problems.push(`${field}.cubes must be a list`);
|
||||
return;
|
||||
}
|
||||
bone.cubes.forEach((cube, j) => {
|
||||
const cube_field = `${field}.cubes[${j}]`;
|
||||
if (!is_object(cube)) {
|
||||
problems.push(`${cube_field} must be { origin, size, uv }`);
|
||||
return;
|
||||
}
|
||||
problems.push(...validate_vec3(cube.origin, `${cube_field}.origin`));
|
||||
problems.push(...validate_vec3(cube.size, `${cube_field}.size`));
|
||||
if (!Array.isArray(cube.uv) || cube.uv.length !== 2 || !cube.uv.every((n) => typeof n === "number")) {
|
||||
problems.push(`${cube_field}.uv must be [u, v], per face uvs aren't supported`);
|
||||
}
|
||||
if (cube.inflate !== undefined && typeof cube.inflate !== "number") {
|
||||
problems.push(`${cube_field}.inflate must be a number`);
|
||||
}
|
||||
});
|
||||
});
|
||||
return problems;
|
||||
}
|
||||
|
||||
// a models/<name>.geo.json, as blockbench exports it for bedrock, to an EntityModelJson. its first geometry is used
|
||||
export function entity_model_from_geo(json: unknown, id: string): { model?: EntityModelJson; problems: Problems } {
|
||||
const geometry = is_object(json) && Array.isArray(json["minecraft:geometry"])
|
||||
? json["minecraft:geometry"][0]
|
||||
: undefined;
|
||||
if (!is_object(geometry) || !is_object(geometry.description)) {
|
||||
return {
|
||||
problems: ['must be a bedrock model: { "minecraft:geometry": [{ "description": ..., "bones": [...] }] }'],
|
||||
};
|
||||
}
|
||||
const bones = Array.isArray(geometry.bones) ? geometry.bones : [];
|
||||
const model = {
|
||||
id,
|
||||
texture_width: geometry.description.texture_width,
|
||||
texture_height: geometry.description.texture_height,
|
||||
bones: bones.map((bone) => {
|
||||
if (!is_object(bone)) return bone;
|
||||
const { name, parent, pivot, rotation } = bone;
|
||||
const cubes = Array.isArray(bone.cubes) ? bone.cubes : [];
|
||||
return {
|
||||
name,
|
||||
...(parent === undefined ? {} : { parent }),
|
||||
pivot: pivot ?? [0, 0, 0],
|
||||
...(rotation === undefined ? {} : { rotation }),
|
||||
cubes: cubes.map((cube) => {
|
||||
if (!is_object(cube)) return cube;
|
||||
const { origin, size, uv, inflate } = cube;
|
||||
return { origin, size, uv, ...(inflate === undefined ? {} : { inflate }) };
|
||||
}),
|
||||
};
|
||||
}),
|
||||
};
|
||||
const problems = validate_entity_model(model);
|
||||
return problems.length === 0 ? { model: model as EntityModelJson, problems } : { problems };
|
||||
}
|
||||
|
||||
function validate_vec3(value: unknown, field: string): Problems {
|
||||
return Array.isArray(value) && value.length === 3 && value.every((n) => typeof n === "number")
|
||||
? []
|
||||
: [`${field} must be [x, y, z]`];
|
||||
}
|
||||
|
||||
// an item id, or a list of them any of which fits
|
||||
function validate_choice(value: unknown, field: string): Problems {
|
||||
if (!Array.isArray(value)) return validate_id(value, field);
|
||||
|
||||
Reference in New Issue
Block a user