All files / hooks useSkeletalAnimation.ts

96.59% Statements 85/88
89.23% Branches 116/130
100% Functions 12/12
100% Lines 80/80

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                                                                                                          11x 11x 11x   11x                           11x               634x       49x                 317x 11x     306x 2x   304x 265x 265x   39x 317x       4x         11x 11x     5x 5x 4x 4x 4x 4x         8x 1x 1x 1x 1x 1x           3x       1x 1x                                                     317x   317x 317x   317x 317x     317x 317x 317x 156x     317x 317x 317x 317x   317x 156x     317x   110x 110x 110x 110x 110x 110x 7x 7x 7x 7x   110x 110x 2x 2x 2x 2x   110x   45x     110x 110x 110x 110x 110x 110x 110x   110x 3x 3x 3x           317x                            
/**
 * Selects and applies skeletal clips for the player renderer.
 * The gameplay timeline owns combat events; this hook only writes visual poses.
 *
 * @module hooks/useSkeletalAnimation
 * @category Hooks
 * @korean 골격애니메이션훅
 */
 
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react";
import {
  getAnimation,
  getAttackAnimation,
  getDefensiveAnimation,
  getFootworkAnimation,
  getStepAnimation,
  performanceMonitor,
} from "../systems/animation";
import { PLAYER_PLAYBACK_CLIPS } from "../systems/animation/catalogs/PlayerPlaybackClips";
import { applyLaterality } from "../systems/animation/core/LateralityTransform";
import {
  prepareSkeletalPose,
  SkeletalPosePlayback,
} from "../systems/animation/core/SkeletalPosePlayback";
import type { TrigramStance } from "../types/common";
import type { PlayerAnimation } from "../types/player-visual";
import type { SkeletalAnimation, SkeletalAnimationState, SkeletalRig } from "../types/skeletal";
 
export interface UseSkeletalAnimationOptions {
  readonly currentAnimation: PlayerAnimation;
  /** Resolved clip name/ID; not a request to resolve the technique again. */
  readonly attackAnimation?: string;
  readonly isBlocking?: boolean;
  readonly stance?: TrigramStance;
  readonly laterality?: "left" | "right";
  /** Change to replay the same action and clip. */
  readonly playbackKey?: string | number;
  /** Visual completion only; do not use to schedule combat events. */
  readonly onAnimationComplete?: () => void;
}
 
export interface UseSkeletalAnimationReturn {
  readonly animState: SkeletalAnimationState;
  readonly animTimeRef: React.MutableRefObject<number>;
  /**
   * Apply the current pose. Optionally pass authored clip-local seconds from an
   * external timeline; this bypasses the internal clock and speed multiplier.
   */
  readonly updateRigAnimation: (rig: SkeletalRig, delta: number, clipTimeSeconds?: number) => void;
  /** The parent owns model facing, including diagonal steps. */
  readonly diagonalRotationY: number | null;
}
 
const MAX_VISUAL_DELTA = 1 / 30;
const MAX_BLEND_SECONDS = 0.09;
const WALK_SEAM_SECONDS = 0.05;
 
const VISUAL_FALLBACKS: Partial<Record<PlayerAnimation, string>> = {
  defend_block_success: "block_high",
  defend_parry: "gam_flowing_block",
  defend_guard_break: "hit",
  defend_recovery: "block_low",
  stance_side_switch: "footwork_switch_step",
  turn_left: "movement_pivot_left",
  turn_right: "movement_pivot_right",
  grapple_entry: "grapple",
  grapple_control: "gon_ground_control_transition",
  grapple_struggle: "joint_lock_defense",
  grapple_escape: "backward_retreat",
};
 
const DIAGONAL_STEP_FOOTWORK: Partial<Record<PlayerAnimation, string>> = {
  step_forward_left: "footwork_circular_left",
  step_forward_right: "footwork_circular_right",
  step_back_left: "footwork_slide_left",
  step_back_right: "footwork_slide_right",
};
 
function isNamedAttack(state: PlayerAnimation): boolean {
  return state === "attack" || state === "technique_execute" || state === "counter";
}
 
function findVisualClip(name: string): SkeletalAnimation | undefined {
  return getAnimation(name) ?? PLAYER_PLAYBACK_CLIPS.get(name) ??
    getFootworkAnimation(name) ?? getStepAnimation(name);
}
 
function selectClip(
  state: PlayerAnimation,
  name: string | undefined,
  stance: TrigramStance | undefined,
): SkeletalAnimation | undefined {
  if (isNamedAttack(state)) {
    return (name ? getAnimation(name) ?? getAttackAnimation(name) : undefined) ??
      getAnimation(state === "counter" ? "counter_strike" : "jab");
  }
  if (state === "defend" || state === "block") {
    return (name ? getDefensiveAnimation(name) : undefined) ?? getAnimation("block");
  }
  if ((state === "idle" || state === "walk" || state === "run") && stance) {
    const variant = getAnimation(`${state === "idle" ? "stance" : state}_${stance}`);
    Eif (variant) return variant;
  }
  const fallback = VISUAL_FALLBACKS[state] ?? DIAGONAL_STEP_FOOTWORK[state];
  return findVisualClip(state) ?? (fallback ? findVisualClip(fallback) : undefined);
}
 
function isDirectional(state: PlayerAnimation): boolean {
  return state.startsWith("step_") || state.startsWith("footwork_") ||
    state.startsWith("turn_") || state.startsWith("fall_") ||
    state.startsWith("ground_");
}
 
