File size: 20,213 Bytes
4d559be
 
 
c174bde
 
624ddb0
c174bde
 
 
 
 
4d559be
c174bde
 
4d559be
 
 
 
c174bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d559be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c174bde
4d559be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c174bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624ddb0
c174bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d559be
c174bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624ddb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d559be
 
 
 
 
 
 
 
 
 
624ddb0
 
4d559be
624ddb0
 
4d559be
 
624ddb0
 
 
 
 
 
 
4d559be
 
624ddb0
 
4d559be
 
624ddb0
 
 
4d559be
 
 
 
 
 
624ddb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d559be
 
 
 
 
 
c174bde
4d559be
 
c174bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d559be
 
c174bde
 
 
 
 
 
 
4d559be
 
 
 
 
 
 
 
c174bde
4d559be
c174bde
4d559be
 
 
 
 
 
 
 
c174bde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// S6 — 黑街夜市 (rebuilt: 烧烤 · 糖水 · 鱼蛋 · 炒粉 …)
//  A busy night market — 烧烤, 糖水, 鱼蛋, 炒粉, 牛杂, 奶茶, 烤鱿鱼, 煲仔饭, 水果 —
//  full of all kinds of people. 阿飞 rides slowly through it (wide → closer → wide).
import { BAR } from './audio'
import { fogBand, glowBlob, lantern, neonSign, vGradient } from './draw'
import { circle, heroRide, ink, personWalk, vendorFront, type FrontHair, type HairStyle, type PersonStyle, type VendorStyle } from './hero'
import type { SceneCtx } from './types'
import { FONT, PALETTE, clamp, easeInOutCubic, fract, hash, lerp, rgba, smoothstep } from './util'

type C2D = CanvasRenderingContext2D

const STALLS = ['烧烤', '糖水', '鱼蛋', '炒粉', '牛杂', '奶茶', '烤鱿鱼', '煲仔饭', '水果'] as const
type Stall = (typeof STALLS)[number]
const AWNING: Record<Stall, [string, string]> = {
  牛杂: ['#6a3a1a', '#f2d8a8'],
  奶茶: ['#d86a9a', '#fbe8f0'],
  烤鱿鱼: ['#1a6a8a', '#efe9dc'],
  煲仔饭: ['#2a7a3a', '#efe9dc'],
  烧烤: ['#c4142f', '#efe9dc'],
  糖水: ['#2a6ac4', '#efe9dc'],
  鱼蛋: ['#e0a01c', '#3a2a14'],
  水果: ['#d0503a', '#f6e6c4'],
  炒粉: ['#8a1e1e', '#e8c890'],
}

// ------------------------------------------------------------------ goods
function basket(ctx: C2D, x: number, y: number, w: number) {
  ctx.beginPath()
  ctx.moveTo(x - w / 2, y - w * 0.28)
  ctx.lineTo(x + w / 2, y - w * 0.28)
  ctx.lineTo(x + w * 0.42, y)
  ctx.lineTo(x - w * 0.42, y)
  ctx.closePath()
  ink(ctx, '#8a5a2a', Math.max(1, w * 0.02))
  ctx.strokeStyle = 'rgba(40,24,10,0.5)'
  ctx.lineWidth = Math.max(1, w * 0.012)
  ctx.beginPath()
  for (let k = 1; k < 4; k++) {
    ctx.moveTo(x - w * (0.5 - k * 0.02), y - w * 0.28 + k * w * 0.07)
    ctx.lineTo(x + w * (0.5 - k * 0.02), y - w * 0.28 + k * w * 0.07)
  }
  for (let k = -3; k <= 3; k++) {
    ctx.moveTo(x + k * w * 0.13, y - w * 0.28)
    ctx.lineTo(x + k * w * 0.11, y)
  }
  ctx.stroke()
}





function steam(ctx: C2D, x: number, y: number, w: number, t: number, seed: number) {
  for (let i = 0; i < 7; i++) {
    const life = fract(t * 0.35 + i / 7 + hash(seed + i))
    ctx.fillStyle = `rgba(255,240,225,${0.12 * (1 - life)})`
    ctx.beginPath()
    ctx.arc(x + Math.sin(t + i + seed) * w * 0.15, y - life * w * 1.4, w * (0.15 + life * 0.35), 0, Math.PI * 2)
    ctx.fill()
  }
}

