rusting_engine 2.5.7

Vulkan 3D game engine with GPU-accelerated physics for massive physics-heavy scenes
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
//! Game-feel kit: tweens, event-triggered sounds, burst effects, and a
//! scene-data HUD.
//!
//! Each piece is an ordinary registered scene component, so scene files,
//! `scene patch`, scenarios, and the editor inspector see and edit it:
//!
//! - [`Tween`] animates one `Transform` property between two values with an
//!   [`Easing`] curve, once, looping, or ping-ponging.
//! - [`SoundCue`] sends a [`SoundEvent`] when its body starts touching
//!   something or when game code calls [`SoundCue::trigger`]. The runner
//!   turns the event into an [`AudioQueue`](super::AudioQueue) request and
//!   plays it; see `GameScene::play_sound` for sounds from game code.
//! - [`BurstEmitter`] spawns short-lived [`BurstParticle`] entities that fly
//!   out, fall, and shrink. Particles copy the emitter's `MeshRenderer`.
//!   With a `rate` it emits every fixed step, across an `area` box.
//! - [`HudElement`] draws a text label or button over the game view (with the
//!   `ui` feature) and sends [`HudButtonPressed`] when a button is clicked.
//!   `{name}` in its text shows the value of the [`Counter`] called `name`.
//! - [`Counter`] is a named integer with an optional target, and a
//!   [`Pickup`] adds to one when a player body touches it, then hides itself
//!   and fires the cue and emitter on the same entity.
//!
//! Tweens, triggers, and particles run on the fixed step, so scenario tests
//! see the same values on every run. Burst directions come from
//! [`RandomSeed`] indexed by the fixed tick and the emitter's `SceneId`.

use std::collections::{BTreeMap, BTreeSet};
use std::f32::consts::{PI, TAU};
use std::time::Duration;

use bevy_ecs::change_detection::DetectChangesMut;
use bevy_ecs::entity::Entity;
use bevy_ecs::prelude::{
    Commands, Component, Or, Query, Res, ResMut, Resource, With, World,
};
use serde::{Deserialize, Serialize};

use super::sim_math;
use super::{
    EventQueue, FrameTime, GlobalTransform, MeshRenderer, PhysicsWorld,
    RandomSeed, SceneId,
};
use crate::Transform;

/// Shape of a tween's progress over time. `apply` maps 0..1 to 0..1 (Back
/// overshoots past 1 on the way).
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize,
)]
pub enum Easing {
    #[default]
    Linear,
    QuadIn,
    QuadOut,
    QuadInOut,
    CubicOut,
    SineInOut,
    BackOut,
    BounceOut,
}

impl Easing {
    #[must_use]
    pub fn apply(self, t: f32) -> f32 {
        let t = t.clamp(0.0, 1.0);
        match self {
            Self::Linear => t,
            Self::QuadIn => t * t,
            Self::QuadOut => 1.0 - (1.0 - t) * (1.0 - t),
            Self::QuadInOut => {
                if t < 0.5 {
                    2.0 * t * t
                } else {
                    let u = -2.0 * t + 2.0;
                    1.0 - u * u / 2.0
                }
            }
            Self::CubicOut => 1.0 - (1.0 - t) * (1.0 - t) * (1.0 - t),
            Self::SineInOut => -(sim_math::sin_cos(PI * t).1 - 1.0) / 2.0,
            Self::BackOut => {
                let c1 = 1.70158;
                let u = t - 1.0;
                1.0 + (c1 + 1.0) * u * u * u + c1 * u * u
            }
            Self::BounceOut => {
                let (n, d) = (7.5625, 2.75);
                if t < 1.0 / d {
                    n * t * t
                } else if t < 2.0 / d {
                    let t = t - 1.5 / d;
                    n * t * t + 0.75
                } else if t < 2.5 / d {
                    let t = t - 2.25 / d;
                    n * t * t + 0.9375
                } else {
                    let t = t - 2.625 / d;
                    n * t * t + 0.984_375
                }
            }
        }
    }
}

/// The `Transform` field a [`Tween`] writes.
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize,
)]
pub enum TweenProperty {
    #[default]
    Position,
    Rotation,
    Scale,
}

/// What a [`Tween`] does after reaching `to`.
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize,
)]
pub enum TweenRepeat {
    /// Stops at `to`.
    #[default]
    Once,
    /// Jumps back to `from` and plays again.
    Loop,
    /// Plays back to `from`, then forward again.
    PingPong,
}

/// Animates one `Transform` property from `from` to `to`. Times are in
/// seconds of game time; rotation values are radians. One tween per entity.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Tween {
    pub property: TweenProperty,
    pub from: [f32; 3],
    pub to: [f32; 3],
    pub duration: f32,
    /// Seconds to hold `from` before starting.
    pub delay: f32,
    pub easing: Easing,
    pub repeat: TweenRepeat,
    /// Time played so far, including the delay.
    #[serde(skip)]
    pub elapsed: f32,
}

impl Default for Tween {
    fn default() -> Self {
        Self {
            property: TweenProperty::Position,
            from: [0.0; 3],
            to: [0.0, 1.0, 0.0],
            duration: 1.0,
            delay: 0.0,
            easing: Easing::QuadInOut,
            repeat: TweenRepeat::PingPong,
            elapsed: 0.0,
        }
    }
}

impl Tween {
    /// Progress through the curve, 0..1, before easing.
    #[must_use]
    pub fn progress(&self) -> f32 {
        if self.duration <= 0.0 {
            return 1.0;
        }
        let played = (self.elapsed - self.delay).max(0.0) / self.duration;
        match self.repeat {
            TweenRepeat::Once => played.min(1.0),
            TweenRepeat::Loop => played.fract(),
            TweenRepeat::PingPong => {
                let phase = played % 2.0;
                if phase > 1.0 {
                    2.0 - phase
                } else {
                    phase
                }
            }
        }
    }

    /// True once a `Once` tween has reached `to`.
    #[must_use]
    pub fn finished(&self) -> bool {
        self.repeat == TweenRepeat::Once
            && self.elapsed >= self.delay + self.duration
    }

    /// The property value at the current time.
    #[must_use]
    pub fn sample(&self) -> [f32; 3] {
        let eased = self.easing.apply(self.progress());
        std::array::from_fn(|axis| {
            self.from[axis] + (self.to[axis] - self.from[axis]) * eased
        })
    }

    /// Starts again from `from`.
    pub fn restart(&mut self) {
        self.elapsed = 0.0;
    }
}

/// How long a [`SoundCue`]'s caption shows.
pub const CUE_CAPTION_SECONDS: f32 = 2.0;

/// Sent by a [`SoundCue`] when it fires. Visible to readers on the next
/// frame, like other events.
#[derive(Clone, Debug, PartialEq)]
pub struct SoundEvent {
    pub entity: Entity,
    /// Asset path of the clip, relative to the project's `assets` folder.
    pub clip: String,
    pub volume: f32,
    /// The cue's caption, empty for none.
    pub caption: String,
}

/// Camera trauma shake: game code adds trauma on a hit or explosion, the
/// shake strength is `trauma` squared, and trauma falls by `decay` per
/// second. The shake moves only the drawn view, never the camera's
/// `Transform`, so it cannot drift.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct CameraShake {
    /// 0 to 1.
    pub trauma: f32,
    /// Trauma lost per second.
    pub decay: f32,
    /// Largest offset in metres along the camera's right, up and back axes.
    pub max_offset: [f32; 3],
    /// Largest roll in radians.
    pub max_roll: f32,
    /// Shake speed in hertz.
    pub frequency: f32,
}

impl Default for CameraShake {
    fn default() -> Self {
        Self {
            trauma: 0.0,
            decay: 1.5,
            max_offset: [0.3, 0.3, 0.0],
            max_roll: 0.05,
            frequency: 12.0,
        }
    }
}

impl CameraShake {
    /// Adds trauma, kept within 0 to 1.
    pub fn add_trauma(&mut self, amount: f32) {
        self.trauma = (self.trauma + amount).clamp(0.0, 1.0);
    }

    /// `matrix` moved and rolled in its own frame by the shake at
    /// `seconds` of game time.
    #[must_use]
    pub fn apply(&self, matrix: [[f32; 4]; 4], seconds: f32) -> [[f32; 4]; 4] {
        let strength = self.trauma * self.trauma;
        if strength <= 0.0 {
            return matrix;
        }
        // Two sines per axis at unrelated phases read as noise and stay
        // smooth from frame to frame.
        let phase = TAU * self.frequency * seconds;
        let wave = |axis: f32| {
            strength
                * (0.6 * (phase + axis * 1.7).sin()
                    + 0.4 * (2.3 * phase + axis * 4.1).sin())
        };
        let mut out = matrix;
        for (axis, offset) in self.max_offset.into_iter().enumerate() {
            let amount = offset * wave(axis as f32);
            for row in 0..3 {
                out[3][row] += matrix[axis][row] * amount;
            }
        }
        let (sin, cos) = (self.max_roll * wave(3.0)).sin_cos();
        for row in 0..3 {
            out[0][row] = matrix[0][row] * cos + matrix[1][row] * sin;
            out[1][row] = matrix[1][row] * cos - matrix[0][row] * sin;
        }
        out
    }
}

/// Bulk spawn: a scene object with this component is copied, children and
/// all, onto a grid of `count` cells `spacing` apart when the game starts
/// (or a scene loads), so a ball pit or a crowd needs one authored object
/// instead of thousands. The original fills cell 0, 0, 0; copies go along
/// positive local X, Y and Z and are named `"<name>#<n>"` from 1. Copies
/// are made in a running game only, never in the editor, and the component
/// is removed once used, so a saved state does not copy again.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct SpawnGrid {
    /// Cells along X, Y and Z, original included.
    pub count: [u32; 3],
    /// Distance between cells along X, Y and Z, in meters.
    pub spacing: [f32; 3],
}

impl Default for SpawnGrid {
    fn default() -> Self {
        Self {
            count: [1; 3],
            spacing: [1.0; 3],
        }
    }
}

/// Squash and stretch: game code calls `scene.squash(name, amount)` on a
/// landing or hit, and a damped spring wobbles the object's `Transform`
/// scale back to rest, keeping its volume. Positive amounts flatten it,
/// negative ones stretch it tall. Colliders scale with the `Transform`, so
/// put this on a visible child, not the physics body.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Squash {
    /// Spring stiffness per second squared; higher wobbles faster.
    pub stiffness: f32,
    /// Damping per second; higher settles sooner.
    pub damping: f32,
    /// Current squash, -0.8 to 0.8; 0 is at rest.
    #[serde(skip)]
    pub amount: f32,
    #[serde(skip)]
    pub velocity: f32,
    /// The scale the object returns to, taken when a squash starts.
    #[serde(skip)]
    pub rest: Option<[f32; 3]>,
}

impl Default for Squash {
    fn default() -> Self {
        Self {
            stiffness: 400.0,
            damping: 14.0,
            amount: 0.0,
            velocity: 0.0,
            rest: None,
        }
    }
}

impl Squash {
    /// Adds `amount` of squash (negative stretches).
    pub fn squash(&mut self, amount: f32) {
        self.amount = (self.amount + amount).clamp(-0.8, 0.8);
    }
}

/// Hit flash: game code calls `scene.flash(name)` and the object and its
/// children draw tinted toward `color`, fading back over `duration`
/// seconds. Only the drawn colors change; the shared material does not.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Flash {
    /// Linear RGB the object turns at full strength.
    pub color: [f32; 3],
    /// Seconds to fade from full tint back to none.
    pub duration: f32,
    /// Seconds of fade left.
    #[serde(skip)]
    pub remaining: f32,
}

impl Default for Flash {
    fn default() -> Self {
        Self {
            color: [1.0; 3],
            duration: 0.12,
            remaining: 0.0,
        }
    }
}

impl Flash {
    /// Starts the flash again at full strength.
    pub fn flash(&mut self) {
        self.remaining = self.duration;
    }

