proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
Documentation
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
//! Game-specific tween presets — ready-to-call functions that wire the tween
//! engine to specific game events: menu navigation, combat, screen transitions,
//! stat changes, level ups, death, victory, and more.
//!
//! Each function takes a `&mut TweenManager` and the minimum context needed
//! (glyph IDs, bar IDs, positions, values), starts the relevant tweens, and
//! returns the tween IDs for optional cancellation.

use std::f32::consts::TAU;
use glam::{Vec3, Vec4};

use super::easing::Easing;
use super::tween_manager::{TweenManager, TweenTarget, TweenId, BarId};
use crate::glyph::GlyphId;

// ═══════════════════════════════════════════════════════════════════════════
// MENU
// ═══════════════════════════════════════════════════════════════════════════

/// Slide the menu cursor glyph from one Y position to another.
pub fn menu_cursor_slide(
    mgr: &mut TweenManager,
    cursor_glyph: GlyphId,
    from_y: f32,
    to_y: f32,
) -> TweenId {
    mgr.cancel_glyph(cursor_glyph);
    mgr.start(
        TweenTarget::GlyphPositionY(cursor_glyph),
        from_y, to_y, 0.1,
        Easing::EaseOutCubic,
    )
}

/// Pulse the cursor glyph's emission to indicate it's selected.
pub fn menu_cursor_pulse(
    mgr: &mut TweenManager,
    cursor_glyph: GlyphId,
) -> TweenId {
    mgr.start(
        TweenTarget::GlyphEmission(cursor_glyph),
        0.3, 0.8, 0.6,
        Easing::EaseInOutSine,
    )
}

/// Menu selection confirm: flash bright then fade.
pub fn menu_select_flash(
    mgr: &mut TweenManager,
    glyphs: &[GlyphId],
) -> Vec<TweenId> {
    let mut ids = Vec::new();
    for &glyph in glyphs {
        ids.push(mgr.start(
            TweenTarget::GlyphEmission(glyph),
            0.0, 2.0, 0.08,
            Easing::EaseOutExpo,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphEmission(glyph),
            2.0, 0.0, 0.2, 0.08,
            Easing::EaseInQuad,
        ));
    }
    ids
}

/// Theme/screen crossfade: fade out old, swap, fade in new.
pub fn screen_crossfade(
    mgr: &mut TweenManager,
    fade_duration: f32,
    on_midpoint: impl FnOnce(&mut TweenManager) + Send + 'static,
) -> TweenId {
    mgr.start_with_callback(
        TweenTarget::ScreenFade,
        0.0, 1.0, fade_duration,
        Easing::EaseInQuad,
        move |mgr| {
            on_midpoint(mgr);
            mgr.start(
                TweenTarget::ScreenFade,
                1.0, 0.0, fade_duration,
                Easing::EaseOutQuad,
            );
        },
    )
}

/// Instant fade to black, hold, then fade in.
pub fn screen_fade_through_black(
    mgr: &mut TweenManager,
    fade_out: f32,
    hold: f32,
    fade_in: f32,
) -> TweenId {
    mgr.start_with_callback(
        TweenTarget::ScreenFade,
        0.0, 1.0, fade_out,
        Easing::EaseInQuad,
        move |mgr| {
            mgr.start_delayed(
                TweenTarget::ScreenFade,
                1.0, 0.0, fade_in, hold,
                Easing::EaseOutQuad,
            );
        },
    )
}

// ═══════════════════════════════════════════════════════════════════════════
// HP / MP / BARS
// ═══════════════════════════════════════════════════════════════════════════

/// Smoothly animate an HP bar from current to new percentage.
pub fn hp_bar_change(
    mgr: &mut TweenManager,
    bar: BarId,
    from_pct: f32,
    to_pct: f32,
) -> TweenId {
    mgr.start_tagged(
        TweenTarget::BarFillPercent(bar),
        from_pct, to_pct, 0.3,
        Easing::EaseOutQuad,
        "hp_bar",
    )
}

