art.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // 星际漫游者 · 太阳系篇 ─ 程序化美术:太阳系星图(SVG)、行星绘制、粒子/庆祝特效。署名:FmodeCode
  2. import { PLANETS } from '../data/planets.js';
  3. import { hash01 } from '../util.js';
  4. // 环形布局:太阳在左上角,行星沿一条“螺旋臂”排开(十颗星一屏可见)
  5. // 太阳在左上(外环),行星沿一条向内收拢的螺旋臂排布:地球最外、太阳最内,画面铺满整个舞台
  6. const CX = 165, CY = 40, R0 = 135, R1 = 30;
  7. const START = 0.5, SWEEP = 2.15;
  8. export function planetPos(i) {
  9. const t = i / (PLANETS.length - 1);
  10. const r = R0 + (R1 - R0) * t;
  11. const a = START + SWEEP * t;
  12. return { x: CX + r * Math.cos(a), y: CY + r * Math.sin(a), r };
  13. }
  14. /** 画一颗行星(返回 SVG 片段)。i 为索引,size 为半径 */
  15. export function planetSvg(i, size = null, extra = '') {
  16. const P = PLANETS[i];
  17. const s = size == null ? P.size * (P.sun ? 0.55 : 0.32) : size;
  18. const gid = `pg${i}${extra.replace(/[^a-z0-9]/gi, '')}`;
  19. if (P.sun) {
  20. return `<g class="planet sun-body">
  21. <defs><radialGradient id="${gid}" cx="45%" cy="42%" r="60%"><stop offset="0" stop-color="#fff6c9"/><stop offset=".45" stop-color="${P.color}"/><stop offset="1" stop-color="#ff7a1f"/></radialGradient></defs>
  22. <circle r="${s * 1.6}" fill="${P.color}" opacity=".18"/>
  23. <circle r="${s * 1.22}" fill="${P.color}" opacity=".22"/>
  24. <circle r="${s}" fill="url(#${gid})"/>
  25. <circle r="${s}" fill="none" stroke="#ffe29b" stroke-width=".8" opacity=".7"/>
  26. </g>`;
  27. }
  28. let band = '';
  29. for (let k = -3; k <= 3; k++) {
  30. const y = (k / 3.6) * s;
  31. const w = Math.sqrt(Math.max(0, s * s - y * y));
  32. band += `<path d="M${-w} ${y} Q 0 ${y + s * 0.12} ${w} ${y}" stroke="${P.color2}" stroke-width="${s * 0.13}" stroke-linecap="round" fill="none" opacity="${0.18 + 0.12 * hash01(i * 9 + k)}"/>`;
  33. }
  34. const ring = P.ring ? `<ellipse rx="${s * 1.75}" ry="${s * 0.42}" fill="none" stroke="${P.color2}" stroke-width="${s * 0.16}" opacity=".75" transform="rotate(-16)"/>
  35. <ellipse rx="${s * 1.4}" ry="${s * 0.33}" fill="none" stroke="${P.color2}" stroke-width=".6" opacity=".5" transform="rotate(-16)"/>` : '';
  36. const belt = P.belt ? `<ellipse rx="${s * 2.1}" ry="${s * 0.5}" fill="none" stroke="${P.color2}" stroke-width="1.2" stroke-dasharray="1 3" opacity=".7" transform="rotate(12)"/>` : '';
  37. const moon = P.moon && !P.sun ? `<circle cx="${s * 1.35}" cy="${-s * 1.1}" r="${Math.max(1.6, s * 0.16)}" fill="#dfe6f3" opacity=".9"/>` : '';
  38. return `<g class="planet-body">
  39. <defs>
  40. <radialGradient id="${gid}" cx="35%" cy="32%" r="75%"><stop offset="0" stop-color="${P.color2}"/><stop offset=".55" stop-color="${P.color}"/><stop offset="1" stop-color="#1a2234"/></radialGradient>
  41. <clipPath id="clip${gid}"><circle r="${s}"/></clipPath>
  42. </defs>
  43. ${belt}
  44. <circle r="${s * 1.45}" fill="${P.color}" opacity=".14" class="halo"/>
  45. <circle r="${s}" fill="url(#${gid})"/>
  46. <g clip-path="url(#clip${gid})">${band}</g>
  47. <path d="M${-s * 0.55} ${-s * 0.35} a ${s * 0.6} ${s * 0.6} 0 0 0 ${s * 0.2} ${s * 0.9}" stroke="rgba(0,0,0,.25)" stroke-width="${s * 0.18}" fill="none" clip-path="url(#clip${gid})"/>
  48. <circle r="${s}" fill="none" stroke="rgba(255,255,255,.18)" stroke-width=".6"/>
  49. ${ring}${moon}
  50. </g>`;
  51. }
  52. /** 完整的太阳系星图(按状态上色:已复苏=绿意,锁定=灰暗,当前=脉冲) */
  53. export function buildSystemSvg(status) {
  54. // status: [{ i, state: 'cleared'|'current'|'locked', pct }]
  55. const stars = [];
  56. const aurora = '<path d="M-20 120 Q 120 40 380 150 L 380 -20 L -20 -20 Z" fill="url(#auroraA)" opacity=".55"/><path d="M-20 300 Q 160 200 380 260 L 380 380 L -20 380 Z" fill="url(#auroraB)" opacity=".5"/>';
  57. for (let k = 0; k < 90; k++) {
  58. const x = hash01(k) * 360, y = hash01(k + 77) * 360, r = 0.3 + hash01(k + 3) * 0.9;
  59. stars.push(`<circle cx="${x.toFixed(1)}" cy="${y.toFixed(1)}" r="${r.toFixed(2)}" fill="#cfe0ff" opacity="${(0.25 + hash01(k + 9) * 0.55).toFixed(2)}"/>`);
  60. }
  61. const orbit = [];
  62. for (const k of [1.14, 1.32, 1.55]) {
  63. const rr = R0 * k;
  64. orbit.push(`<path d="M${CX + rr * Math.cos(START)} ${CY + rr * Math.sin(START)} A ${rr} ${rr} 0 0 1 ${CX + rr * Math.cos(START + SWEEP)} ${CY + rr * Math.sin(START + SWEEP)}" fill="none" stroke="rgba(150,190,255,.08)" stroke-width=".6" stroke-dasharray="2 6"/>`);
  65. }
  66. const arms = [];
  67. for (let i = 0; i < PLANETS.length - 1; i++) {
  68. const a = planetPos(i), b = planetPos(i + 1);
  69. const done = status[i].state === 'cleared';
  70. arms.push(`<path d="M${a.x} ${a.y} Q ${(a.x + b.x) / 2 - 8} ${(a.y + b.y) / 2 + 10} ${b.x} ${b.y}" fill="none" stroke="${done ? 'rgba(95,214,138,.55)' : 'rgba(150,190,255,.18)'}" stroke-width="${done ? 1.4 : 1}" ${done ? '' : 'stroke-dasharray="3 4"'}/>`);
  71. }
  72. const nodes = PLANETS.map((P, i) => {
  73. const p = i === PLANETS.length - 1 ? { x: CX, y: CY } : planetPos(i), st = status[i];
  74. const cls = `system-planet ${st.state}${st.state === 'current' ? ' pulse' : ''}`;
  75. const check = st.state === 'cleared' ? `<g transform="translate(${p.x + 14},${p.y - 16})"><circle r="5.5" fill="#1f7a4a" stroke="#9cffc0" stroke-width="1"/><path d="m-2.6 0 1.8 1.8 3.6-3.8" stroke="#eafff1" stroke-width="1.4" fill="none" stroke-linecap="round"/></g>` : '';
  76. const lock = st.state === 'locked' ? `<g transform="translate(${p.x + 12},${p.y - 13})"><rect x="-4" y="-3" width="8" height="7" rx="1.5" fill="#22314f"/><path d="M-2 -3v-2a2 2 0 0 1 4 0v2" stroke="#8fa2c4" stroke-width="1" fill="none"/></g>` : '';
  77. const label = `<text class="label" x="${p.x}" y="${p.y + (P.sun ? 40 : P.size * 0.42 + 14)}">${P.name}</text>`;
  78. return `<g class="${cls}" data-planet="${i}" transform="translate(${p.x} ${p.y})" role="button" aria-label="${P.name}">${planetSvg(i)}${check}${lock}${label}</g>`;
  79. });
  80. const defs = '<defs><radialGradient id="auroraA" cx="30%" cy="0%" r="90%"><stop offset="0" stop-color="#1f5cff" stop-opacity=".22"/><stop offset="1" stop-color="#1f5cff" stop-opacity="0"/></radialGradient><radialGradient id="auroraB" cx="70%" cy="100%" r="90%"><stop offset="0" stop-color="#5fd68a" stop-opacity=".16"/><stop offset="1" stop-color="#5fd68a" stop-opacity="0"/></radialGradient></defs>';
  81. return `${defs}${aurora}<g class="stars">${stars.join('')}</g>${orbit.join('')}${arms.join('')}${nodes.join('')}`;
  82. }
  83. /** 小行星圆点(抽屉列表里用) */
  84. export function orbCss(i) { const P = PLANETS[i]; return `background: radial-gradient(circle at 34% 30%, ${P.color2}, ${P.color} 60%, #1a2234)`; }
  85. // ── 特效(DOM 层)────────────────────────────────────
  86. export function fxAt(x, y, text, cls = '') {
  87. const layer = document.getElementById('fx');
  88. const e = document.createElement('div');
  89. e.className = 'fly ' + cls; e.textContent = text;
  90. e.style.left = x + 'px'; e.style.top = y + 'px';
  91. layer.appendChild(e);
  92. setTimeout(() => e.remove(), 900);
  93. }
  94. export function sparksAt(x, y, n = 8, color = '#9cffc0') {
  95. const layer = document.getElementById('fx');
  96. for (let k = 0; k < n; k++) {
  97. const e = document.createElement('div');
  98. e.className = 'spark';
  99. const a = Math.random() * Math.PI * 2, d = 24 + Math.random() * 44;
  100. e.style.left = x + 'px'; e.style.top = y + 'px';
  101. e.style.setProperty('--dx', Math.cos(a) * d + 'px');
  102. e.style.setProperty('--dy', Math.sin(a) * d + 'px');
  103. if (color) { e.style.background = color; e.style.boxShadow = `0 0 10px ${color}`; }
  104. layer.appendChild(e);
  105. setTimeout(() => e.remove(), 650);
  106. }
  107. }
  108. export function rippleAt(x, y) {
  109. const layer = document.getElementById('fx');
  110. const e = document.createElement('div');
  111. e.className = 'ripple'; e.style.left = (x - 40) + 'px'; e.style.top = (y - 40) + 'px'; e.style.width = '80px'; e.style.height = '80px';
  112. layer.appendChild(e); setTimeout(() => e.remove(), 720);
  113. }
  114. export function confetti() {
  115. const layer = document.getElementById('fx');
  116. const colors = ['#5fd68a', '#9cffc0', '#f4c95d', '#7fb2ff', '#ffd6a5', '#ffffff'];
  117. for (let k = 0; k < 70; k++) {
  118. const e = document.createElement('div');
  119. e.className = 'confetti';
  120. e.style.left = Math.random() * 100 + 'vw';
  121. e.style.top = '-20px';
  122. e.style.background = colors[k % colors.length];
  123. e.style.setProperty('--x0', '0px');
  124. e.style.setProperty('--dx', (Math.random() - 0.5) * 160 + 'px');
  125. e.style.animationDelay = (Math.random() * 0.5) + 's';
  126. e.style.animationDuration = (1.6 + Math.random() * 1.2) + 's';
  127. layer.appendChild(e);
  128. setTimeout(() => e.remove(), 3200);
  129. }
  130. }
  131. export function globalFlash() {
  132. const app = document.getElementById('app');
  133. app.classList.remove('global-flash'); void app.offsetWidth; app.classList.add('global-flash');
  134. }