    /// Tint strength now, 1 when the flash starts and 0 once it ends.
    #[must_use]
    pub fn strength(&self) -> f32 {
        if self.duration > 0.0 {
            (self.remaining / self.duration).clamp(0.0, 1.0)
        } else {
            0.0
        }
    }
}

/// Real time left on a hit-stop freeze. The windowed loop holds back that
/// much real time from the runtime, so fixed ticks pause for a moment and
/// then carry on exactly as they would have. Simulation results never
/// change, and headless runs, which step ticks directly, ignore it.
#[derive(Resource, Clone, Copy, Debug, Default, PartialEq)]
pub struct HitStop {
    pub remaining: Duration,
}

impl HitStop {
    /// Freezes for `seconds` (0 to 1), unless a longer freeze is running.
    pub fn stop(&mut self, seconds: f32) {
        // Whole microseconds, so 0.05 is exactly 50 ms rather than f32 noise.
        let seconds = Duration::from_micros(
            (seconds.clamp(0.0, 1.0) * 1e6).round() as u64,
        );
        self.remaining = self.remaining.max(seconds);
    }
}

/// Takes a real frame delta and returns what is left of it after any
/// running hit-stop has eaten its share.
pub fn after_hit_stop(world: &mut World, delta: Duration) -> Duration {
    let Some(mut stop) = world.get_resource_mut::<HitStop>() else {
        return delta;
    };
    if stop.remaining.is_zero() {
        return delta;
    }
    let eaten = stop.remaining.min(delta);
    stop.remaining -= eaten;
    delta - eaten
}

/// Per fixed step: fades hit flashes.
pub(super) fn fade_flashes(
    time: Res<FrameTime>,
    mut flashes: Query<&mut Flash>,
) {
    let dt = time.fixed_delta.as_secs_f32();
    for mut flash in &mut flashes {
        if flash.remaining > 0.0 {
            flash.remaining = (flash.remaining - dt).max(0.0);
        }
    }
}

/// Per fixed step: moves squash springs and scales their objects.
pub(super) fn spring_squash(
    time: Res<FrameTime>,
    mut squashes: Query<(&mut Squash, &mut Transform)>,
) {
    let dt = time.fixed_delta.as_secs_f32();
    for (mut squash, mut transform) in &mut squashes {
        if squash.amount == 0.0 && squash.velocity == 0.0 {
            continue;
        }
        let rest = *squash.rest.get_or_insert(transform.scale);
        let force = -squash.stiffness * squash.amount
            - squash.damping * squash.velocity;
        squash.velocity += force * dt;
        squash.amount = (squash.amount + squash.velocity * dt).clamp(-0.8, 0.8);
        if squash.amount.abs() < 1e-4 && squash.velocity.abs() < 1e-3 {
            squash.amount = 0.0;
            squash.velocity = 0.0;
            squash.rest = None;
            transform.scale = rest;
            continue;
        }
        let tall = 1.0 - squash.amount;
        let wide = 1.0 / tall.sqrt();
        transform.scale = [rest[0] * wide, rest[1] * tall, rest[2] * wide];
    }
}

/// Per fixed step: lets camera trauma fade.
pub(super) fn decay_camera_shake(
    time: Res<FrameTime>,
    mut shakes: Query<&mut CameraShake>,
) {
    let dt = time.fixed_delta.as_secs_f32();
    for mut shake in &mut shakes {
        if shake.trauma > 0.0 {
            shake.trauma = (shake.trauma - shake.decay * dt).max(0.0);
        }
    }
}

/// Asks for a sound. See the module docs.
#[derive(Component, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct SoundCue {
    pub clip: String,
    /// Linear gain, 1 is the clip's own level.
    pub volume: f32,
    /// Fire when this body's collider starts touching another one.
    pub on_collision: bool,
    /// Caption shown for [`CUE_CAPTION_SECONDS`] each time it fires, such
    /// as `[glass breaks]`; empty shows none.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub caption: String,
    /// Closing speed in m/s at which a collision plays at full `volume`;
    /// slower hits play quieter in proportion. 0 plays every hit at full
    /// `volume`.
    pub full_volume_speed: f32,
    /// With `on_collision`, only touches of a collider whose
    /// `PhysicsMaterial::name` is this count; empty for any.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub with_material: String,
    /// Fire when a GPU physics event with this registered name arrives for
    /// this body; empty for none.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub on_gpu_event: String,
    /// Set by [`SoundCue::trigger`]; the next fixed step fires and clears it.
    #[serde(skip)]
    pub triggered: bool,
    #[serde(skip)]
    pub touching: bool,
    /// Closing speed of the contact that triggered it; `None` when game
    /// code did.
    #[serde(skip)]
    pub hit_speed: Option<f32>,
}

impl Default for SoundCue {
    fn default() -> Self {
        Self {
            clip: String::new(),
            volume: 1.0,
            on_collision: true,
            caption: String::new(),
            full_volume_speed: 0.0,
            with_material: String::new(),
            on_gpu_event: String::new(),
            triggered: false,
            touching: false,
            hit_speed: None,
        }
    }
}

impl SoundCue {
    pub fn trigger(&mut self) {
        self.triggered = true;
        self.hit_speed = None;
    }
}

/// Spawns a burst of particles. See the module docs.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct BurstEmitter {
    pub count: u32,
    /// Launch speed in metres per second.
    pub speed: f32,
    /// Seconds each particle lives.
    pub lifetime: f32,
    /// Particle size as a factor of the emitter's scale.
    pub particle_scale: f32,
    /// Downward acceleration in metres per second squared.
    pub gravity: f32,
    /// Fire when this body's collider starts touching another one.
    pub on_collision: bool,
    /// Particles per second emitted every fixed step while above 0, with
    /// no trigger: rain, smoke, sparks.
    pub rate: f32,
    /// Half extents in metres of a world-aligned box around the emitter
    /// where particles start; 0 starts them all at its center.
    pub area: [f32; 3],
    /// Height factor of each particle, so 8 makes a falling streak.
    pub stretch: f32,
    /// Carries the fraction of a particle `rate` has not emitted yet.
    #[serde(skip)]
    pub pending: f32,
    /// Set by [`BurstEmitter::trigger`]; the next fixed step fires and
    /// clears it.
    #[serde(skip)]
    pub triggered: bool,
    #[serde(skip)]
    pub touching: bool,
}

impl Default for BurstEmitter {
    fn default() -> Self {
        Self {
            count: 12,
            speed: 3.0,
            lifetime: 0.6,
            particle_scale: 0.15,
            gravity: 9.81,
            on_collision: true,
            rate: 0.0,
            area: [0.0; 3],
            stretch: 1.0,
            pending: 0.0,
            triggered: false,
            touching: false,
        }
    }
}

impl BurstEmitter {
    pub fn trigger(&mut self) {
        self.triggered = true;
    }
}

/// One particle from a [`BurstEmitter`]. Runtime only; never saved.
#[derive(Component, Clone, Copy, Debug, PartialEq)]
pub struct BurstParticle {
    pub velocity: [f32; 3],
    pub gravity: f32,
    pub remaining: f32,
    pub lifetime: f32,
    pub scale: [f32; 3],
}

/// Screen corner, edge, or center a [`HudElement`] is placed against.
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize,
)]
pub enum HudAnchor {
    #[default]
    TopLeft,
    Top,
    TopRight,
    Center,
    BottomLeft,
    Bottom,
    BottomRight,
}

/// A text label or button drawn over the game view. `offset` is in logical
/// pixels and points from the anchor toward the screen center.
#[derive(Component, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct HudElement {
    pub text: String,
    pub anchor: HudAnchor,
    pub offset: [f32; 2],
    pub font_size: f32,
    /// sRGB text color with alpha.
    pub color: [f32; 4],
    /// Draw as a button that sends [`HudButtonPressed`] when clicked.
    pub button: bool,
    /// Name of a [`Counter`] that must reach its target before this element
    /// shows.
    pub requires: Option<String>,
    /// Name of a camera: the element anchors to that camera's viewport and
    /// shows only while the camera is active.
    pub camera: Option<String>,
    /// Name of an object: the element sits on that object's place on
    /// screen (plus `follow_offset` in world space), aligned by `anchor`,
    /// with `offset` in pixels (+y down). Hidden while the object is behind
    /// the camera or missing.
    pub follow: Option<String>,
    pub follow_offset: [f32; 3],
    /// A TTF or OTF file relative to `assets/`, such as `fonts/pixel.ttf`;
    /// none uses the built-in font. Glyphs the file lacks come from the
    /// built-in font.
    pub font: Option<String>,
}

impl Default for HudElement {
    fn default() -> Self {
        Self {
            text: "Text".into(),
            anchor: HudAnchor::TopLeft,
            offset: [16.0, 16.0],
            font_size: 18.0,
            color: [1.0; 4],
            button: false,
            requires: None,
            camera: None,
            follow: None,
            follow_offset: [0.0; 3],
            font: None,
        }
    }
}

/// A weighted loot table on a named object, edited in the scene and rolled
/// by game code's `GameScene::roll_loot_table`.
#[derive(
    Component, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize,
)]
#[serde(default)]
pub struct LootTable {
    pub entries: Vec<LootEntry>,
}

/// A branching conversation on a named object, edited in the scene and
/// stepped by game code's `GameScene::start_dialogue` and
/// `advance_dialogue`. `current` is the id of the line being shown, empty
/// when the dialogue is not running, so snapshots and saves keep the spot.
#[derive(
    Component, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize,
)]
#[serde(default)]
pub struct Dialogue {
    pub lines: Vec<DialogueLine>,
    pub current: String,
}

/// One [`Dialogue`] line. `speaker`, `text` and choice texts are
/// translation keys (shown as they are when the locale lacks them). With
/// no choices the dialogue moves on to `next`; an empty or unknown `next`
/// ends it.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct DialogueLine {
    pub id: String,
    pub speaker: String,
    pub text: String,
    pub next: String,
    pub choices: Vec<DialogueChoice>,
}

/// A player answer on a [`DialogueLine`]: picking it adds `add` to the
/// counter `counter` (empty for none) and moves on to `next`.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct DialogueChoice {
    pub text: String,
    pub next: String,
    pub counter: String,
    pub add: i32,
}

/// One [`LootTable`] entry: `item` is picked with a chance of `weight`
/// over the table's total.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct LootEntry {
    pub item: String,
    pub weight: u32,
}

/// Hit points of a player, enemy or crate, changed by game code's
/// `GameScene::damage`. Objects with the same non-empty `team` are allies.
/// Scenarios can expect `/components/rusting.health/value`.
#[derive(Component, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Health {
    pub value: i32,
    /// Healing stops here.
    pub max: i32,
    pub team: String,
}

/// Sent by `GameScene::damage` to the damaged object, so a handler
/// connected with `App::connect` and registered as
/// `In<Signal<Damaged>>` reacts to hits and heals.
#[derive(bevy_ecs::event::EntityEvent, Clone, Debug, PartialEq, Eq)]
pub struct Damaged {
    pub entity: Entity,
    /// The amount asked for; negative heals.
    pub amount: i32,
    /// Hit points after the change.
    pub health: i32,
}

impl Default for Health {
    fn default() -> Self {
        Self {
            value: 3,
            max: 3,
            team: String::new(),
        }
    }
}

/// The current state of an object's state machine, such as an enemy's
/// `"patrol"` or `"chase"`, set by game code's `GameScene::set_state`.
/// Scenarios can expect `/components/rusting.state/state`.
///
/// `transitions` make it a state machine the engine runs: each fixed tick
/// the first transition that applies moves the object to its `to` state.
#[derive(
    Component, Clone, Debug, Default, PartialEq, Serialize, Deserialize,
)]
#[serde(default)]
pub struct ObjectState {
    pub state: String,
    /// The fixed tick the state was entered on.
    pub since_tick: u64,
    pub transitions: Vec<StateTransition>,
    pub actions: Vec<StateAction>,
}

