All files / systems/animation AnimationStateMachine.ts

87.02% Statements 161/185
83.16% Branches 84/101
100% Functions 25/25
86.88% Lines 159/183

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                          32x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     32x                               32x                     365x 365x 365x 365x 365x 365x                       365x                       365x                       365x                       365x                 365x                       365x                     365x 365x                             9975x 9975x                     9975x 9975x 9975x 9975x     9975x 9975x     9975x 1969x 1969x 1969x     1969x 1377x       1969x 108x   49x     59x 59x 46x         59x 35x     35x   35x 35x     35x 35x   35x 35x 35x 35x 35x   35x 35x                                                                         24x     7x 7x 7x 7x 7x   7x 7x   17x           17x 9x       17x 17x 17x 17x 17x   17x 3x                   9975x   9975x                                                             8371x 4x       8367x 5x     8362x 8362x   8362x         8362x       2x       8360x 8356x 11x         8360x 2x       8360x 8360x 8360x 8360x 8360x 8360x     8360x 78x     8360x                   749x                   644x                   6x                   14x                 52x 52x 52x 52x 52x 52x                   2x                                                       8083x     8083x 1x 1x     8082x                   198x                   27x 4x     23x     23x         23x                                                                                 157x   157x               157x     157x     157x     157x 1x     157x                               78x                                                               1086x 3x     1083x 1083x     1083x 1083x   1083x 1270x 1083x 1083x 1083x         1522x 1083x 1015x               68x 68x         1086x     1086x                               5x                       11x                                                   9x 9x   2x                         5x                       7x                                                 9x                       12x                                                                     6x         6x 3x                 6x                                                                 2x 1x     1x              
/**
 * Player Animation State Machine for Black Trigram
 * 
 * Manages player character animations with frame-accurate timing at 60fps.
 * Supports animation priorities, transitions, and event callbacks.
 * 
 * Based on game-design.md specifications:
 * - Attack: 12 frames (200ms at 60fps)
 * - Block: 4 frames (67ms at 60fps)
 * - Walk: 6 frames
 * - Stance change: 600ms
 * 
 * @module systems/animation/AnimationStateMachine
 * @category Animation
 * @korean 애니메이션상태머신
 */
 
import { canInterrupt } from "./AnimationPriority";
import { isTransitionAllowed, getStanceTransition, type StanceTransition } from "./AnimationTransitions";
import { 
  createMotionPredictionState, 
  updateMotionPrediction,
  predictFutureKeyframe,
  type MotionPredictionState,
  type EasingName,
} from "./KeyframeInterpolation";
import { TrigramStance } from "../../types/common";
import { AnimationState } from "./types";
import type {
  AnimationConfig,
  AnimationEvents,
  AnimationMachineState,
  AnimationPriority,
  AnimationUpdateResult,
  FallType,
} from "./types";
import type { AnimationKeyframe } from "../../types/skeletal";
import { STEP_PRIORITY } from "./types";
import { FALL_TO_GROUND_MAP } from "./types";
 
/**
 * Default animation configurations based on game-design.md
 * 
 * Frame timings:
 * - Attack: 12 frames = 200ms at 60fps
 * - Block: 4 frames = 67ms at 60fps
 * - Walk: 6 frames = 100ms at 60fps
 * - Hit: 4 frames = 67ms at 60fps
 * - Stance change: 36 frames = 600ms at 60fps
 * - Stance guards: 4-6 frames = breathing animation at 60fps
 * - Tactical steps: 18 frames = 300ms at 60fps, 30cm distance
 * 
 * Defensive animations (방어 애니메이션):
 * - Block Success (막기): 8 frames = 133ms - absorb impact, maintain guard
 * - Parry Deflect (받아넘기기): 10 frames = 167ms - redirect attack, counter window
 * - Guard Break (방어붕괴): 15 frames = 250ms - arms forced wide, vulnerable
 * - Guard Recovery (방어복구): 12 frames = 200ms - restore guard position
 * 
 * @korean 기본애니메이션설정
 */
