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 | 11x 11x 11x 11x 11x 11x 11x 11x 11x | /**
* useTrainingActions Hook - Training Action Handlers
*
* Custom hook for managing training action handlers.
* Mirrors useCombatActions pattern for consistency.
*
* @korean 훈련액션훅 - 훈련 액션 핸들러 관리
*/
import { useCallback, useRef } from "react";
import { AnimationState } from "../../../systems/animation/types";
import { TRIGRAM_STANCES_ORDER } from "../../../systems/trigram/types";
import { Position, TrigramStance } from "../../../types/common";
import { TrainingActions, TrainingScreenState } from "./useTrainingState";
export interface UseTrainingActionsConfig {
readonly state: TrainingScreenState;
readonly actions: TrainingActions;
readonly playerPosition: Position;
readonly player3DPosition: [number, number, number];
readonly dummyPosition: [number, number, number];
readonly audio: {
readonly playSFX: (sound: string) => void;
};
readonly onPlayerUpdate: (updates: {
currentStance?: TrigramStance;
lastActionTime?: number;
position?: Position;
}) => void;
readonly playerAnimation: {
readonly transitionTo: (state: AnimationState) => boolean;
readonly currentState: string;
};
/** External ref to store pending attack data - shared with animation events */
readonly pendingAttackRef: React.MutableRefObject<{
accuracy: number;
vitalPoint: string;
} | null>;
}
export interface UseTrainingActionsReturn {
readonly handleStartTraining: () => void;
readonly handleStopTraining: () => void;
readonly handleDummyHit: (vitalPointId: string) => boolean;
readonly handleDummyDefeated: () => void;
readonly handleStanceChange: (stanceIndex: number) => void;
readonly handleAttack: () => void;
}
/**
* Calculate hit accuracy based on distance from player to dummy
* Uses squared distance to avoid expensive Math.sqrt
*/
function calculateHitAccuracy(
playerPos: [number, number, number],
dummyPos: [number, number, number]
): number {
const dx = playerPos[0] - dummyPos[0];
const dz = playerPos[2] - dummyPos[2];
const squaredDistance = dx * dx + dz * dz;
// Max effective range is 8 units (squared = 64)
return Math.max(0, 1 - squaredDistance / 64);
}
/**
* useTrainingActions hook
* Provides training action handlers with proper memoization
*/
export function useTrainingActions(
config: UseTrainingActionsConfig
): UseTrainingActionsReturn {
const {
state,
actions,
player3DPosition,
dummyPosition,
audio,
onPlayerUpdate,
playerAnimation,
pendingAttackRef,
} = config;
// Ref to store timeout for dummy reset
const dummyResetTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const handleStartTraining = useCallback(() => {
actions.startTraining();
audio.playSFX("menu_select");
}, [actions, audio]);
const handleStopTraining = useCallback(() => {
// Clear any pending dummy reset timeout
if (dummyResetTimeoutRef.current) {
clearTimeout(dummyResetTimeoutRef.current);
dummyResetTimeoutRef.current = null;
}
actions.stopTraining();
audio.playSFX("menu_back");
}, [actions, audio]);
const handleDummyDefeated = useCallback(() => {
actions.setFeedback("훈련 더미 무력화! | Dummy Defeated!");
audio.playSFX("ki_release");
// Clear any existing timeout
if (dummyResetTimeoutRef.current) {
clearTimeout(dummyResetTimeoutRef.current);
}
// Reset dummy health after delay
dummyResetTimeoutRef.current = setTimeout(() => {
actions.resetDummy();
}, 2000);
}, [actions, audio]);
const handleDummyHit = useCallback(
(_vitalPointId: string): boolean => {
const accuracy = calculateHitAccuracy(player3DPosition, dummyPosition);
// Determine hit position (dummy center)
const hitPosition: [number, number, number] = [
dummyPosition[0],
1.5,
dummyPosition[2],
];
if (accuracy > 0.5) {
const points = Math.round(accuracy * 100);
const damage = Math.round(accuracy * 15); // 0-15 damage based on accuracy
const isPerfect = accuracy > 0.9;
// Register hit with state (only counts if training)
if (state.isTraining) {
actions.registerHit(points, damage, isPerfect);
}
// Determine feedback and sound
let effectType: "success" | "perfect";
if (isPerfect) {
actions.setFeedback("완벽한 타격! | Perfect Strike!");
audio.playSFX("ki_release");
effectType = "perfect";
} else if (accuracy > 0.7) {
actions.setFeedback("좋은 타격! | Good Strike!");
audio.playSFX("ki_charge");
effectType = "success";
} else {
actions.setFeedback("타격 성공 | Strike Success");
audio.playSFX("menu_click");
effectType = "success";
}
// Add hit effect
actions.addHitEffect({
position: hitPosition,
type: effectType,
visible: true,
damage,
});
return true;
} else {
// Register miss (only counts if training)
if (state.isTraining) {
actions.registerMiss();
}
actions.setFeedback("빗나감 | Miss - Get closer!");
audio.playSFX("menu_navigate");
// Add miss effect
actions.addHitEffect({
position: hitPosition,
type: "miss",
visible: true,
});
return false;
}
},
[state.isTraining, player3DPosition, dummyPosition, actions, audio]
);
const handleStanceChange = useCallback(
(stanceIndex: number) => {
actions.setStanceIndex(stanceIndex);
const stance = TRIGRAM_STANCES_ORDER[stanceIndex];
if (stance) {
playerAnimation.transitionTo("stance_change");
onPlayerUpdate({ currentStance: stance });
audio.playSFX("stance_change");
}
},
[actions, onPlayerUpdate, audio, playerAnimation]
);
const handleAttack = useCallback(() => {
// Calculate attack accuracy and store it (allow attacks even when not training for exploration)
const accuracy = calculateHitAccuracy(player3DPosition, dummyPosition);
pendingAttackRef.current = {
accuracy,
vitalPoint: state.selectedVitalPoint ?? "generic",
};
// Trigger attack animation - this will fire onFrame event at frame 6
playerAnimation.transitionTo("attack");
// Play attack sound
audio.playSFX("whoosh");
}, [
state.selectedVitalPoint,
player3DPosition,
dummyPosition,
playerAnimation,
audio,
pendingAttackRef,
]);
return {
handleStartTraining,
handleStopTraining,
handleDummyHit,
handleDummyDefeated,
handleStanceChange,
handleAttack,
};
}
export default useTrainingActions;
|