impl ObjectState {
    /// The counter additions `actions` make when leaving `from` for `to`:
    /// exit actions of `from`, then enter actions of `to`.
    pub fn changes(&self, from: &str, to: &str) -> Vec<(String, i32)> {
        let exits = self.actions.iter().filter(|a| a.exit && a.state == from);
        let enters = self.actions.iter().filter(|a| !a.exit && a.state == to);
        exits
            .chain(enters)
            .filter(|action| !action.counter.is_empty())
            .map(|action| (action.counter.clone(), action.add))
            .collect()
    }
}

/// Adds `add` to the counter `counter` (created when missing) when an
/// [`ObjectState`] enters `state`, or leaves it when `exit` is set, by a
/// transition or `GameScene::set_state`.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct StateAction {
    pub state: String,
    pub exit: bool,
    pub counter: String,
    pub add: i32,
}

/// One edge of an [`ObjectState`] machine. It applies when the object is
/// in `from` (or `from` is empty), has been there `after_seconds`, and the
/// counter called `counter` (when not empty) is at least `at_least`.
/// When `held` is not empty, its input action must be held too, and when
/// `touching` is not empty, the object must touch an object of that name.
/// Taking it adds `then_add` to the counter `then_counter` (when not
/// empty), creating the counter if needed.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct StateTransition {
    pub from: String,
    pub to: String,
    pub after_seconds: f32,
    pub counter: String,
    pub at_least: i32,
    pub held: String,
    pub touching: String,
    pub then_counter: String,
    pub then_add: i32,
}

/// Moves each [`ObjectState`] along its first applying transition. Guards
/// read the counters from before this tick's transitions, and the counter
/// additions apply after all of them, so the order of objects does not
/// matter.
pub fn run_state_machines(
    mut commands: Commands,
    time: Res<FrameTime>,
    mut machines: Query<(Entity, &mut ObjectState)>,
    mut counters: Query<(&mut Counter, Option<&SceneId>)>,
    (actions, input): (
        Option<Res<super::ActionMap>>,
        Option<Res<super::RuntimeInput>>,
    ),
    physics: Option<Res<PhysicsWorld>>,
    names: Query<&super::Name>,
) {
    let touching = |entity: Entity, name: &str| {
        name.is_empty()
            || physics.as_ref().is_some_and(|physics| {
                physics.contacts().iter().any(|contact| {
                    let other = if contact.a == entity {
                        contact.b
                    } else if contact.b == entity {
                        contact.a
                    } else {
                        return false;
                    };
                    names.get(other).is_ok_and(|other| other.0 == name)
                })
            })
    };
    let held = |action: &str| {
        action.is_empty()
            || actions
                .as_ref()
                .zip(input.as_ref())
                .is_some_and(|(actions, input)| actions.held(input, action))
    };
    let mut adds = std::collections::BTreeMap::<String, i32>::new();
    let delta = time.fixed_delta.as_secs_f64();
    for (entity, mut machine) in &mut machines {
        let elapsed =
            time.fixed_tick.saturating_sub(machine.since_tick) as f64 * delta;
        let next = machine.transitions.iter().find(|edge| {
            (edge.from.is_empty() || edge.from == machine.state)
                && edge.to != machine.state
                && elapsed + 1e-9 >= f64::from(edge.after_seconds)
                && held(&edge.held)
                && touching(entity, &edge.touching)
                && (edge.counter.is_empty()
                    || find_counter(counters.iter(), &edge.counter)
                        .is_some_and(|counter| counter.value >= edge.at_least))
        });
        if let Some(edge) = next {
            let mut changes = vec![(edge.then_counter.clone(), edge.then_add)];
            changes.extend(machine.changes(&machine.state, &edge.to));
            for (counter, value) in changes {
                if !counter.is_empty() {
                    let add = adds.entry(counter).or_default();
                    *add = add.saturating_add(value);
                }
            }
            let to = edge.to.clone();
            machine.state = to;
            machine.since_tick = time.fixed_tick;
        }
    }
    for (name, add) in adds {
        match find_counter(counters.iter_mut(), &name) {
            Some(mut counter) => {
                counter.value = counter.value.saturating_add(add);
            }
            // Made the way `GameScene::set_counter` makes one, so
            // scenarios and saves find it.
            None => commands.queue(move |world: &mut World| {
                let order = super::next_spawn_order(world);
                world.spawn((
                    super::Name(name.clone()),
                    SceneId::new(),
                    order,
                    Counter {
                        name,
                        value: add,
                        target: None,
                    },
                ));
            }),
        }
    }
}

/// A named integer shown by HUD `{name}` placeholders and raised by
/// [`Pickup`]s.
#[derive(Component, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Counter {
    pub name: String,
    pub value: i32,
    /// The counter is complete once `value` reaches this.
    pub target: Option<i32>,
}

impl Default for Counter {
    fn default() -> Self {
        Self {
            name: "score".into(),
            value: 0,
            target: None,
        }
    }
}

impl Counter {
    #[must_use]
    pub fn complete(&self) -> bool {
        self.target.is_some_and(|target| self.value >= target)
    }
}

/// Collected once when a body with a `PlatformerController` or
/// `PlayerController` touches this entity's collider (make it a sensor).
/// Collecting adds `value` to the [`Counter`] named `counter`, hides the
/// entity, and triggers its [`SoundCue`] and [`BurstEmitter`].
#[derive(Component, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Pickup {
    pub counter: String,
    pub value: i32,
    /// Name of a [`Counter`] that must be complete before this can be
    /// collected.
    pub requires: Option<String>,
    pub collected: bool,
}

impl Default for Pickup {
    fn default() -> Self {
        Self {
            counter: "score".into(),
            value: 1,
            requires: None,
            collected: false,
        }
    }
}

/// The counter called `name` with the lowest `SceneId`, so duplicates
/// resolve the same way on every run.
pub(crate) fn find_counter<'a, C: std::ops::Deref<Target = Counter>>(
    counters: impl Iterator<Item = (C, Option<&'a SceneId>)>,
    name: &str,
) -> Option<C> {
    counters
        .filter(|(counter, _)| counter.name == name)
        .min_by_key(|(_, id)| id.map(|id| id.0))
        .map(|(counter, _)| counter)
}

/// Multiplies the size of HUD text and captions, for players who need
/// larger text; set by `GameScene::set_text_scale`. 1 is as authored.
#[derive(Resource, Clone, Copy, Debug, PartialEq)]
pub struct TextScale(pub f32);

impl Default for TextScale {
    fn default() -> Self {
        Self(1.0)
    }
}

/// The action a `{binding:action}` HUD button waits to rebind: the next
/// key pressed replaces its inputs on that device, Escape cancels.
#[derive(Resource, Clone, Debug, Default, PartialEq, Eq)]
pub struct RebindWait(pub Option<String>);

/// The current locale's strings, set by `GameScene::set_locale` from
/// `assets/locales/<locale>.json`, a flat JSON object of key to text.
#[derive(Resource, Clone, Debug, Default, PartialEq, Eq)]
pub struct Translations {
    pub locale: String,
    pub strings: std::collections::BTreeMap<String, String>,
}

impl Translations {
    /// The text for `key`, or `key` itself when the table lacks it, so a
    /// missing translation shows where it is missing.
    #[must_use]
    pub fn get<'a>(&'a self, key: &'a str) -> &'a str {
        self.strings.get(key).map_or(key, String::as_str)
    }

    /// The text for `key.<form>`, with `{count}` replaced by `count`,
    /// where the form is the locale's CLDR plural category for `count`
    /// (`one`, `few`, `many` or `other`); `key.other` when the table lacks
    /// that form, and `key` when it lacks both.
    #[must_use]
    pub fn plural(&self, key: &str, count: i64) -> String {
        let form = plural_form(&self.locale, count);
        self.strings
            .get(&format!("{key}.{form}"))
            .or_else(|| self.strings.get(&format!("{key}.other")))
            .map_or(key, String::as_str)
            .replace("{count}", &count.to_string())
    }
}

/// The CLDR plural category of a whole `count` in `locale` (its language
/// before any `-` or `_`).
// ponytail: integer rules for the common game languages; every other
// language uses English's one/other. Add Arabic's zero/two and Welsh when
// a game ships them.
fn plural_form(locale: &str, count: i64) -> &'static str {
    let language = locale.split(['-', '_']).next().unwrap_or_default();
    let n = count.unsigned_abs();
    let (ten, hundred) = (n % 10, n % 100);
    let few = (2..=4).contains(&ten) && !(12..=14).contains(&hundred);
    match language {
        "ja" | "zh" | "ko" | "th" | "vi" | "id" | "ms" | "tr" => "other",
        "fr" | "pt" if n <= 1 => "one",
        "ru" | "uk" | "be" | "sr" | "hr" | "bs" => match () {
            () if ten == 1 && hundred != 11 => "one",
            () if few => "few",
            () => "many",
        },
        "pl" => match () {
            () if n == 1 => "one",
            () if few => "few",
            () => "many",
        },
        "cs" | "sk" => match n {
            1 => "one",
            2..=4 => "few",
            _ => "other",
        },
        _ if n == 1 => "one",
        _ => "other",
    }
}

/// Replaces every `{tr:key}` with its translation, then every `{name}`
/// whose name is a counter with its value, so a translation can hold
/// counter placeholders.
#[must_use]
pub fn hud_text<'a>(
    text: &str,
    counters: impl Iterator<Item = (&'a Counter, Option<&'a SceneId>)> + Clone,
    translations: Option<&Translations>,
) -> String {
    let translated = fill_placeholders(text, |name| {
        let key = name.strip_prefix("tr:")?;
        Some(translations.map_or(key, |t| t.get(key)).to_owned())
    });
    fill_placeholders(&translated, |name| {
        find_counter(counters.clone(), name)
            .map(|counter| counter.value.to_string())
    })
}

/// The text of a `{dialogue:Object}` HUD placeholder: the line the named
/// object's dialogue shows, as `speaker: text` translated; and of
/// `{dialogue:Object/n}`: the line's choice `n` (from 1), or `Continue`
/// (the `dialogue.continue` translation) for `/1` on a line with no
/// choices. Empty when the dialogue is not running or has no such
/// choice; `None` when no object with a dialogue has that name.
#[must_use]
pub fn dialogue_placeholder<'a>(
    placeholder: &str,
    mut dialogues: impl Iterator<Item = (&'a super::Name, &'a Dialogue)>,
    translations: Option<&Translations>,
) -> Option<String> {
    let key = placeholder.strip_prefix("dialogue:")?;
    let (object, choice) = match key.rsplit_once('/') {
        Some((object, n)) => (object, Some(n.parse::<usize>().ok()?)),
        None => (key, None),
    };
    let (_, dialogue) = dialogues.find(|(name, _)| name.0 == object)?;
    let tr = |key: &str| translations.map_or(key, |t| t.get(key)).to_owned();
    let Some(line) = dialogue
        .lines
        .iter()
        .find(|line| line.id == dialogue.current && !line.id.is_empty())
    else {
        return Some(String::new());
    };
    Some(match choice {
        None if line.speaker.is_empty() => tr(&line.text),
        None => format!("{}: {}", tr(&line.speaker), tr(&line.text)),
        Some(1) if line.choices.is_empty() => translations
            .and_then(|t| t.strings.get("dialogue.continue").cloned())
            .unwrap_or_else(|| "Continue".to_owned()),
        Some(n) => n
            .checked_sub(1)
            .and_then(|i| line.choices.get(i))
            .map(|choice| tr(&choice.text))
            .unwrap_or_default(),
    })
}

/// The text of a `{state:Object}` HUD placeholder: the named object's
/// `rusting.state`, translated when the locale has it as a key, so a
/// quest stage such as `quest.find_hammer` reads as a sentence. `None`
/// when no object with a state has that name.
#[must_use]
pub fn state_placeholder<'a>(
    placeholder: &str,
    mut states: impl Iterator<Item = (&'a super::Name, &'a ObjectState)>,
    translations: Option<&Translations>,
) -> Option<String> {
    let object = placeholder.strip_prefix("state:")?;
    let (_, state) = states.find(|(name, _)| name.0 == object)?;
    Some(
        translations
            .map_or(&*state.state, |t| t.get(&state.state))
            .to_owned(),
    )
}