export const DEFAULT_ANIMATION_CONFIGS: Map<AnimationState, AnimationConfig> =
  new Map<AnimationState, AnimationConfig>([
    [
      AnimationState.IDLE,
      {
        state: AnimationState.IDLE,
        frames: 4,
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 4 / 60,
      },
    ],
    [
      AnimationState.WALK,
      {
        state: AnimationState.WALK,
        frames: 6,
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 1 as AnimationPriority,
        duration: 6 / 60,
      },
    ],
    [
      AnimationState.RUN,
      {
        state: AnimationState.RUN,
        frames: 8,
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 2 as AnimationPriority,
        duration: 8 / 60,
      },
    ],
    [
      AnimationState.STANCE_CHANGE,
      {
        state: AnimationState.STANCE_CHANGE,
        frames: 36, // 600ms at 60fps
        fps: 60,
        loop: false,
        interruptible: false,
        priority: 3 as AnimationPriority,
        duration: 0.6,
        easing: "smooth-transition", // Smooth S-curve for stance changes
      },
    ],
    [
      AnimationState.STANCE_SIDE_SWITCH,
      {
        state: AnimationState.STANCE_SIDE_SWITCH,
        frames: 24, // 400ms at 60fps for left↔right switch
        fps: 60,
        loop: false,
        interruptible: false,
        priority: 3 as AnimationPriority,
        duration: 0.4,
      },
    ],
    [
      AnimationState.DEFEND,
      {
        state: AnimationState.DEFEND,
        frames: 4, // 67ms at 60fps
        fps: 60,
        loop: false,
        interruptible: true,
        priority: 4 as AnimationPriority,
        duration: 4 / 60,
      },
    ],
    // Defensive animations (방어 애니메이션) - Enhanced guard break system
    [
      AnimationState.DEFEND_BLOCK_SUCCESS,
      {
        state: AnimationState.DEFEND_BLOCK_SUCCESS,
        frames: 8, // 133ms at 60fps - absorb impact, maintain guard
        fps: 60,
        loop: false,
        interruptible: false, // Must complete block animation
        priority: 6 as AnimationPriority, // Higher than defend, same as hit
        duration: 0.133,
        easing: "controlled-slow", // Controlled deceleration for impact absorption
      },
    ],
    [
      AnimationState.DEFEND_PARRY,
      {
        state: AnimationState.DEFEND_PARRY,
        frames: 10, // 167ms at 60fps - redirect attack, open counter opportunity
        fps: 60,
        loop: false,
        interruptible: false, // Must complete parry animation
        priority: 7 as AnimationPriority, // Higher than block, creates counter window
        duration: 0.167,
        counterWindow: 0.2, // 200ms counter-attack opportunity after parry
      },
    ],
    [
      AnimationState.DEFEND_GUARD_BREAK,
      {
        state: AnimationState.DEFEND_GUARD_BREAK,
        frames: 15, // 250ms at 60fps - arms forced wide, vulnerable state
        fps: 60,
        loop: false,
        interruptible: false, // Cannot interrupt guard break
        priority: 8 as AnimationPriority, // Highest priority (same as fall)
        duration: 0.25,
        vulnerabilityDuration: 0.5, // 500ms vulnerable state after guard break
      },
    ],
    [
      AnimationState.DEFEND_RECOVERY,
      {
        state: AnimationState.DEFEND_RECOVERY,
        frames: 12, // 200ms at 60fps - restore guard position
        fps: 60,
        loop: false,
        interruptible: true, // Can be interrupted by attacks
        priority: 2 as AnimationPriority, // Same as run, lower than defend
        duration: 0.2,
        easing: "natural-motion", // Physics-based recovery
      },
    ],
    [
      AnimationState.ATTACK,
      {
        state: AnimationState.ATTACK,
        frames: 12, // 200ms at 60fps
        fps: 60,
        loop: false,
        interruptible: true,
        priority: STEP_PRIORITY,
        duration: 12 / 60,
        easing: "explosive-power", // Explosive acceleration for attacks
      },
    ],
    [
      AnimationState.HIT,
      {
        state: AnimationState.HIT,
        frames: 4, // 67ms at 60fps
        fps: 60,
        loop: false,
        interruptible: false,
        priority: 6 as AnimationPriority,
        duration: 4 / 60,
      },
    ],
    [
      AnimationState.KO,
      {
        state: AnimationState.KO,
        frames: 30, // 500ms at 60fps
        fps: 60,
        loop: false,
        interruptible: false,
        priority: 7 as AnimationPriority,
        duration: 0.5,
      },
    ],
    // Fall animations (낙법 애니메이션) - Priority 8 (highest)
    [
      AnimationState.FALL_FORWARD,
      {
        state: AnimationState.FALL_FORWARD,
        frames: 24, // 400ms at 60fps - stumble forward, knee collapse, hands brace, face-down
        fps: 60,
        loop: false,
        interruptible: false,
        priority: 8 as AnimationPriority,
        duration: 0.4,
      },
    ],
    [
      AnimationState.FALL_BACKWARD,
      {
        state: AnimationState.FALL_BACKWARD,
        frames: 30, // 500ms at 60fps - backward stumble, sit, back impact, supine
        fps: 60,
        loop: false,
        interruptible: false,
        priority: 8 as AnimationPriority,
        duration: 0.5,
      },
    ],
    [
      AnimationState.FALL_SIDE_LEFT,
      {
        state: AnimationState.FALL_SIDE_LEFT,
        frames: 27, // 450ms at 60fps - rotation, shoulder roll, side sprawl
        fps: 60,
        loop: false,
        interruptible: false,
        priority: 8 as AnimationPriority,
        duration: 0.45,
      },
    ],
    [
      AnimationState.FALL_SIDE_RIGHT,
      {
        state: AnimationState.FALL_SIDE_RIGHT,
        frames: 27, // 450ms at 60fps - rotation, shoulder roll, side sprawl
        fps: 60,
        loop: false,
        interruptible: false,
        priority: 8 as AnimationPriority,
        duration: 0.45,
      },
    ],
    // Ground state animations (지면 자세) - Breathing loops
    [
      AnimationState.GROUND_PRONE,
      {
        state: AnimationState.GROUND_PRONE,
        frames: 4, // Breathing loop on ground (face down)
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 4 / 60,
      },
    ],
    [
      AnimationState.GROUND_SUPINE,
      {
        state: AnimationState.GROUND_SUPINE,
        frames: 4, // Breathing loop on ground (face up)
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 4 / 60,
      },
    ],
    [
      AnimationState.GROUND_SIDE_LEFT,
      {
        state: AnimationState.GROUND_SIDE_LEFT,
        frames: 4, // Breathing loop on ground (left side)
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 4 / 60,
      },
    ],
    [
      AnimationState.GROUND_SIDE_RIGHT,
      {
        state: AnimationState.GROUND_SIDE_RIGHT,
        frames: 4, // Breathing loop on ground (right side)
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 4 / 60,
      },
    ],
    // Recovery animations (기상 애니메이션) - Priority 9 (higher than falls)
    [
      AnimationState.RECOVERY_PRONE_STANDUP,
      {
        state: AnimationState.RECOVERY_PRONE_STANDUP,
        frames: 30, // 500ms at 60fps - push up from prone, rise to standing
        fps: 60,
        loop: false,
        interruptible: false, // Last 6 frames (100ms) are interruptible
        priority: 9 as AnimationPriority,
        duration: 0.5,
      },
    ],
    [
      AnimationState.RECOVERY_SUPINE_STANDUP,
      {
        state: AnimationState.RECOVERY_SUPINE_STANDUP,
        frames: 36, // 600ms at 60fps - sit up, roll forward, stand
        fps: 60,
        loop: false,
        interruptible: false, // Last 6 frames (100ms) are interruptible
        priority: 9 as AnimationPriority,
        duration: 0.6,
      },
    ],
    [
      AnimationState.RECOVERY_ROLL,
      {
        state: AnimationState.RECOVERY_ROLL,
        frames: 24, // 400ms at 60fps - roll to side, spring to feet (quick recovery)
        fps: 60,
        loop: false,
        interruptible: false, // Last 6 frames (100ms) are interruptible
        priority: 9 as AnimationPriority,
        duration: 0.4,
      },
    ],
    [
      AnimationState.RECOVERY_DEFENSIVE,
      {
        state: AnimationState.RECOVERY_DEFENSIVE,
        frames: 42, // 700ms at 60fps - slow rise with guard up (vulnerable but defended)
        fps: 60,
        loop: false,
        interruptible: false, // Last 6 frames (100ms) are interruptible
        priority: 9 as AnimationPriority,
        duration: 0.7,
      },
    ],
    // 180-degree turn animations (180도 회전 애니메이션)
    [
      AnimationState.TURN_LEFT,
      {
        state: AnimationState.TURN_LEFT,
        frames: 12, // 200ms at 60fps for 180° turn
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY, // Same priority as attacks/steps - committed action
        duration: 12 / 60,
      },
    ],
    [
      AnimationState.TURN_RIGHT,
      {
        state: AnimationState.TURN_RIGHT,
        frames: 12, // 200ms at 60fps for 180° turn
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY, // Same priority as attacks/steps - committed action
        duration: 12 / 60,
      },
    ],
    // Stance-specific guard animations (팔괘 방어 자세)
    [
      AnimationState.STANCE_GUARD_GEON,
      {
        state: AnimationState.STANCE_GUARD_GEON,
        frames: 6, // Breathing animation
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 6 / 60,
      },
    ],
    [
      AnimationState.STANCE_GUARD_TAE,
      {
        state: AnimationState.STANCE_GUARD_TAE,
        frames: 6, // Breathing animation
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 6 / 60,
      },
    ],
    [
      AnimationState.STANCE_GUARD_LI,
      {
        state: AnimationState.STANCE_GUARD_LI,
        frames: 4, // Controlled breathing
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 4 / 60,
      },
    ],
    [
      AnimationState.STANCE_GUARD_JIN,
      {
        state: AnimationState.STANCE_GUARD_JIN,
        frames: 5, // Deep breathing
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 5 / 60,
      },
    ],
    [
      AnimationState.STANCE_GUARD_SON,
      {
        state: AnimationState.STANCE_GUARD_SON,
        frames: 6, // Rhythmic breathing
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 6 / 60,
      },
    ],
    [
      AnimationState.STANCE_GUARD_GAM,
      {
        state: AnimationState.STANCE_GUARD_GAM,
        frames: 6, // Flowing breathing
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 6 / 60,
      },
    ],
    [
      AnimationState.STANCE_GUARD_GAN,
      {
        state: AnimationState.STANCE_GUARD_GAN,
        frames: 4, // Steady breathing
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 4 / 60,
      },
    ],
    [
      AnimationState.STANCE_GUARD_GON,
      {
        state: AnimationState.STANCE_GUARD_GON,
        frames: 5, // Deep diaphragm breathing
        fps: 60,
        loop: true,
        interruptible: true,
        priority: 0 as AnimationPriority,
        duration: 5 / 60,
      },
    ],
    // Tactical step animations (전술적 발걸음)
    // 18 frames = 300ms at 60fps, 30cm distance per step
    [
      AnimationState.STEP_FORWARD,
      {
        state: AnimationState.STEP_FORWARD,
        frames: 18,
        fps: 60,
        loop: false,
        interruptible: false, // Non-interruptible for commitment
        priority: STEP_PRIORITY,
        duration: 0.3,
      },
    ],
    [
      AnimationState.STEP_BACK,
      {
        state: AnimationState.STEP_BACK,
        frames: 18,
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.3,
      },
    ],
    [
      AnimationState.STEP_LEFT,
      {
        state: AnimationState.STEP_LEFT,
        frames: 18,
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.3,
      },
    ],
    [
      AnimationState.STEP_RIGHT,
      {
        state: AnimationState.STEP_RIGHT,
        frames: 18,
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.3,
      },
    ],
    [
      AnimationState.STEP_FORWARD_LEFT,
      {
        state: AnimationState.STEP_FORWARD_LEFT,
        frames: 18,
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.3,
      },
    ],
    [
      AnimationState.STEP_FORWARD_RIGHT,
      {
        state: AnimationState.STEP_FORWARD_RIGHT,
        frames: 18,
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.3,
      },
    ],
    [
      AnimationState.STEP_BACK_LEFT,
      {
        state: AnimationState.STEP_BACK_LEFT,
        frames: 18,
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.3,
      },
    ],
    [
      AnimationState.STEP_BACK_RIGHT,
      {
        state: AnimationState.STEP_BACK_RIGHT,
        frames: 18,
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.3,
      },
    ],
    // Footwork patterns (보법) - Korean martial arts specialized footwork
    // Circular step (원형보) - Lateral movement maintaining guard facing
    [
      AnimationState.FOOTWORK_CIRCULAR_LEFT,
      {
        state: AnimationState.FOOTWORK_CIRCULAR_LEFT,
        frames: 18, // 300ms at 60fps
        fps: 60,
        loop: false,
        interruptible: false, // Committed footwork
        priority: STEP_PRIORITY, // Same as tactical steps
        duration: 0.3,
      },
    ],
    [
      AnimationState.FOOTWORK_CIRCULAR_RIGHT,
      {
        state: AnimationState.FOOTWORK_CIRCULAR_RIGHT,
        frames: 18, // 300ms at 60fps
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.3,
      },
    ],
    // Pivot step (축족회전) - Rotation on planted foot
    [
      AnimationState.FOOTWORK_PIVOT_LEFT,
      {
        state: AnimationState.FOOTWORK_PIVOT_LEFT,
        frames: 15, // 250ms at 60fps
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.25,
      },
    ],
    [
      AnimationState.FOOTWORK_PIVOT_RIGHT,
      {
        state: AnimationState.FOOTWORK_PIVOT_RIGHT,
        frames: 15, // 250ms at 60fps
        fps: 60,
        loop: false,
        interruptible: false,
        priority: STEP_PRIORITY,
        duration: 0.25,
      },
    ],
    // Slide step (미끄럼보) - Both feet move together
    [
      AnimationState.FOOTWORK_SLIDE_FORWARD,
      {
        state: AnimationState.FOOTWORK_SLIDE_FORWARD,
        frames: 12, // 200ms at 60fps
        fps: 60,
        loop: false,
        interruptible: true, // Can be interrupted
        priority: 4 as AnimationPriority, // Same as defend
        duration: 0.2,
      },
    ],
    [
      AnimationState.FOOTWORK_SLIDE_BACK,
      {
        state: AnimationState.FOOTWORK_SLIDE_BACK,
        frames: 12, // 200ms at 60fps
        fps: 60,
        loop: false,
        interruptible: true,
        priority: 4 as AnimationPriority,
        duration: 0.2,
      },
    ],
    [
      AnimationState.FOOTWORK_SLIDE_LEFT,
      {
        state: AnimationState.FOOTWORK_SLIDE_LEFT,
        frames: 12, // 200ms at 60fps
        fps: 60,
        loop: false,
        interruptible: true,
        priority: 4 as AnimationPriority,
        duration: 0.2,
      },
    ],
    [
      AnimationState.FOOTWORK_SLIDE_RIGHT,
      {
        state: AnimationState.FOOTWORK_SLIDE_RIGHT,
        frames: 12, // 200ms at 60fps
        fps: 60,
        loop: false,
        interruptible: true,
        priority: 4 as AnimationPriority,
        duration: 0.2,
      },
    ],
    // Shuffle step (섞음보) - Quick micro-adjustment
    [
      AnimationState.FOOTWORK_SHUFFLE,
      {
        state: AnimationState.FOOTWORK_SHUFFLE,
        frames: 6, // 100ms at 60fps
        fps: 60,
        loop: false,
        interruptible: true,
        priority: 3 as AnimationPriority, // Same as stance_change
        duration: 0.1,
      },
    ],
  ]);
 
