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 | 9x 183x 183x 183x 183x 183x 91x 91x 91x 91x 3x 3x 88x 2x 2x 2x 2x 86x 6x 6x 80x 1x 1x 79x 1x 1x 78x 3x 3x 3x 2x 75x 1x 1x 74x 1x 1x 73x 73x 90x 88x 90x 90x 183x 102x 102x 102x 102x 102x 5x 5x 5x 5x 183x | /**
* useSkeletalAnimation - Shared hook for skeletal animation management
*
* Centralizes skeletal animation state and frame updates for player characters.
* Reduces code duplication across SkeletalPlayer3D, Player3DWithTransitions,
* and screen components.
*
* @module hooks/useSkeletalAnimation
* @category Hooks
* @korean 골격애니메이션훅
*/
import { useCallback, useEffect, useRef, useState } from "react";
import {
applyKeyframeToRig,
getAnimation,
getAttackAnimation,
getDefensiveAnimation,
getFootworkAnimation,
getStepAnimation,
updateAnimation,
} from "../systems/animation";
import type { PlayerAnimation } from "../types/player-visual";
import type {
SkeletalAnimation,
SkeletalAnimationState,
SkeletalRig,
} from "../types/skeletal";
/**
* Options for useSkeletalAnimation hook
* @korean 골격애니메이션훅옵션
*/
export interface UseSkeletalAnimationOptions {
/** Current animation name */
readonly currentAnimation: PlayerAnimation;
/** Specific attack animation name (for attack state) */
readonly attackAnimation?: string;
/** Whether player is blocking */
readonly isBlocking?: boolean;
/** Callback when animation completes */
readonly onAnimationComplete?: () => void;
}
/**
* Return type for useSkeletalAnimation hook
* @korean 골격애니메이션훅반환타입
*/
export interface UseSkeletalAnimationReturn {
/** Current animation state */
readonly animState: SkeletalAnimationState;
/** Animation time reference (seconds) */
readonly animTimeRef: React.MutableRefObject<number>;
/** Update animation and apply to rig (call in useFrame) */
readonly updateRigAnimation: (rig: SkeletalRig, delta: number) => void;
/** Diagonal rotation override for step animations */
readonly diagonalRotationY: number | null;
}
/**
* Set of diagonal step animations for O(1) lookup
* @korean 대각선스텝애니메이션집합
*/
const DIAGONAL_STEP_ANIMATIONS = new Set([
"step_forward_left",
"step_forward_right",
"step_back_left",
"step_back_right",
]);
/**
* useSkeletalAnimation hook
*
* Manages skeletal animation state and frame updates for player characters.
* Handles animation selection based on player state (idle, walk, attack, etc.)
* and applies keyframes to the skeletal rig.
*
* @param options - Animation options
* @returns Animation state and update function
*
* @example
* ```tsx
* const { animState, animTimeRef, updateRigAnimation, diagonalRotationY } =
* useSkeletalAnimation({
* currentAnimation: "walk",
* isBlocking: false,
* onAnimationComplete: () => console.log("Animation completed"),
* });
*
* // In useFrame callback
* useFrame((_, delta) => {
* updateRigAnimation(rig, delta);
* });
* ```
*
* @korean 골격애니메이션훅
*/
export function useSkeletalAnimation(
options: UseSkeletalAnimationOptions
): UseSkeletalAnimationReturn {
const {
currentAnimation,
attackAnimation,
isBlocking = false,
onAnimationComplete,
} = options;
// Animation state
const [animState, setAnimState] = useState<SkeletalAnimationState>({
currentAnimation: null,
currentTime: 0,
isPlaying: false,
playbackSpeed: 1.0,
previousKeyframeIndex: 0,
nextKeyframeIndex: 1,
});
// Animation time ref (updated at 60fps without triggering re-renders)
const animTimeRef = useRef(0);
// Diagonal step rotation override (Y-axis rotation in radians)
const [diagonalRotationY, setDiagonalRotationY] = useState<number | null>(
null
);
// Load animation when currentAnimation or blocking state changes
useEffect(() => {
// Reset animation time whenever animation changes
animTimeRef.current = 0;
let selectedAnim: SkeletalAnimation | undefined;
let playbackSpeed = 1.0;
let shouldClearDiagonalRotation = true;
if (currentAnimation === "attack" && attackAnimation) {
// Attack animation - first check stance-specific attacks, then generic
selectedAnim =
getAttackAnimation(attackAnimation) ?? getAnimation(attackAnimation);
playbackSpeed = 1.0;
} else if (currentAnimation === "defend" || isBlocking) {
// Block/defend animation - check stance-specific defensive animations first
// If attackAnimation contains a defensive animation name, use it
Iif (attackAnimation) {
selectedAnim = getDefensiveAnimation(attackAnimation);
}
// Fall back to generic block animation
Eif (!selectedAnim) {
selectedAnim = getAnimation("block");
}
playbackSpeed = 1.0;
} else if (currentAnimation === "walk") {
// Walking animation
selectedAnim = getAnimation("walk");
playbackSpeed = 1.0;
} else if (currentAnimation === "stance_change") {
// Stance change animation
selectedAnim = getAnimation("idle_stance");
playbackSpeed = 1.2; // Slightly faster for responsiveness
} else if (currentAnimation === "hit") {
// Hit reaction - stop animation
setAnimState((prev) => ({
...prev,
isPlaying: false,
currentTime: 0,
}));
return;
} else if (currentAnimation?.startsWith("step_")) {
// Tactical step animation
selectedAnim = getStepAnimation(currentAnimation);
playbackSpeed = 1.0;
// Handle diagonal step rotation
if (DIAGONAL_STEP_ANIMATIONS.has(currentAnimation)) {
shouldClearDiagonalRotation = false;
// Diagonal rotation will be handled by parent component
// This hook only manages the flag
}
} else if (currentAnimation?.startsWith("footwork_")) {
// Footwork pattern animation
selectedAnim = getFootworkAnimation(currentAnimation);
playbackSpeed = 1.0;
} else if (currentAnimation?.startsWith("stance_guard_")) {
// Stance guard animation - use idle as base
selectedAnim = getAnimation("idle_stance");
playbackSpeed = 0.5; // Slow breathing animation
} else {
// Idle animation (fallback)
selectedAnim = getAnimation("idle_stance");
playbackSpeed = 0.5; // Slow breathing animation
}
// Clear diagonal rotation for non-diagonal animations
if (shouldClearDiagonalRotation) {
setDiagonalRotationY(null);
}
// Update animation state
Eif (selectedAnim) {
setAnimState({
currentAnimation: selectedAnim,
currentTime: 0,
isPlaying: true,
playbackSpeed,
previousKeyframeIndex: 0,
nextKeyframeIndex: 1,
});
}
}, [currentAnimation, attackAnimation, isBlocking]);
// Update animation and apply to rig (called at 60fps in useFrame)
const updateRigAnimation = useCallback(
(targetRig: SkeletalRig, delta: number) => {
Eif (animState.isPlaying && animState.currentAnimation) {
// Update animation time and get interpolated keyframe
const result = updateAnimation(
animState.currentAnimation,
animTimeRef.current,
delta,
animState.playbackSpeed
);
// Apply keyframe to rig
applyKeyframeToRig(targetRig, result.keyframe);
// Update time ref
animTimeRef.current = result.time;
// Handle animation completion
if (result.completed) {
animTimeRef.current = 0;
setAnimState((prev) => ({
...prev,
isPlaying: false,
currentTime: 0,
}));
// Trigger callback
Eif (onAnimationComplete) {
onAnimationComplete();
}
}
}
},
[animState, onAnimationComplete]
);
return {
animState,
animTimeRef,
updateRigAnimation,
diagonalRotationY,
};
}
|