// 星际漫游者 · 太阳系篇 ─ 程序化美术:太阳系星图(SVG)、行星绘制、粒子/庆祝特效。署名:FmodeCode
import { PLANETS } from '../data/planets.js';
import { hash01 } from '../util.js';
// 环形布局:太阳在左上角,行星沿一条“螺旋臂”排开(十颗星一屏可见)
// 太阳在左上(外环),行星沿一条向内收拢的螺旋臂排布:地球最外、太阳最内,画面铺满整个舞台
const CX = 165, CY = 40, R0 = 135, R1 = 30;
const START = 0.5, SWEEP = 2.15;
export function planetPos(i) {
const t = i / (PLANETS.length - 1);
const r = R0 + (R1 - R0) * t;
const a = START + SWEEP * t;
return { x: CX + r * Math.cos(a), y: CY + r * Math.sin(a), r };
}
/** 画一颗行星(返回 SVG 片段)。i 为索引,size 为半径 */
export function planetSvg(i, size = null, extra = '') {
const P = PLANETS[i];
const s = size == null ? P.size * (P.sun ? 0.55 : 0.32) : size;
const gid = `pg${i}${extra.replace(/[^a-z0-9]/gi, '')}`;
if (P.sun) {
return `
`;
}
let band = '';
for (let k = -3; k <= 3; k++) {
const y = (k / 3.6) * s;
const w = Math.sqrt(Math.max(0, s * s - y * y));
band += ``;
}
const ring = P.ring ? `
` : '';
const belt = P.belt ? `` : '';
const moon = P.moon && !P.sun ? `` : '';
return `
${belt}
${band}
${ring}${moon}
`;
}
/** 完整的太阳系星图(按状态上色:已复苏=绿意,锁定=灰暗,当前=脉冲) */
export function buildSystemSvg(status) {
// status: [{ i, state: 'cleared'|'current'|'locked', pct }]
const stars = [];
const aurora = '';
for (let k = 0; k < 90; k++) {
const x = hash01(k) * 360, y = hash01(k + 77) * 360, r = 0.3 + hash01(k + 3) * 0.9;
stars.push(``);
}
const orbit = [];
for (const k of [1.14, 1.32, 1.55]) {
const rr = R0 * k;
orbit.push(``);
}
const arms = [];
for (let i = 0; i < PLANETS.length - 1; i++) {
const a = planetPos(i), b = planetPos(i + 1);
const done = status[i].state === 'cleared';
arms.push(``);
}
const nodes = PLANETS.map((P, i) => {
const p = i === PLANETS.length - 1 ? { x: CX, y: CY } : planetPos(i), st = status[i];
const cls = `system-planet ${st.state}${st.state === 'current' ? ' pulse' : ''}`;
const check = st.state === 'cleared' ? `` : '';
const lock = st.state === 'locked' ? `` : '';
const label = `${P.name}`;
return `${planetSvg(i)}${check}${lock}${label}`;
});
const defs = '';
return `${defs}${aurora}${stars.join('')}${orbit.join('')}${arms.join('')}${nodes.join('')}`;
}
/** 小行星圆点(抽屉列表里用) */
export function orbCss(i) { const P = PLANETS[i]; return `background: radial-gradient(circle at 34% 30%, ${P.color2}, ${P.color} 60%, #1a2234)`; }
// ── 特效(DOM 层)────────────────────────────────────
export function fxAt(x, y, text, cls = '') {
const layer = document.getElementById('fx');
const e = document.createElement('div');
e.className = 'fly ' + cls; e.textContent = text;
e.style.left = x + 'px'; e.style.top = y + 'px';
layer.appendChild(e);
setTimeout(() => e.remove(), 900);
}
export function sparksAt(x, y, n = 8, color = '#9cffc0') {
const layer = document.getElementById('fx');
for (let k = 0; k < n; k++) {
const e = document.createElement('div');
e.className = 'spark';
const a = Math.random() * Math.PI * 2, d = 24 + Math.random() * 44;
e.style.left = x + 'px'; e.style.top = y + 'px';
e.style.setProperty('--dx', Math.cos(a) * d + 'px');
e.style.setProperty('--dy', Math.sin(a) * d + 'px');
if (color) { e.style.background = color; e.style.boxShadow = `0 0 10px ${color}`; }
layer.appendChild(e);
setTimeout(() => e.remove(), 650);
}
}
export function rippleAt(x, y) {
const layer = document.getElementById('fx');
const e = document.createElement('div');
e.className = 'ripple'; e.style.left = (x - 40) + 'px'; e.style.top = (y - 40) + 'px'; e.style.width = '80px'; e.style.height = '80px';
layer.appendChild(e); setTimeout(() => e.remove(), 720);
}
export function confetti() {
const layer = document.getElementById('fx');
const colors = ['#5fd68a', '#9cffc0', '#f4c95d', '#7fb2ff', '#ffd6a5', '#ffffff'];
for (let k = 0; k < 70; k++) {
const e = document.createElement('div');
e.className = 'confetti';
e.style.left = Math.random() * 100 + 'vw';
e.style.top = '-20px';
e.style.background = colors[k % colors.length];
e.style.setProperty('--x0', '0px');
e.style.setProperty('--dx', (Math.random() - 0.5) * 160 + 'px');
e.style.animationDelay = (Math.random() * 0.5) + 's';
e.style.animationDuration = (1.6 + Math.random() * 1.2) + 's';
layer.appendChild(e);
setTimeout(() => e.remove(), 3200);
}
}
export function globalFlash() {
const app = document.getElementById('app');
app.classList.remove('global-flash'); void app.offsetWidth; app.classList.add('global-flash');
}