All files / utils player3DHelpers.ts

95.45% Statements 21/22
96.55% Branches 28/29
100% Functions 4/4
93.75% Lines 15/16

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                                              3x                                             131x                     204x 11x 4x 2x                     143x 142x 140x     139x   2x   1x       1x     135x                                                                                               136x                                                                                    
/**
 * Utility functions for Player3D component integration
 *
 * Converts PlayerState from combat system to Player3DUnifiedProps for rendering
 * with SkeletalPlayer3D (28-bone articulated body model).
 *
 * @module utils/player3DHelpers
 * @category Utilities
 * @korean 플레이어3D도우미
 */
 
import type { PlayerState } from "../systems";
import type { AnimationState } from "../systems/animation/types";
import type {
  BalanceState,
  Player3DUnifiedProps,
  PlayerAnimation,
} from "../types/player-visual";
 
/**
 * Static mapping from AnimationState to PlayerAnimation
 * @korean 애니메이션상태맵
 */
const ANIMATION_STATE_MAP: Record<AnimationState, PlayerAnimation> = {
  idle: "idle",
  walk: "walk",
  run: "walk", // Map run to walk for now (SkeletalPlayer3D doesn't have run animation yet)
  attack: "attack",
  defend: "defend",
  hit: "hit",
  stance_change: "stance_change",
  ko: "death", // Map ko to death
};
 
/**
 * Convert AnimationState to PlayerAnimation
 *
 * Maps the animation system's state types to SkeletalPlayer3D's animation types.
 *
 * @param animState - Animation state from the animation system
 * @returns Corresponding PlayerAnimation type
 * @korean 애니메이션상태변환
 */
export function animationStateToPlayerAnimation(
  animState: AnimationState
): PlayerAnimation {
  return ANIMATION_STATE_MAP[animState];
}
 
/**
 * Convert balance number (0-100) to BalanceState enum
 *
 * @param balance - Balance value from PlayerState (0-100)
 * @returns BalanceState enum value
 * @korean 균형상태변환
 */
export function getBalanceState(balance: number): BalanceState {
  if (balance >= 80) return "READY";
  if (balance >= 50) return "SHAKEN";
  if (balance >= 20) return "VULNERABLE";
  return "HELPLESS";
}
 
/**
 * Get current animation state from PlayerState
 *
 * @param player - Current player state
 * @returns PlayerAnimation enum value
 * @korean 애니메이션상태가져오기
 */
export function getPlayerAnimation(player: PlayerState): PlayerAnimation {
  if (player.isStunned) return "hit";
  if (player.isBlocking) return "defend";
  if (player.isCountering) return "counter";
 
  // Check combat state (CombatState enum values are lowercase strings)
  switch (player.combatState) {
    case "attacking":
      return "attack";
    case "defending":
      return "defend";
    case "stunned":
      return "hit";
    case "recovering":
      return "idle";
    case "idle":
    default:
      return "idle";
  }
}
 
/**
 * Converts PlayerState to Player3DUnifiedProps for visual rendering.
 *
 * Note: This function converts base PlayerState properties used in combat.
 * Training-specific stats (misses, accuracy, comboCount) are optional in PlayerState
 * and handled separately in training contexts.
 *
 * @param player - The player state to convert
 * @param position - 3D position [x, y, z]
 * @param rotation - Rotation in radians
 * @param options - Display and behavior options
 * @returns Props for SkeletalPlayer3D component (28-bone articulated body model)
 * @korean 플레이어상태변환
 *
 * @example
 * ```tsx
 * const playerProps = convertPlayerStateToProps(
 *   playerState,
 *   [-3, 0, 0],
 *   0,
 *   { isMobile: false, showVitalPoints: false }
 * );
 *
 * <SkeletalPlayer3D {...playerProps} />
 * ```
 */
export function convertPlayerStateToProps(
  player: PlayerState,
  position: [number, number, number],
  rotation: number,
  options: {
    readonly isMobile?: boolean;
    readonly facing?: "left" | "right";
    readonly scale?: number;
    readonly showDetails?: boolean;
    readonly showHealthBar?: boolean;
    readonly showStanceIndicator?: boolean;
    readonly onAnimationComplete?: () => void;
    // Facial expression options - 얼굴 표정 옵션
    readonly enableFacialExpressions?: boolean;
    readonly enableEyeTracking?: boolean;
    readonly opponentPosition?: [number, number, number];
  } = {}
): Player3DUnifiedProps {
  return {
    playerId: player.id,
    archetype: player.archetype,
    stance: player.currentStance,
    position,
    rotation,
 
    // Health and resources
    health: player.health,
    maxHealth: player.maxHealth,
    stamina: player.stamina,
    ki: player.ki,
 
    // Combat states
    pain: player.pain,
    balance: getBalanceState(player.balance),
    consciousness: player.consciousness,
 
    // Combat flags
    isBlocking: player.isBlocking,
    isStunned: player.isStunned,
    isCountering: player.isCountering,
 
    // Animation (derived from combat state)
    currentAnimation: getPlayerAnimation(player),
 
    // Display options
    name: player.name,
    isMobile: options.isMobile ?? false,
    facing: options.facing ?? "right",
    scale: options.scale ?? 1,
    showDetails: options.showDetails,
    showHealthBar: options.showHealthBar,
    showStanceIndicator: options.showStanceIndicator,
    onAnimationComplete: options.onAnimationComplete,
 
    // Facial expression options - 얼굴 표정 옵션
    enableFacialExpressions: options.enableFacialExpressions ?? false,
    enableEyeTracking: options.enableEyeTracking ?? true,
    opponentPosition: options.opponentPosition,
  };
}