/** * Shared helpers for tier tree sprite generation (32×48 px, bottom-centred trunk). * Run via: node client/scripts/generate-tier-tree-sprites.mjs [tier...] */ import { join, dirname } from "node:path"; import { fileURLToPath } from "node:url"; import { writeRgbaPng, makeRgba, fillRect } from "../../../server/src/assets/placeholderPng.js"; import { MATERIAL_TINT_HEX, WoodTier } from "../../../shared/definitions/materialTiers.js"; export { fillRect }; export const TREE_W = 32; export const TREE_H = 48; export const ASSETS_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "assets", "blocks"); /** @typedef {"summer"|"fall"|"winter"} TreeSeason */ const TRUNK = Object.freeze([92, 58, 34, 255]); const STUMP = Object.freeze([78, 48, 28, 255]); const SEASON_FOLIAGE = Object.freeze({ summer: (tier) => foliageFromWood(tier, 1), fall: () => [210, 110, 42, 255], winter: () => [148, 158, 148, 255] }); const SEASON_SNOW = Object.freeze([235, 240, 248, 255]); function foliageFromWood (tier, strength = 1) { const hex = MATERIAL_TINT_HEX[tier] ?? MATERIAL_TINT_HEX[WoodTier.pine]; const greenBoost = 0.55 + strength * 0.35; return [ Math.round(hex.r * 255 * 0.35 + 40 * greenBoost), Math.round(hex.g * 255 * 0.55 + 90 * greenBoost), Math.round(hex.b * 255 * 0.25 + 24), 255 ]; } export function createTreeImage () { return makeRgba(TREE_W, TREE_H, [0, 0, 0, 0]); } export function setPixel (image, x, y, rgba) { if (x < 0 || y < 0 || x >= image.width || y >= image.height) { return; } image.pixels.set(rgba, (y * image.width + x) * 4); } export function fillCircle (image, cx, cy, radius, rgba) { const r2 = radius * radius; for (let y = Math.floor(cy - radius); y <= Math.ceil(cy + radius); y++) { for (let x = Math.floor(cx - radius); x <= Math.ceil(cx + radius); x++) { const dx = x - cx; const dy = y - cy; if (dx * dx + dy * dy <= r2) { setPixel(image, x, y, rgba); } } } } export function fillEllipse (image, cx, cy, rx, ry, rgba) { for (let y = Math.floor(cy - ry); y <= Math.ceil(cy + ry); y++) { for (let x = Math.floor(cx - rx); x <= Math.ceil(cx + rx); x++) { const nx = (x - cx) / Math.max(1, rx); const ny = (y - cy) / Math.max(1, ry); if (nx * nx + ny * ny <= 1) { setPixel(image, x, y, rgba); } } } } export function drawTrunk (image, x, y0, y1, width = 3) { fillRect(image, x - Math.floor(width / 2), y0, width, y1 - y0 + 1, TRUNK); } export function drawStump (image, cx = 16, topY = 38) { fillRect(image, cx - 4, topY, 8, 10, STUMP); fillRect(image, cx - 3, topY - 1, 6, 2, [68, 42, 24, 255]); } /** * @param {TreeSeason} season * @param {boolean} stump * @param {(image: ReturnType, season: TreeSeason, stump: boolean) => void} drawCanopy */ export function renderTree (tier, season, stump, drawCanopy) { const image = createTreeImage(); const foliage = SEASON_FOLIAGE[season](tier); if (stump) { drawStump(image); writeTreePng(tier, season, stump, image); return; } drawCanopy(image, season, stump, foliage); if (season === "winter") { fillEllipse(image, 16, 16, 11, 7, SEASON_SNOW); fillRect(image, 10, 8, 12, 3, SEASON_SNOW); } writeTreePng(tier, season, stump, image); } function writeTreePng (tier, season, stump, image) { const folder = join(ASSETS_ROOT, `${tier}-tree`); const name = stump ? `${season}-stump` : season; writeRgbaPng(join(folder, `${name}.png`), TREE_W, TREE_H, image.pixels); } export function writeTierTrees (tier, drawCanopy) { for (const season of ["summer", "fall", "winter"]) { renderTree(tier, season, false, drawCanopy); renderTree(tier, season, true, drawCanopy); } }