function goods(ctx: C2D, kind: Stall, x: number, y: number, w: number, t: number, seed: number) {
  switch (kind) {
    case '牛杂': {
      ctx.beginPath()
      ctx.roundRect(x - w * 0.3, y - w * 0.2, w * 0.6, w * 0.2, w * 0.03)
      ink(ctx, '#9aa3ad', Math.max(1, w * 0.01))
      ctx.fillStyle = '#7a3a1a'
      ctx.fillRect(x - w * 0.28, y - w * 0.2, w * 0.56, w * 0.03)
      for (let i = 0; i < 6; i++) {
        ctx.fillStyle = i % 2 ? '#a0603a' : '#c8905a'
        ctx.beginPath()
        ctx.ellipse(x - w * 0.22 + i * w * 0.09, y - w * 0.21, w * 0.035, w * 0.02, 0, 0, Math.PI * 2)
        ctx.fill()
      }
      steam(ctx, x, y - w * 0.22, w * 0.5, t, seed)
      break
    }
    case '奶茶': {
      for (let i = 0; i < 6; i++) {
        const cx = x - w * 0.3 + i * w * 0.12
        ctx.beginPath()
        ctx.moveTo(cx - w * 0.035, y - w * 0.16)
        ctx.lineTo(cx + w * 0.035, y - w * 0.16)
        ctx.lineTo(cx + w * 0.028, y)
        ctx.lineTo(cx - w * 0.028, y)
        ctx.closePath()
        ink(ctx, ['#e8d0a8', '#f0b8c8', '#c8e0a8'][i % 3], Math.max(1, w * 0.006))
        ctx.fillStyle = '#2a1a10'
        for (let k = 0; k < 3; k++) ctx.fillRect(cx - w * 0.02 + k * w * 0.014, y - w * 0.025, w * 0.01, w * 0.01)
        ctx.strokeStyle = '#ff5fa2'
        ctx.beginPath()
        ctx.moveTo(cx, y - w * 0.16)
        ctx.lineTo(cx + w * 0.01, y - w * 0.23)
        ctx.stroke()
      }
      break
    }
    case '烤鱿鱼': {
      ctx.fillStyle = '#1a1a1c'
      ctx.fillRect(x - w * 0.4, y - w * 0.06, w * 0.8, w * 0.06)
      glowBlob(ctx, x, y - w * 0.05, w * 0.45, '#ff5a1c', 0.55)
      for (let i = 0; i < 5; i++) {
        const sx = x - w * 0.3 + i * w * 0.15
        ctx.strokeStyle = '#d8b07a'
        ctx.beginPath()
        ctx.moveTo(sx, y - w * 0.06)
        ctx.lineTo(sx, y - w * 0.36)
        ctx.stroke()
        ctx.fillStyle = '#d88a4a'
        ctx.beginPath()
        ctx.ellipse(sx, y - w * 0.26, w * 0.04, w * 0.08, 0, 0, Math.PI * 2)
        ctx.fill()
        ctx.strokeStyle = '#c0703a'
        ctx.beginPath()
        for (let k = -2; k <= 2; k++) {
          ctx.moveTo(sx + k * w * 0.012, y - w * 0.18)
          ctx.lineTo(sx + k * w * 0.02, y - w * 0.1)
        }
        ctx.stroke()
      }
      steam(ctx, x, y - w * 0.3, w * 0.4, t, seed)
      break
    }
    case '煲仔饭': {
      for (let i = 0; i < 4; i++) {
        const cx = x - w * 0.3 + i * w * 0.2
        ctx.beginPath()
        ctx.ellipse(cx, y - w * 0.06, w * 0.08, w * 0.06, 0, 0, Math.PI * 2)
        ink(ctx, '#3a2414', Math.max(1, w * 0.008))
        ctx.fillStyle = '#e8dcc0'
        ctx.beginPath()
        ctx.ellipse(cx, y - w * 0.1, w * 0.065, w * 0.025, 0, 0, Math.PI * 2)
        ctx.fill()
        ctx.fillStyle = '#a0301a'
        ctx.fillRect(cx - w * 0.03, y - w * 0.115, w * 0.02, w * 0.012)
        ctx.fillStyle = '#5fbf3a'
        ctx.fillRect(cx + w * 0.01, y - w * 0.11, w * 0.015, w * 0.01)
        glowBlob(ctx, cx, y, w * 0.08, '#ff5a1c', 0.5)
        steam(ctx, cx, y - w * 0.12, w * 0.2, t, seed + i)
      }
      break
    }
    case '烧烤': {
      ctx.fillStyle = '#1a1a1c'
      ctx.fillRect(x - w * 0.4, y - w * 0.08, w * 0.8, w * 0.08)
      glowBlob(ctx, x, y - w * 0.06, w * 0.5, '#ff5a1c', 0.6 + 0.2 * Math.sin(t * 8 + seed))
      for (let i = 0; i < 9; i++) {
        const sx = x + (i / 8 - 0.5) * w * 0.7
        ctx.strokeStyle = '#d8b07a'
        ctx.lineWidth = Math.max(1, w * 0.008)
        ctx.beginPath()
        ctx.moveTo(sx - w * 0.06, y - w * 0.12)
        ctx.lineTo(sx + w * 0.06, y - w * 0.07)
        ctx.stroke()
        ctx.fillStyle = '#7a3a1a'
        for (let k = 0; k < 3; k++) ctx.fillRect(sx - w * 0.04 + k * w * 0.03, y - w * 0.115 + k * w * 0.012, w * 0.022, w * 0.022)
      }
      steam(ctx, x, y - w * 0.15, w * 0.5, t, seed)
      break
    }
    case '糖水':
    case '鱼蛋': {
      for (const dx of [-0.22, 0.22]) {
        ctx.beginPath()
        ctx.roundRect(x + dx * w - w * 0.16, y - w * 0.22, w * 0.32, w * 0.22, w * 0.03)
        ink(ctx, '#b8c0c8', Math.max(1, w * 0.01))
        ctx.fillStyle = kind === '糖水' ? '#e8c890' : '#d8a84a'
        ctx.fillRect(x + dx * w - w * 0.14, y - w * 0.22, w * 0.28, w * 0.03)
        steam(ctx, x + dx * w, y - w * 0.25, w * 0.35, t, seed + dx * 10)
      }
      if (kind === '鱼蛋')
        for (let i = 0; i < 5; i++) {
          ctx.strokeStyle = '#d8b07a'
          ctx.beginPath()
          ctx.moveTo(x - w * 0.25 + i * w * 0.03, y - w * 0.2)
          ctx.lineTo(x - w * 0.27 + i * w * 0.03, y - w * 0.4)
          ctx.stroke()
          ctx.fillStyle = '#e8b04a'
          for (let k = 0; k < 3; k++) {
            ctx.beginPath()
            ctx.arc(x - w * 0.265 + i * w * 0.03, y - w * (0.3 + k * 0.035), w * 0.016, 0, Math.PI * 2)
            ctx.fill()
          }
        }
      break
    }
    case '水果': {
      const cols = ['#ff8a1c', '#ffd23a', '#c4142f', '#5fbf3a']
      for (let r = 0; r < 4; r++)
        for (let c = 0; c <= 6 - r * 2; c++) {
          ctx.fillStyle = cols[(c + r + seed) % 4]
          ctx.beginPath()
          ctx.arc(x + (c - (6 - r * 2) / 2) * w * 0.07, y - w * (0.035 + r * 0.06), w * 0.035, 0, Math.PI * 2)
          ctx.fill()
        }
      break
    }
    case '炒粉': {
      // wok with fire bursts
      ctx.beginPath()
      ctx.arc(x, y - w * 0.12, w * 0.2, 0, Math.PI)
      ink(ctx, '#202228', Math.max(1, w * 0.01))
      const burst = Math.max(0, Math.sin(t * 2.3 + seed))
      if (burst > 0.4) {
        ctx.save()
        ctx.globalCompositeOperation = 'lighter'
        glowBlob(ctx, x, y - w * 0.2, w * 0.5 * burst, '#ff7a2e', 0.9)
        glowBlob(ctx, x, y - w * 0.3, w * 0.25 * burst, '#ffe08a', 0.9)
        ctx.restore()
      }
      steam(ctx, x, y - w * 0.2, w * 0.4, t, seed)
      break
    }
  }
}