/**
 * Player Animation State Machine
 * 
 * Manages animation state, transitions, and timing with frame-accurate updates.
 * Integrates priority system and event callbacks.
 * 
 * @example
 * ```typescript
 * const machine = new PlayerAnimationStateMachine(DEFAULT_ANIMATION_CONFIGS, {
 *   onAnimationStart: (state) => console.log(`Started ${state}`),
 *   onAnimationComplete: (state) => console.log(`Completed ${state}`),
 *   onFrame: (frame, state) => {
 *     if (state === "attack" && frame === 6) {
 *       // Execute attack at midpoint (frame 6 of 12)
 *       executeAttackLogic();
 *     }
 *   }
 * });
 * 
 * // In game loop (useFrame)
 * useFrame((state, delta) => {
 *   const result = machine.update(delta);
 *   updatePlayerVisuals(result.state, result.frame);
 * });
 * 
 * // Trigger animations
 * machine.transitionTo("attack");
 * ```
 * 
 * @korean 플레이어애니메이션상태머신
 */
export class PlayerAnimationStateMachine {
  /**
   * Static mapping from TrigramStance to guard AnimationState
   * Prevents repeated object allocation in transitionToStanceGuard()
   * @korean 자세방어상태맵
   */
  private static readonly GUARD_STATE_MAP: Record<TrigramStance, AnimationState> = {
    [TrigramStance.GEON]: AnimationState.STANCE_GUARD_GEON,
    [TrigramStance.TAE]: AnimationState.STANCE_GUARD_TAE,
    [TrigramStance.LI]: AnimationState.STANCE_GUARD_LI,
    [TrigramStance.JIN]: AnimationState.STANCE_GUARD_JIN,
    [TrigramStance.SON]: AnimationState.STANCE_GUARD_SON,
    [TrigramStance.GAM]: AnimationState.STANCE_GUARD_GAM,
    [TrigramStance.GAN]: AnimationState.STANCE_GUARD_GAN,
    [TrigramStance.GON]: AnimationState.STANCE_GUARD_GON,
  };
 
