| 1 | /** |
| 2 | * Shared helpers for tier tree sprite generation (32×48 px, bottom-centred trunk). |
| 3 | * Run via: node client/scripts/generate-tier-tree-sprites.mjs [tier...] |
| 4 | */ |
| 5 | import { join, dirname } from "node:path"; |
| 6 | import { fileURLToPath } from "node:url"; |
| 7 | import { writeRgbaPng, makeRgba, fillRect } from "../../../server/src/assets/placeholderPng.js"; |
| 8 | import { MATERIAL_TINT_HEX, WoodTier } from "../../../shared/definitions/materialTiers.js"; |
| 9 | |
| 10 | export { fillRect }; |
| 11 | |
| 12 | export const TREE_W = 32; |
| 13 | export const TREE_H = 48; |
| 14 | export const ASSETS_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "assets", "blocks"); |
| 15 | |
| 16 | /** @typedef {"summer"|"fall"|"winter"} TreeSeason */ |
| 17 | |
| 18 | const TRUNK = Object.freeze([92, 58, 34, 255]); |
| 19 | const STUMP = Object.freeze([78, 48, 28, 255]); |
| 20 | |
| 21 | const SEASON_FOLIAGE = Object.freeze({ |
| 22 | summer: (tier) => foliageFromWood(tier, 1), |
| 23 | fall: () => [210, 110, 42, 255], |
| 24 | winter: () => [148, 158, 148, 255] |
| 25 | }); |
| 26 | |
| 27 | const SEASON_SNOW = Object.freeze([235, 240, 248, 255]); |
| 28 | |
| 29 | function foliageFromWood (tier, strength = 1) |
| 30 | { |
| 31 | const hex = MATERIAL_TINT_HEX[tier] ?? MATERIAL_TINT_HEX[WoodTier.pine]; |
| 32 | const greenBoost = 0.55 + strength * 0.35; |
| 33 | return [ |
| 34 | Math.round(hex.r * 255 * 0.35 + 40 * greenBoost), |
| 35 | Math.round(hex.g * 255 * 0.55 + 90 * greenBoost), |
| 36 | Math.round(hex.b * 255 * 0.25 + 24), |
| 37 | 255 |
| 38 | ]; |
| 39 | } |
| 40 | |
| 41 | export function createTreeImage () |
| 42 | { |
| 43 | return makeRgba(TREE_W, TREE_H, [0, 0, 0, 0]); |
| 44 | } |
| 45 | |
| 46 | export function setPixel (image, x, y, rgba) |
| 47 | { |
| 48 | if (x < 0 || y < 0 || x >= image.width || y >= image.height) |
| 49 | { return; } |
| 50 | image.pixels.set(rgba, (y * image.width + x) * 4); |
| 51 | } |
| 52 | |
| 53 | export function fillCircle (image, cx, cy, radius, rgba) |
| 54 | { |
| 55 | const r2 = radius * radius; |
| 56 | for (let y = Math.floor(cy - radius); y <= Math.ceil(cy + radius); y++) |
| 57 | { |
| 58 | for (let x = Math.floor(cx - radius); x <= Math.ceil(cx + radius); x++) |
| 59 | { |
| 60 | const dx = x - cx; |
| 61 | const dy = y - cy; |
| 62 | if (dx * dx + dy * dy <= r2) |
| 63 | { setPixel(image, x, y, rgba); } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | export function fillEllipse (image, cx, cy, rx, ry, rgba) |
| 69 | { |
| 70 | for (let y = Math.floor(cy - ry); y <= Math.ceil(cy + ry); y++) |
| 71 | { |
| 72 | for (let x = Math.floor(cx - rx); x <= Math.ceil(cx + rx); x++) |
| 73 | { |
| 74 | const nx = (x - cx) / Math.max(1, rx); |
| 75 | const ny = (y - cy) / Math.max(1, ry); |
| 76 | if (nx * nx + ny * ny <= 1) |
| 77 | { setPixel(image, x, y, rgba); } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | export function drawTrunk (image, x, y0, y1, width = 3) |
| 83 | { |
| 84 | fillRect(image, x - Math.floor(width / 2), y0, width, y1 - y0 + 1, TRUNK); |
| 85 | } |
| 86 | |
| 87 | export function drawStump (image, cx = 16, topY = 38) |
| 88 | { |
| 89 | fillRect(image, cx - 4, topY, 8, 10, STUMP); |
| 90 | fillRect(image, cx - 3, topY - 1, 6, 2, [68, 42, 24, 255]); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param {TreeSeason} season |
| 95 | * @param {boolean} stump |
| 96 | * @param {(image: ReturnType<typeof makeRgba>, season: TreeSeason, stump: boolean) => void} drawCanopy |
| 97 | */ |
| 98 | export function renderTree (tier, season, stump, drawCanopy) |
| 99 | { |
| 100 | const image = createTreeImage(); |
| 101 | const foliage = SEASON_FOLIAGE[season](tier); |
| 102 | if (stump) |
| 103 | { |
| 104 | drawStump(image); |
| 105 | writeTreePng(tier, season, stump, image); |
| 106 | return; |
| 107 | } |
| 108 | drawCanopy(image, season, stump, foliage); |
| 109 | if (season === "winter") |
| 110 | { |
| 111 | fillEllipse(image, 16, 16, 11, 7, SEASON_SNOW); |
| 112 | fillRect(image, 10, 8, 12, 3, SEASON_SNOW); |
| 113 | } |
| 114 | writeTreePng(tier, season, stump, image); |
| 115 | } |
| 116 | |
| 117 | function writeTreePng (tier, season, stump, image) |
| 118 | { |
| 119 | const folder = join(ASSETS_ROOT, `${tier}-tree`); |
| 120 | const name = stump ? `${season}-stump` : season; |
| 121 | writeRgbaPng(join(folder, `${name}.png`), TREE_W, TREE_H, image.pixels); |
| 122 | } |
| 123 | |
| 124 | export function writeTierTrees (tier, drawCanopy) |
| 125 | { |
| 126 | for (const season of ["summer", "fall", "winter"]) |
| 127 | { |
| 128 | renderTree(tier, season, false, drawCanopy); |
| 129 | renderTree(tier, season, true, drawCanopy); |
| 130 | } |
| 131 | } |