// ------------------------------------------------------------------ one stall
function stall(ctx: C2D, kind: Stall, x: number, groundY: number, w: number, u: number, t: number, seed: number, vendor = true) {
  const h = w * 0.95
  const counterY = groundY - h * 0.38
  const roofY = groundY - h * 0.95
  const [c1, c2] = AWNING[kind]
  // back wall glow
  ctx.fillStyle = '#2a1a10'
  ctx.fillRect(x - w / 2, roofY, w, counterY - roofY)
  glowBlob(ctx, x, counterY - h * 0.25, w * 0.7, PALETTE.amber, 0.3)
  // vendor behind the counter
  if (vendor) {
    vendorFront(ctx, x + w * 0.04, counterY, h * 0.62, vendorStyle(seed + 77), t, seed)
  }
  // poles
  ctx.fillStyle = '#1a1410'
  ctx.fillRect(x - w / 2, roofY, w * 0.025, groundY - roofY)
  ctx.fillRect(x + w / 2 - w * 0.025, roofY, w * 0.025, groundY - roofY)
  // counter
  ctx.beginPath()
  ctx.rect(x - w * 0.48, counterY, w * 0.96, groundY - counterY)
  ink(ctx, '#3a2414', Math.max(1, u * 2))
  ctx.fillStyle = '#5a3a20'
  ctx.fillRect(x - w * 0.5, counterY - u * 6, w, u * 8)
  goods(ctx, kind, x, counterY, w, t, seed)
  // striped awning (scalloped edge)
  const n = 8
  for (let i = 0; i < n; i++) {
    ctx.fillStyle = i % 2 ? c2 : c1
    ctx.beginPath()
    ctx.moveTo(x - w * 0.56 + (i / n) * w * 1.12, roofY - h * 0.12)
    ctx.lineTo(x - w * 0.56 + ((i + 1) / n) * w * 1.12, roofY - h * 0.12)
    ctx.lineTo(x - w * 0.56 + ((i + 1) / n) * w * 1.12, roofY + h * 0.04)
    ctx.arc(x - w * 0.56 + ((i + 0.5) / n) * w * 1.12, roofY + h * 0.04, (w * 0.56) / n, 0, Math.PI)
    ctx.closePath()
    ctx.fill()
  }
  ctx.strokeStyle = '#120c08'
  ctx.lineWidth = Math.max(1, u * 2)
  ctx.strokeRect(x - w * 0.56, roofY - h * 0.12, w * 1.12, h * 0.16)
  // hanging bulbs
  for (let i = 0; i < 3; i++) {
    const bx = x + (i - 1) * w * 0.3
    const sw = Math.sin(t * 1.4 + i + seed) * u * 4
    ctx.strokeStyle = '#120c08'
    ctx.beginPath()
    ctx.moveTo(bx, roofY + h * 0.04)
    ctx.lineTo(bx + sw, roofY + h * 0.14)
    ctx.stroke()
    glowBlob(ctx, bx + sw, roofY + h * 0.16, w * 0.22, '#ffd08a', 0.75)
    circle(ctx, { x: bx + sw, y: roofY + h * 0.16 }, w * 0.025, '#fff1c8', 0)
  }
  // painted sign board on top
  ctx.beginPath()
  ctx.rect(x - w * 0.3, roofY - h * 0.3, w * 0.6, h * 0.17)
  ink(ctx, '#f2e6c8', Math.max(1, u * 2))
  ctx.fillStyle = '#a01020'
  ctx.font = `${h * 0.12}px ${FONT.neon}`
  ctx.textAlign = 'center'
  ctx.textBaseline = 'middle'
  ctx.fillText(kind, x, roofY - h * 0.215)
}