/// The text of a `{binding:action}` HUD placeholder: the inputs the
/// scene's `rusting.input_action` binds to `action`, joined with ` / `,
/// or `Press a key` (the `binding.waiting` translation) while `waiting`
/// names that action. `None` for other placeholders.
#[must_use]
pub fn binding_placeholder<'a>(
    placeholder: &str,
    mut actions: impl Iterator<Item = &'a super::InputAction>,
    waiting: Option<&str>,
    translations: Option<&Translations>,
) -> Option<String> {
    let action = placeholder.strip_prefix("binding:")?;
    if waiting == Some(action) {
        return Some(
            translations
                .and_then(|t| t.strings.get("binding.waiting").cloned())
                .unwrap_or_else(|| "Press a key".to_owned()),
        );
    }
    Some(
        actions
            .find(|found| found.action == action)
            .map(|found| found.inputs.join(" / "))
            .unwrap_or_default(),
    )
}

/// Replaces every `{name}` that `value` knows; leaves the rest as written.
fn fill_placeholders(
    text: &str,
    value: impl Fn(&str) -> Option<String>,
) -> String {
    let mut out = String::with_capacity(text.len());
    let mut rest = text;
    while let Some(open) = rest.find('{') {
        out.push_str(&rest[..open]);
        let after = &rest[open + 1..];
        let filled = after
            .find('}')
            .and_then(|close| value(&after[..close]).map(|text| (text, close)));
        match filled {
            Some((text, close)) => {
                out.push_str(&text);
                rest = &after[close + 1..];
            }
            None => {
                out.push('{');
                rest = after;
            }
        }
    }
    out.push_str(rest);
    out
}

/// Scene-wide clear color behind everything drawn, as linear RGBA. The
/// first one found sets [`RenderSettings::background_color`](super::RenderSettings).
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct SceneBackground {
    pub color: [f32; 4],
}

impl Default for SceneBackground {
    fn default() -> Self {
        Self {
            color: super::RenderSettings::default().background_color,
        }
    }
}

/// Per frame: copies the scene's background into the render settings.
pub(super) fn apply_scene_background(
    settings: Option<ResMut<super::RenderSettings>>,
    backgrounds: Query<(&SceneBackground, Option<&SceneId>)>,
) {
    let first = backgrounds
        .iter()
        .min_by_key(|(_, id)| id.map(|id| id.0))
        .map(|(background, _)| background.color);
    if let (Some(mut settings), Some(color)) = (settings, first) {
        if settings.background_color != color {
            settings.background_color = color;
        }
    }
}

/// Daylight sky computed from the sun: blue overhead, pale toward the
/// horizon, orange at sunset, dark at night. It is drawn behind everything
/// and lights surfaces from every side and in their reflections, like an
/// [`EnvironmentMap`] (which wins for lighting when both exist). The sun
/// is the shadow-casting directional light, else the first one. The one
/// on the entity with the lowest ID is used.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct ProceduralSky {
    /// Multiplies the sky's brightness, which follows the sun's.
    pub intensity: f32,
    /// 0 is clear air, 1 a hazy, washed-out sky with a wide glow around
    /// the sun.
    pub haze: f32,
    /// Linear RGB albedo of the ground seen below the horizon.
    pub ground_color: [f32; 3],
    /// Diameter of the drawn sun disc in degrees; 0 hides it. The real sun
    /// is 0.53.
    pub sun_size: f32,
    /// Share of the sky under clouds: 0 clear, 1 overcast. Clouds catch
    /// the sun at their edges and hide its disc.
    pub cloud_cover: f32,
    /// 0 white clouds, 1 dark storm clouds where they are thick.
    pub cloud_darkness: f32,
    /// How fast the clouds drift, in metres per second at their height of
    /// 2 km.
    pub cloud_speed: f32,
    /// Direction the clouds drift toward, as world x and z.
    pub wind_direction: [f32; 2],
}

impl Default for ProceduralSky {
    fn default() -> Self {
        Self {
            intensity: 1.0,
            haze: 0.2,
            ground_color: [0.3, 0.28, 0.25],
            sun_size: 1.0,
            cloud_cover: 0.0,
            cloud_darkness: 0.3,
            cloud_speed: 10.0,
            wind_direction: [1.0, 0.0],
        }
    }
}

/// Equirectangular (2:1) sky image that surfaces reflect and that lights
/// them from every side, replacing the [`SkyLight`](super::SkyLight)
/// hemisphere. Rough surfaces sample blurrier mip levels. A Radiance `.hdr`
/// image keeps light brighter than white, such as the sun. With
/// `background`, the image is also drawn behind the scene. The one on the
/// entity with the lowest ID is used.
#[derive(Component, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct EnvironmentMap {
    /// Image path relative to the project's `assets` folder.
    pub texture: std::path::PathBuf,
    pub intensity: f32,
    /// Draws the image behind the scene, in place of the clear color.
    pub background: bool,
    /// The loaded `texture`, set by the engine.
    #[serde(skip)]
    pub handle: Option<crate::assets::Handle<crate::assets::TextureAsset>>,
}

impl Default for EnvironmentMap {
    fn default() -> Self {
        Self {
            texture: std::path::PathBuf::new(),
            intensity: 1.0,
            background: false,
            handle: None,
        }
    }
}

/// Box around the entity's position whose surfaces reflect the scene as
/// seen from that position, in place of the [`EnvironmentMap`]. Reflections
/// are projected onto the box walls, so a probe sized to its room lines up
/// with the walls. Overlapping probes blend; up to four are used.
// ponytail: captured when a probe is added or changes, not every frame, so
// moving objects do not show in probe reflections.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct ReflectionProbe {
    /// Half size of the box along each world axis.
    pub extents: [f32; 3],
    pub intensity: f32,
}

impl Default for ReflectionProbe {
    fn default() -> Self {
        Self {
            extents: [5.0; 3],
            intensity: 1.0,
        }
    }
}

/// Grid of light probes filling a world-aligned box of half size `extents`
/// around the object: each probe records the light arriving from six
/// directions, and surfaces inside the box take their ambient light from
/// the nearest probes instead of the sky or environment, so a dark room
/// stays dark and a red wall tints what stands next to it. The first one in
/// entity order is used. Baked when the grid is added or changes; the High
/// quality profile also recaptures two probes per frame, so moving objects
/// and lights relight the grid within a few frames (one bounce). Eco and
/// Balanced keep the baked probes.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct LightProbeGrid {
    /// Half size of the box along each world axis.
    pub extents: [f32; 3],
    /// Probes along each axis, spread from wall to wall. The renderer
    /// clamps each to 1..=8 and the total to 64.
    pub resolution: [u32; 3],
    pub intensity: f32,
}

impl Default for LightProbeGrid {
    fn default() -> Self {
        Self {
            extents: [5.0; 3],
            resolution: [3; 3],
            intensity: 1.0,
        }
    }
}

/// Bakes the light bouncing onto this object into a lightmap: six square
/// faces of `resolution` texels wrapped around its mesh's box. Each texel
/// sees the sky and the direct light reflected off other lightmapped
/// objects, and replaces the sky, environment and probe light on the
/// object's surface. Direct light stays real-time.
// ponytail: box-projected, so concave meshes share texels between hidden
// and visible faces; a UV2 unwrap would fix that. Baked when lightmapped
// objects, their meshes or materials, or lights change.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Lightmap {
    /// Texels along each side of each face, clamped to 2..=64.
    pub resolution: u32,
}

impl Default for Lightmap {
    fn default() -> Self {
        Self { resolution: 16 }
    }
}

/// Scene-wide wind: leans every [`Foliage`] copy and swaying material as a
/// [`FieldKind::Wind`](super::FieldKind) force field would, everywhere and
/// without a sensor, and pushes particles by their emitter's `wind_factor`.
/// Physics bodies do not feel it. The first `Wind` by entity is used.
#[derive(Component, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Wind {
    /// World direction the wind blows toward.
    pub direction: [f32; 3],
    /// Push in m/s², like a wind force field's `strength`.
    pub strength: f32,
    /// Gust size as a share of `strength`, 0 for steady wind.
    pub gusts: f32,
}

impl Default for Wind {
    fn default() -> Self {
        Self {
            direction: [1.0, 0.0, 0.0],
            strength: 5.0,
            gusts: 0.5,
        }
    }
}

impl Wind {
    /// The scene's wind: the first by entity.
    #[must_use]
    pub fn of_scene(world: &World) -> Option<Self> {
        let mut winds = world.try_query::<(Entity, &Self)>()?;
        Self::first(winds.iter(world)).cloned()
    }

    /// The first wind by entity among `winds`.
    pub fn first<'a>(
        winds: impl IntoIterator<Item = (Entity, &'a Self)>,
    ) -> Option<&'a Self> {
        winds
            .into_iter()
            .min_by_key(|(entity, _)| entity.index_u32())
            .map(|(_, wind)| wind)
    }

    /// The same wind as a force field of kind `Wind`.
    #[must_use]
    pub fn field(&self) -> super::ForceField {
        super::ForceField {
            kind: super::FieldKind::Wind,
            strength: self.strength,
            direction: self.direction,
            turbulence: self.gusts,
            ..super::ForceField::default()
        }
    }

    /// Push in m/s² at world `point` after `seconds`, gusts included.
    #[must_use]
    pub fn push_at(&self, point: [f32; 3], seconds: f32) -> [f32; 3] {
        self.field()
            .push(point.into(), nalgebra::Vector3::zeros(), seconds, None)
            .into()
    }

    /// The field filling the world, for the renderer's lean.
    pub(crate) fn gpu_field(&self) -> super::GpuForceField {
        super::GpuForceField {
            model: GlobalTransform::default().matrix,
            // A box 200 km across, encoded like `GpuCollider::shape`.
            shape: [0.0, 1e5, 1e5, 1e5],
            field: self.field(),
        }
    }
}

/// Scatters `count` copies of this object's mesh over a disc of `radius`
/// metres on its local XZ plane, each turned and scaled at random by
/// `seed`, in place of the single object. Copies share the object's
/// material, so they draw as one instanced batch. Wind and other force
/// fields lean each copy away from its base by `sway` per m/s² of push.
/// `conform` sets each copy on the heightfield below it, and `layers` keeps
/// only the copies standing on chosen terrain layers. On a parent, every
/// mesh under it is copied with each placement, so a two-mesh palm
/// scatters whole.
#[derive(Component, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Foliage {
    /// Copies drawn, at most [`MAX_FOLIAGE_COPIES`].
    pub count: u32,
    /// Disc radius in metres.
    pub radius: f32,
    /// Picks the scatter; the same seed gives the same layout.
    pub seed: u32,
    /// Smallest and largest uniform scale of a copy.
    pub scale_min: f32,
    pub scale_max: f32,
    /// Lean per m/s² of force field push, in metres per metre of height.
    pub sway: f32,
    /// Moves each copy up or down onto the [`crate::runtime::Heightfield`]
    /// straight below or above it, so the scatter follows hills instead of
    /// the object's flat disc. Copies off every heightfield stay where the
    /// disc puts them. Placed again when the object or ground changes.
    pub conform: bool,
    /// Indices into the [`crate::runtime::Terrain`] layers under the copies:
    /// a copy is drawn only where these layers make up at least half of
    /// the ground's blend, such as grass but not sand. Empty draws every
    /// copy; copies off every terrain are dropped.
    pub layers: Vec<u32>,
}

/// Most copies one [`Foliage`] draws.
pub const MAX_FOLIAGE_COPIES: u32 = 65_536;

impl Default for Foliage {
    fn default() -> Self {
        Self {
            count: 100,
            radius: 5.0,
            seed: 0,
            scale_min: 0.8,
            scale_max: 1.2,
            sway: 0.03,
            conform: false,
            layers: Vec::new(),
        }
    }
}