/// Ghost bar (recent damage indicator): delays, then slides down.
pub fn hp_ghost_bar(
    mgr: &mut TweenManager,
    bar: BarId,
    from_pct: f32,
    to_pct: f32,
) -> TweenId {
    mgr.start_delayed(
        TweenTarget::BarGhostPercent(bar),
        from_pct, to_pct, 0.5, 0.3,
        Easing::EaseInQuad,
    )
}

/// Full HP change: animate both fill and ghost bar.
pub fn hp_bar_damage(
    mgr: &mut TweenManager,
    bar: BarId,
    old_pct: f32,
    new_pct: f32,
) -> (TweenId, TweenId) {
    let fill = hp_bar_change(mgr, bar, old_pct, new_pct);
    let ghost = hp_ghost_bar(mgr, bar, old_pct, new_pct);
    (fill, ghost)
}

/// MP bar smooth change (no ghost).
pub fn mp_bar_change(
    mgr: &mut TweenManager,
    bar: BarId,
    from_pct: f32,
    to_pct: f32,
) -> TweenId {
    mgr.start_tagged(
        TweenTarget::BarFillPercent(bar),
        from_pct, to_pct, 0.25,
        Easing::EaseOutCubic,
        "mp_bar",
    )
}

/// XP bar fill (slower, more satisfying).
pub fn xp_bar_fill(
    mgr: &mut TweenManager,
    bar: BarId,
    from_pct: f32,
    to_pct: f32,
) -> TweenId {
    mgr.start(
        TweenTarget::BarFillPercent(bar),
        from_pct, to_pct, 0.8,
        Easing::EaseOutBack,
    )
}

// ═══════════════════════════════════════════════════════════════════════════
// DAMAGE NUMBERS
// ═══════════════════════════════════════════════════════════════════════════

/// Spawn a damage number arc: rises, scales from big to normal, fades out.
pub fn damage_number_arc(
    mgr: &mut TweenManager,
    glyph: GlyphId,
    start_y: f32,
    rise_height: f32,
) -> Vec<TweenId> {
    vec![
        // Rise upward
        mgr.start(
            TweenTarget::GlyphPositionY(glyph),
            start_y, start_y + rise_height, 1.0,
            Easing::EaseOutQuad,
        ),
        // Fade out
        mgr.start(
            TweenTarget::GlyphAlpha(glyph),
            1.0, 0.0, 1.0,
            Easing::EaseInQuad,
        ),
        // Start big, spring to normal size
        mgr.start(
            TweenTarget::GlyphScale(glyph),
            2.0, 1.0, 0.2,
            Easing::Spring { stiffness: 8.0, damping: 0.4 },
        ),
    ]
}

/// Critical hit damage number: bigger arc, gold flash, longer duration.
pub fn crit_damage_number(
    mgr: &mut TweenManager,
    glyph: GlyphId,
    start_y: f32,
    rise_height: f32,
) -> Vec<TweenId> {
    vec![
        mgr.start(
            TweenTarget::GlyphPositionY(glyph),
            start_y, start_y + rise_height * 1.5, 1.2,
            Easing::EaseOutQuad,
        ),
        mgr.start(
            TweenTarget::GlyphAlpha(glyph),
            1.0, 0.0, 1.2,
            Easing::EaseInCubic,
        ),
        mgr.start(
            TweenTarget::GlyphScale(glyph),
            3.0, 1.2, 0.3,
            Easing::Spring { stiffness: 6.0, damping: 0.3 },
        ),
        // Gold emission flash
        mgr.start(
            TweenTarget::GlyphEmission(glyph),
            3.0, 0.5, 0.3,
            Easing::EaseOutExpo,
        ),
    ]
}