// ------------------------------------------------------------------ backdrop
function tenements(s: SceneCtx, scroll: number, baseY: number) {
  const { ctx, W, H, u, t } = s
  const cell = u * 260
  const sc = scroll * 0.35
  const k0 = Math.floor(sc / cell) - 1
  for (let k = k0; k < k0 + Math.ceil(W / cell) + 3; k++) {
    const x = k * cell - sc
    const top = baseY - H * (0.55 + 0.25 * hash(k * 3.3))
    ctx.fillStyle = k % 2 ? '#1a1418' : '#16121a'
    ctx.fillRect(x, top, cell + 1, baseY - top)
    // windows + balconies
    for (let r = 0; r < 8; r++) {
      const wy = top + u * 30 + r * u * 70
      if (wy > baseY - u * 60) break
      for (let c = 0; c < 3; c++) {
        const lit = hash(k * 7 + r * 3 + c) > 0.35
        ctx.fillStyle = lit ? (hash(k + r + c) > 0.8 ? '#7fc8d8' : '#e8a050') : '#0c0a0e'
        ctx.globalAlpha = lit ? 0.8 : 1
        ctx.fillRect(x + u * 20 + c * u * 80, wy, u * 50, u * 36)
        ctx.globalAlpha = 1
      }
      ctx.fillStyle = '#0a080c'
      ctx.fillRect(x + u * 10, wy + u * 40, cell - u * 20, u * 6)
      // laundry on poles
      if (hash(k * 5 + r) > 0.6) {
        ctx.strokeStyle = '#3a3036'
        ctx.beginPath()
        ctx.moveTo(x + u * 10, wy + u * 48)
        ctx.lineTo(x + cell - u * 10, wy + u * 48)
        ctx.stroke()
        for (let i = 0; i < 5; i++) {
          ctx.fillStyle = ['#c4142f', '#e8e2d6', '#2a6ac4', '#e8a01c'][(i + r) % 4]
          ctx.fillRect(x + u * 20 + i * u * 45, wy + u * 48, u * 26, u * (22 + 10 * Math.sin(t + i)))
        }
      }
    }
    // vertical neon
    if (hash(k * 1.9) > 0.45) {
      const words = ['夜市', '糖水', '牛杂', '粥面', '凉茶', '冰室']
      const word = words[((k % words.length) + words.length) % words.length]
      neonSign(ctx, x + cell * 0.78, top + u * 40, u * 46, u * 120, word, [PALETTE.red, PALETTE.cyan, PALETTE.sodium, '#ff5fa2'][((k % 4) + 4) % 4], 0.9 + 0.1 * Math.sin(t * 3 + k), true)
    }
  }
}

