| 1 | import { mkdirSync, writeFileSync } from "node:fs"; |
| 2 | import { deflateSync } from "node:zlib"; |
| 3 | |
| 4 | const spells = [ |
| 5 | { id: "spell-light", rgb: [255, 248, 192] }, |
| 6 | { id: "spell-spark", rgb: [255, 144, 64] } |
| 7 | ]; |
| 8 | |
| 9 | mkdirSync(new URL("../assets/fx/projectile/", import.meta.url), { recursive: true }); |
| 10 | |
| 11 | for (const spell of spells) |
| 12 | { |
| 13 | for (let frame = 0; frame < 3; frame++) |
| 14 | { |
| 15 | const png = buildPng5(spell.rgb); |
| 16 | writeFileSync( |
| 17 | new URL(`../assets/fx/projectile/${spell.id}-${frame}.png`, import.meta.url), |
| 18 | png |
| 19 | ); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | function buildPng5 ([r, g, b]) |
| 24 | { |
| 25 | const row = Buffer.alloc(1 + 5 * 4); |
| 26 | row[0] = 0; |
| 27 | for (let x = 0; x < 5; x++) |
| 28 | { |
| 29 | const i = 1 + x * 4; |
| 30 | row[i] = r; |
| 31 | row[i + 1] = g; |
| 32 | row[i + 2] = b; |
| 33 | row[i + 3] = x === 0 || x === 4 ? 0 : 255; |
| 34 | } |
| 35 | const raw = Buffer.concat(Array.from({ length: 5 }, () => row)); |
| 36 | return encodePng(5, 5, deflateSync(raw)); |
| 37 | } |
| 38 | |
| 39 | function encodePng (w, h, idat) |
| 40 | { |
| 41 | const signature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]); |
| 42 | const ihdr = chunk("IHDR", (() => |
| 43 | { |
| 44 | const buf = Buffer.alloc(13); |
| 45 | buf.writeUInt32BE(w, 0); |
| 46 | buf.writeUInt32BE(h, 4); |
| 47 | buf[8] = 8; buf[9] = 6; |
| 48 | return buf; |
| 49 | })()); |
| 50 | return Buffer.concat([signature, ihdr, chunk("IDAT", idat), chunk("IEND", Buffer.alloc(0))]); |
| 51 | } |
| 52 | |
| 53 | function chunk (type, data) |
| 54 | { |
| 55 | const len = Buffer.alloc(4); |
| 56 | len.writeUInt32BE(data.length, 0); |
| 57 | const t = Buffer.from(type); |
| 58 | const crc = crc32(Buffer.concat([t, data])); |
| 59 | const c = Buffer.alloc(4); |
| 60 | c.writeUInt32BE(crc >>> 0, 0); |
| 61 | return Buffer.concat([len, t, data, c]); |
| 62 | } |
| 63 | |
| 64 | function crc32 (buf) |
| 65 | { |
| 66 | let c = ~0; |
| 67 | for (let i = 0; i < buf.length; i++) |
| 68 | { |
| 69 | c ^= buf[i]; |
| 70 | for (let k = 0; k < 8; k++) |
| 71 | { c = (c >>> 1) ^ (c & 1 ? 0xedb88320 : 0); } |
| 72 | } |
| 73 | return ~c; |
| 74 | } |