  /**
   * Static reverse mapping from guard AnimationState to TrigramStance
   * Prevents repeated object allocation in getCurrentGuardStance()
   * @korean 방어상태자세맵
   */
  private static readonly STANCE_FROM_GUARD_MAP: Record<string, TrigramStance> = {
    [AnimationState.STANCE_GUARD_GEON]: TrigramStance.GEON,
    [AnimationState.STANCE_GUARD_TAE]: TrigramStance.TAE,
    [AnimationState.STANCE_GUARD_LI]: TrigramStance.LI,
    [AnimationState.STANCE_GUARD_JIN]: TrigramStance.JIN,
    [AnimationState.STANCE_GUARD_SON]: TrigramStance.SON,
    [AnimationState.STANCE_GUARD_GAM]: TrigramStance.GAM,
    [AnimationState.STANCE_GUARD_GAN]: TrigramStance.GAN,
    [AnimationState.STANCE_GUARD_GON]: TrigramStance.GON,
  };
 
  private currentState: AnimationState = AnimationState.IDLE;
  private frameIndex = 0;
  private timeAccumulator = 0;
  private previousState: AnimationState | null = null;
  private justStarted = false;
  private justCompleted = false;
  
  /**
   * Current stance transition data (null when not in stance_change animation)
   * 
   * **Korean**: 현재 자세 전환 데이터
   * 
   * Tracks the active stance transition for use during stance_change animation.
   * Provides access to keyframes and blend weights for smooth interpolation.
   * 
   * @korean 현재자세전환데이터
   */
  private currentStanceTransition: StanceTransition | null = null;
 
