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 | 55x 8391x 8391x 8391x 8081x 310x 7x 303x 8x 9x 55x 12x 15x 1x 14x 1x 13x 7x 2x 1x 2x 1x 391x 391x 391x 41x 4x 4x 2x 2x 39x 39x 39x 29x 29x 29x 19x 10x 39x 39x 9x 1x 8x 8x 14x 8x 5x 3x 3x 3x 8x 8x 8x 2x 3x 22x 23x 4x 17x 15x 2x 19x 28x 19x 6x 13x 8x 10x | /**
* Animation priority system for Black Trigram
*
* Determines which animations can interrupt others based on priority levels.
* Higher priority animations can interrupt lower priority ones.
*
* Priority order: recovery > fall > ko > hit > attack > defend > stance_change > movement > idle
*
* Enhanced with:
* - Deterministic conflict resolution for simultaneous equal-priority animations
* - Animation queue system for pending animations
* - Interruptibility windows (frame-based interrupt control)
* - Korean terminology for all priority levels and conflicts
*
* @module systems/animation/AnimationPriority
* @category Animation
* @korean 애니메이션우선순위
*/
import { AnimationPriority, AnimationState, STEP_PRIORITY } from "./types";
/**
* Map animation states to their priority levels
*
* Stance guard animations have same priority as idle (0) since they are
* also idle states, just stance-specific.
* Tactical steps have priority 5 (same as attacks) to ensure commitment.
*
* @korean 애니메이션우선순위맵
*/
export const ANIMATION_PRIORITY_MAP: Record<AnimationState, AnimationPriority> = {
idle: AnimationPriority.IDLE,
walk: AnimationPriority.WALK,
run: AnimationPriority.RUN,
stance_change: AnimationPriority.STANCE_CHANGE,
stance_side_switch: AnimationPriority.STANCE_CHANGE, // Same priority as stance_change
defend: AnimationPriority.DEFEND,
// Defensive animations (방어 애니메이션)
defend_block_success: AnimationPriority.HIT, // Priority 6 - same as hit
defend_parry: AnimationPriority.KO, // Priority 7 - higher than block
defend_guard_break: AnimationPriority.FALL, // Priority 8 - highest (same as fall)
defend_recovery: AnimationPriority.RUN, // Priority 2 - interruptible recovery
attack: AnimationPriority.ATTACK,
hit: AnimationPriority.HIT,
ko: AnimationPriority.KO,
// Stance-specific guard animations (팔괘 방어 자세)
stance_guard_geon: AnimationPriority.IDLE,
stance_guard_tae: AnimationPriority.IDLE,
stance_guard_li: AnimationPriority.IDLE,
stance_guard_jin: AnimationPriority.IDLE,
stance_guard_son: AnimationPriority.IDLE,
stance_guard_gam: AnimationPriority.IDLE,
stance_guard_gan: AnimationPriority.IDLE,
stance_guard_gon: AnimationPriority.IDLE,
// Tactical step animations (전술적 발걸음) - non-interruptible
step_forward: STEP_PRIORITY,
step_back: STEP_PRIORITY,
step_left: STEP_PRIORITY,
step_right: STEP_PRIORITY,
step_forward_left: STEP_PRIORITY,
step_forward_right: STEP_PRIORITY,
step_back_left: STEP_PRIORITY,
step_back_right: STEP_PRIORITY,
// Fall animations (낙법) - highest priority
fall_forward: AnimationPriority.FALL,
fall_backward: AnimationPriority.FALL,
fall_side_left: AnimationPriority.FALL,
fall_side_right: AnimationPriority.FALL,
// Ground states (지면 자세) - idle priority
ground_prone: AnimationPriority.IDLE,
ground_supine: AnimationPriority.IDLE,
ground_side_left: AnimationPriority.IDLE,
ground_side_right: AnimationPriority.IDLE,
// 180-degree turn animations (180도 회전) - same as steps (committed action)
turn_left: STEP_PRIORITY,
turn_right: STEP_PRIORITY,
// Footwork patterns (보법) - Korean martial arts specialized footwork
footwork_circular_left: STEP_PRIORITY,
footwork_circular_right: STEP_PRIORITY,
footwork_pivot_left: STEP_PRIORITY,
footwork_pivot_right: STEP_PRIORITY,
footwork_slide_forward: AnimationPriority.DEFEND,
footwork_slide_back: AnimationPriority.DEFEND,
footwork_slide_left: AnimationPriority.DEFEND,
footwork_slide_right: AnimationPriority.DEFEND,
footwork_shuffle: AnimationPriority.STANCE_CHANGE,
// Recovery animations (기상 애니메이션) - highest priority (can interrupt anything)
recovery_prone_standup: AnimationPriority.RECOVERY,
recovery_supine_standup: AnimationPriority.RECOVERY,
recovery_roll: AnimationPriority.RECOVERY,
recovery_defensive: AnimationPriority.RECOVERY,
};
/**
* Check if an animation can interrupt another based on priority
*
* @param current - Current animation state
* @param requested - Requested animation state
* @param currentInterruptible - Whether current animation is interruptible
* @returns Whether the requested animation can interrupt the current one
*
* @example
* ```typescript
* // Hit can interrupt attack
* canInterrupt("attack", "hit", true); // true
*
* // Attack cannot interrupt hit
* canInterrupt("hit", "attack", true); // false
*
* // Nothing can interrupt non-interruptible animations
* canInterrupt("attack", "hit", false); // false (unless same priority)
* ```
*
* @korean 중단가능여부확인
*/
export function canInterrupt(
current: AnimationState,
requested: AnimationState,
currentInterruptible: boolean
): boolean {
const currentPriority = ANIMATION_PRIORITY_MAP[current];
const requestedPriority = ANIMATION_PRIORITY_MAP[requested];
// Same priority animations can always transition
if (currentPriority === requestedPriority) {
return true;
}
// Non-interruptible animations can only be interrupted by higher priority
if (!currentInterruptible) {
return requestedPriority > currentPriority;
}
// Interruptible animations can be interrupted by same or higher priority
return requestedPriority >= currentPriority;
}
/**
* Get the priority level for an animation state
*
* @param state - Animation state
* @returns Priority level
*
* @korean 우선순위가져오기
*/
export function getPriority(state: AnimationState): AnimationPriority {
return ANIMATION_PRIORITY_MAP[state];
}
/**
* Compare two animation priorities
*
* @param state1 - First animation state
* @param state2 - Second animation state
* @returns Positive if state1 has higher priority, negative if state2 has higher priority, 0 if equal
*
* @korean 우선순위비교
*/
export function comparePriority(
state1: AnimationState,
state2: AnimationState
): number {
return ANIMATION_PRIORITY_MAP[state1] - ANIMATION_PRIORITY_MAP[state2];
}
// ===== Korean Priority Level Terminology (우선순위 등급 용어) =====
/**
* Korean names for each priority level
*
* Maps priority levels to their Korean martial arts terminology:
* - IDLE (0): 대기 (Daegi) - Waiting/Ready state
* - WALK (1): 보행 (Bohaeng) - Walking movement
* - RUN (2): 질주 (Jilju) - Running movement
* - STANCE_CHANGE (3): 자세전환 (Jase Jeonhwan) - Stance transition
* - DEFEND (4): 방어 (Bangeo) - Defense action
* - ATTACK (5): 공격 (Gonggyeok) - Attack action
* - HIT (6): 피격 (Pigyeok) - Being hit
* - KO (7): 기절 (Gijeol) - Knockout
* - FALL (8): 낙법 (Nakbeop) - Falling technique
* - RECOVERY (9): 기상 (Gisang) - Recovery/Getting up
*
* @korean 우선순위한글용어
*/
export const PRIORITY_LEVEL_KOREAN_NAMES: Record<AnimationPriority, {
korean: string;
romanized: string;
english: string;
}> = {
[AnimationPriority.IDLE]: {
korean: "대기",
romanized: "Daegi",
english: "Ready State",
},
[AnimationPriority.WALK]: {
korean: "보행",
romanized: "Bohaeng",
english: "Walking",
},
[AnimationPriority.RUN]: {
korean: "질주",
romanized: "Jilju",
english: "Running",
},
[AnimationPriority.STANCE_CHANGE]: {
korean: "자세전환",
romanized: "Jase Jeonhwan",
english: "Stance Change",
},
[AnimationPriority.DEFEND]: {
korean: "방어",
romanized: "Bangeo",
english: "Defense",
},
[AnimationPriority.ATTACK]: {
korean: "공격",
romanized: "Gonggyeok",
english: "Attack",
},
[AnimationPriority.HIT]: {
korean: "피격",
romanized: "Pigyeok",
english: "Hit",
},
[AnimationPriority.KO]: {
korean: "기절",
romanized: "Gijeol",
english: "Knockout",
},
[AnimationPriority.FALL]: {
korean: "낙법",
romanized: "Nakbeop",
english: "Falling",
},
[AnimationPriority.RECOVERY]: {
korean: "기상",
romanized: "Gisang",
english: "Recovery",
},
};
/**
* Get Korean name for a priority level
*
* @param priority - Animation priority level
* @returns Korean terminology object
*
* @korean 우선순위한글이름가져오기
*/
export function getPriorityKoreanName(priority: AnimationPriority): {
korean: string;
romanized: string;
english: string;
} {
return PRIORITY_LEVEL_KOREAN_NAMES[priority];
}
// ===== Conflict Resolution (충돌 해결) =====
/**
* Conflict resolution strategy for equal-priority animations
*
* **Korean**: 충돌 해결 전략
*
* Determines how to handle when two animations of equal priority
* are requested simultaneously:
*
* - `timestamp`: First animation requested wins (FIFO - 선입선출)
* - `state_order`: Use AnimationState enum order as tiebreaker
* - `current`: Keep current animation running
* - `requested`: Always switch to requested animation
*
* @korean 충돌해결전략
*/
export type ConflictResolutionStrategy =
| "timestamp" // 시간순서 (First requested wins)
| "state_order" // 상태순서 (Enum order tiebreaker)
| "current" // 현재유지 (Keep current)
| "requested"; // 요청수락 (Accept requested)
/**
* Animation request with timestamp for conflict resolution
*
* **Korean**: 애니메이션 요청
*
* Tracks animation requests with timestamps to enable deterministic
* conflict resolution when multiple equal-priority animations compete.
*
* @korean 애니메이션요청
*/
export interface AnimationRequest {
/** Requested animation state */
readonly state: AnimationState;
/** Request timestamp in milliseconds (from performance.now()) */
readonly timestamp: number;
/** Request priority */
readonly priority: AnimationPriority;
/**
* Whether this is a forced request (wins conflict resolution against non-forced requests)
*
* Note: This only affects conflict resolution between equal-priority animations in the queue.
* It does not bypass priority checks or interrupt rules for the initial transition.
*
* @korean 강제요청
*/
readonly forced?: boolean;
}
/**
* Resolve conflict between two equal-priority animations
*
* **Korean**: 충돌 해결
*
* Uses the specified strategy to deterministically decide which animation
* should play when both have equal priority.
*
* @param current - Current animation request
* @param requested - Requested animation request
* @param strategy - Conflict resolution strategy
* @returns Which animation should play: 'current' or 'requested'
*
* @example
* ```typescript
* const current = {
* state: AnimationState.ATTACK,
* timestamp: 1000,
* priority: AnimationPriority.ATTACK,
* };
* const requested = {
* state: AnimationState.STEP_FORWARD,
* timestamp: 1001,
* priority: AnimationPriority.ATTACK,
* };
*
* // Timestamp strategy: current wins (1000 < 1001)
* resolveConflict(current, requested, "timestamp"); // "current"
*
* // Requested strategy: always take new animation
* resolveConflict(current, requested, "requested"); // "requested"
* ```
*
* @korean 충돌해결
*/
export function resolveConflict(
current: AnimationRequest,
requested: AnimationRequest,
strategy: ConflictResolutionStrategy = "timestamp"
): "current" | "requested" {
// If only one is forced, forced wins
if (requested.forced && !current.forced) {
return "requested";
}
if (current.forced && !requested.forced) {
return "current";
}
// If both are forced OR both are normal, use conflict resolution strategy
// Apply conflict resolution strategy
switch (strategy) {
case "timestamp":
// Earlier timestamp wins (FIFO)
return current.timestamp <= requested.timestamp ? "current" : "requested";
case "state_order":
// Use enum value comparison as tiebreaker
return current.state <= requested.state ? "current" : "requested";
case "current":
// Always keep current animation
return "current";
case "requested":
// Always accept requested animation
return "requested";
default:
// Default to timestamp strategy
return current.timestamp <= requested.timestamp ? "current" : "requested";
}
}
// ===== Animation Queue System (애니메이션 대기열) =====
/**
* Animation queue for managing pending animations
*
* **Korean**: 애니메이션 대기열
*
* Stores pending animation requests that should play after the current
* animation completes. Useful for:
* - Buffering input during non-interruptible animations
* - Creating animation chains/combos
* - Handling rapid input sequences
*
* Queue is priority-ordered: highest priority animations are dequeued first.
*
* @korean 애니메이션대기열
*/
export class AnimationQueue {
private queue: AnimationRequest[] = [];
private maxSize: number;
private conflictStrategy: ConflictResolutionStrategy;
/**
* Create a new animation queue
*
* @param maxSize - Maximum queue size (default: 3)
* @param conflictStrategy - Strategy for resolving equal-priority conflicts
*
* @korean 생성자
*/
constructor(
maxSize: number = 3,
conflictStrategy: ConflictResolutionStrategy = "timestamp"
) {
this.maxSize = maxSize;
this.conflictStrategy = conflictStrategy;
}
/**
* Add animation request to queue
*
* **Korean**: 대기열에 추가
*
* Adds an animation request to the queue if space is available.
* Queue is kept sorted by priority (highest first).
*
* @param request - Animation request to enqueue
* @returns Whether request was successfully enqueued
*
* @korean 대기열추가
*/
enqueue(request: AnimationRequest): boolean {
// Check if queue is full
if (this.queue.length >= this.maxSize) {
// Queue is sorted by priority (highest first), so last element is lowest priority
const lowestPriorityItem = this.queue[this.queue.length - 1];
if (request.priority > lowestPriorityItem.priority) {
// Remove lowest priority item to make space
this.queue.pop();
} else {
return false; // Queue full, request discarded
}
}
// Insert request into queue while maintaining sort by priority (highest first).
// Use binary search (O(log n)) to find insertion position, then splice (O(n)) to insert.
// Overall: O(n) per insertion, avoiding an O(n log n) full sort of the entire queue.
let low = 0;
let high = this.queue.length;
// Binary search to find insertion index
while (low < high) {
const mid = (low + high) >> 1;
const midPriority = this.queue[mid].priority;
if (midPriority < request.priority) {
// New request has higher priority; search left half
high = mid;
} else {
// New request has equal or lower priority; search right half
low = mid + 1;
}
}
// Insert at computed index to keep queue sorted (highest priority first)
this.queue.splice(low, 0, request);
return true;
}
/**
* Remove and return highest priority request from queue
*
* **Korean**: 대기열에서 제거
*
* Dequeues the highest priority animation request.
* If multiple requests have equal priority, uses conflict resolution strategy.
*
* @returns Next animation request or null if queue is empty
*
* @korean 대기열제거
*/
dequeue(): AnimationRequest | null {
if (this.queue.length === 0) {
return null;
}
// Get all requests with highest priority
const highestPriority = this.queue[0].priority;
const highestPriorityRequests = this.queue.filter(
r => r.priority === highestPriority
);
let selectedRequest: AnimationRequest;
if (highestPriorityRequests.length === 1) {
selectedRequest = highestPriorityRequests[0];
} else {
// Multiple equal-priority requests - use conflict resolution
selectedRequest = highestPriorityRequests.reduce((current, next) => {
const winner = resolveConflict(current, next, this.conflictStrategy);
return winner === "current" ? current : next;
});
}
// Remove selected request from queue
const index = this.queue.indexOf(selectedRequest);
this.queue.splice(index, 1);
return selectedRequest;
}
/**
* Peek at next request without removing it
*
* **Korean**: 다음 요청 확인
*
* @returns Next animation request or null if queue is empty
*
* @korean 다음요청확인
*/
peek(): AnimationRequest | null {
return this.queue.length > 0 ? this.queue[0] : null;
}
/**
* Clear all pending requests
*
* **Korean**: 대기열 초기화
*
* @korean 대기열초기화
*/
clear(): void {
this.queue = [];
}
/**
* Get number of pending requests
*
* **Korean**: 대기열 크기
*
* @returns Number of pending requests
*
* @korean 대기열크기
*/
size(): number {
return this.queue.length;
}
/**
* Check if queue is empty
*
* **Korean**: 대기열 비어있음
*
* @returns True if queue has no pending requests
*
* @korean 대기열비어있음
*/
isEmpty(): boolean {
return this.queue.length === 0;
}
/**
* Check if queue is full
*
* **Korean**: 대기열 가득참
*
* @returns True if queue is at maximum capacity
*
* @korean 대기열가득찬
*/
isFull(): boolean {
return this.queue.length >= this.maxSize;
}
/**
* Get all pending requests (read-only)
*
* **Korean**: 모든 대기 요청
*
* @returns Array of pending requests
*
* @korean 모든대기요청
*/
getAll(): readonly AnimationRequest[] {
return [...this.queue];
}
/**
* Get maximum queue size
*
* **Korean**: 최대 대기열 크기
*
* @returns Maximum queue capacity
*
* @korean 최대대기열크기
*/
getMaxSize(): number {
return this.maxSize;
}
/**
* Set conflict resolution strategy
*
* **Korean**: 충돌 해결 전략 설정
*
* Updates the strategy used to resolve equal-priority conflicts.
*
* @param strategy - New conflict resolution strategy
*
* @korean 충돌해결전략설정
*/
setConflictStrategy(strategy: ConflictResolutionStrategy): void {
this.conflictStrategy = strategy;
}
}
// ===== Interruptibility Windows (중단 가능 구간) =====
/**
* Interruptibility window definition
*
* **Korean**: 중단 가능 구간
*
* Defines specific frame ranges where an animation can be interrupted,
* allowing for more nuanced control than simple boolean interruptibility.
*
* Example: Attack animation might be interruptible only in startup frames (0-3)
* and recovery frames (10-12), but not during active frames (4-9).
*
* @korean 중단가능구간
*/
export interface InterruptibilityWindow {
/** Starting frame (inclusive) */
readonly startFrame: number;
/** Ending frame (inclusive) */
readonly endFrame: number;
/** Minimum priority required to interrupt during this window */
readonly minPriorityToInterrupt: AnimationPriority;
}
/**
* Check if an animation can be interrupted at a specific frame
*
* **Korean**: 프레임별 중단 가능 여부
*
* Determines if an animation can be interrupted based on:
* - Current frame index
* - Interruptibility windows
* - Requested animation priority
*
* @param currentFrame - Current frame index in animation
* @param requestedPriority - Priority of animation trying to interrupt
* @param windows - Array of interruptibility windows
* @returns Whether animation can be interrupted at this frame
*
* @example
* ```typescript
* const attackWindows: InterruptibilityWindow[] = [
* { startFrame: 0, endFrame: 3, minPriorityToInterrupt: AnimationPriority.ATTACK },
* { startFrame: 10, endFrame: 12, minPriorityToInterrupt: AnimationPriority.DEFEND },
* ];
*
* // Frame 2: interruptible by attacks or higher
* canInterruptAtFrame(2, AnimationPriority.HIT, attackWindows); // true
* canInterruptAtFrame(2, AnimationPriority.DEFEND, attackWindows); // false
*
* // Frame 5: not interruptible (no window)
* canInterruptAtFrame(5, AnimationPriority.KO, attackWindows); // false
* ```
*
* @korean 프레임별중단가능여부
*/
export function canInterruptAtFrame(
currentFrame: number,
requestedPriority: AnimationPriority,
windows: readonly InterruptibilityWindow[]
): boolean {
// Find window that contains current frame
const activeWindow = windows.find(
w => currentFrame >= w.startFrame && currentFrame <= w.endFrame
);
// If no window contains this frame, cannot interrupt
if (!activeWindow) {
return false;
}
// Check if requested priority meets window's minimum
return requestedPriority >= activeWindow.minPriorityToInterrupt;
}
/**
* Get interruptibility window for current frame
*
* **Korean**: 현재 프레임 중단 가능 구간
*
* Returns the active interruptibility window for the given frame,
* or null if frame is not in any window.
*
* @param currentFrame - Current frame index
* @param windows - Array of interruptibility windows
* @returns Active window or null
*
* @korean 현재프레임중단가능구간
*/
export function getInterruptibilityWindow(
currentFrame: number,
windows: readonly InterruptibilityWindow[]
): InterruptibilityWindow | null {
return windows.find(
w => currentFrame >= w.startFrame && currentFrame <= w.endFrame
) ?? null;
}
|