/// Healing number: green, floats up gently.
pub fn heal_number(
    mgr: &mut TweenManager,
    glyph: GlyphId,
    start_y: f32,
) -> Vec<TweenId> {
    vec![
        mgr.start(
            TweenTarget::GlyphPositionY(glyph),
            start_y, start_y + 2.0, 0.8,
            Easing::EaseOutSine,
        ),
        mgr.start(
            TweenTarget::GlyphAlpha(glyph),
            1.0, 0.0, 0.8,
            Easing::EaseInQuad,
        ),
        mgr.start(
            TweenTarget::GlyphScale(glyph),
            1.5, 1.0, 0.15,
            Easing::EaseOutBack,
        ),
    ]
}

// ═══════════════════════════════════════════════════════════════════════════
// STAT CHANGES
// ═══════════════════════════════════════════════════════════════════════════

/// Flash a stat glyph gold when a stat increases.
pub fn stat_increase_flash(
    mgr: &mut TweenManager,
    glyph: GlyphId,
) -> Vec<TweenId> {
    vec![
        // Flash bright
        mgr.start(
            TweenTarget::GlyphEmission(glyph),
            0.0, 2.0, 0.1,
            Easing::EaseOutQuad,
        ),
        // Fade back
        mgr.start_delayed(
            TweenTarget::GlyphEmission(glyph),
            2.0, 0.0, 0.3, 0.1,
            Easing::EaseInQuad,
        ),
        // Scale pop
        mgr.start(
            TweenTarget::GlyphScale(glyph),
            1.0, 1.3, 0.08,
            Easing::EaseOutQuad,
        ),
        mgr.start_delayed(
            TweenTarget::GlyphScale(glyph),
            1.3, 1.0, 0.15, 0.08,
            Easing::EaseOutBack,
        ),
    ]
}

/// Flash red when a stat decreases.
pub fn stat_decrease_flash(
    mgr: &mut TweenManager,
    glyph: GlyphId,
) -> Vec<TweenId> {
    vec![
        mgr.start(
            TweenTarget::GlyphColorR(glyph),
            1.0, 1.0, 0.1,
            Easing::Flash,
        ),
        mgr.start(
            TweenTarget::GlyphEmission(glyph),
            0.0, 1.5, 0.08,
            Easing::EaseOutQuad,
        ),
        mgr.start_delayed(
            TweenTarget::GlyphEmission(glyph),
            1.5, 0.0, 0.2, 0.08,
            Easing::EaseInQuad,
        ),
    ]
}

// ═══════════════════════════════════════════════════════════════════════════
// STATUS EFFECTS
// ═══════════════════════════════════════════════════════════════════════════

/// Apply a status effect visual: tint + emission pulse.
pub fn status_effect_apply(
    mgr: &mut TweenManager,
    entity_glyphs: &[GlyphId],
    emission_color_intensity: f32,
) -> Vec<TweenId> {
    let mut ids = Vec::new();
    for &glyph in entity_glyphs {
        ids.push(mgr.start(
            TweenTarget::GlyphEmission(glyph),
            0.0, emission_color_intensity, 0.15,
            Easing::EaseOutExpo,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphEmission(glyph),
            emission_color_intensity, 0.0, 0.3, 0.15,
            Easing::EaseInQuad,
        ));
    }
    ids
}

// ═══════════════════════════════════════════════════════════════════════════
// LEVEL UP
// ═══════════════════════════════════════════════════════════════════════════

/// Level up pulse ring: expanding ring of glyphs radiating outward.
///
/// Returns the glyph IDs and tween IDs for the ring particles.
/// The caller must spawn the glyphs before calling this.
pub fn level_up_ring(
    mgr: &mut TweenManager,
    ring_glyphs: &[GlyphId],
    center_x: f32,
    center_y: f32,
    max_radius: f32,
) -> Vec<TweenId> {
    let count = ring_glyphs.len();
    let mut ids = Vec::new();

    for (i, &glyph) in ring_glyphs.iter().enumerate() {
        let angle = i as f32 * TAU / count as f32;
        let target_x = center_x + angle.cos() * max_radius;
        let target_y = center_y + angle.sin() * max_radius;

        // Expand outward
        ids.push(mgr.start(
            TweenTarget::GlyphPositionX(glyph),
            center_x, target_x, 0.5,
            Easing::EaseOutQuad,
        ));
        ids.push(mgr.start(
            TweenTarget::GlyphPositionY(glyph),
            center_y, target_y, 0.5,
            Easing::EaseOutQuad,
        ));
        // Fade out
        ids.push(mgr.start(
            TweenTarget::GlyphAlpha(glyph),
            1.0, 0.0, 0.5,
            Easing::EaseInQuad,
        ));
        // Bright emission
        ids.push(mgr.start(
            TweenTarget::GlyphEmission(glyph),
            2.0, 0.0, 0.5,
            Easing::EaseOutQuad,
        ));
    }

    ids
}

