All files / systems/animation/builders KickPhaseApplicator.ts

89.47% Statements 51/57
62.66% Branches 47/75
80% Functions 4/5
89.47% Lines 51/57

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                                                                                                                                                                    2137x     2137x 2137x 2137x 2137x     2137x 2137x   2137x 2137x                 2137x 2137x                 2137x 2137x       2137x         2137x                                             273x   273x 273x   273x 273x     273x 273x   273x 273x       273x                                             271x   271x 271x   271x 271x     271x 271x   271x 271x 271x 271x       271x                                             108x   108x 108x 108x 108x   108x 108x   108x 108x   108x 108x             108x 108x                 108x                                      
/**
 * Kick Phase Application Utilities
 *
 * Utilities for applying kick phase poses to keyframes with integrated
 * anatomy awareness (foot highlighting for kicks).
 * 발차기 단계 적용 유틸리티 (해부학 통합)
 *
 * @module systems/animation/KickPhaseApplicator
 * @korean 발차기단계적용기
 */
 
import { BoneName } from "@/types/skeletal";
import type { KeyframeConfig } from "./KeyframeConfig";
import { KICK_PHASES } from "./MartialArtsConstants";
 
/**
 * Interface for basic kick phases (CHAMBER, EXTENSION, HIGH_PEAK)
 * These phases have pelvis as a tuple [x, y, z]
 */
interface BasicKickPhase {
  readonly hip: readonly [number, number, number];
  readonly knee: readonly [number, number, number];
  readonly ankle?: readonly [number, number, number];
  readonly supportKnee?: readonly [number, number, number];
  readonly pelvis?: readonly [number, number, number];
}
 
/**
 * Interface for rotational kick phases (ROUNDHOUSE_CHAMBER, SIDE_CHAMBER)
 * These phases have pelvisY as a single Y-axis value
 */
interface RotationalKickPhase {
  readonly hip: readonly [number, number, number];
  readonly knee: readonly [number, number, number];
  readonly pelvisY?: number;
  readonly spineY?: number;
  readonly spineLean?: number;
}
 
/** Phase name keys */
export type KickPhaseName = keyof typeof KICK_PHASES;
 
/** Kick side for left/right leg distinction */
export type KickSide = "left" | "right";
 
/**
 * Apply basic kick phase to a KeyframeConfig with anatomy integration
 * Handles common kick phase bones: hip, knee, supportKnee, pelvis, ankle
 * Now includes automatic foot highlighting for kick visualization
 *
 * @param kf - KeyframeConfig to apply phase to
 * @param phase - Basic kick phase data (CHAMBER, EXTENSION, HIGH_PEAK)
 * @param options - Configuration including anatomy options
 *
 * @example
 * ```typescript
 * // Apply kick with automatic foot highlight
 * applyKickPhaseToConfig(kf, KICK_PHASES.EXTENSION, {
 *   highlightKickingFoot: true
 * });
 * ```
 *
 * @korean KeyframeConfig에발차기단계적용
 */
export function applyKickPhaseToConfig(
  kf: KeyframeConfig,
  phase: BasicKickPhase,
  options: {
    readonly includeAnkle?: boolean;
    readonly includePelvis?: boolean;
    readonly resetFoot?: boolean;
    // Anatomy integration
    readonly side?: KickSide;
    readonly highlightKickingFoot?: boolean;
  } = {}
): void {
  const {
    includeAnkle = false,
    includePelvis = true,
    resetFoot = false,
    side = "right",
    highlightKickingFoot = false,
  } = options;
 
  // Select bones based on kicking leg
  const hipBone = side === "right" ? BoneName.HIP_R : BoneName.HIP_L;
  const kneeBone = side === "right" ? BoneName.KNEE_R : BoneName.KNEE_L;
  const footBone = side === "right" ? BoneName.FOOT_R : BoneName.FOOT_L;
  const supportKneeBone = side === "right" ? BoneName.KNEE_L : BoneName.KNEE_R;
 
  // Required bones for all kick phases
  kf.rotate(hipBone, phase.hip[0], phase.hip[1], phase.hip[2]);
  kf.rotate(kneeBone, phase.knee[0], phase.knee[1], phase.knee[2]);
 
  Eif (phase.supportKnee) {
    kf.rotate(
      supportKneeBone,
      phase.supportKnee[0],
      phase.supportKnee[1],
      phase.supportKnee[2]
    );
  }
 
  // Optional pelvis
  Eif (includePelvis && phase.pelvis) {
    kf.rotate(
      BoneName.PELVIS,
      phase.pelvis[0],
      phase.pelvis[1],
      phase.pelvis[2]
    );
  }
 
  // Optional ankle
  Eif (includeAnkle && phase.ankle) {
    kf.rotate(footBone, phase.ankle[0], phase.ankle[1], phase.ankle[2]);
  }
 
  // Reset foot position
  Iif (resetFoot) {
    kf.rotate(footBone, 0, 0, 0);
  }
 
  // Anatomy integration: Highlight kicking foot
  Iif (highlightKickingFoot) {
    kf.setFootHighlight(side, true);
  }
}
 
/**
 * Apply roundhouse-specific kick phase with anatomy integration
 * Includes pelvisY and spineY single-axis rotations
 *
 * @param kf - KeyframeConfig to apply phase to
 * @param phase - Rotational kick phase data (ROUNDHOUSE_CHAMBER)
 * @param options - Configuration including anatomy options
 *
 * @korean 돌려차기단계적용
 */