function stringLights(s: SceneCtx, scroll: number, y0: number) {
  const { ctx, W, u, t } = s
  const span = u * 420
  const sc = scroll * 0.7
  const k0 = Math.floor(sc / span) - 1
  for (let k = k0; k < k0 + Math.ceil(W / span) + 2; k++) {
    const x0 = k * span - sc
    ctx.strokeStyle = '#1a120c'
    ctx.lineWidth = Math.max(1, u * 2)
    ctx.beginPath()
    for (let i = 0; i <= 20; i++) {
      const q = i / 20
      const x = x0 + q * span
      const y = y0 + Math.sin(q * Math.PI) * u * 60
      if (i) ctx.lineTo(x, y)
      else ctx.moveTo(x, y)
    }
    ctx.stroke()
    for (let i = 1; i < 10; i++) {
      const q = i / 10
      const x = x0 + q * span
      const y = y0 + Math.sin(q * Math.PI) * u * 60 + u * 6
      const on = 0.7 + 0.3 * Math.sin(t * 3 + i + k)
      glowBlob(ctx, x, y, u * 26, i % 3 ? '#ffd08a' : '#ff8a6a', 0.7 * on)
      circle(ctx, { x, y }, u * 5, '#fff1c8', 0)
    }
    if (k % 2 === 0) lantern(ctx, x0 + span * 0.5, y0 + u * 120, u * 28, Math.sin(t * 1.2 + k) * 0.1, 1)
  }
}