/// Level up screen flash.
pub fn level_up_screen_flash(mgr: &mut TweenManager) -> Vec<TweenId> {
    vec![
        mgr.start(TweenTarget::ScreenBloom, 0.5, 2.0, 0.15, Easing::EaseOutExpo),
        mgr.start_delayed(TweenTarget::ScreenBloom, 2.0, 0.5, 0.4, 0.15, Easing::EaseInQuad),
        mgr.start(TweenTarget::ScreenVignette, 0.0, 0.4, 0.1, Easing::EaseOutQuad),
        mgr.start_delayed(TweenTarget::ScreenVignette, 0.4, 0.0, 0.3, 0.1, Easing::EaseInQuad),
    ]
}

// ═══════════════════════════════════════════════════════════════════════════
// COMBAT
// ═══════════════════════════════════════════════════════════════════════════

/// Entity hit recoil: brief scale squish + position bump.
pub fn entity_hit_recoil(
    mgr: &mut TweenManager,
    entity_glyphs: &[GlyphId],
    hit_direction_x: f32,
) -> Vec<TweenId> {
    let mut ids = Vec::new();
    for &glyph in entity_glyphs {
        // Scale squish
        ids.push(mgr.start(
            TweenTarget::GlyphScaleX(glyph),
            1.0, 0.8, 0.05,
            Easing::EaseOutQuad,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphScaleX(glyph),
            0.8, 1.0, 0.1, 0.05,
            Easing::EaseOutBack,
        ));
        ids.push(mgr.start(
            TweenTarget::GlyphScaleY(glyph),
            1.0, 1.2, 0.05,
            Easing::EaseOutQuad,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphScaleY(glyph),
            1.2, 1.0, 0.1, 0.05,
            Easing::EaseOutBack,
        ));
        // Emission flash
        ids.push(mgr.start(
            TweenTarget::GlyphEmission(glyph),
            0.0, 1.5, 0.05,
            Easing::Flash,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphEmission(glyph),
            1.5, 0.0, 0.15, 0.05,
            Easing::EaseInQuad,
        ));
    }
    // Screen trauma
    ids.push(mgr.start(TweenTarget::CameraTrauma, 0.0, 0.3, 0.1, Easing::Flash));
    ids
}

/// Boss entrance: dramatic camera + screen effects.
pub fn boss_entrance(mgr: &mut TweenManager) -> Vec<TweenId> {
    vec![
        // Vignette crush
        mgr.start(TweenTarget::ScreenVignette, 0.0, 0.8, 0.3, Easing::EaseInQuad),
        mgr.start_delayed(TweenTarget::ScreenVignette, 0.8, 0.2, 0.5, 0.3, Easing::EaseOutQuad),
        // Chromatic aberration spike
        mgr.start(TweenTarget::ScreenChromaticAberration, 0.0, 0.8, 0.2, Easing::EaseOutExpo),
        mgr.start_delayed(TweenTarget::ScreenChromaticAberration, 0.8, 0.0, 0.4, 0.2, Easing::EaseInQuad),
        // Desaturation
        mgr.start(TweenTarget::ScreenSaturation, 1.0, 0.3, 0.3, Easing::EaseInQuad),
        mgr.start_delayed(TweenTarget::ScreenSaturation, 0.3, 1.0, 0.5, 0.3, Easing::EaseOutQuad),
        // Camera trauma
        mgr.start(TweenTarget::CameraTrauma, 0.0, 0.5, 0.3, Easing::EaseOutExpo),
    ]
}

