Files

103 lines
5.9 KiB
JavaScript

// 方块定义 + 程序化贴图图集(4×4 格,每格 16px,最近邻采样保持像素味)
export const AIR = 0
export const BLOCKS = {
1: { name: '草方块', tiles: ['grass_top', 'grass_side', 'dirt'] },
2: { name: '泥土', tiles: ['dirt', 'dirt', 'dirt'] },
3: { name: '石头', tiles: ['stone', 'stone', 'stone'] },
4: { name: '沙子', tiles: ['sand', 'sand', 'sand'] },
5: { name: '水', tiles: ['water', 'water', 'water'], liquid: true },
6: { name: '原木', tiles: ['log_top', 'log_side', 'log_top'] },
7: { name: '树叶', tiles: ['leaves', 'leaves', 'leaves'] },
8: { name: '木板', tiles: ['planks', 'planks', 'planks'] },
9: { name: '玻璃', tiles: ['glass', 'glass', 'glass'], transparent: true },
10: { name: '砖块', tiles: ['brick', 'brick', 'brick'] },
11: { name: '基岩', tiles: ['bedrock', 'bedrock', 'bedrock'] },
12: { name: '雪', tiles: ['snow', 'snow', 'snow'] },
13: { name: '圆石', tiles: ['cobble', 'cobble', 'cobble'] },
14: { name: '砂砾', tiles: ['gravel', 'gravel', 'gravel'] },
}
export const HOTBAR = [1, 3, 8, 6, 7, 4, 9, 10, 12]
const TILE = 16, COLS = 4, SIZE = TILE * COLS
function rng(seed) {
let s = seed >>> 0
return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296 }
}
// 每块贴图的像素配方:基色 + 噪点 + 局部特征
const PAINT = {
grass_top: (c) => { base(c, [96, 160, 74]); speck(c, [70, 128, 54], 0.35); speck(c, [120, 186, 92], 0.18) },
grass_side: (c) => { base(c, [134, 96, 67]); dirtNoise(c); const g = c.getContext('2d'); g.fillStyle = '#5f9e46'; g.fillRect(0, 0, TILE, 4); for (let x = 0; x < TILE; x++) if (x % 3) g.fillRect(x, 4, 1, 1 + (x % 2)) },
dirt: (c) => { base(c, [134, 96, 67]); dirtNoise(c) },
stone: (c) => { base(c, [128, 128, 128]); speck(c, [104, 104, 104], 0.4); speck(c, [150, 150, 150], 0.2) },
cobble: (c) => { base(c, [120, 120, 120]); const g = c.getContext('2d'); g.strokeStyle = 'rgba(80,80,80,.85)'; for (let i = 0; i < 5; i++) { g.beginPath(); g.moveTo((i * 5) % TILE, 0); g.lineTo(((i * 5) % TILE + 7) % TILE, TILE); g.stroke() } speck(c, [150, 150, 150], 0.25) },
sand: (c) => { base(c, [220, 208, 160]); speck(c, [236, 226, 186], 0.3); speck(c, [200, 186, 138], 0.25) },
water: (c) => { base(c, [58, 118, 200]); const g = c.getContext('2d'); g.fillStyle = 'rgba(255,255,255,.16)'; g.fillRect(0, 2, TILE, 2); g.fillRect(0, 9, TILE, 2) },
log_side: (c) => { base(c, [110, 84, 52]); const g = c.getContext('2d'); g.strokeStyle = 'rgba(70,52,30,.9)'; for (let x = 1; x < TILE; x += 4) { g.beginPath(); g.moveTo(x, 0); g.lineTo(x + (x % 3) - 1, TILE); g.stroke() } },
log_top: (c) => { base(c, [150, 118, 76]); const g = c.getContext('2d'); g.strokeStyle = 'rgba(96,72,42,.9)'; for (let r = 2; r < 8; r += 2) { g.beginPath(); g.arc(8, 8, r, 0, 7); g.stroke() } },
leaves: (c) => { base(c, [46, 110, 52]); speck(c, [32, 84, 40], 0.45); speck(c, [72, 140, 66], 0.3) },
planks: (c) => { base(c, [176, 138, 86]); const g = c.getContext('2d'); g.fillStyle = 'rgba(120,90,52,.9)'; g.fillRect(0, 0, TILE, 1); g.fillRect(0, 8, TILE, 1); g.fillRect(0, 15, TILE, 1); g.fillRect(7, 0, 1, 8); g.fillRect(12, 8, 1, 8) },
glass: (c) => { const g = c.getContext('2d'); g.clearRect(0, 0, TILE, TILE); g.strokeStyle = 'rgba(220,240,255,.85)'; g.lineWidth = 1; g.strokeRect(0.5, 0.5, 15, 15); g.strokeStyle = 'rgba(255,255,255,.35)'; g.beginPath(); g.moveTo(2, 13); g.lineTo(12, 3); g.stroke() },
brick: (c) => { base(c, [150, 74, 60]); const g = c.getContext('2d'); g.fillStyle = 'rgba(214,190,180,.95)'; g.fillRect(0, 7, TILE, 1); g.fillRect(0, 15, TILE, 1); for (let y = 0; y < TILE; y += 8) for (let x = (y % 16 ? 4 : 0); x < TILE; x += 8) g.fillRect(x, y, 1, 7) },
bedrock: (c) => { base(c, [60, 60, 64]); speck(c, [30, 30, 34], 0.6); speck(c, [110, 110, 116], 0.3) },
snow: (c) => { base(c, [242, 246, 252]); speck(c, [224, 232, 244], 0.3) },
gravel: (c) => { base(c, [128, 120, 112]); speck(c, [92, 86, 80], 0.5); speck(c, [168, 160, 152], 0.35) },
}
const NAMES = Object.keys(PAINT)
const index = Object.fromEntries(NAMES.map((n, i) => [n, i]))
const ctxOf = (c) => (typeof c.getContext === 'function' ? c.getContext('2d') : c)
function base(c, [r, g, b]) { const ctx = ctxOf(c); ctx.fillStyle = `rgb(${r},${g},${b})`; ctx.fillRect(0, 0, TILE, TILE) }
function speck(c, [r, g, b], density, seed = 7) {
const ctx = ctxOf(c)
const rand = rng(seed * 2654435761)
ctx.fillStyle = `rgb(${r},${g},${b})`
for (let y = 0; y < TILE; y++) for (let x = 0; x < TILE; x++) if (rand() < density) ctx.fillRect(x, y, 1, 1)
}
function dirtNoise(c) { speck(c, [112, 78, 52], 0.35, 3); speck(c, [152, 112, 78], 0.25, 11) }
export function makeAtlas() {
const canvas = document.createElement('canvas')
canvas.width = canvas.height = SIZE
const ctx = canvas.getContext('2d')
ctx.imageSmoothingEnabled = false
NAMES.forEach((name, i) => {
const c = document.createElement('canvas')
c.width = c.height = TILE
const g = c.getContext('2d')
PAINT[name](c)
const col = i % COLS, row = Math.floor(i / COLS)
ctx.drawImage(c, col * TILE, row * TILE)
})
return canvas
}
// 每个面取对应贴图的 uv 矩形(tile 索引 + 0.5px 内缩避免接缝)
export function tileUV(name) {
const i = index[name] ?? 0
const col = i % COLS, row = Math.floor(i / COLS)
const pad = 0.02
const u0 = (col + pad) / COLS, u1 = (col + 1 - pad) / COLS
const v0 = 1 - (row + 1 - pad) / COLS, v1 = 1 - (row + pad) / COLS
return { u0, u1, v0, v1 }
}
export function tileIndexOf(name) {
return index[name] ?? 0
}
export function faceTile(id, face) {
const def = BLOCKS[id]
if (!def) return 'stone'
// face: 0 top, 1 bottom, 2-5 sides
if (face === 0) return def.tiles[0]
if (face === 1) return def.tiles[2]
return def.tiles[1]
}
export function isSolid(id) { return id !== AIR && !BLOCKS[id]?.liquid }
export function isOpaque(id) { return id !== AIR && !BLOCKS[id]?.transparent && !BLOCKS[id]?.liquid }