impl Foliage {
    /// Local matrix of each copy: a point in the disc, a yaw and a scale.
    #[must_use]
    pub fn placements(&self) -> Vec<nalgebra::Matrix4<f32>> {
        let seed = crate::runtime::RandomSeed(u64::from(self.seed));
        let streams = [
            "foliage_angle",
            "foliage_distance",
            "foliage_yaw",
            "foliage_scale",
        ]
        .map(|name| crate::runtime::RandomSeed::stream(name, 0));
        (0..u64::from(self.count.min(MAX_FOLIAGE_COPIES)))
            .map(|index| {
                let [angle, distance, yaw, scale] =
                    streams.map(|stream| seed.unit(index, stream));
                // sqrt spreads copies evenly over the disc's area.
                let distance = self.radius.max(0.0) * distance.sqrt();
                let angle = angle * TAU;
                let scale =
                    self.scale_min + (self.scale_max - self.scale_min) * scale;
                nalgebra::Matrix4::new_translation(&nalgebra::Vector3::new(
                    distance * angle.cos(),
                    0.0,
                    distance * angle.sin(),
                )) * nalgebra::Matrix4::from_axis_angle(
                    &nalgebra::Vector3::y_axis(),
                    yaw * TAU,
                ) * nalgebra::Matrix4::new_scaling(scale)
            })
            .collect()
    }
}

/// Shear that leans everything above world point `base` by `lean` (x and
/// z metres per metre of height), keeping heights.
#[must_use]
pub fn lean_matrix(base: [f32; 3], lean: [f32; 2]) -> nalgebra::Matrix4<f32> {
    let base = nalgebra::Vector3::from(base);
    #[rustfmt::skip]
    let shear = nalgebra::Matrix4::new(
        1.0, lean[0], 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, lean[1], 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0,
    );
    nalgebra::Matrix4::new_translation(&base)
        * shear
        * nalgebra::Matrix4::new_translation(&-base)
}

/// Exponential height fog: the scene fades into `color` with distance, and
/// the fog thins out above `height`. Looking toward the sun brightens it.
/// The one on the entity with the lowest ID is used.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Fog {
    /// Linear RGB color that distant objects fade into.
    pub color: [f32; 3],
    /// Extinction per metre at `height`. 0.01 hides things about 300 m away.
    pub density: f32,
    /// World height, in metres, where the fog has its full `density`.
    pub height: f32,
    /// How fast the fog thins with height, per metre. 0 makes it uniform.
    pub height_falloff: f32,
    /// Brightens fog toward the shadow-casting (or first) directional light.
    pub sun_scatter: f32,
    /// How much the background behind everything fades into the fog.
    pub sky_affect: f32,
    /// How much shadows block the glow toward the sun, 0 to 1: at 1 the
    /// fog behind shadow-casting objects stays dark, showing light shafts.
    /// Needs the sun to cast shadows.
    pub light_shafts: f32,
}

impl Default for Fog {
    fn default() -> Self {
        Self {
            color: [0.5, 0.6, 0.7],
            density: 0.01,
            height: 0.0,
            height_falloff: 0.1,
            sun_scatter: 0.3,
            sky_affect: 1.0,
            light_shafts: 0.0,
        }
    }
}

impl Fog {
    /// Blends toward `other` by `t` (0 keeps `self`, 1 gives `other`).
    #[must_use]
    pub fn lerp(self, other: Self, t: f32) -> Self {
        let mix = |a: f32, b: f32| a + (b - a) * t;
        Self {
            color: std::array::from_fn(|i| mix(self.color[i], other.color[i])),
            density: mix(self.density, other.density),
            height: mix(self.height, other.height),
            height_falloff: mix(self.height_falloff, other.height_falloff),
            sun_scatter: mix(self.sun_scatter, other.sun_scatter),
            sky_affect: mix(self.sky_affect, other.sky_affect),
            light_shafts: mix(self.light_shafts, other.light_shafts),
        }
    }
}

/// Uniform fog filling a box of half size `extents` around the object,
/// turned and scaled with it: mist in a valley, smoke in a room. Volumes
/// stack in front of `rusting.fog`; the renderer uses the first eight in
/// entity order.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct FogVolume {
    /// Linear RGB color the box fades into.
    pub color: [f32; 3],
    /// Extinction per metre inside the box. 1 hides things 3 m deep.
    pub density: f32,
    /// Half size of the box in the object's local axes, in metres.
    pub extents: [f32; 3],
}

impl Default for FogVolume {
    fn default() -> Self {
        Self {
            color: [0.6, 0.6, 0.65],
            density: 0.5,
            extents: [2.0, 2.0, 2.0],
        }
    }
}

/// Makes this object's `rusting.fog` and `rusting.color_grading` apply only
/// around it: fully while the active camera is inside the box of half size
/// `extents` around the object's position (world axes), fading out over
/// `blend` metres outside the box. Where volumes overlap, the higher
/// `priority` wins. Fog and grading on objects without a volume apply
/// everywhere else.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct PostVolume {
    /// Half size of the box in metres, along the world axes.
    pub extents: [f32; 3],
    /// Distance outside the box, in metres, over which the volume fades
    /// out. 0 switches at the box wall.
    pub blend: f32,
    /// Higher volumes blend over lower ones where they overlap.
    pub priority: i32,
}

impl Default for PostVolume {
    fn default() -> Self {
        Self {
            extents: [5.0, 3.0, 5.0],
            blend: 1.0,
            priority: 0,
        }
    }
}

impl PostVolume {
    /// How much the volume applies to a camera at `eye`: 1 inside the box,
    /// fading to 0 at `blend` metres outside it.
    #[must_use]
    pub fn weight(&self, center: [f32; 3], eye: [f32; 3]) -> f32 {
        let outside = (0..3)
            .map(|i| {
                ((eye[i] - center[i]).abs() - self.extents[i])
                    .max(0.0)
                    .powi(2)
            })
            .sum::<f32>()
            .sqrt();
        if outside <= 0.0 {
            1.0
        } else if self.blend > 0.0 {
            (1.0 - outside / self.blend).max(0.0)
        } else {
            0.0
        }
    }
}

/// Glow around bright pixels. Light above `threshold` spreads over the
/// screen before tone mapping. The one on the entity with the lowest ID is
/// used.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Bloom {
    /// Share of the light above `threshold` added back as glow.
    pub intensity: f32,
    /// Linear brightness where the glow starts, with a soft knee below it.
    pub threshold: f32,
    /// 0 keeps the glow tight around bright pixels; 1 spreads it widely.
    pub spread: f32,
}

impl Default for Bloom {
    fn default() -> Self {
        Self {
            intensity: 0.5,
            threshold: 1.0,
            spread: 0.7,
        }
    }
}

/// Exposure that follows the scene's brightness, like an eye adapting. The
/// renderer measures the average brightness of the lit image every frame
/// and moves the exposure toward `key / average`, within the limits. It
/// multiplies the `ToneMapping` exposure, which stays as compensation. The
/// one on the entity with the lowest ID is used.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct AutoExposure {
    /// Average linear brightness the image is brought to; 0.18 is mid grey.
    pub key: f32,
    /// Lowest exposure, reached in very bright scenes.
    pub min_exposure: f32,
    /// Highest exposure, reached in very dark scenes.
    pub max_exposure: f32,
    /// How fast the exposure follows a change, per second. The first
    /// frame adapts at once.
    pub speed: f32,
}

impl Default for AutoExposure {
    fn default() -> Self {
        Self {
            key: 0.18,
            min_exposure: 0.25,
            max_exposure: 4.0,
            speed: 1.5,
        }
    }
}

/// Camera lens blur: objects away from `focus_distance` blur, the more the
/// farther from it, up to `blur`, the blur radius far behind the focus as a
/// fraction of the screen height. An object at half the focus distance
/// blurs as much. The one on the entity with the lowest ID is used.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct DepthOfField {
    /// Distance from the camera in metres that stays sharp.
    pub focus_distance: f32,
    /// Largest blur radius as a fraction of the screen height.
    pub blur: f32,
}

impl Default for DepthOfField {
    fn default() -> Self {
        Self {
            focus_distance: 10.0,
            blur: 0.01,
        }
    }
}

/// Camera motion blur: the image smears along the screen motion of each
/// pixel since the last frame, from the camera moving or turning. Objects
/// moving on their own do not blur. A jump of more than about a sixth of
/// the screen in one frame counts as a cut and does not blur. The one on
/// the entity with the lowest ID is used.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct MotionBlur {
    /// Fraction of the last frame's motion the smear covers, like a
    /// camera's shutter: 0.5 is a 180-degree shutter, 1 the whole frame.
    pub intensity: f32,
}

impl Default for MotionBlur {
    fn default() -> Self {
        Self { intensity: 0.5 }
    }
}

/// Shows what a camera sees on this object's mesh, like a CCTV monitor. The
/// camera's image replaces the base color and emissive maps of the object's
/// material, so a material with black base color and emissive `[1, 1, 1]`
/// is an unlit screen. Screens that share a material show the same feed;
/// give each screen its own material (a different `name` is enough). The
/// camera can stay inactive.
#[derive(Component, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct CameraScreen {
    /// Name of the camera entity to show.
    pub camera: String,
    /// Feed resolution in pixels.
    pub size: [u32; 2],
    /// Draws the feed every this many frames; 2 halves its cost. Screens
    /// with the same value take turns, so they do not all draw together.
    pub update_every: u32,
    /// Off keeps the last image without drawing; swap the material to show
    /// a dark monitor. A feed is drawn once when it first appears either way.
    pub enabled: bool,
    /// The feed's own color grading, such as scanlines and grain for a CCTV
    /// look. Without one the feed uses the scene's.
    pub grading: Option<ColorGrading>,
    /// Multiplies the scene's tone mapping exposure for this feed only, so
    /// dark rooms read on a monitor without lighting them for the player.
    /// 1 shows the feed as the player would see it.
    pub exposure: f32,
}

impl Default for CameraScreen {
    fn default() -> Self {
        Self {
            camera: String::new(),
            size: [640, 360],
            update_every: 1,
            enabled: true,
            grading: None,
            exposure: 1.0,
        }
    }
}

/// Color grading applied after tone mapping: contrast, saturation, a tint
/// for dark and for bright tones, a vignette, and film and CRT/VHS effects
/// (grain, chromatic aberration, scanlines, color bleed, a rolling noise
/// band, distortion). The defaults change nothing. The one on the entity with the lowest ID is used.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct ColorGrading {
    /// Contrast around mid grey. 1 leaves the image as is; 1.2 deepens
    /// shadows and brightens highlights.
    pub contrast: f32,
    /// 0 is grey, 1 leaves colors as they are, above 1 makes them stronger.
    pub saturation: f32,
    /// Linear RGB multiplied into dark tones, e.g. a cool `[0.9, 0.95, 1.1]`.
    pub shadows: [f32; 3],
    /// Linear RGB multiplied into bright tones, e.g. a warm `[1.1, 1.0, 0.9]`.
    pub highlights: [f32; 3],
    /// How much the corners darken. 0 is none, 1 makes them black.
    pub vignette: f32,
    /// Film grain strength, 0 to 1. The pattern changes every fixed tick
    /// and is the same for the same tick, so captures stay repeatable.
    pub grain: f32,
    /// Splits red and blue toward the edges like a cheap lens, 0 to 1.
    pub chromatic_aberration: f32,
    /// Darkens every other pixel row like a CRT, 0 to 1. Clearest on small
    /// camera-screen feeds.
    pub scanlines: f32,
    /// Smears color sideways while keeping the brightness sharp, like VHS
    /// tape, 0 to 1.
    pub color_bleed: f32,
    /// A band of static that rolls down the image, 0 to 1.
    pub noise_band: f32,
    /// Bulges the image like a curved tube and wobbles its rows over time,
    /// 0 to 1. Corners pushed off the image turn black.
    pub distortion: f32,
    /// Rain on the camera lens, 0 to 1: round drops that appear, bend the
    /// image behind them like small lenses and fade after a few seconds.
    /// Higher values cover more of the lens. Follows the fixed tick.
    pub lens_droplets: f32,
}