  /**
   * Motion prediction state for latency reduction
   * 
   * **Korean**: 동작 예측 상태
   * 
   * Tracks animation velocities for motion prediction to reduce perceived latency.
   * Updated each frame with velocity calculations for smooth anticipation.
   * 
   * @korean 동작예측상태
   */
  private motionPrediction: MotionPredictionState = createMotionPredictionState();
 
  /**
   * Enable motion prediction for latency reduction
   * 
   * **Korean**: 동작 예측 활성화
   * 
   * When enabled, predicts future animation frames based on current velocity
   * to reduce perceived input latency by 16-33ms (1-2 frames at 60fps).
   * 
   * @korean 동작예측활성화
   */
  private enableMotionPrediction: boolean = false;
 
  /**
   * Motion prediction time ahead (seconds)
   * 
   * **Korean**: 예측 시간
   * 
   * How far ahead to predict motion (default: 1 frame = 16.67ms at 60fps).
   * Typical range: 0.016-0.033 seconds for <50ms total latency.
   * 
   * @korean 예측시간
   */
  private predictionTimeAhead: number = 0.01667; // 1 frame at 60fps
  
  /**
   * Previous keyframe for motion prediction velocity calculation
   * 
   * **Korean**: 이전 키프레임
   * 
   * @korean 이전키프레임
   */
  private previousKeyframe: AnimationKeyframe | null = null;
 
  /**
   * Preferred easing function for smooth transitions
   * 
   * **Korean**: 선호 이징 함수
   * 
   * Default easing curve for animation blending and transitions.
   * Can be overridden per animation or transition.
   * 
   * @korean 선호이징함수
   */
  private preferredEasing: EasingName = "natural-motion";
 
  /**
   * Create a new animation state machine
   * 
   * @param animations - Map of animation configurations
   * @param events - Optional event callbacks
   * 
   * @korean 생성자
   */
  constructor(
    private readonly animations: Map<AnimationState, AnimationConfig>,
    private readonly events?: AnimationEvents
  ) {}
 
  /**
   * Update animation state with delta time
   * 
   * Call this in useFrame for 60fps updates.
   * Handles frame progression, looping, and completion.
   * 
   * @param deltaTime - Time elapsed since last update (in seconds)
   * @returns Animation update result with current state and frame
   * 
   * @korean 업데이트
   */
  update(deltaTime: number): AnimationUpdateResult {
    const currentAnim = this.animations.get(this.currentState);
    Iif (!currentAnim) {
      return {
        state: this.currentState,
        frame: 0,
        progress: 0,
        justCompleted: false,
        justStarted: false,
      };
    }
 
    // Reset just started/completed flags
    const wasJustStarted = this.justStarted;
    this.justStarted = false;
    const previousJustCompleted = this.justCompleted;
    this.justCompleted = false;
 
    // Accumulate time
    this.timeAccumulator += deltaTime;
    const frameDuration = 1 / currentAnim.fps;
 
    // Check if we should advance to next frame
    if (this.timeAccumulator >= frameDuration) {
      const previousFrame = this.frameIndex;
      this.frameIndex++;
      this.timeAccumulator -= frameDuration;
 
      // Emit frame event
      if (this.events?.onFrame && previousFrame !== this.frameIndex) {
        this.events.onFrame(this.frameIndex, this.currentState);
      }
 
      // Handle animation completion
      if (this.frameIndex >= currentAnim.frames) {
        if (currentAnim.loop) {
          // Loop back to start
          this.frameIndex = 0;
        } else {
          // Animation completed
          this.justCompleted = true;
          if (this.events?.onAnimationComplete) {
            this.events.onAnimationComplete(this.currentState);
          }
 
          // Auto-transition logic
          // Fall animations transition to ground states using the mapping
          if (this.currentState.startsWith("fall_")) {
            const fallType = this.currentState.replace("fall_", "");
            
            // Validate that fallType is a valid FallType before using in map
            if (fallType === "forward" || fallType === "backward" || 
                fallType === "side_left" || fallType === "side_right") {
              const groundState = FALL_TO_GROUND_MAP[fallType as FallType];
              const groundAnimKey = `ground_${groundState}`;
 
              // Validate that the constructed ground animation state actually exists
              if (DEFAULT_ANIMATION_CONFIGS.has(groundAnimKey as AnimationState)) {
                const groundAnimState = groundAnimKey as AnimationState;
 
                this.previousState = this.currentState;
                this.currentState = groundAnimState;
                this.frameIndex = 0;
                this.timeAccumulator = 0;
                this.justStarted = true;
 
                Eif (this.events?.onAnimationStart) {
                  this.events.onAnimationStart(groundAnimState);
                }
              } else E{
                // Fallback: if mapping is invalid, safely transition to idle
                // instead of entering an undefined animation state.
                console.warn(
                  "[AnimationStateMachine] Invalid ground animation mapping for fall type:",
                  fallType,
                  "->",
                  groundAnimKey
                );
                this.previousState = this.currentState;
                this.currentState = AnimationState.IDLE;
                this.frameIndex = 0;
                this.timeAccumulator = 0;
                this.justStarted = true;
 
                if (this.events?.onAnimationStart) {
                  this.events.onAnimationStart(AnimationState.IDLE);
                }
              }
            } else E{
              // Invalid fall type - fallback to idle
              console.warn(
                "[AnimationStateMachine] Invalid fall animation state:",
                this.currentState
              );
              this.previousState = this.currentState;
              this.currentState = AnimationState.IDLE;
              this.frameIndex = 0;
              this.timeAccumulator = 0;
              this.justStarted = true;
 
              if (this.events?.onAnimationStart) {
                this.events.onAnimationStart(AnimationState.IDLE);
              }
            }
          }
          // Recovery animations transition to idle when complete
          else if (this.currentState.startsWith("recovery_")) {
            this.previousState = this.currentState;
            this.currentState = AnimationState.IDLE;
            this.frameIndex = 0;
            this.timeAccumulator = 0;
            this.justStarted = true;
 
            Eif (this.events?.onAnimationStart) {
              this.events.onAnimationStart(AnimationState.IDLE);
            }
          }
          // Non-fall, non-recovery, non-looping animations transition to idle
          else if (this.currentState !== AnimationState.IDLE && 
                   this.currentState !== AnimationState.KO &&
                   !this.currentState.startsWith("ground_")) {
            // Clear stance transition data if completing stance_change
            if (this.currentState === AnimationState.STANCE_CHANGE) {
              this.clearStanceTransition();
            }
            
            // Direct transition to idle without interrupt event
            this.previousState = this.currentState;
            this.currentState = AnimationState.IDLE;
            this.frameIndex = 0;
            this.timeAccumulator = 0;
            this.justStarted = true;
 
            if (this.events?.onAnimationStart) {
              this.events.onAnimationStart(AnimationState.IDLE);
            }
          } else E{
            // Stay on last frame (for ko and ground states)
            this.frameIndex = currentAnim.frames - 1;
          }
        }
      }
    }
 
    const progress = currentAnim.frames > 0 ? this.frameIndex / currentAnim.frames : 0;
 
    return {
      state: this.currentState,
      frame: this.frameIndex,
      progress,
      justCompleted: previousJustCompleted,
      justStarted: wasJustStarted,
    };
  }
 