export function applyRoundhousePhaseToConfig(
  kf: KeyframeConfig,
  phase: RotationalKickPhase,
  options: {
    readonly side?: KickSide;
    readonly highlightKickingFoot?: boolean;
  } = {}
): void {
  const { side = "right", highlightKickingFoot = false } = options;
 
  const hipBone = side === "right" ? BoneName.HIP_R : BoneName.HIP_L;
  const kneeBone = side === "right" ? BoneName.KNEE_R : BoneName.KNEE_L;
 
  kf.rotate(hipBone, phase.hip[0], phase.hip[1], phase.hip[2]);
  kf.rotate(kneeBone, phase.knee[0], phase.knee[1], phase.knee[2]);
 
  // Y-axis only rotations for roundhouse
  Eif (phase.pelvisY !== undefined) {
    kf.rotate(BoneName.PELVIS, 0, phase.pelvisY, 0);
  }
  Eif (phase.spineY !== undefined) {
    kf.rotate(BoneName.SPINE_UPPER, 0, phase.spineY, 0);
  }
 
  // Anatomy integration
  Iif (highlightKickingFoot) {
    kf.setFootHighlight(side, true);
  }
}
 
/**
 * Apply side kick phase with anatomy integration
 * Includes pelvisY, spineY, and spineLean
 *
 * @param kf - KeyframeConfig to apply phase to
 * @param phase - Rotational kick phase data (SIDE_CHAMBER)
 * @param options - Configuration including anatomy options
 *
 * @korean 옆차기단계적용
 */
export function applySideKickPhaseToConfig(
  kf: KeyframeConfig,
  phase: RotationalKickPhase,
  options: {
    readonly side?: KickSide;
    readonly highlightKickingFoot?: boolean;
  } = {}
): void {
  const { side = "right", highlightKickingFoot = false } = options;
 
  const hipBone = side === "right" ? BoneName.HIP_R : BoneName.HIP_L;
  const kneeBone = side === "right" ? BoneName.KNEE_R : BoneName.KNEE_L;
 
  kf.rotate(hipBone, phase.hip[0], phase.hip[1], phase.hip[2]);
  kf.rotate(kneeBone, phase.knee[0], phase.knee[1], phase.knee[2]);
 
  // Side kick specific rotations
  Eif (phase.pelvisY !== undefined) {
    kf.rotate(BoneName.PELVIS, 0, phase.pelvisY, 0);
  }
  Eif (phase.spineY !== undefined) {
    const lean = phase.spineLean ?? 0;
    kf.rotate(BoneName.SPINE_LOWER, 0, phase.spineY, 0);
    kf.rotate(BoneName.SPINE_UPPER, 0, phase.spineY, lean);
  }
 
  // Anatomy integration
  Iif (highlightKickingFoot) {
    kf.setFootHighlight(side, true);
  }
}
 
/**
 * Apply high peak phase (axe kick rise) with anatomy integration
 * Includes full pelvis tuple and support knee
 *
 * @param kf - KeyframeConfig to apply phase to
 * @param phase - Basic kick phase data (HIGH_PEAK)
 * @param options - Configuration including anatomy options
 *
 * @korean 높이올리기단계적용
 */
export function applyHighPeakPhaseToConfig(
  kf: KeyframeConfig,
  phase: BasicKickPhase,
  options: {
    readonly side?: KickSide;
    readonly highlightKickingFoot?: boolean;
  } = {}
): void {
  const { side = "right", highlightKickingFoot = false } = options;
 
  const hipBone = side === "right" ? BoneName.HIP_R : BoneName.HIP_L;
  const kneeBone = side === "right" ? BoneName.KNEE_R : BoneName.KNEE_L;
  const footBone = side === "right" ? BoneName.FOOT_R : BoneName.FOOT_L;
  const supportKneeBone = side === "right" ? BoneName.KNEE_L : BoneName.KNEE_R;
 
  kf.rotate(hipBone, phase.hip[0], phase.hip[1], phase.hip[2]);
  kf.rotate(kneeBone, phase.knee[0], phase.knee[1], phase.knee[2]);
 
  Eif (phase.ankle) {
    kf.rotate(footBone, phase.ankle[0], phase.ankle[1], phase.ankle[2]);
  }
  Eif (phase.supportKnee) {
    kf.rotate(
      supportKneeBone,
      phase.supportKnee[0],
      phase.supportKnee[1],
      phase.supportKnee[2]
    );
  }
  Eif (phase.pelvis) {
    kf.rotate(
      BoneName.PELVIS,
      phase.pelvis[0],
      phase.pelvis[1],
      phase.pelvis[2]
    );
  }
 
  // Anatomy integration
  Iif (highlightKickingFoot) {
    kf.setFootHighlight(side, true);
  }
}
 
/** Union type for any kick phase from KICK_PHASES */
export type KickPhase = (typeof KICK_PHASES)[keyof typeof KICK_PHASES];
 
/**
 * Get a kick phase by name
 *
 * @param phaseName - Name of the phase from KICK_PHASES
 * @returns The kick phase data
 *
 * @korean 발차기단계가져오기
 */
export function getKickPhase(phaseName: KickPhaseName): KickPhase {
  return KICK_PHASES[phaseName];
}