All files / systems/animation KeyframeConfig.ts

56.25% Statements 9/16
0% Branches 0/2
50% Functions 2/4
56.25% Lines 9/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                                                                        17602x     17602x     17602x     17602x     17602x                                                                 541034x 541034x                                       15600x 15600x                                                
/**
 * Keyframe Configuration Helper
 *
 * Helper class for configuring animation keyframes with bone rotations and positions.
 * 애니메이션 키프레임 설정 헬퍼 클래스
 *
 * @module systems/animation/KeyframeConfig
 * @korean 키프레임설정
 */
 
import * as THREE from "three";
import type { AnimationKeyframe } from "../../types/skeletal";
import { BoneName } from "../../types/skeletal";
 
// Forward declaration for builder type
interface AnimationBuilderLike {
  _addKeyframe(kf: AnimationKeyframe): void;
}
 
/**
 * Keyframe configuration helper
 *
 * Provides a fluent API for configuring bone rotations and positions
 * within a single keyframe.
 *
 * @example
 * ```typescript
 * const kf = new KeyframeConfig();
 * kf.rotate(BoneName.SHOULDER_R, -0.5, 0, 0.3)
 *   .position(BoneName.HAND_R, 0.1, 0, 0);
 * ```
 *
 * @korean 키프레임설정헬퍼
 */
export class KeyframeConfig {
  /** Bone rotation data (bone -> euler rotation) */
  rotations = new Map<BoneName, THREE.Euler>();
 
  /** Bone position data (bone -> position offset) */
  positions = new Map<BoneName, THREE.Vector3>();
 
  /** Parent builder reference for fluent chaining */
  private builder: AnimationBuilderLike | null = null;
 
  /** Keyframe time */
  private time: number = 0;
 
  /** Keyframe easing function */
  private easing: string = "linear";
 
  /**
   * Associate this keyframe with a builder for the `done()` method
   * @internal
   */
  setBuilder(
    builder: AnimationBuilderLike,
    time: number,
    easing: string
  ): void {
    this.builder = builder;
    this.time = time;
    this.easing = easing;
  }
 
  /**
   * Set bone rotation (euler angles in radians)
   *
   * @param bone - Target bone name
   * @param x - X rotation (radians)
   * @param y - Y rotation (radians)
   * @param z - Z rotation (radians)
   * @returns this for chaining
   *
   * @example
   * ```typescript
   * kf.rotate(BoneName.SHOULDER_R, -0.5, 0, 0.3);
   * ```
   *
   * @korean 뼈회전설정
   */
  rotate(bone: BoneName, x: number, y: number, z: number): this {
    this.rotations.set(bone, new THREE.Euler(x, y, z));
    return this;
  }
 
  /**
   * Set bone position offset
   *
   * @param bone - Target bone name
   * @param x - X position offset
   * @param y - Y position offset
   * @param z - Z position offset
   * @returns this for chaining
   *
   * @example
   * ```typescript
   * kf.position(BoneName.FOOT_R, 0, 0.5, 0.3);
   * ```
   *
   * @korean 뼈위치설정
   */
  position(bone: BoneName, x: number, y: number, z: number): this {
    this.positions.set(bone, new THREE.Vector3(x, y, z));
    return this;
  }
 
  /**
   * Complete keyframe configuration and return to builder
   *
   * @returns Parent builder for method chaining
   * @throws Error if not associated with a builder
   *
   * @korean 키프레임완료
   */
  done<T extends AnimationBuilderLike>(): T {
    if (!this.builder) {
      throw new Error("KeyframeConfig not associated with builder");
    }
    this.builder._addKeyframe({
      time: this.time,
      easing: this.easing as "linear" | "ease-in" | "ease-out" | "ease-in-out",
      boneRotations: this.rotations,
      bonePositions: this.positions,
    });
    return this.builder as T;
  }
}