// 连续行军 Canvas:只读 state 快照(由 main.js 传入),不持有游戏状态。 // 场景:深空 → 星云 → 旋臂 → 星系边界 → 空洞 → 空洞之外;树带向左行进,跳关时世界整体跃迁。署名:FmodeCode const SCENES = [ { bg0: '#121a2b', bg1: '#1b2a44', star: '#dfe8f5', neb: 'rgba(95,168,211,.10)', tree: '#5FD68A', ground: '#2c4a43', label: '地球外侧 · 柯伊伯带' }, { bg0: '#0f1826', bg1: '#1a2745', star: '#e6eeff', neb: 'rgba(108,92,231,.12)', tree: '#7FB2FF', ground: '#2b3550', label: '太阳系边缘 · 奥尔特云' }, { bg0: '#16112a', bg1: '#2a1c45', star: '#f1e9ff', neb: 'rgba(199,125,255,.13)', tree: '#C77DFF', ground: '#3d2a52', label: '猎户旋臂 · 星云海' }, { bg0: '#0d1524', bg1: '#14213a', star: '#e3ecff', neb: 'rgba(127,178,255,.13)', tree: '#9FE7D2', ground: '#223b45', label: '银河系外缘 · 旋涡边缘' }, { bg0: '#0c111c', bg1: '#171b25', star: '#c9d1dd', neb: 'rgba(255,255,255,.05)', tree: '#E8D9A8', ground: '#2b2d33', label: '本星系群 · 引力潮' }, { bg0: '#101724', bg1: '#1f2b3d', star: '#ffffff', neb: 'rgba(47,174,107,.12)', tree: '#7BE3A6', ground: '#1f3b30', label: '空洞之外 · 未知星空' }, ]; export class MarchScene { constructor(canvas) { this.cv = canvas; this.ctx = canvas.getContext('2d', { alpha: false }); this.dpr = 1; this.w = 0; this.h = 0; this.stars = []; this.trees = []; this.ties = []; this.jump = 0; // 跳关跃迁强度 0..1 this.offset = 0; // 世界偏移(向左滚动的像素) this.time = 0; this.lastTs = 0; this.running = true; this.ro = null; this._resize(); window.addEventListener('resize', () => this._resize()); if (typeof ResizeObserver !== 'undefined') { this.ro = new ResizeObserver(() => this._resize()); this.ro.observe(this.cv.parentElement); } this._seed(); this._loop = this._loop.bind(this); requestAnimationFrame(this._loop); } _resize() { const r = this.cv.parentElement.getBoundingClientRect(); this.dpr = Math.min(2, window.devicePixelRatio || 1); this.w = Math.max(200, Math.round(r.width)); this.h = Math.max(160, Math.round(r.height)); this.cv.width = Math.round(this.w * this.dpr); this.cv.height = Math.round(this.h * this.dpr); this.cv.style.width = this.w + 'px'; this.cv.style.height = this.h + 'px'; this._seed(); } _seed() { const rnd = (a, b) => a + Math.random() * (b - a); this.stars = Array.from({ length: 90 }, () => ({ x: rnd(0, this.w), y: rnd(0, this.h * 0.72), r: rnd(0.4, 1.5), a: rnd(0.25, 0.9), sp: rnd(0.05, 0.35) })); // 树带:一串“树”形状,间隔随机 this.trees = []; let x = -40; while (x < this.w + 160) { const s = rnd(0.65, 1.05); this.trees.push({ x, s, hue: Math.random(), sway: rnd(0, 6.28) }); x += rnd(54, 96) * s; } } /** 每帧由外部调用:传入当前视图数据 */ update(view) { this.view = view; } /** 跳关表现:整体跃迁 + 爆发光 */ burst(k) { this.jump = Math.min(1, this.jump + 0.45 + 0.15 * k); } _tint(hex, a) { // #rrggbb → rgba const n = parseInt(hex.slice(1), 16); return `rgba(${(n >> 16) & 255},${(n >> 8) & 255},${n & 255},${a})`; } _loop(ts) { if (!this.running) return; const dt = Math.min(0.05, (ts - (this.lastTs || ts)) / 1000); this.lastTs = ts; this.time += dt; this._draw(dt); requestAnimationFrame(this._loop); } _draw(dt) { const { ctx, w, h } = this; const v = this.view || {}; ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0); const ch = Math.max(0, Math.min(5, v.chapter || 0)); const sc = SCENES[ch]; const lo = Math.max(0, ch - 1), hi = Math.min(5, ch + 1); const tCh = (v.chapter || 0) - ch; // 章节内连续过渡(里程碑附近渐变) const bg = ch; const g = ctx.createLinearGradient(0, 0, 0, h); g.addColorStop(0, SCENES[bg].bg0); g.addColorStop(1, SCENES[bg].bg1); ctx.fillStyle = g; ctx.fillRect(0, 0, w, h); // 星云带(斜向柔光) ctx.save(); ctx.globalAlpha = 0.9; const neb = ctx.createLinearGradient(0, h * 0.2, w, h * 0.55); neb.addColorStop(0, 'rgba(0,0,0,0)'); neb.addColorStop(0.45, sc.neb); neb.addColorStop(1, 'rgba(0,0,0,0)'); ctx.fillStyle = neb; ctx.beginPath(); ctx.moveTo(0, h * 0.42); for (let x = 0; x <= w; x += 24) ctx.lineTo(x, h * 0.42 + Math.sin(x * 0.012 + this.time * 0.15) * 16 + Math.sin(x * 0.03) * 5); ctx.lineTo(w, h * 0.18); ctx.lineTo(0, h * 0.24); ctx.closePath(); ctx.fill(); ctx.restore(); const layerMul = v.layerSec ? 1 : 1; const speedPx = Math.max(0, Math.min(90, 14 + 26 * (v.layersPerSec || 0))) * layerMul; // 层/秒 → 像素/秒 const jumpK = this.jump; const camShift = jumpK * 240 * (1 - jumpK) * 3; // 世界整体横向跃迁 const move = speedPx * (1 + 3 * jumpK) * dt; this.offset = (this.offset + move) % 100000; const jitterY = jumpK > 0 ? Math.sin(this.time * 60) * 2 * jumpK : 0; // 星空(视差三层) for (const s of this.stars) { const depth = s.r * 1.4; let x = s.x - this.offset * (0.04 + 0.12 * depth) - camShift * (0.1 + depth * 0.4); x = ((x % (w + 20)) + w + 20) % (w + 20) - 10; const a = s.a * (0.6 + 0.4 * Math.sin(this.time * 1.3 + s.x)); ctx.fillStyle = this._tint(sc.star, a); ctx.fillRect(x, s.y, s.r, s.r); if (s.r > 1.2) { // 星芒 ctx.fillStyle = this._tint(sc.star, a * 0.25); ctx.fillRect(x - 1.5, s.y + s.r / 2 - 0.25, s.r + 3, 0.5); } } // 地面(根系基座) const gy = h * 0.8; ctx.fillStyle = SCENES[bg].ground; ctx.globalAlpha = 0.6; ctx.fillRect(0, gy + 26, w, h - gy - 26); ctx.globalAlpha = 1; ctx.strokeStyle = this._tint(sc.star, 0.25); ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(0, gy + 26); ctx.lineTo(w, gy + 26); ctx.stroke(); // 根系(地面上的曲线) ctx.strokeStyle = this._tint(sc.tree, 0.35); ctx.lineWidth = 1.2; for (let k = 0; k < 5; k++) { ctx.beginPath(); for (let x = -20; x <= w + 20; x += 12) { const y = gy + 34 + k * 9 + Math.sin((x + this.offset * (0.6 + k * 0.1)) * 0.02 + k) * (8 - k); if (x === -20) ctx.moveTo(x, y); else ctx.lineTo(x, y); } ctx.stroke(); } // 世界树剪影:带(成排的树) const speed = speedPx * (1 + 2.5 * jumpK); for (const t of this.trees) { let x = t.x - (this.offset * speed / Math.max(1, speedPx)) % (w + 160); x = ((x % (w + 160)) + w + 160) % (w + 160) - 80; const tx = x - camShift; this._tree(ctx, tx, gy + 26, t.s, sc.tree, t.sway + this.time * 0.8); } // 跳关:白色跃迁线 + 光点 if (jumpK > 0.01) { ctx.save(); ctx.globalAlpha = jumpK * 0.8; ctx.strokeStyle = 'rgba(255,255,255,.85)'; for (let i = 0; i < 14; i++) { const y = ((i * 61 + this.time * 400) % (h * 0.75)); const x = (i * 97 + this.offset * 0.3) % w; ctx.lineWidth = 1 + (i % 3) * 0.5; ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + 60 + 120 * jumpK, y); ctx.stroke(); } ctx.globalAlpha = jumpK * 0.35; ctx.fillStyle = sc.tree; ctx.fillRect(0, 0, w, h); ctx.restore(); this.jump = Math.max(0, jumpK - dt * 2.4); } // 雾/边界:右侧空洞方向 const edge = ctx.createLinearGradient(w * 0.62, 0, w, 0); edge.addColorStop(0, 'rgba(10,14,22,0)'); edge.addColorStop(1, ch >= 4 ? 'rgba(240,243,248,.18)' : 'rgba(10,14,22,.55)'); ctx.fillStyle = edge; ctx.fillRect(w * 0.62, 0, w * 0.38, h); // 当前层列(每 10 层一根刻度) this._marks(ctx, v, gy); } _tree(ctx, x, baseY, s, color, sway) { const H = 70 * s; ctx.save(); ctx.translate(x, baseY); const lean = Math.sin(sway) * 1.6; ctx.strokeStyle = this._tint(color, 0.85); ctx.lineWidth = 2.2 * s; ctx.lineCap = 'round'; ctx.beginPath(); ctx.moveTo(0, 0); ctx.quadraticCurveTo(lean, -H * 0.55, lean * 2, -H); ctx.stroke(); // 分枝 ctx.lineWidth = 1.4 * s; for (const [t0, dir, len] of [[0.45, -1, 14], [0.62, 1, 12], [0.78, -1, 9], [0.88, 1, 7]]) { const bx = lean * (t0 * 2), by = -H * t0; ctx.beginPath(); ctx.moveTo(bx, by); ctx.quadraticCurveTo(bx + dir * len * s * 0.6, by - len * s * 0.4, bx + dir * len * s, by - len * s * 0.9); ctx.stroke(); } // 树冠:小圆+点 ctx.fillStyle = this._tint(color, 0.9); const cx = lean * 2, cy = -H; ctx.beginPath(); ctx.arc(cx, cy, 4.2 * s, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = this._tint(color, 0.5); for (const [dx, dy] of [[-9, 4], [8, 2], [-4, -8], [6, -7], [0, 9]]) { ctx.beginPath(); ctx.arc(cx + dx * s, cy + dy * s, 1.8 * s, 0, Math.PI * 2); ctx.fill(); } // 根 ctx.strokeStyle = this._tint(color, 0.45); ctx.lineWidth = 1.2 * s; ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(-8 * s, 10 * s); ctx.moveTo(0, 0); ctx.lineTo(9 * s, 9 * s); ctx.stroke(); ctx.restore(); } _marks(ctx, v, gy) { const { w, h } = this; const layer = v.layerFloat || 0; const base = Math.floor(layer / 10) * 10; const frac = (layer - base) / 10; ctx.font = '10px "SF Mono", Menlo, monospace'; ctx.textAlign = 'center'; for (let i = -1; i < 8; i++) { const L = base + i * 10; if (L < 0) continue; const x = w * 0.18 + (i - frac) * (w * 0.11) + (1 - frac) * 0; if (x < -10 || x > w + 10) continue; const xx = w * 0.18 + (i - frac) * (w * 0.11); const major = L % 50 === 0; ctx.strokeStyle = major ? 'rgba(255,255,255,.55)' : 'rgba(255,255,255,.22)'; ctx.lineWidth = major ? 1.4 : 1; ctx.beginPath(); ctx.moveTo(xx, gy + 4); ctx.lineTo(xx, gy + (major ? 20 : 12)); ctx.stroke(); ctx.fillStyle = major ? 'rgba(255,255,255,.75)' : 'rgba(255,255,255,.38)'; ctx.fillText(String(L), xx, gy + (major ? 32 : 24)); } // 当前层指针 const px = w * 0.18; ctx.fillStyle = 'rgba(255,255,255,.95)'; ctx.beginPath(); ctx.moveTo(px, gy + 26); ctx.lineTo(px - 4, gy + 32); ctx.lineTo(px + 4, gy + 32); ctx.closePath(); ctx.fill(); } destroy() { this.running = false; } }