// realistic outfits: [top, top shade, short sleeves?] + bottoms
const TOPS: [string, string, boolean][] = [
  ['#e9e6df', '#c8c4bb', true], // white tee
  ['#1c1d21', '#111214', true], // black tee
  ['#7a7d84', '#5c5f66', false], // grey hoodie
  ['#2c3e5c', '#1f2d44', true], // navy polo
  ['#a8323a', '#7e2228', true], // red tee
  ['#4a5a3a', '#36432a', false], // olive jacket
  ['#4a6a90', '#36506e', false], // denim jacket
  ['#d8c8a8', '#b8a888', true], // beige shirt
  ['#2a2a30', '#1c1c20', false], // black jacket
  ['#e8b0b8', '#c8909a', true], // pink tee
]
const BOTTOMS: [string, string][] = [
  ['#34486a', '#26364f'], // blue jeans
  ['#1e1e22', '#141417'], // black trousers
  ['#a89470', '#887652'], // khaki
  ['#55585e', '#3f4146'], // grey
]
const SKIRTS = ['#1e1e22', '#2c3e5c', '#c8b896', '#7a2a30']
const SKIN = ['#f0d0b0', '#e2bc94', '#c8966c', '#a8744c', '#f4dcc0']
const HAIRC = ['#121212', '#2a1a10', '#4a3020', '#8a8a8a', '#c8c8c8', '#6a3a1a']
const STYLES: HairStyle[] = ['short', 'long', 'ponytail', 'bun', 'bald', 'cap', 'hat', 'short', 'long']
const CARRY: PersonStyle['carry'][] = ['none', 'skewer', 'cup', 'phone', 'bag', 'none', 'skewer', 'cup']

function personStyle(i: number): PersonStyle {
  const r = (k: number) => hash(i * 17.3 + k * 3.1)
  const hair = STYLES[Math.floor(r(1) * STYLES.length)]
  const elder = r(2) > 0.86
  const kid = !elder && r(3) > 0.82
  const top = TOPS[Math.floor(r(4) * TOPS.length)]
  const bottom = BOTTOMS[Math.floor(r(6) * BOTTOMS.length)]
  const female = hair === 'long' || hair === 'ponytail' || hair === 'bun'
  const skin = SKIN[Math.floor(r(7) * SKIN.length)]
  const skirt = female && !elder && r(10) > 0.55 ? SKIRTS[Math.floor(r(11) * SKIRTS.length)] : undefined
  return {
    palette: {
      sweater: top[0],
      sweaterShade: top[1],
      sweaterHi: top[0],
      rib: top[1],
      pants: bottom[0],
      pantsShade: bottom[1],
      skin,
      skinShade: '#9a7458',
      hair: elder ? HAIRC[3 + Math.floor(r(8) * 2)] : HAIRC[Math.floor(r(8) * 3)],
      shoe: ['#1a1a1e', '#e8e2d6', '#5a3a2a'][Math.floor(r(9) * 3)],
      sole: '#d8d2c8',
    },
    hair: elder && hair === 'long' ? 'short' : hair,
    skirt,
    legs: skirt ? (r(12) > 0.5 ? '#1a1a1e' : skin) : undefined,
    shortSleeves: top[2],
    scale: kid ? 0.62 : elder ? 0.92 : 0.9 + 0.2 * r(12),
    stoop: elder ? 1 : 0,
    carry: elder ? 'cane' : CARRY[Math.floor(r(13) * CARRY.length)],
  }
}

const VHAIR: FrontHair[] = ['short', 'chef', 'cap', 'scarf', 'bun', 'bald', 'long', 'short']
function vendorStyle(seed: number): VendorStyle {
  const r = (k: number) => hash(seed * 31.7 + k * 7.3)
  const hair = VHAIR[Math.floor(r(1) * VHAIR.length)]
  const top = TOPS[Math.floor(r(2) * TOPS.length)]
  const male = hair === 'bald' || hair === 'cap' || hair === 'chef' || (hair === 'short' && r(3) > 0.3)
  return {
    palette: {
      sweater: top[0],
      sweaterShade: top[1],
      sweaterHi: top[0],
      rib: top[1],
      skin: SKIN[Math.floor(r(4) * SKIN.length)],
      skinShade: '#9a7458',
      hair: r(5) > 0.75 ? HAIRC[3] : HAIRC[Math.floor(r(6) * 3)],
    },
    hair,
    hatColor: ['#c4142f', '#2a6ac4', '#e8b01c', '#2a7a4a', '#f4f2ee'][Math.floor(r(7) * 5)],
    apron: r(8) > 0.4 ? ['#2a2a30', '#e8e2d6', '#7a2a30', '#2c3e5c'][Math.floor(r(9) * 4)] : undefined,
    glasses: r(10) > 0.75,
    mustache: male && r(11) > 0.6,
    smile: r(12) > 0.4,
    scale: 0.92 + 0.16 * r(13),
  }
}