/// Combat hit screen shake.
pub fn combat_hit_shake(
    mgr: &mut TweenManager,
    intensity: f32,
) -> TweenId {
    mgr.start(
        TweenTarget::CameraTrauma,
        0.0, intensity, 0.08,
        Easing::Flash,
    )
}

// ═══════════════════════════════════════════════════════════════════════════
// DEATH
// ═══════════════════════════════════════════════════════════════════════════

/// "YOU DIED" text scales up from 0 with spring easing.
pub fn death_text_reveal(
    mgr: &mut TweenManager,
    text_glyphs: &[GlyphId],
) -> Vec<TweenId> {
    let mut ids = Vec::new();
    for (i, &glyph) in text_glyphs.iter().enumerate() {
        let delay = i as f32 * 0.03; // stagger each character
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphScale(glyph),
            0.0, 1.0, 0.4, delay,
            Easing::Spring { stiffness: 6.0, damping: 0.35 },
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphAlpha(glyph),
            0.0, 1.0, 0.2, delay,
            Easing::EaseOutQuad,
        ));
    }
    // Screen effects
    ids.push(mgr.start(TweenTarget::ScreenSaturation, 1.0, 0.0, 1.5, Easing::EaseInQuad));
    ids.push(mgr.start(TweenTarget::ScreenVignette, 0.0, 0.9, 1.0, Easing::EaseInCubic));
    ids.push(mgr.start(TweenTarget::ScreenChromaticAberration, 0.0, 0.3, 0.5, Easing::EaseOutQuad));
    ids
}

/// Entity death: glyphs scatter and fade.
pub fn entity_death_scatter(
    mgr: &mut TweenManager,
    entity_glyphs: &[GlyphId],
    entity_x: f32,
    entity_y: f32,
) -> Vec<TweenId> {
    let mut ids = Vec::new();
    let count = entity_glyphs.len();
    for (i, &glyph) in entity_glyphs.iter().enumerate() {
        let angle = i as f32 * TAU / count.max(1) as f32;
        let dist = 3.0 + (i as f32 * 0.5);
        ids.push(mgr.start(
            TweenTarget::GlyphPositionX(glyph),
            entity_x, entity_x + angle.cos() * dist, 0.6,
            Easing::EaseOutQuad,
        ));
        ids.push(mgr.start(
            TweenTarget::GlyphPositionY(glyph),
            entity_y, entity_y + angle.sin() * dist, 0.6,
            Easing::EaseOutQuad,
        ));
        ids.push(mgr.start(
            TweenTarget::GlyphAlpha(glyph),
            1.0, 0.0, 0.6,
            Easing::EaseInQuad,
        ));
        ids.push(mgr.start(
            TweenTarget::GlyphRotation(glyph),
            0.0, TAU * (if i % 2 == 0 { 1.0 } else { -1.0 }), 0.6,
            Easing::EaseOutQuad,
        ));
    }
    ids
}

// ═══════════════════════════════════════════════════════════════════════════
// VICTORY
// ═══════════════════════════════════════════════════════════════════════════

/// Score counter rolls up from 0 to final value.
pub fn score_roll_up(
    mgr: &mut TweenManager,
    final_score: f32,
    duration: f32,
) -> TweenId {
    mgr.start(
        TweenTarget::Named("score_display".to_string()),
        0.0, final_score, duration,
        Easing::EaseOutCubic,
    )
}

