Add tag enum

This commit is contained in:
Idrees Hassan
2026-01-18 18:14:40 -05:00
parent 579967a302
commit 9f7d864e57
9 changed files with 131 additions and 99 deletions

View File

@@ -240,6 +240,22 @@
return document.documentElement.clientHeight;
}
const TAG = {
DEFAULT: "default",
TUFT: "tuft",
};
class Layer {
/**
* @param {string[][]} pixels
* @param {string} [tag]
*/
constructor(pixels, tag = TAG.DEFAULT) {
this.pixels = pixels;
this.tag = tag;
}
}
/**
* Palette color names
* @type {Record<string, string>}
@@ -347,7 +363,7 @@
[PALETTE.UNDERBELLY]: "#d7cfcb",
[PALETTE.WING]: "#b1b5c5",
[PALETTE.WING_EDGE]: "#9d9fa9",
}, ["tuft"]),
}, [TAG.TUFT]),
europeanRobin: new BirdType("European Robin",
"Native to western Europe, this is the quintessential robin. Quite friendly, you'll often find them searching for worms.", {
[PALETTE.FOOT]: "#af8e75",
@@ -369,7 +385,7 @@
[PALETTE.UNDERBELLY]: "#dc3719",
[PALETTE.WING]: "#d23215",
[PALETTE.WING_EDGE]: "#b1321c",
}, ["tuft"]),
}, [TAG.TUFT]),
americanGoldfinch: new BirdType("American Goldfinch",
"Coloured a brilliant yellow, this bird feeds almost entirely on the seeds of plants such as thistle, sunflowers, and coneflowers.", {
[PALETTE.BEAK]: "#ffaf34",
@@ -446,17 +462,6 @@
}),
};
class Layer {
/**
* @param {string[][]} pixels
* @param {string} [tag]
*/
constructor(pixels, tag = "default") {
this.pixels = pixels;
this.tag = tag;
}
}
class Frame {
/** @type {{ [tag: string]: string[][] }} */
@@ -471,10 +476,10 @@
for (let layer of layers) {
tags.add(layer.tag);
}
tags.add("default");
tags.add(TAG.DEFAULT);
for (let tag of tags) {
let maxHeight = layers.reduce((max, layer) => Math.max(max, layer.pixels.length), 0);
if (layers[0].tag !== "default") {
if (layers[0].tag !== TAG.DEFAULT) {
throw new Error("First layer must have the 'default' tag");
}
this.pixels = layers[0].pixels.map(row => row.slice());
@@ -484,7 +489,7 @@
}
// Combine layers
for (let i = 1; i < layers.length; i++) {
if (layers[i].tag === "default" || layers[i].tag === tag) {
if (layers[i].tag === TAG.DEFAULT || layers[i].tag === tag) {
let layerPixels = layers[i].pixels;
let topMargin = maxHeight - layerPixels.length;
for (let y = 0; y < layerPixels.length; y++) {
@@ -502,8 +507,8 @@
* @param {string} [tag]
* @returns {string[][]}
*/
getPixels(tag = "default") {
return this.#pixelsByTag[tag] ?? this.#pixelsByTag["default"];
getPixels(tag = TAG.DEFAULT) {
return this.#pixelsByTag[tag] ?? this.#pixelsByTag[TAG.DEFAULT];
}
/**