impl ColorGrading {
    /// The default, which changes nothing; usable in constants.
    pub const DEFAULT: Self = Self {
        contrast: 1.0,
        saturation: 1.0,
        shadows: [1.0; 3],
        highlights: [1.0; 3],
        vignette: 0.0,
        grain: 0.0,
        chromatic_aberration: 0.0,
        scanlines: 0.0,
        color_bleed: 0.0,
        noise_band: 0.0,
        distortion: 0.0,
        lens_droplets: 0.0,
    };

    /// Blends toward `other` by `t` (0 keeps `self`, 1 gives `other`).
    #[must_use]
    pub fn lerp(self, other: Self, t: f32) -> Self {
        let mix = |a: f32, b: f32| a + (b - a) * t;
        let mix3 =
            |a: [f32; 3], b: [f32; 3]| std::array::from_fn(|i| mix(a[i], b[i]));
        Self {
            contrast: mix(self.contrast, other.contrast),
            saturation: mix(self.saturation, other.saturation),
            shadows: mix3(self.shadows, other.shadows),
            highlights: mix3(self.highlights, other.highlights),
            vignette: mix(self.vignette, other.vignette),
            grain: mix(self.grain, other.grain),
            chromatic_aberration: mix(
                self.chromatic_aberration,
                other.chromatic_aberration,
            ),
            scanlines: mix(self.scanlines, other.scanlines),
            color_bleed: mix(self.color_bleed, other.color_bleed),
            noise_band: mix(self.noise_band, other.noise_band),
            distortion: mix(self.distortion, other.distortion),
            lens_droplets: mix(self.lens_droplets, other.lens_droplets),
        }
    }
}

impl Default for ColorGrading {
    fn default() -> Self {
        Self::DEFAULT
    }
}

/// Screen-space ambient occlusion: darkens ambient and sky light in creases
/// and corners where nearby geometry blocks it. Off on the Eco quality
/// profile. The one on the entity with the lowest ID is used.
#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct AmbientOcclusion {
    /// World-space distance, in metres, that is searched for occluders.
    pub radius: f32,
    /// Exponent on the occlusion. Above 1 darkens creases more.
    pub intensity: f32,
    /// Strength of light bounced off nearby surfaces within `radius`
    /// (screen-space indirect light): a red wall tints the floor beside
    /// it. 0 turns it off; it runs on the High quality profile only.
    pub indirect: f32,
}

impl Default for AmbientOcclusion {
    fn default() -> Self {
        Self {
            radius: 1.0,
            intensity: 1.0,
            indirect: 0.0,
        }
    }
}

/// Projects an image through the [`SpotLight`](super::SpotLight) on the
/// same entity, like a slide in a projector: the light's color is
/// multiplied by the image, which fills the outer cone with its top
/// toward the light's up (+Y). Black parts block the light.
// ponytail: four cookies per frame (the first four cookie spot lights in
// entity order); move to a texture array if scenes need more.
#[derive(
    Component, Clone, Debug, Default, PartialEq, Serialize, Deserialize,
)]
#[serde(default)]
pub struct LightCookie {
    /// Image path relative to the project's `assets` folder.
    pub texture: std::path::PathBuf,
    /// The loaded `texture`, set by the engine.
    #[serde(skip)]
    pub handle: Option<crate::assets::Handle<crate::assets::TextureAsset>>,
}

/// Paints an image onto the surfaces inside a box of half size `extents`
/// around the object, projected along its forward axis (-Z) with the
/// image's top along +Y: a poster on a wall, a crack, a puddle stain.
/// Surfaces facing away from the projection get none of it.
// ponytail: sixteen decals and four distinct images per frame (the first
// in entity order); move to a texture array and clustered decals if
// scenes need more.
#[derive(Component, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Decal {
    /// Image path relative to the project's `assets` folder; empty paints
    /// plain `color`.
    pub texture: std::path::PathBuf,
    /// Linear RGBA multiplied with the image; alpha scales its coverage.
    pub color: [f32; 4],
    /// Half size of the box in the object's local axes, in metres.
    pub extents: [f32; 3],
    /// Roughness painted with the image, by its coverage: low for a wet
    /// stain or a puddle. `None` keeps the surface's.
    pub roughness: Option<f32>,
    /// When its parent object breaks ([`super::Fracture`]): true moves the
    /// decal onto the piece nearest it, so it stays on that piece's surface;
    /// false removes it with the object.
    pub survive_fracture: bool,
    /// The loaded `texture`, set by the engine.
    #[serde(skip)]
    pub handle: Option<crate::assets::Handle<crate::assets::TextureAsset>>,
}

impl Default for Decal {
    fn default() -> Self {
        Self {
            texture: std::path::PathBuf::new(),
            color: [1.0; 4],
            extents: [0.5, 0.5, 0.25],
            roughness: None,
            survive_fracture: true,
            handle: None,
        }
    }
}

/// Color lookup table applied after tone mapping and [`ColorGrading`]: an
/// image strip of N squares of N x N pixels side by side (for example
/// 256 x 16 or 1024 x 32). Red runs across each square, green down it, and
/// blue from square to square; an identity strip changes nothing. Grade a
/// screenshot with an identity strip pasted in, in any image editor, then
/// save the strip. The one on the entity with the lowest ID is used.
#[derive(Component, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct ColorLut {
    /// Image path relative to the project's `assets` folder.
    pub texture: std::path::PathBuf,
    /// 0 leaves the image as is, 1 applies the table fully.
    pub intensity: f32,
    /// The loaded `texture`, set by the engine.
    #[serde(skip)]
    pub handle: Option<crate::assets::Handle<crate::assets::TextureAsset>>,
}

/// A `#rusting post 1` shader run on every pixel of the finished image,
/// after tone mapping, [`ColorGrading`] and [`ColorLut`]. See
/// `docs/surface-shaders.md`. A shader that fails to compile leaves the
/// image as is; the error is in `SceneRenderer::post_shader_error`. The one
/// on the entity with the lowest ID is used.
#[derive(
    Component, Clone, Debug, Default, PartialEq, Serialize, Deserialize,
)]
#[serde(default)]
pub struct PostShader {
    /// GLSL source starting with `#rusting post 1`.
    pub source: String,
}

/// A `#rusting sky 1` shader that colors the background wherever no
/// surface was drawn, starting from the [`ProceduralSky`] or background
/// [`EnvironmentMap`] if there is one. Fog still covers it. See
/// `docs/surface-shaders.md`. A shader that fails to compile leaves the
/// built-in background; the error is in `SceneRenderer::sky_shader_error`.
/// The one on the entity with the lowest ID is used.
#[derive(
    Component, Clone, Debug, Default, PartialEq, Serialize, Deserialize,
)]
#[serde(default)]
pub struct SkyShader {
    /// GLSL source starting with `#rusting sky 1`.
    pub source: String,
}

/// A `#rusting fog 1` shader that changes the fog over every surface and
/// the background, starting from the [`Fog`] and [`FogVolume`]s if there
/// are any. See `docs/surface-shaders.md`. A shader that fails to compile
/// leaves the built-in fog; the error is in `SceneRenderer::sky_shader_error`
/// and `SceneRenderer::surface_shader_errors`. The one on the entity with
/// the lowest ID is used.
#[derive(
    Component, Clone, Debug, Default, PartialEq, Serialize, Deserialize,
)]
#[serde(default)]
pub struct FogShader {
    /// GLSL source starting with `#rusting fog 1`.
    pub source: String,
}

impl Default for ColorLut {
    fn default() -> Self {
        Self {
            texture: std::path::PathBuf::new(),
            intensity: 1.0,
            handle: None,
        }
    }
}

/// Loads the image of each added or changed [`EnvironmentMap`],
/// [`LightCookie`], [`Decal`], [`ColorLut`] and [`super::Sprite`]. An image whose load failed (file missing or not written
/// yet) is tried again about every two seconds.
pub(super) fn load_environment_maps(
    assets: Option<ResMut<crate::assets::AssetServer>>,
    mut maps: Query<&mut EnvironmentMap>,
    mut cookies: Query<&mut LightCookie>,
    mut luts: Query<&mut ColorLut>,
    mut decals: Query<&mut Decal>,
    mut sprites: Query<&mut super::Sprite>,
    mut frame: bevy_ecs::prelude::Local<u32>,
) {
    let Some(mut assets) = assets else {
        return;
    };
    *frame = frame.wrapping_add(1);
    let retry_frame = (*frame).is_multiple_of(120);
    let mut load = |changed: bool,
                    texture: &std::path::Path,
                    handle: &mut Option<
        crate::assets::Handle<crate::assets::TextureAsset>,
    >,
                    what: &str| {
        let retry =
            handle.is_none() && !texture.as_os_str().is_empty() && retry_frame;
        if !changed && !retry {
            return;
        }
        *handle = (!texture.as_os_str().is_empty())
            .then(|| {
                assets
                    .textures
                    .handle_for_path(texture)
                    .map(Ok)
                    .unwrap_or_else(|| assets.load_texture(texture))
                    .map_err(|error| eprintln!("{what}: {error}"))
                    .ok()
            })
            .flatten();
    };
    for mut map in &mut maps {
        let changed =
            bevy_ecs::change_detection::DetectChanges::is_changed(&map);
        let map = map.bypass_change_detection();
        load(changed, &map.texture, &mut map.handle, "environment map");
    }
    for mut cookie in &mut cookies {
        let changed =
            bevy_ecs::change_detection::DetectChanges::is_changed(&cookie);
        let cookie = cookie.bypass_change_detection();
        load(changed, &cookie.texture, &mut cookie.handle, "light cookie");
    }
    for mut lut in &mut luts {
        let changed =
            bevy_ecs::change_detection::DetectChanges::is_changed(&lut);
        let lut = lut.bypass_change_detection();
        load(changed, &lut.texture, &mut lut.handle, "color lut");
    }
    for mut decal in &mut decals {
        let changed =
            bevy_ecs::change_detection::DetectChanges::is_changed(&decal);
        let decal = decal.bypass_change_detection();
        load(changed, &decal.texture, &mut decal.handle, "decal");
    }
    for mut sprite in &mut sprites {
        let changed =
            bevy_ecs::change_detection::DetectChanges::is_changed(&sprite);
        let sprite = sprite.bypass_change_detection();
        load(changed, &sprite.texture, &mut sprite.handle, "sprite");
        load(
            changed,
            &sprite.normal_map,
            &mut sprite.normal_handle,
            "sprite normal map",
        );
    }
    // Normal maps are data, not color, whatever their file says.
    // ponytail: a hot reload of the file turns it back to sRGB until the
    // sprite changes; flag linear at decode if that bites.
    for sprite in &sprites {
        let Some(handle) = sprite.normal_handle else {
            continue;
        };
        if assets.textures.get(handle).is_some_and(|texture| {
            texture.color_space == crate::assets::TextureColorSpace::Srgb
        }) {
            assets.textures.get_mut(handle).unwrap().color_space =
                crate::assets::TextureColorSpace::Linear;
        }
    }
}

/// Sent the frame a HUD button is clicked.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HudButtonPressed {
    pub entity: Entity,
}

/// Per fixed step: advances every tween and writes its property.
pub(super) fn advance_tweens(
    time: Res<FrameTime>,
    mut tweens: Query<(&mut Tween, &mut Transform)>,
) {
    let dt = time.fixed_delta.as_secs_f32();
    for (mut tween, mut transform) in &mut tweens {
        if tween.finished() {
            continue;
        }
        tween.elapsed += dt;
        let value = tween.sample();
        match tween.property {
            TweenProperty::Position => transform.position = value,
            TweenProperty::Rotation => transform.rotation = value,
            TweenProperty::Scale => transform.scale = value,
        }
    }
}