/// Victory text entrance: each character drops in with bounce.
pub fn victory_text_drop(
    mgr: &mut TweenManager,
    text_glyphs: &[GlyphId],
    target_y: f32,
) -> Vec<TweenId> {
    let mut ids = Vec::new();
    for (i, &glyph) in text_glyphs.iter().enumerate() {
        let delay = i as f32 * 0.05;
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphPositionY(glyph),
            target_y + 10.0, target_y, 0.4, delay,
            Easing::EaseOutBounce,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphAlpha(glyph),
            0.0, 1.0, 0.1, delay,
            Easing::EaseOutQuad,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphEmission(glyph),
            2.0, 0.3, 0.3, delay,
            Easing::EaseOutQuad,
        ));
    }
    // Bloom flash
    ids.push(mgr.start(TweenTarget::ScreenBloom, 0.5, 3.0, 0.2, Easing::EaseOutExpo));
    ids.push(mgr.start_delayed(TweenTarget::ScreenBloom, 3.0, 0.5, 0.5, 0.2, Easing::EaseInQuad));
    ids
}

// ═══════════════════════════════════════════════════════════════════════════
// CHARACTER CREATION / ASSEMBLY
// ═══════════════════════════════════════════════════════════════════════════

/// Assemble entity: glyphs fly in from scattered positions to formation targets.
pub fn entity_assemble(
    mgr: &mut TweenManager,
    glyph_starts: &[(GlyphId, f32, f32)], // (glyph, start_x, start_y)
    glyph_targets: &[(f32, f32)],          // (target_x, target_y)
    stagger: f32,
) -> Vec<TweenId> {
    let mut ids = Vec::new();
    for (i, ((glyph, sx, sy), (tx, ty))) in glyph_starts.iter().zip(glyph_targets.iter()).enumerate() {
        let delay = i as f32 * stagger;
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphPositionX(*glyph),
            *sx, *tx, 0.3, delay,
            Easing::EaseOutBack,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphPositionY(*glyph),
            *sy, *ty, 0.3, delay,
            Easing::EaseOutBack,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphAlpha(*glyph),
            0.0, 1.0, 0.15, delay,
            Easing::EaseOutQuad,
        ));
    }
    ids
}

// ═══════════════════════════════════════════════════════════════════════════
// CRAFTING
// ═══════════════════════════════════════════════════════════════════════════

/// Shatter: glyphs explode outward from a center point.
pub fn crafting_shatter(
    mgr: &mut TweenManager,
    glyphs: &[GlyphId],
    center_x: f32,
    center_y: f32,
) -> Vec<TweenId> {
    entity_death_scatter(mgr, glyphs, center_x, center_y)
}

/// Forge: glyphs converge to center and flash.
pub fn crafting_forge(
    mgr: &mut TweenManager,
    glyph_positions: &[(GlyphId, f32, f32)],
    center_x: f32,
    center_y: f32,
) -> Vec<TweenId> {
    let mut ids = Vec::new();
    for (i, (glyph, sx, sy)) in glyph_positions.iter().enumerate() {
        let delay = i as f32 * 0.02;
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphPositionX(*glyph),
            *sx, center_x, 0.3, delay,
            Easing::EaseInQuad,
        ));
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphPositionY(*glyph),
            *sy, center_y, 0.3, delay,
            Easing::EaseInQuad,
        ));
    }
    // Screen flash at convergence
    ids.push(mgr.start_delayed(TweenTarget::ScreenBloom, 0.5, 3.0, 0.1, 0.3, Easing::Flash));
    ids.push(mgr.start_delayed(TweenTarget::ScreenBloom, 3.0, 0.5, 0.3, 0.4, Easing::EaseInQuad));
    ids.push(mgr.start_delayed(TweenTarget::CameraTrauma, 0.0, 0.2, 0.1, 0.3, Easing::Flash));
    ids
}

// ═══════════════════════════════════════════════════════════════════════════
// FLOOR NAVIGATION
// ═══════════════════════════════════════════════════════════════════════════

/// Slide the room cursor to a new position.
pub fn room_cursor_slide(
    mgr: &mut TweenManager,
    cursor_glyph: GlyphId,
    from_x: f32,
    from_y: f32,
    to_x: f32,
    to_y: f32,
) -> Vec<TweenId> {
    mgr.cancel_glyph(cursor_glyph);
    vec![
        mgr.start(TweenTarget::GlyphPositionX(cursor_glyph), from_x, to_x, 0.15, Easing::EaseOutCubic),
        mgr.start(TweenTarget::GlyphPositionY(cursor_glyph), from_y, to_y, 0.15, Easing::EaseOutCubic),
    ]
}