function crowd(s: SceneCtx, scroll: number, groundY: number, h: number, seed: number, count: number) {
  const { W, t, ctx } = s
  for (let i = 0; i < count; i++) {
    const id = seed * 100 + i
    const dir = hash(id * 1.3) > 0.5 ? 1 : -1
    const speed = (0.5 + 0.4 * hash(id)) * h * 0.55
    const span = W + h * 2
    const x = (((hash(id * 3.7) * span + dir * t * speed - scroll) % span) + span) % span - h
    personWalk(ctx, x, groundY + h * 0.02 * hash(id * 9), h, fract(t * 0.8 + hash(id)), personStyle(id), t, dir)
  }
}

function marketStreet(s: SceneCtx, scroll: number, opts: { crowd: boolean }) {
  const { ctx, W, H, P, u, t } = s
  const groundY = H * (P ? 0.8 : 0.82)
  vGradient(ctx, 0, 0, W, H, [
    [0, '#080610'],
    [0.5, '#1e1018'],
    [1, '#120a08'],
  ])
  tenements(s, scroll, groundY - H * 0.05)
  fogBand(ctx, W, groundY - H * 0.3, H * 0.12, '#ff9a4a', 0.12)
  stringLights(s, scroll, H * 0.08)
  // stalls row
  const w = P ? W * 0.62 : H * 0.62
  const k0 = Math.floor(scroll / w) - 1
  for (let k = k0; k < k0 + Math.ceil(W / w) + 2; k++) {
    const kind = STALLS[((k % STALLS.length) + STALLS.length) % STALLS.length]
    stall(ctx, kind, k * w - scroll + w / 2, groundY - H * 0.02, w * 0.92, u, t, k)
  }
  // wet ground with warm reflections
  vGradient(ctx, 0, groundY, W, H - groundY, [
    [0, '#1a120c'],
    [1, '#080605'],
  ])
  ctx.save()
  ctx.globalCompositeOperation = 'lighter'
  for (let k = k0; k < k0 + Math.ceil(W / w) + 2; k++) {
    const x = k * w - scroll + w / 2
    const g = ctx.createLinearGradient(0, groundY, 0, H)
    g.addColorStop(0, 'rgba(255,170,90,0.22)')
    g.addColorStop(1, 'rgba(255,170,90,0)')
    ctx.fillStyle = g
    ctx.fillRect(x - w * 0.35, groundY, w * 0.7, H - groundY)
  }
  ctx.restore()
  // people behind 阿飞 (further away, smaller)
  if (opts.crowd) crowd(s, scroll * 0.9, groundY + H * 0.03, P ? H * 0.22 : H * 0.34, 1, 10)
  return groundY
}


export function sMarket(s: SceneCtx) {
  const { ctx, W, H, P, lt, t, u } = s
  const shot = lt < 3 * BAR ? 0 : lt < 5 * BAR ? 1 : 2
  const zoom = shot === 1 ? 1.45 : 1
  const scroll = (lt + shot * 9) * u * 75
  ctx.save()
  if (zoom !== 1) {
    // closer tracking shot: frame 阿飞 among the crowd
    ctx.translate(W * 0.42, H * 0.78)
    ctx.scale(zoom, zoom)
    ctx.translate(-W * 0.42, -H * 0.78)
  }
  const groundY = marketStreet(s, scroll, { crowd: true })
  const bs = P ? W * 0.32 : H * 0.26
  let x: number
  if (shot === 0) x = lerp(-bs, W * 0.42, easeInOutCubic(clamp(lt / (2.6 * BAR))))
  else if (shot === 1) x = W * 0.42
  else x = lerp(W * 0.42, W + bs * 1.2, smoothstep(BAR * 1.2, s.dur - 5 * BAR, lt - 5 * BAR))
  heroRide(ctx, x, groundY + H * 0.06, bs, { facing: 1, t, spin: (lt * u * 90) / (bs * 0.23), light: 1, beam: true, wind: 0.15, smile: shot === 2, eyes: 'open', tuck: 0, rim: rgba(PALETTE.amber, 0.7) })
  // people in front (closer to camera, bigger)
  crowd(s, scroll * 1.25, groundY + H * 0.16, P ? H * 0.32 : H * 0.5, 2 + shot, 7)
  ctx.restore()
}