/// The material name a contact touches on `other`: a heightfield cell's
/// or tagged triangle's own surface where it has one, else the collider's
/// [`super::PhysicsMaterial`].
pub(super) fn surface_name<'a>(
    surface: Option<u8>,
    field: Option<&'a super::Heightfield>,
    tagged: Option<&'a super::MeshSurfaces>,
    material: Option<&'a super::PhysicsMaterial>,
) -> Option<&'a str> {
    surface
        .and_then(|index| {
            let surfaces = field
                .map(|field| &field.surfaces)
                .or(tagged.map(|tagged| &tagged.surfaces))?;
            surfaces.get(usize::from(index))
        })
        .map(|surface| surface.name.as_str())
        .or_else(|| material.map(|material| material.name.as_str()))
}

/// Per fixed step, after physics: marks cues and emitters whose body
/// started touching something.
pub(super) fn trigger_on_contact(
    physics: Res<PhysicsWorld>,
    materials: Query<&super::PhysicsMaterial>,
    fields: Query<&super::Heightfield>,
    tagged: Query<&super::MeshSurfaces>,
    mut cues: Query<(Entity, &mut SoundCue)>,
    mut emitters: Query<(Entity, &mut BurstEmitter)>,
) {
    if cues.is_empty() && emitters.is_empty() {
        return;
    }
    // Fastest closing speed per touching body, and per body and the
    // material of what it touches.
    let mut touching = BTreeMap::<Entity, f32>::new();
    let mut by_material = BTreeMap::<(Entity, &str), f32>::new();
    for contact in physics.contacts() {
        for (body, other) in [(contact.a, contact.b), (contact.b, contact.a)] {
            let speed = touching.entry(body).or_default();
            *speed = speed.max(contact.speed);
            let name = surface_name(
                contact.surface,
                fields.get(other).ok(),
                tagged.get(other).ok(),
                materials.get(other).ok(),
            );
            if let Some(name) = name {
                let speed = by_material.entry((body, name)).or_default();
                *speed = speed.max(contact.speed);
            }
        }
    }
    for (entity, mut cue) in &mut cues {
        let hit = if cue.with_material.is_empty() {
            touching.get(&entity)
        } else {
            by_material.get(&(entity, cue.with_material.as_str()))
        }
        .filter(|_| cue.on_collision)
        .copied();
        if hit.is_some() && !cue.touching {
            cue.triggered = true;
            cue.hit_speed = hit;
        }
        cue.touching = hit.is_some();
    }
    for (entity, mut emitter) in &mut emitters {
        let now = emitter.on_collision && touching.contains_key(&entity);
        emitter.triggered |= now && !emitter.touching;
        emitter.touching = now;
    }
}

/// Per frame: triggers sound cues named by this frame's GPU physics events.
/// The cue fires on the next fixed step.
pub(super) fn trigger_on_gpu_events(
    events: Option<Res<EventQueue<super::GpuPhysicsEvent>>>,
    registry: Option<Res<super::GpuEventRegistry>>,
    mut cues: Query<&mut SoundCue>,
) {
    let (Some(events), Some(registry)) = (events, registry) else {
        return;
    };
    for event in events.iter() {
        let Ok(mut cue) = cues.get_mut(event.entity) else {
            continue;
        };
        if !cue.on_gpu_event.is_empty()
            && registry.id(&cue.on_gpu_event) == Some(event.event_id)
        {
            cue.trigger();
        }
    }
}

/// Bodies that collect pickups.
type IsPlayer = Or<(
    With<super::PlatformerController>,
    With<super::PlayerController>,
)>;

type PickupParts = (
    Entity,
    &'static mut Pickup,
    Option<&'static SceneId>,
    Option<&'static mut SoundCue>,
    Option<&'static mut BurstEmitter>,
);

/// Per fixed step, after physics: collects pickups a player touches, in
/// `SceneId` order.
pub(super) fn collect_pickups(
    mut commands: Commands,
    physics: Res<PhysicsWorld>,
    players: Query<(), IsPlayer>,
    mut pickups: Query<PickupParts>,
    mut counters: Query<(&mut Counter, Option<&SceneId>)>,
) {
    let touched: BTreeSet<Entity> = physics
        .contacts()
        .iter()
        .flat_map(|contact| [(contact.a, contact.b), (contact.b, contact.a)])
        .filter(|(_, other)| players.contains(*other))
        .map(|(entity, _)| entity)
        .collect();
    if touched.is_empty() {
        return;
    }
    let mut ready: Vec<_> = pickups
        .iter_mut()
        .filter(|(entity, pickup, ..)| {
            !pickup.collected && touched.contains(entity)
        })
        .collect();
    ready.sort_by_key(|(entity, _, id, ..)| (id.map(|id| id.0), *entity));
    for (entity, mut pickup, _, cue, emitter) in ready {
        if let Some(required) = &pickup.requires {
            let done = find_counter(counters.iter(), required)
                .is_some_and(|counter| counter.complete());
            if !done {
                continue;
            }
        }
        pickup.collected = true;
        if let Some(mut counter) =
            find_counter(counters.iter_mut(), &pickup.counter)
        {
            counter.value += pickup.value;
        }
        commands
            .entity(entity)
            .insert(super::Visibility { visible: false });
        if let Some(mut cue) = cue {
            cue.trigger();
        }
        if let Some(mut emitter) = emitter {
            emitter.trigger();
        }
    }
}

/// Per fixed step: sends a [`SoundEvent`] for every triggered cue.
pub(super) fn fire_sound_cues(
    mut events: ResMut<EventQueue<SoundEvent>>,
    mut cues: Query<(Entity, &mut SoundCue)>,
) {
    for (entity, mut cue) in &mut cues {
        if std::mem::take(&mut cue.triggered) {
            let mut volume = cue.volume;
            match cue.hit_speed.take() {
                Some(speed) if cue.full_volume_speed > 0.0 => {
                    volume *= (speed / cue.full_volume_speed).min(1.0);
                }
                _ => {}
            }
            events.send(SoundEvent {
                entity,
                clip: cue.clip.clone(),
                volume,
                caption: cue.caption.clone(),
            });
        }
    }
}

type EmitterParts = (
    &'static mut BurstEmitter,
    &'static Transform,
    Option<&'static GlobalTransform>,
    Option<&'static SceneId>,
    Option<&'static MeshRenderer>,
    Entity,
);

/// Per fixed step: spawns particles for every triggered emitter and for
/// every emitter with a `rate`.
pub(super) fn fire_bursts(
    mut commands: Commands,
    time: Res<FrameTime>,
    seed: Res<RandomSeed>,
    mut emitters: Query<EmitterParts>,
) {
    let dt = time.fixed_delta.as_secs_f32();
    for (mut emitter, transform, global, id, mesh, entity) in &mut emitters {
        let mut count = 0;
        if std::mem::take(&mut emitter.triggered) {
            count += u64::from(emitter.count);
        }
        if emitter.rate > 0.0 {
            emitter.pending += emitter.rate * dt;
            let whole = emitter.pending.floor();
            emitter.pending -= whole;
            count += whole as u64;
        }
        if count == 0 {
            continue;
        }
        let origin = global.map_or(transform.position, |global| {
            let column = global.matrix[3];
            [column[0], column[1], column[2]]
        });
        // Authored emitters keep their stream across loads; others fall
        // back to the entity, which repeats for the same spawn order.
        let key = id.map_or(entity.to_bits(), |id| {
            let bits = id.0.as_u128();
            (bits as u64) ^ ((bits >> 64) as u64)
        });
        let stream = RandomSeed::stream("bursts", key);
        let area = RandomSeed::stream("burst_area", key);
        let mut scale =
            transform.scale.map(|axis| axis * emitter.particle_scale);
        scale[1] *= emitter.stretch;
        for index in 0..count {
            let up = seed.unit(time.fixed_tick, stream ^ (index << 1));
            let turn = seed.unit(time.fixed_tick, stream ^ (index << 1 | 1));
            let side = (1.0 - up * up).sqrt();
            let (sin, cos) = (turn * TAU).sin_cos();
            let velocity =
                [side * cos, up, side * sin].map(|axis| axis * emitter.speed);
            let particle = BurstParticle {
                velocity,
                gravity: emitter.gravity,
                remaining: emitter.lifetime,
                lifetime: emitter.lifetime,
                scale,
            };
            let position = std::array::from_fn(|axis| {
                let unit = seed
                    .unit(time.fixed_tick, area ^ (index * 3 + axis as u64));
                origin[axis] + (unit * 2.0 - 1.0) * emitter.area[axis]
            });
            let transform = Transform {
                position,
                scale,
                ..Transform::default()
            };
            let mut spawned = commands.spawn((transform, particle));
            if let Some(mesh) = mesh {
                spawned.insert(*mesh);
            }
        }
    }
}

/// Per fixed step: moves, shrinks, and removes burst particles.
pub(super) fn update_burst_particles(
    mut commands: Commands,
    time: Res<FrameTime>,
    mut particles: Query<(Entity, &mut BurstParticle, &mut Transform)>,
) {
    let dt = time.fixed_delta.as_secs_f32();
    for (entity, mut particle, mut transform) in &mut particles {
        particle.remaining -= dt;
        if particle.remaining <= 0.0 {
            commands.entity(entity).despawn();
            continue;
        }
        particle.velocity[1] -= particle.gravity * dt;
        for axis in 0..3 {
            transform.position[axis] += particle.velocity[axis] * dt;
        }
        let left = particle.remaining / particle.lifetime;
        transform.scale = particle.scale.map(|axis| axis * left);
    }
}

/// Frame rate and frame time the window runner shows in the top-right
/// corner while F3 or `RUSTING_PERF` turns it on. Absent, nothing is drawn.
#[derive(Resource, Clone, Debug, Default, PartialEq)]
pub struct PerfOverlay(pub String);

/// The [`PerfOverlay`] text, top right over a dark band.
#[cfg(feature = "ui")]
fn draw_perf_overlay(context: &egui::Context, overlay: Option<&PerfOverlay>) {
    let Some(PerfOverlay(text)) = overlay else {
        return;
    };
    egui::Area::new(egui::Id::new("rusting.perf_overlay"))
        .sense(egui::Sense::hover())
        .anchor(egui::Align2::RIGHT_TOP, egui::vec2(-8.0, 8.0))
        .fade_in(false)
        .order(egui::Order::Foreground)
        .show(context, |ui| {
            egui::Frame::new()
                .fill(egui::Color32::from_black_alpha(170))
                .inner_margin(egui::Margin::symmetric(8, 4))
                .corner_radius(4.0)
                .show(ui, |ui| {
                    ui.add(
                        egui::Label::new(
                            egui::RichText::new(text)
                                .monospace()
                                .color(egui::Color32::WHITE),
                        )
                        .selectable(false),
                    );
                });
        });
}

/// Caption lines of the sounds playing, bottom center over a dark band.
#[cfg(feature = "ui")]
fn draw_captions(
    context: &egui::Context,
    audio: Option<&super::AudioQueue>,
    settings: &super::CaptionSettings,
) {
    let lines = audio.map(super::AudioQueue::captions).unwrap_or_default();
    if !settings.enabled || lines.is_empty() {
        return;
    }
    let screen = context.screen_rect();
    // Hover only, like the HUD, so captions never take focus.
    egui::Area::new(egui::Id::new("rusting.captions"))
        .sense(egui::Sense::hover())
        .anchor(egui::Align2::CENTER_BOTTOM, egui::vec2(0.0, -48.0))
        .fade_in(false)
        .order(egui::Order::Foreground)
        .show(context, |ui| {
            ui.set_max_width(screen.width() * 0.8);
            egui::Frame::new()
                .fill(egui::Color32::from_black_alpha(170))
                .inner_margin(egui::Margin::symmetric(12, 6))
                .corner_radius(4.0)
                .show(ui, |ui| {
                    for line in lines {
                        ui.add(
                            egui::Label::new(
                                egui::RichText::new(line)
                                    .size(settings.size)
                                    .color(egui::Color32::WHITE),
                            )
                            .selectable(false),
                        );
                    }
                });
        });
}

/// The font files HUD elements asked for, loaded into egui.
#[cfg(feature = "ui")]
#[derive(Default)]
pub(super) struct HudFonts {
    /// Every file asked for, false when it did not load.
    tried: BTreeMap<String, bool>,
    definitions: egui::FontDefinitions,
}