  /**
   * Attempt to transition to a new animation state
   * 
   * Checks transition rules and priority system before transitioning.
   * 
   * @param newState - Target animation state
   * @returns Whether transition was successful
   * 
   * @example
   * ```typescript
   * // Successful transitions
   * machine.transitionTo("walk"); // idle -> walk
   * machine.transitionTo("attack"); // walk -> attack
   * 
   * // Failed transition (invalid or lower priority)
   * machine.transitionTo("walk"); // attack -> walk (blocked, must complete first)
   * ```
   * 
   * @korean 상태전환
   */
  transitionTo(newState: AnimationState): boolean {
    // Don't transition to same state
    if (this.currentState === newState) {
      return false;
    }
 
    // Check if transition is allowed by rules
    if (!isTransitionAllowed(this.currentState, newState)) {
      return false;
    }
 
    const currentAnim = this.animations.get(this.currentState);
    const newAnim = this.animations.get(newState);
 
    Iif (!newAnim) {
      return false;
    }
 
    // Check priority system
    if (
      currentAnim &&
      !canInterrupt(this.currentState, newState, currentAnim.interruptible)
    ) {
      return false;
    }
 
    // Emit interrupt event if current animation wasn't completed
    if (this.frameIndex < (currentAnim?.frames ?? 0) - 1) {
      if (this.events?.onAnimationInterrupted) {
        this.events.onAnimationInterrupted(this.currentState, newState);
      }
    }
 
    // Clear stance transition data if interrupting stance_change
    if (this.currentState === AnimationState.STANCE_CHANGE) {
      this.clearStanceTransition();
    }
 
    // Execute transition
    this.previousState = this.currentState;
    this.currentState = newState;
    this.frameIndex = 0;
    this.timeAccumulator = 0;
    this.justStarted = true;
    this.justCompleted = false;
 
    // Emit start event
    if (this.events?.onAnimationStart) {
      this.events.onAnimationStart(newState);
    }
 
    return true;
  }
 
  /**
   * Get current animation state
   * 
   * @returns Current animation state
   * @korean 현재상태가져오기
   */
  getCurrentState(): AnimationState {
    return this.currentState;
  }
 
  /**
   * Get current frame index
   * 
   * @returns Current frame index (0 to frames-1)
   * @korean 현재프레임가져오기
   */
  getCurrentFrame(): number {
    return this.frameIndex;
  }
 
  /**
   * Get previous animation state
   * 
   * @returns Previous animation state or null
   * @korean 이전상태가져오기
   */
  getPreviousState(): AnimationState | null {
    return this.previousState;
  }
 
  /**
   * Get current animation configuration
   * 
   * @returns Current animation config or undefined
   * @korean 현재애니메이션설정가져오기
   */
  getCurrentAnimation(): AnimationConfig | undefined {
    return this.animations.get(this.currentState);
  }
 
  /**
   * Reset animation state machine to idle
   * 
   * @korean 초기화
   */
  reset(): void {
    this.currentState = AnimationState.IDLE;
    this.frameIndex = 0;
    this.timeAccumulator = 0;
    this.previousState = null;
    this.justStarted = false;
    this.justCompleted = false;
  }
 
  /**
   * Get full internal state (for debugging/testing)
   * 
   * @returns Current state machine state
   * @korean 상태가져오기
   */
  getState(): AnimationMachineState {
    return {
      currentState: this.currentState,
      frameIndex: this.frameIndex,
      timeAccumulator: this.timeAccumulator,
      isPlaying: true,
      previousState: this.previousState,
    };
  }
 
  /**
   * Transition to stance-specific guard animation
   * 
   * Convenience method to transition to a stance guard based on trigram stance.
   * Automatically maps trigram stance to corresponding guard animation state.
   * 
   * @param stance - Trigram stance identifier
   * @returns Whether transition was successful
   * 
   * @example
   * ```typescript
   * // When player changes to Fire stance
   * machine.transitionToStanceGuard(TrigramStance.LI);
   * // Internally transitions to "stance_guard_li" animation state
   * ```
   * 
   * @korean 자세방어전환
   */
  transitionToStanceGuard(stance: TrigramStance): boolean {
    const guardAnimationState = PlayerAnimationStateMachine.GUARD_STATE_MAP[stance];
    
    // Verify the guard animation exists in our configs
    if (!guardAnimationState || !this.animations.has(guardAnimationState)) {
      console.warn(`No guard animation configured for stance: ${stance}`);
      return false;
    }
 
    return this.transitionTo(guardAnimationState);
  }
 
  /**
   * Check if current animation is a stance guard
   * 
   * @returns True if currently in a stance guard animation
   * @korean 자세방어상태확인
   */
  isInStanceGuard(): boolean {
    return this.currentState.startsWith("stance_guard_");
  }
 