/// Room transition: brief camera pan.
pub fn room_transition_pan(
    mgr: &mut TweenManager,
    from_x: f32,
    to_x: f32,
    from_y: f32,
    to_y: f32,
) -> Vec<TweenId> {
    mgr.cancel_tag("room_pan");
    vec![
        mgr.start_tagged(TweenTarget::CameraPositionX, from_x, to_x, 0.3, Easing::EaseInOutCubic, "room_pan"),
        mgr.start_tagged(TweenTarget::CameraPositionY, from_y, to_y, 0.3, Easing::EaseInOutCubic, "room_pan"),
    ]
}

// ═══════════════════════════════════════════════════════════════════════════
// UTILITY
// ═══════════════════════════════════════════════════════════════════════════

/// Pulse a glyph's emission in a loop (for highlights, cursors, etc.).
/// Returns the tween ID — cancel to stop pulsing.
pub fn pulse_emission(
    mgr: &mut TweenManager,
    glyph: GlyphId,
    min_emission: f32,
    max_emission: f32,
    period: f32,
) -> TweenId {
    // Use a yoyo tween for continuous pulsing.
    let tween = super::Tween::new(min_emission, max_emission, period * 0.5, Easing::EaseInOutSine)
        .with_repeat(-1, true);
    let state = super::TweenState::new(tween);
    mgr.push_raw(
        TweenTarget::GlyphEmission(glyph),
        state,
        0.0,
        Some("pulse".to_string()),
        None,
    )
}

/// Text typewriter reveal: stagger alpha on each glyph.
pub fn typewriter_reveal(
    mgr: &mut TweenManager,
    glyphs: &[GlyphId],
    chars_per_second: f32,
) -> Vec<TweenId> {
    let mut ids = Vec::new();
    let interval = 1.0 / chars_per_second.max(0.01);
    for (i, &glyph) in glyphs.iter().enumerate() {
        let delay = i as f32 * interval;
        ids.push(mgr.start_delayed(
            TweenTarget::GlyphAlpha(glyph),
            0.0, 1.0, 0.05, delay,
            Easing::Step,
        ));
    }
    ids
}

// ── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn damage_number_arc_creates_three_tweens() {
        let mut mgr = TweenManager::new();
        let glyph = GlyphId(42);
        let ids = damage_number_arc(&mut mgr, glyph, 0.0, 3.0);
        assert_eq!(ids.len(), 3);
    }

    #[test]
    fn hp_bar_damage_creates_two_tweens() {
        let mut mgr = TweenManager::new();
        let bar = BarId(0);
        let (fill, ghost) = hp_bar_damage(&mut mgr, bar, 1.0, 0.7);
        assert!(mgr.is_active(fill));
        assert!(mgr.is_active(ghost));
    }

    #[test]
    fn level_up_ring_creates_four_per_glyph() {
        let mut mgr = TweenManager::new();
        let glyphs = vec![GlyphId(0), GlyphId(1), GlyphId(2)];
        let ids = level_up_ring(&mut mgr, &glyphs, 0.0, 0.0, 5.0);
        assert_eq!(ids.len(), 12); // 4 per glyph × 3 glyphs
    }

    #[test]
    fn screen_crossfade_chains() {
        let mut mgr = TweenManager::new();
        screen_crossfade(&mut mgr, 0.2, |_| {});
        mgr.tick(0.25); // complete first fade
        assert!(mgr.active_count() >= 1, "Should have started fade-in");
    }

    #[test]
    fn score_roll_up_named() {
        let mut mgr = TweenManager::new();
        score_roll_up(&mut mgr, 1000.0, 1.0);
        mgr.tick(0.5);
        let val = mgr.get_named("score_display");
        assert!(val > 0.0 && val < 1000.0);
    }
}