/// The egui family for `font` under `assets`, loading the file on first
/// use. `None` when the file is not a readable font, and on the frame it
/// loads: egui binds new fonts at the start of the next pass, and naming an
/// unbound family panics.
#[cfg(feature = "ui")]
fn hud_font(
    context: &egui::Context,
    fonts: &mut HudFonts,
    assets: &std::path::Path,
    font: &str,
) -> Option<egui::FontFamily> {
    let family = egui::FontFamily::Name(font.into());
    if let Some(&loaded) = fonts.tried.get(font) {
        return (loaded && context.fonts(|f| f.families().contains(&family)))
            .then_some(family);
    }
    let path = assets.join(font);
    let bytes = crate::assets::read_file(&path).ok().filter(|bytes| {
        // egui panics on a font it cannot parse; this one checks safely.
        crate::text_texture::text_texture_in_font(
            "Aa",
            crate::text_texture::TextStyle::default(),
            bytes,
        )
        .is_some()
    });
    fonts.tried.insert(font.to_owned(), bytes.is_some());
    let Some(bytes) = bytes else {
        eprintln!(
            "warning: rusting.hud: {} is not a readable TTF or OTF font; \
             the text uses the built-in font.",
            path.display()
        );
        return None;
    };
    let definitions = &mut fonts.definitions;
    definitions.font_data.insert(
        font.to_owned(),
        std::sync::Arc::new(egui::FontData::from_owned(bytes)),
    );
    let fallback = definitions.families[&egui::FontFamily::Proportional].iter();
    let list = std::iter::once(font.to_owned()).chain(fallback.cloned());
    definitions.families.insert(family, list.collect());
    context.set_fonts(definitions.clone());
    None
}

/// Per frame: draws HUD elements in reading order and reports clicks.
#[cfg(feature = "ui")]
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
pub(super) fn draw_hud(
    ui: Res<super::RuntimeUi>,
    mut pressed: ResMut<EventQueue<HudButtonPressed>>,
    elements: Query<(Entity, &HudElement, Option<&SceneId>)>,
    counters: Query<(&Counter, Option<&SceneId>)>,
    visibility: Query<&super::Visibility>,
    parents: Query<&super::Parent>,
    cameras: Query<(
        Entity,
        &super::Name,
        &super::Camera,
        Option<&GlobalTransform>,
    )>,
    audio: Option<Res<super::AudioQueue>>,
    caption_settings: Option<Res<super::CaptionSettings>>,
    perf: Option<Res<PerfOverlay>>,
    (translations, dialogues, states, text_scale, actions, rebind, named): (
        Option<Res<Translations>>,
        Query<(&super::Name, &Dialogue)>,
        Query<(&super::Name, &ObjectState)>,
        Option<Res<TextScale>>,
        Query<&super::InputAction>,
        Option<Res<RebindWait>>,
        Query<(&super::Name, &GlobalTransform)>,
    ),
    (folder, mut fonts): (
        Option<Res<crate::project_runner::ProjectFolder>>,
        bevy_ecs::system::Local<HudFonts>,
    ),
) {
    let assets = folder.map_or_else(
        || std::path::PathBuf::from("assets"),
        |folder| folder.0.join("assets"),
    );
    let scale = text_scale.map_or(1.0, |scale| scale.0);
    let captions = caption_settings.as_deref().cloned().unwrap_or_default();
    draw_perf_overlay(ui.context(), perf.as_deref());
    draw_captions(
        ui.context(),
        audio.as_deref(),
        &super::CaptionSettings {
            size: captions.size * scale,
            ..captions
        },
    );
    // Hidden like a mesh: by itself or by any parent.
    let visible = |entity: Entity| {
        std::iter::successors(Some(entity), |&entity| {
            parents.get(entity).ok().map(|parent| parent.0)
        })
        .take(1024)
        .all(|entity| visibility.get(entity).map_or(true, |v| v.visible))
    };
    let mut elements: Vec<_> = elements.iter().collect();
    elements.sort_by_key(|(entity, _, id)| (id.map(|id| id.0), *entity));
    // Each shown element with its area, alignment and anchored point.
    let mut placed = Vec::new();
    for (entity, element, _) in elements {
        if !visible(entity) {
            continue;
        }
        if let Some(required) = &element.requires {
            if !find_counter(counters.iter(), required)
                .is_some_and(|counter| counter.complete())
            {
                continue;
            }
        }
        let screen = ui.context().screen_rect();
        // The named camera, else the one the window shows, as
        // `audio::active_camera` picks it.
        let camera = match &element.camera {
            None => cameras
                .iter()
                .filter(|(.., camera, _)| camera.active)
                .max_by_key(|(entity, _, camera, _)| {
                    (
                        camera.viewport.is_none(),
                        camera.priority,
                        std::cmp::Reverse(entity.to_bits()),
                    )
                }),
            Some(wanted) => {
                let found = cameras.iter().find(|(_, name, camera, _)| {
                    name.0 == *wanted && camera.active
                });
                if found.is_none() {
                    continue;
                }
                found
            }
        };
        let area = match camera.and_then(|(.., camera, _)| camera.viewport) {
            Some([x, y, w, h]) if element.camera.is_some() => {
                egui::Rect::from_min_size(
                    screen.min + egui::vec2(x, y) * screen.size(),
                    egui::vec2(w, h) * screen.size(),
                )
            }
            _ => screen,
        };
        let (align, inward) = match element.anchor {
            HudAnchor::TopLeft => (egui::Align2::LEFT_TOP, [1.0, 1.0]),
            HudAnchor::Top => (egui::Align2::CENTER_TOP, [1.0, 1.0]),
            HudAnchor::TopRight => (egui::Align2::RIGHT_TOP, [-1.0, 1.0]),
            HudAnchor::Center => (egui::Align2::CENTER_CENTER, [1.0, 1.0]),
            HudAnchor::BottomLeft => (egui::Align2::LEFT_BOTTOM, [1.0, -1.0]),
            HudAnchor::Bottom => (egui::Align2::CENTER_BOTTOM, [1.0, -1.0]),
            HudAnchor::BottomRight => {
                (egui::Align2::RIGHT_BOTTOM, [-1.0, -1.0])
            }
        };
        let offset = egui::vec2(
            element.offset[0] * inward[0],
            element.offset[1] * inward[1],
        );
        let point = match &element.follow {
            None => align.pos_in_rect(&area) + offset,
            Some(target) => {
                let Some(on_screen) = camera.and_then(|(.., camera, view)| {
                    let (_, at) =
                        named.iter().find(|(name, _)| name.0 == *target)?;
                    let at = at.matrix[3];
                    let point = [0, 1, 2]
                        .map(|axis| at[axis] + element.follow_offset[axis]);
                    super::picking::project_point(
                        point.into(),
                        *camera,
                        *view?,
                        area.min.into(),
                        area.size().into(),
                    )
                }) else {
                    continue;
                };
                egui::pos2(on_screen[0], on_screen[1])
                    + egui::vec2(element.offset[0], element.offset[1])
            }
        };
        placed.push((entity, element, align, point));
    }
    // Drawn in reading order, top to bottom then left to right, because
    // egui moves keyboard and gamepad focus in drawing order.
    placed.sort_by(|a, b| {
        (a.3.y.total_cmp(&b.3.y)).then(a.3.x.total_cmp(&b.3.x))
    });
    for (entity, element, align, point) in placed {
        let [r, g, b, a] =
            element.color.map(|c| (c.clamp(0.0, 1.0) * 255.0) as u8);
        let filled = fill_placeholders(&element.text, |placeholder| {
            dialogue_placeholder(
                placeholder,
                dialogues.iter(),
                translations.as_deref(),
            )
            .or_else(|| {
                state_placeholder(
                    placeholder,
                    states.iter(),
                    translations.as_deref(),
                )
            })
            .or_else(|| {
                binding_placeholder(
                    placeholder,
                    actions.iter(),
                    rebind.as_ref().and_then(|wait| wait.0.as_deref()),
                    translations.as_deref(),
                )
            })
        });
        let filled =
            hud_text(&filled, counters.iter(), translations.as_deref());
        // An empty element draws nothing, so a dialogue's unused choice
        // buttons hide.
        if filled.trim().is_empty() {
            continue;
        }
        let mut text = egui::RichText::new(filled)
            .size(element.font_size * scale)
            .color(egui::Color32::from_rgba_unmultiplied(r, g, b, a));
        if let Some(font) = &element.font {
            if let Some(family) =
                hud_font(ui.context(), &mut fonts, &assets, font)
            {
                text = text.family(family);
            }
        }
        // Measure this frame's text: an anchored egui area places itself by
        // last frame's size, so a value that grew ran past the edge.
        let context = ui.context();
        let galley = egui::WidgetText::from(text.clone()).into_galley_impl(
            context,
            &context.style(),
            egui::text::TextWrapping::no_max_width(),
            egui::FontSelection::Default,
            egui::Align::LEFT,
        );
        let padding = if element.button {
            2.0 * context.style().spacing.button_padding
        } else {
            egui::Vec2::ZERO
        };
        let position = align.anchor_size(point, galley.size() + padding).min;
        let id = egui::Id::new(("rusting.hud", entity));
        if !element.button {
            // Painted straight onto a layer: a new egui area stays hidden
            // for its first frame, which a tick-0 capture is. Like a Godot
            // label, plain text lets the pointer through.
            context
                .layer_painter(egui::LayerId::new(egui::Order::Middle, id))
                .galley(position, galley, egui::Color32::PLACEHOLDER);
            continue;
        }
        // Hover only: an area that senses clicks takes keyboard and gamepad
        // focus ahead of its button. No fade-in: an element shown for a few
        // ticks would stay faint.
        egui::Area::new(id)
            .sense(egui::Sense::hover())
            .fixed_pos(position)
            .fade_in(false)
            .show(context, |ui| {
                if ui.button(text).clicked() {
                    pressed.send(HudButtonPressed { entity });
                }
            });
    }
}

#[cfg(all(test, feature = "ui"))]
mod tests {
    use super::*;

    #[test]
    fn hud_fonts_bind_on_the_pass_after_they_load() {
        let assets = std::env::temp_dir()
            .join(format!("rusting-hud-font-{}", std::process::id()));
        std::fs::create_dir_all(assets.join("fonts")).unwrap();
        let pixel = crate::fontgen::synth("pixel").unwrap();
        std::fs::write(assets.join("fonts/pixel.ttf"), pixel).unwrap();
        std::fs::write(assets.join("fonts/bad.ttf"), "no").unwrap();
        let context = egui::Context::default();
        let mut fonts = HudFonts::default();
        let mut pass = |font: &str| {
            let mut family = None;
            let _ = context.run(egui::RawInput::default(), |context| {
                family = hud_font(context, &mut fonts, &assets, font);
            });
            family
        };
        // Loaded on the first pass, usable from the second.
        assert_eq!(pass("fonts/pixel.ttf"), None);
        let family = pass("fonts/pixel.ttf").expect("bound after one pass");
        assert_eq!(family, egui::FontFamily::Name("fonts/pixel.ttf".into()));
        for font in ["fonts/bad.ttf", "fonts/missing.ttf"] {
            assert_eq!(pass(font), None);
            assert_eq!(pass(font), None, "{font} never binds");
        }
        // The pixel font draws text differently from the built-in one,
        // and the fallback keeps characters it lacks.
        let width = |family: egui::FontFamily, text: &str| {
            context.fonts(|fonts| {
                fonts
                    .layout_no_wrap(
                        text.into(),
                        egui::FontId::new(16.0, family),
                        egui::Color32::WHITE,
                    )
                    .size()
                    .x
            })
        };
        let proportional = egui::FontFamily::Proportional;
        assert_ne!(
            width(family.clone(), "Score"),
            width(proportional, "Score")
        );
        assert!(width(family, "é") > 0.0);
        std::fs::remove_dir_all(assets).unwrap();
    }
}