  /**
   * Get current guard stance if in a guard animation
   * 
   * @returns Trigram stance or null if not in guard
   * @korean 현재방어자세가져오기
   */
  getCurrentGuardStance(): TrigramStance | null {
    if (!this.isInStanceGuard()) {
      return null;
    }
 
    const stance = PlayerAnimationStateMachine.STANCE_FROM_GUARD_MAP[this.currentState];
    
    // Validate that we got a valid stance
    Iif (!stance) {
      console.warn(`Invalid guard state detected: ${this.currentState}`);
      return null;
    }
 
    return stance;
  }
 
  /**
   * Transition to stance_change animation with specific stance transition data
   * 
   * **Korean**: 자세 전환 애니메이션 시작
   * 
   * Initiates a stance change animation with the specific transition data
   * from the 64-transition matrix. This provides stance-specific keyframes
   * and blend weights for smooth interpolation.
   * 
   * @param fromStance - Source trigram stance
   * @param toStance - Target trigram stance
   * @returns Whether transition was successful
   * 
   * @example
   * ```typescript
   * // Start transition from Heaven to Lake stance
   * const success = machine.transitionToStanceChange(
   *   TrigramStance.GEON, 
   *   TrigramStance.TAE
   * );
   * 
   * if (success) {
   *   // During update loop, use getStanceTransitionBlend() to interpolate
   *   const blend = machine.getStanceTransitionBlend();
   *   if (blend) {
   *     // Apply blend weights to stance poses
   *     applyStanceBlend(blend);
   *   }
   * }
   * ```
   * 
   * @korean 자세전환애니메이션시작
   */
  transitionToStanceChange(
    fromStance: TrigramStance,
    toStance: TrigramStance
  ): boolean {
    // Get the specific transition data from the 64-transition matrix
    const transitionData = getStanceTransition(fromStance, toStance);
    
    Iif (!transitionData) {
      console.warn(
        `[AnimationStateMachine] No transition data found for ${fromStance} -> ${toStance}`
      );
      return false;
    }
 
    // Store current transition data in case we need to restore it
    const previousTransitionData = this.currentStanceTransition;
 
    // Temporarily set the new transition data
    this.currentStanceTransition = transitionData;
 
    // Initiate the stance_change animation
    const success = this.transitionTo(AnimationState.STANCE_CHANGE);
 
    // If transition failed, restore previous transition data
    if (!success) {
      this.currentStanceTransition = previousTransitionData;
    }
 
    return success;
  }
 
  /**
   * Get current stance transition data
   * 
   * **Korean**: 현재 자세 전환 데이터 가져오기
   * 
   * Returns the active stance transition data during stance_change animation.
   * Null if not currently in a stance transition.
   * 
   * @returns Current stance transition or null
   * 
   * @korean 현재자세전환데이터가져오기
   */
  getCurrentStanceTransition(): StanceTransition | null {
    return this.currentStanceTransition;
  }
 
  /**
   * Get interpolated blend weights for current stance transition frame
   * 
   * **Korean**: 현재 프레임 블렌드 가중치
   * 
   * Returns the interpolated blend data for the current frame during
   * stance_change animation. Uses the keyframe data from the transition
   * matrix to provide smooth stance interpolation.
   * 
   * @returns Blend data with stance and weight, or null if not in transition
   * 
   * @example
   * ```typescript
   * // In rendering loop during stance transition
   * const blend = machine.getStanceTransitionBlend();
   * if (blend) {
   *   console.log(`Frame ${blend.frame}: ${blend.stance} at ${blend.blend}x weight`);
   *   // Apply blended pose: blend.blend * targetPose + (1 - blend.blend) * sourcePose
   * }
   * ```
   * 
   * @korean 현재프레임블렌드가중치
   */
  getStanceTransitionBlend(): {
    frame: number;
    stance: TrigramStance | 'neutral';
    blend: number;
  } | null {
    // Only valid during stance_change animation
    if (this.currentState !== AnimationState.STANCE_CHANGE || !this.currentStanceTransition) {
      return null;
    }
 
    const keyframes = this.currentStanceTransition.keyframes;
    const currentFrame = this.frameIndex;
 
    // Find the two keyframes to interpolate between
    let prevKeyframe = keyframes[0];
    let nextKeyframe = keyframes[keyframes.length - 1];
 
    for (let i = 0; i < keyframes.length - 1; i++) {
      if (keyframes[i].frame <= currentFrame && keyframes[i + 1].frame > currentFrame) {
        prevKeyframe = keyframes[i];
        nextKeyframe = keyframes[i + 1];
        break;
      }
    }
 
    // If we're exactly on a keyframe, return it directly
    const exactKeyframe = keyframes.find(kf => kf.frame === currentFrame);
    if (exactKeyframe) {
      return {
        frame: currentFrame,
        stance: exactKeyframe.stance,
        blend: exactKeyframe.blend,
      };
    }
 
    // Linear interpolation between keyframes
    const frameRange = nextKeyframe.frame - prevKeyframe.frame;
    const frameProgress = frameRange > 0 
      ? (currentFrame - prevKeyframe.frame) / frameRange 
      : 0;
 
    const interpolatedBlend = 
      prevKeyframe.blend + (nextKeyframe.blend - prevKeyframe.blend) * frameProgress;
 
    // Use the next keyframe's stance as we're transitioning towards it
    return {
      frame: currentFrame,
      stance: nextKeyframe.stance,
      blend: interpolatedBlend,
    };
  }
 
  /**
   * Check if currently in a stance transition animation
   * 
   * **Korean**: 자세 전환 중 확인
   * 
   * @returns True if currently executing a stance_change animation
   * @korean 자세전환중확인
   */
  isInStanceTransition(): boolean {
    return this.currentState === AnimationState.STANCE_CHANGE && this.currentStanceTransition !== null;
  }
 