const mirroredClips = new WeakMap<SkeletalAnimation, SkeletalAnimation>();
const walkCycles = new WeakMap<SkeletalAnimation, SkeletalAnimation>();
 
function mirror(clip: SkeletalAnimation): SkeletalAnimation {
  const cached = mirroredClips.get(clip);
  if (cached) return cached;
  const mirrored = applyLaterality(clip, "left");
  mirroredClips.set(clip, mirrored);
  mirroredClips.set(mirrored, clip);
  return mirrored;
}
 
/** Stance walks are single strides. Alternate leads instead of snapping back to frame zero. */
function loopWalk(clip: SkeletalAnimation): SkeletalAnimation {
  if (clip.loop || !clip.keyframes[0]) return clip;
  const cached = walkCycles.get(clip);
  Iif (cached) return cached;
  const oppositeStart = clip.duration + WALK_SEAM_SECONDS;
  const duration = oppositeStart * 2;
  const cycle = {
    ...clip,
    loop: true,
    duration,
    keyframes: [
      ...clip.keyframes,
      ...mirror(clip).keyframes.map((key) => ({ ...key, time: key.time + oppositeStart })),
      { ...clip.keyframes[0], time: duration },
    ],
  };
  walkCycles.set(clip, cycle);
  return cycle;
}
 
interface PlaybackSession {
  readonly clip: SkeletalAnimation;
  readonly speed: number;
}
 
interface LivePlayback {
  readonly rig: SkeletalRig;
  readonly pose: SkeletalPosePlayback;
  session: PlaybackSession;
  blendTime: number;
  completed: boolean;
}
 
export function useSkeletalAnimation(
  {
    currentAnimation,
    attackAnimation,
    isBlocking = false,
    stance,
    laterality = "right",
    playbackKey,
    onAnimationComplete,
  }: UseSkeletalAnimationOptions,
): UseSkeletalAnimationReturn {
  const passive = currentAnimation === "idle" || currentAnimation.startsWith("stance_") &&
    currentAnimation !== "stance_change" && currentAnimation !== "stance_side_switch";
  const state = passive && isBlocking ? "defend" : currentAnimation;
  const name = isNamedAttack(currentAnimation) || currentAnimation === "defend" ||
    currentAnimation === "block" ? attackAnimation : undefined;
  const baseClip = selectClip(state, name, stance) ?? getAnimation("idle");
  const oriented = baseClip && laterality === "left" && !isDirectional(state)
    ? mirror(baseClip)
    : baseClip;
  const clip = oriented && state === "walk" ? loopWalk(oriented) : oriented;
  const speed = state === "stance_change" ? 1.2 : passive && state !== "defend" ? 0.5 : 1;
  const session = useMemo(
    () => (clip ? { clip, speed, state, name, playbackKey } : null),
    [clip, speed, state, name, playbackKey],
  );
  const prepared = useMemo(() => clip ? prepareSkeletalPose(clip) : null, [clip]);
  const live = useRef<LivePlayback | null>(null);
  const animTimeRef = useRef(0);
  const [finished, setFinished] = useState<PlaybackSession | null>(null);
 
  useLayoutEffect(() => {
    animTimeRef.current = 0;
  }, [session]);
 
  const updateRigAnimation = useCallback(
    (rig: SkeletalRig, delta: number, clipTimeSeconds?: number) => {
      Iif (!session || !prepared) return;
      const start = performance.now();
      const dt = Number.isFinite(delta) ? Math.min(MAX_VISUAL_DELTA, Math.max(0, delta)) : 0;
      const { duration, loop } = session.clip;
      const blendDuration = Math.min(MAX_BLEND_SECONDS, duration * 0.3);
      if (live.current?.rig !== rig) {
        const alreadyCompleted = live.current?.session === session && live.current.completed;
        const pose = new SkeletalPosePlayback(rig);
        Iif (alreadyCompleted) pose.apply(prepared, duration);
        live.current = { rig, pose, session, blendTime: blendDuration, completed: alreadyCompleted };
      }
      const playback = live.current;
      if (playback.session !== session) {
        playback.pose.capture();
        playback.session = session;
        playback.blendTime = 0;
        playback.completed = false;
      }
      if (playback.completed) return;
 
      const next = clipTimeSeconds !== undefined && Number.isFinite(clipTimeSeconds)
        ? Math.max(0, clipTimeSeconds)
        : animTimeRef.current + dt * session.speed;
      const time = loop && duration > 0 ? next % duration : Math.min(next, duration);
      const done = !loop && time >= duration;
      playback.blendTime = Math.min(blendDuration, playback.blendTime + dt);
      const alpha = done || blendDuration <= 0 ? 1 : playback.blendTime / blendDuration;
      playback.pose.apply(prepared, time, alpha * alpha * (3 - 2 * alpha));
      animTimeRef.current = time;
      performanceMonitor.recordFrame(performance.now() - start);
 
      if (done) {
        playback.completed = true;
        setFinished(session);
        onAnimationComplete?.();
      }
    },
    [onAnimationComplete, prepared, session],
  );
 
  return {
    animState: {
      currentAnimation: clip ?? null,
      currentTime: session && finished === session ? session.clip.duration : 0,
      isPlaying: session !== null && finished !== session,
      playbackSpeed: speed,
      previousKeyframeIndex: 0,
      nextKeyframeIndex: 1,
    },
    animTimeRef,
    updateRigAnimation,
    diagonalRotationY: null,
  };
}