march.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // 连续行军 Canvas:只读 state 快照(由 main.js 传入),不持有游戏状态。
  2. // 场景:深空 → 星云 → 旋臂 → 星系边界 → 空洞 → 空洞之外;树带向左行进,跳关时世界整体跃迁。署名:FmodeCode
  3. const SCENES = [
  4. { bg0: '#121a2b', bg1: '#1b2a44', star: '#dfe8f5', neb: 'rgba(95,168,211,.10)', tree: '#5FD68A', ground: '#2c4a43', label: '地球外侧 · 柯伊伯带' },
  5. { bg0: '#0f1826', bg1: '#1a2745', star: '#e6eeff', neb: 'rgba(108,92,231,.12)', tree: '#7FB2FF', ground: '#2b3550', label: '太阳系边缘 · 奥尔特云' },
  6. { bg0: '#16112a', bg1: '#2a1c45', star: '#f1e9ff', neb: 'rgba(199,125,255,.13)', tree: '#C77DFF', ground: '#3d2a52', label: '猎户旋臂 · 星云海' },
  7. { bg0: '#0d1524', bg1: '#14213a', star: '#e3ecff', neb: 'rgba(127,178,255,.13)', tree: '#9FE7D2', ground: '#223b45', label: '银河系外缘 · 旋涡边缘' },
  8. { bg0: '#0c111c', bg1: '#171b25', star: '#c9d1dd', neb: 'rgba(255,255,255,.05)', tree: '#E8D9A8', ground: '#2b2d33', label: '本星系群 · 引力潮' },
  9. { bg0: '#101724', bg1: '#1f2b3d', star: '#ffffff', neb: 'rgba(47,174,107,.12)', tree: '#7BE3A6', ground: '#1f3b30', label: '空洞之外 · 未知星空' },
  10. ];
  11. export class MarchScene {
  12. constructor(canvas) {
  13. this.cv = canvas;
  14. this.ctx = canvas.getContext('2d', { alpha: false });
  15. this.dpr = 1; this.w = 0; this.h = 0;
  16. this.stars = [];
  17. this.trees = [];
  18. this.ties = [];
  19. this.jump = 0; // 跳关跃迁强度 0..1
  20. this.offset = 0; // 世界偏移(向左滚动的像素)
  21. this.time = 0;
  22. this.lastTs = 0;
  23. this.running = true;
  24. this.ro = null;
  25. this._resize();
  26. window.addEventListener('resize', () => this._resize());
  27. if (typeof ResizeObserver !== 'undefined') { this.ro = new ResizeObserver(() => this._resize()); this.ro.observe(this.cv.parentElement); }
  28. this._seed();
  29. this._loop = this._loop.bind(this);
  30. requestAnimationFrame(this._loop);
  31. }
  32. _resize() {
  33. const r = this.cv.parentElement.getBoundingClientRect();
  34. this.dpr = Math.min(2, window.devicePixelRatio || 1);
  35. this.w = Math.max(200, Math.round(r.width));
  36. this.h = Math.max(160, Math.round(r.height));
  37. this.cv.width = Math.round(this.w * this.dpr);
  38. this.cv.height = Math.round(this.h * this.dpr);
  39. this.cv.style.width = this.w + 'px';
  40. this.cv.style.height = this.h + 'px';
  41. this._seed();
  42. }
  43. _seed() {
  44. const rnd = (a, b) => a + Math.random() * (b - a);
  45. 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) }));
  46. // 树带:一串“树”形状,间隔随机
  47. this.trees = [];
  48. let x = -40;
  49. while (x < this.w + 160) {
  50. const s = rnd(0.65, 1.05);
  51. this.trees.push({ x, s, hue: Math.random(), sway: rnd(0, 6.28) });
  52. x += rnd(54, 96) * s;
  53. }
  54. }
  55. /** 每帧由外部调用:传入当前视图数据 */
  56. update(view) { this.view = view; }
  57. /** 跳关表现:整体跃迁 + 爆发光 */
  58. burst(k) { this.jump = Math.min(1, this.jump + 0.45 + 0.15 * k); }
  59. _tint(hex, a) {
  60. // #rrggbb → rgba
  61. const n = parseInt(hex.slice(1), 16);
  62. return `rgba(${(n >> 16) & 255},${(n >> 8) & 255},${n & 255},${a})`;
  63. }
  64. _loop(ts) {
  65. if (!this.running) return;
  66. const dt = Math.min(0.05, (ts - (this.lastTs || ts)) / 1000);
  67. this.lastTs = ts;
  68. this.time += dt;
  69. this._draw(dt);
  70. requestAnimationFrame(this._loop);
  71. }
  72. _draw(dt) {
  73. const { ctx, w, h } = this;
  74. const v = this.view || {};
  75. ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0);
  76. const ch = Math.max(0, Math.min(5, v.chapter || 0));
  77. const sc = SCENES[ch];
  78. const lo = Math.max(0, ch - 1), hi = Math.min(5, ch + 1);
  79. const tCh = (v.chapter || 0) - ch; // 章节内连续过渡(里程碑附近渐变)
  80. const bg = ch;
  81. const g = ctx.createLinearGradient(0, 0, 0, h);
  82. g.addColorStop(0, SCENES[bg].bg0);
  83. g.addColorStop(1, SCENES[bg].bg1);
  84. ctx.fillStyle = g;
  85. ctx.fillRect(0, 0, w, h);
  86. // 星云带(斜向柔光)
  87. ctx.save();
  88. ctx.globalAlpha = 0.9;
  89. const neb = ctx.createLinearGradient(0, h * 0.2, w, h * 0.55);
  90. neb.addColorStop(0, 'rgba(0,0,0,0)');
  91. neb.addColorStop(0.45, sc.neb);
  92. neb.addColorStop(1, 'rgba(0,0,0,0)');
  93. ctx.fillStyle = neb;
  94. ctx.beginPath();
  95. ctx.moveTo(0, h * 0.42);
  96. 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);
  97. ctx.lineTo(w, h * 0.18); ctx.lineTo(0, h * 0.24); ctx.closePath();
  98. ctx.fill();
  99. ctx.restore();
  100. const layerMul = v.layerSec ? 1 : 1;
  101. const speedPx = Math.max(0, Math.min(90, 14 + 26 * (v.layersPerSec || 0))) * layerMul; // 层/秒 → 像素/秒
  102. const jumpK = this.jump;
  103. const camShift = jumpK * 240 * (1 - jumpK) * 3; // 世界整体横向跃迁
  104. const move = speedPx * (1 + 3 * jumpK) * dt;
  105. this.offset = (this.offset + move) % 100000;
  106. const jitterY = jumpK > 0 ? Math.sin(this.time * 60) * 2 * jumpK : 0;
  107. // 星空(视差三层)
  108. for (const s of this.stars) {
  109. const depth = s.r * 1.4;
  110. let x = s.x - this.offset * (0.04 + 0.12 * depth) - camShift * (0.1 + depth * 0.4);
  111. x = ((x % (w + 20)) + w + 20) % (w + 20) - 10;
  112. const a = s.a * (0.6 + 0.4 * Math.sin(this.time * 1.3 + s.x));
  113. ctx.fillStyle = this._tint(sc.star, a);
  114. ctx.fillRect(x, s.y, s.r, s.r);
  115. if (s.r > 1.2) { // 星芒
  116. ctx.fillStyle = this._tint(sc.star, a * 0.25);
  117. ctx.fillRect(x - 1.5, s.y + s.r / 2 - 0.25, s.r + 3, 0.5);
  118. }
  119. }
  120. // 地面(根系基座)
  121. const gy = h * 0.8;
  122. ctx.fillStyle = SCENES[bg].ground;
  123. ctx.globalAlpha = 0.6;
  124. ctx.fillRect(0, gy + 26, w, h - gy - 26);
  125. ctx.globalAlpha = 1;
  126. ctx.strokeStyle = this._tint(sc.star, 0.25);
  127. ctx.lineWidth = 1;
  128. ctx.beginPath(); ctx.moveTo(0, gy + 26); ctx.lineTo(w, gy + 26); ctx.stroke();
  129. // 根系(地面上的曲线)
  130. ctx.strokeStyle = this._tint(sc.tree, 0.35);
  131. ctx.lineWidth = 1.2;
  132. for (let k = 0; k < 5; k++) {
  133. ctx.beginPath();
  134. for (let x = -20; x <= w + 20; x += 12) {
  135. const y = gy + 34 + k * 9 + Math.sin((x + this.offset * (0.6 + k * 0.1)) * 0.02 + k) * (8 - k);
  136. if (x === -20) ctx.moveTo(x, y); else ctx.lineTo(x, y);
  137. }
  138. ctx.stroke();
  139. }
  140. // 世界树剪影:带(成排的树)
  141. const speed = speedPx * (1 + 2.5 * jumpK);
  142. for (const t of this.trees) {
  143. let x = t.x - (this.offset * speed / Math.max(1, speedPx)) % (w + 160);
  144. x = ((x % (w + 160)) + w + 160) % (w + 160) - 80;
  145. const tx = x - camShift;
  146. this._tree(ctx, tx, gy + 26, t.s, sc.tree, t.sway + this.time * 0.8);
  147. }
  148. // 跳关:白色跃迁线 + 光点
  149. if (jumpK > 0.01) {
  150. ctx.save();
  151. ctx.globalAlpha = jumpK * 0.8;
  152. ctx.strokeStyle = 'rgba(255,255,255,.85)';
  153. for (let i = 0; i < 14; i++) {
  154. const y = ((i * 61 + this.time * 400) % (h * 0.75));
  155. const x = (i * 97 + this.offset * 0.3) % w;
  156. ctx.lineWidth = 1 + (i % 3) * 0.5;
  157. ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + 60 + 120 * jumpK, y); ctx.stroke();
  158. }
  159. ctx.globalAlpha = jumpK * 0.35;
  160. ctx.fillStyle = sc.tree;
  161. ctx.fillRect(0, 0, w, h);
  162. ctx.restore();
  163. this.jump = Math.max(0, jumpK - dt * 2.4);
  164. }
  165. // 雾/边界:右侧空洞方向
  166. const edge = ctx.createLinearGradient(w * 0.62, 0, w, 0);
  167. edge.addColorStop(0, 'rgba(10,14,22,0)');
  168. edge.addColorStop(1, ch >= 4 ? 'rgba(240,243,248,.18)' : 'rgba(10,14,22,.55)');
  169. ctx.fillStyle = edge;
  170. ctx.fillRect(w * 0.62, 0, w * 0.38, h);
  171. // 当前层列(每 10 层一根刻度)
  172. this._marks(ctx, v, gy);
  173. }
  174. _tree(ctx, x, baseY, s, color, sway) {
  175. const H = 70 * s;
  176. ctx.save();
  177. ctx.translate(x, baseY);
  178. const lean = Math.sin(sway) * 1.6;
  179. ctx.strokeStyle = this._tint(color, 0.85);
  180. ctx.lineWidth = 2.2 * s;
  181. ctx.lineCap = 'round';
  182. ctx.beginPath();
  183. ctx.moveTo(0, 0);
  184. ctx.quadraticCurveTo(lean, -H * 0.55, lean * 2, -H);
  185. ctx.stroke();
  186. // 分枝
  187. ctx.lineWidth = 1.4 * s;
  188. for (const [t0, dir, len] of [[0.45, -1, 14], [0.62, 1, 12], [0.78, -1, 9], [0.88, 1, 7]]) {
  189. const bx = lean * (t0 * 2), by = -H * t0;
  190. ctx.beginPath();
  191. ctx.moveTo(bx, by);
  192. ctx.quadraticCurveTo(bx + dir * len * s * 0.6, by - len * s * 0.4, bx + dir * len * s, by - len * s * 0.9);
  193. ctx.stroke();
  194. }
  195. // 树冠:小圆+点
  196. ctx.fillStyle = this._tint(color, 0.9);
  197. const cx = lean * 2, cy = -H;
  198. ctx.beginPath(); ctx.arc(cx, cy, 4.2 * s, 0, Math.PI * 2); ctx.fill();
  199. ctx.fillStyle = this._tint(color, 0.5);
  200. for (const [dx, dy] of [[-9, 4], [8, 2], [-4, -8], [6, -7], [0, 9]]) {
  201. ctx.beginPath(); ctx.arc(cx + dx * s, cy + dy * s, 1.8 * s, 0, Math.PI * 2); ctx.fill();
  202. }
  203. // 根
  204. ctx.strokeStyle = this._tint(color, 0.45);
  205. ctx.lineWidth = 1.2 * s;
  206. ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(-8 * s, 10 * s); ctx.moveTo(0, 0); ctx.lineTo(9 * s, 9 * s); ctx.stroke();
  207. ctx.restore();
  208. }
  209. _marks(ctx, v, gy) {
  210. const { w, h } = this;
  211. const layer = v.layerFloat || 0;
  212. const base = Math.floor(layer / 10) * 10;
  213. const frac = (layer - base) / 10;
  214. ctx.font = '10px "SF Mono", Menlo, monospace';
  215. ctx.textAlign = 'center';
  216. for (let i = -1; i < 8; i++) {
  217. const L = base + i * 10;
  218. if (L < 0) continue;
  219. const x = w * 0.18 + (i - frac) * (w * 0.11) + (1 - frac) * 0;
  220. if (x < -10 || x > w + 10) continue;
  221. const xx = w * 0.18 + (i - frac) * (w * 0.11);
  222. const major = L % 50 === 0;
  223. ctx.strokeStyle = major ? 'rgba(255,255,255,.55)' : 'rgba(255,255,255,.22)';
  224. ctx.lineWidth = major ? 1.4 : 1;
  225. ctx.beginPath(); ctx.moveTo(xx, gy + 4); ctx.lineTo(xx, gy + (major ? 20 : 12)); ctx.stroke();
  226. ctx.fillStyle = major ? 'rgba(255,255,255,.75)' : 'rgba(255,255,255,.38)';
  227. ctx.fillText(String(L), xx, gy + (major ? 32 : 24));
  228. }
  229. // 当前层指针
  230. const px = w * 0.18;
  231. ctx.fillStyle = 'rgba(255,255,255,.95)';
  232. ctx.beginPath(); ctx.moveTo(px, gy + 26); ctx.lineTo(px - 4, gy + 32); ctx.lineTo(px + 4, gy + 32); ctx.closePath(); ctx.fill();
  233. }
  234. destroy() { this.running = false; }
  235. }