  /**
   * Clear stance transition data (called automatically when transition completes)
   * 
   * **Korean**: 자세 전환 데이터 초기화
   * 
   * @internal
   * @korean 자세전환데이터초기화
   */
  private clearStanceTransition(): void {
    this.currentStanceTransition = null;
  }
 
  /**
   * Enable or disable motion prediction
   * 
   * **Korean**: 동작 예측 설정
   * 
   * Enables motion prediction to reduce perceived input latency by predicting
   * future animation frames based on current velocity (1-2 frames ahead).
   * 
   * @param enabled - Whether to enable motion prediction
   * @param predictionTime - Optional: time ahead to predict (default: 16.67ms)
   * 
   * @example
   * ```typescript
   * // Enable motion prediction for 1 frame (16.67ms at 60fps)
   * machine.setMotionPrediction(true);
   * 
   * // Enable with 2 frames prediction (33.33ms)
   * machine.setMotionPrediction(true, 0.03333);
   * ```
   * 
   * @korean 동작예측설정
   */
  setMotionPrediction(enabled: boolean, predictionTime?: number): void {
    this.enableMotionPrediction = enabled;
    if (predictionTime !== undefined) {
      // Clamp to 50ms maximum for <50ms total latency
      this.predictionTimeAhead = Math.min(predictionTime, 0.05);
    }
  }
 
  /**
   * Get motion prediction state
   * 
   * **Korean**: 동작 예측 상태 가져오기
   * 
   * @returns Current motion prediction state
   * @korean 동작예측상태가져오기
   */
  getMotionPredictionState(): MotionPredictionState {
    return this.motionPrediction;
  }
 
  /**
   * Check if motion prediction is enabled
   * 
   * **Korean**: 동작 예측 활성화 확인
   * 
   * @returns True if motion prediction is enabled
   * @korean 동작예측활성화확인
   */
  isMotionPredictionEnabled(): boolean {
    return this.enableMotionPrediction;
  }
 
  /**
   * Set preferred easing function for transitions
   * 
   * **Korean**: 선호 이징 함수 설정
   * 
   * Sets the default easing curve for animation transitions.
   * Can use presets like "natural-motion", "smooth-transition", etc.
   * 
   * @param easingName - Easing function name
   * 
   * @example
   * ```typescript
   * // Use natural motion for Korean martial arts
   * machine.setPreferredEasing("natural-motion");
   * 
   * // Use explosive power for strike animations
   * machine.setPreferredEasing("explosive-power");
   * ```
   * 
   * @korean 선호이징함수설정
   */
  setPreferredEasing(easingName: EasingName): void {
    this.preferredEasing = easingName;
  }
 
  /**
   * Get preferred easing function
   * 
   * **Korean**: 선호 이징 함수 가져오기
   * 
   * @returns Current preferred easing name
   * @korean 선호이징함수가져오기
   */
  getPreferredEasing(): EasingName {
    return this.preferredEasing;
  }
 
  /**
   * Update motion prediction with skeletal keyframe data
   * 
   * **Korean**: 동작 예측 업데이트
   * 
   * This should be called from the skeletal animation layer when applying
   * interpolated keyframes to the rig. It updates velocity tracking for
   * motion prediction to reduce perceived latency.
   * 
   * Integration point: Call this from your skeletal animation system after
   * computing the current interpolated keyframe (e.g., from getInterpolatedKeyframe).
   * 
   * @param currentKeyframe - Current skeletal animation keyframe with bone positions/rotations
   * @param deltaTime - Time elapsed since last update
   * 
   * @example
   * ```typescript
   * // In your skeletal animation update loop:
   * const currentKeyframe = getInterpolatedKeyframe(animation, time);
   * 
   * // Update motion prediction (for next frame)
   * if (machine.isMotionPredictionEnabled()) {
   *   machine.updateMotionPredictionState(currentKeyframe, deltaTime);
   * }
   * 
   * // Apply keyframe to rig
   * applyKeyframeToRig(rig, currentKeyframe);
   * ```
   * 
   * @korean 동작예측업데이트
   */
  updateMotionPredictionState(currentKeyframe: AnimationKeyframe, deltaTime: number): void {
    Iif (!this.enableMotionPrediction) {
      return;
    }
 
    // Update velocity tracking if we have a previous keyframe
    if (this.previousKeyframe) {
      this.motionPrediction = updateMotionPrediction(
        this.motionPrediction,
        this.previousKeyframe,
        currentKeyframe,
        deltaTime
      );
    }
 
    // Store current keyframe for next update
    this.previousKeyframe = currentKeyframe;
  }
 
  /**
   * Get predicted future keyframe for latency reduction
   * 
   * **Korean**: 예측된 미래 키프레임 가져오기
   * 
   * Returns a keyframe predicted ahead by predictionTimeAhead (default: 1 frame).
   * This reduces perceived input latency by showing where the animation will be
   * in the near future rather than where it currently is.
   * 
   * Integration point: Use this instead of the current keyframe when applying
   * to the rig if motion prediction is enabled.
   * 
   * @param currentKeyframe - Current skeletal animation keyframe
   * @returns Predicted future keyframe, or current if prediction disabled
   * 
   * @example
   * ```typescript
   * // In your skeletal animation update loop:
   * let keyframeToApply = currentKeyframe;
   * 
   * if (machine.isMotionPredictionEnabled()) {
   *   keyframeToApply = machine.getPredictedKeyframe(currentKeyframe);
   * }
   * 
   * applyKeyframeToRig(rig, keyframeToApply);
   * ```
   * 
   * @korean 예측키프레임가져오기
   */
  getPredictedKeyframe(currentKeyframe: AnimationKeyframe): AnimationKeyframe {
    if (!this.enableMotionPrediction || !this.previousKeyframe) {
      return currentKeyframe;
    }
 
    return predictFutureKeyframe(
      currentKeyframe,
      this.motionPrediction,
      this.predictionTimeAhead
    );
  }
}