All files / components/screens/combat/hooks useGrapplingAudio.ts

88.5% Statements 77/87
77.27% Branches 34/44
93.33% Functions 14/15
92.5% Lines 74/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 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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333                                                      1x         1x             1x                           1x                                         7x   1x   5x               1x                 1x 17x 17x 17x 17x     17x   17x 17x 18x 17x             17x 20x 20x     20x 1x       19x 1x     18x           17x 18x 18x       18x                         17x   8x   7x 7x     7x   7x 7x                           17x   4x   4x     4x   4x 4x                           17x   2x   2x     2x   2x 2x                               17x   3x   2x     2x   2x 2x                           17x   1x   1x     1x   1x 1x                           17x   2x   2x     2x   2x 2x                           17x     4x 1x       4x 1x       4x             17x                        
/**
 * Grappling Audio Hook for Black Trigram
 * 
 * **Korean**: 잡기 오디오 훅 (Grapple Audio Hook)
 * 
 * Provides audio feedback for grappling actions including:
 * - Grab connection sounds
 * - Struggle/escape attempt sounds
 * - Successful escape sounds
 * - Bone crack sounds for joint locks
 * - Counter-attack sounds
 * - Limb exposure warning sounds
 * 
 * Uses placeholder audio until grappling assets are created.
 * 
 * @module components/screens/combat/hooks/useGrapplingAudio
 * @category Combat Audio
 * @korean 잡기오디오훅
 */
 
import { useCallback, useEffect, useRef } from "react";
import { useAudio } from "../../../../audio/AudioProvider";
import { GrappleState, GrappleTarget } from "../../../../types/common";
 
/**
 * Minimum interval between audio plays to prevent audio chaos (ms)
 */
const MIN_AUDIO_INTERVAL = 100;
 
/**
 * Maximum simultaneous grappling sounds
 */
const MAX_SIMULTANEOUS_SOUNDS = 3;
 
/**
 * Grappling sound type identifiers
 * 
 * **Korean**: 잡기 소리 유형 (japgi sori yuhyeong)
 */
const GRAPPLING_SOUND_TYPES = {
  CONNECT: "grapple_connect",
  STRUGGLE: "grapple_struggle",
  ESCAPE: "grapple_escape",
  BONE_CRACK: "bone_crack",
  COUNTER_ATTACK: "counter_attack",
  LIMB_EXPOSURE_WARNING: "limb_exposure_warning",
} as const;
 
/**
 * Placeholder audio mapping until grappling assets are created
 * 
 * Maps grappling sound IDs to existing combat audio assets
 */
const GRAPPLING_AUDIO_PLACEHOLDERS: Record<string, string> = {
  [GRAPPLING_SOUND_TYPES.CONNECT]: "attack_medium",
  [GRAPPLING_SOUND_TYPES.STRUGGLE]: "hit_medium",
  [GRAPPLING_SOUND_TYPES.ESCAPE]: "attack_light",
  [GRAPPLING_SOUND_TYPES.BONE_CRACK]: "hit_critical",
  [GRAPPLING_SOUND_TYPES.COUNTER_ATTACK]: "attack_critical",
  [GRAPPLING_SOUND_TYPES.LIMB_EXPOSURE_WARNING]: "energy_pulse",
};
 
/**
 * Note: Audio system automatically selects from registered variations
 * based on the base ID. No need to manually construct variant IDs.
 * The AudioManager.playSFX() method handles variation selection internally.
 */
 
/**
 * Get audio intensity modifier for grapple target
 * 
 * Different body parts have different audio characteristics
 */
function getTargetVolumeModifier(target: GrappleTarget): number {
  switch (target) {
    case GrappleTarget.HAND:
      return 0.8; // Quieter, smaller target
    case GrappleTarget.ARM:
      return 1.0; // Standard volume
    case GrappleTarget.LEG:
      return 1.1; // Slightly louder, larger target
    case GrappleTarget.TORSO:
      return 1.2; // Louder, central mass
    case GrappleTarget.NECK:
      return 0.9; // Critical but controlled
    case GrappleTarget.BOTH_ARMS:
      return 1.3; // Very loud, large grapple
  }
}
 
/**
 * Hook for playing grappling-related audio
 * 
 * @returns Methods for playing grappling sounds
 */
export const useGrapplingAudio = () => {
  const audio = useAudio();
  const lastPlayTime = useRef<Record<string, number>>({});
  const activeSoundCount = useRef(0); // Track concurrent sound instances
  const activeTimers = useRef<Set<NodeJS.Timeout>>(new Set()); // Track active timers for cleanup
 
  // Cleanup all active timers on unmount
  useEffect(() => {
    // Capture the current ref value for cleanup
    const timers = activeTimers.current;
    return () => {
      timers.forEach((timer) => clearTimeout(timer));
      timers.clear();
    };
  }, []);
 
  /**
   * Check if we can play a sound (rate limiting)
   */
  const canPlaySound = useCallback((soundType: string): boolean => {
    const now = Date.now();
    const lastTime = lastPlayTime.current[soundType] ?? 0;
 
    // Rate limiting check
    if (now - lastTime < MIN_AUDIO_INTERVAL) {
      return false;
    }
 
    // Check simultaneous sounds limit (track actual concurrent plays)
    if (activeSoundCount.current >= MAX_SIMULTANEOUS_SOUNDS) {
      return false;
    }
 
    return true;
  }, []);
 
  /**
   * Register a sound as active and auto-remove after duration
   */
  const registerActiveSound = useCallback((duration = 500) => {
    activeSoundCount.current++;
    const timer = setTimeout(() => {
      activeSoundCount.current = Math.max(0, activeSoundCount.current - 1);
      activeTimers.current.delete(timer);
    }, duration);
    activeTimers.current.add(timer);
  }, []);
 
  /**
   * Play grapple connect sound
   * 
   * **Korean**: 잡기 연결 소리 (japgi yeongyeol sori)
   * 
   * Plays when grapple is successfully established
   * 
   * @param target - Body part being grappled
   * @param volume - Volume multiplier (0-1)
   */
  const playGrappleConnect = useCallback(
    (target: GrappleTarget, volume = 1.0) => {
      if (!canPlaySound(GRAPPLING_SOUND_TYPES.CONNECT)) return;
 
      const placeholderId = GRAPPLING_AUDIO_PLACEHOLDERS[GRAPPLING_SOUND_TYPES.CONNECT];
      const volumeModifier = getTargetVolumeModifier(target);
 
      // AudioManager automatically selects from registered variations
      audio.playSFX(placeholderId, volume * volumeModifier * 0.8);
 
      lastPlayTime.current[GRAPPLING_SOUND_TYPES.CONNECT] = Date.now();
      registerActiveSound(300);
    },
    [audio, canPlaySound, registerActiveSound]
  );
 
  /**
   * Play grapple struggle sound
   * 
   * **Korean**: 탈출 시도 소리 (talchul sido sori)
   * 
   * Plays during escape attempts
   * 
   * @param intensity - Struggle intensity (0-1)
   */
  const playGrappleStruggle = useCallback(
    (intensity = 0.8) => {
      Iif (!canPlaySound(GRAPPLING_SOUND_TYPES.STRUGGLE)) return;
 
      const placeholderId = GRAPPLING_AUDIO_PLACEHOLDERS[GRAPPLING_SOUND_TYPES.STRUGGLE];
 
      // AudioManager automatically selects from registered variations
      audio.playSFX(placeholderId, intensity * 0.7);
 
      lastPlayTime.current[GRAPPLING_SOUND_TYPES.STRUGGLE] = Date.now();
      registerActiveSound(250);
    },
    [audio, canPlaySound, registerActiveSound]
  );
 
  /**
   * Play grapple escape sound
   * 
   * **Korean**: 탈출 성공 소리 (talchul seonggong sori)
   * 
   * Plays when defender successfully escapes
   * 
   * @param volume - Volume multiplier (0-1)
   */
  const playGrappleEscape = useCallback(
    (volume = 1.0) => {
      Iif (!canPlaySound(GRAPPLING_SOUND_TYPES.ESCAPE)) return;
 
      const placeholderId = GRAPPLING_AUDIO_PLACEHOLDERS[GRAPPLING_SOUND_TYPES.ESCAPE];
 
      // AudioManager automatically selects from registered variations
      audio.playSFX(placeholderId, volume * 0.9);
 
      lastPlayTime.current[GRAPPLING_SOUND_TYPES.ESCAPE] = Date.now();
      registerActiveSound(400);
    },
    [audio, canPlaySound, registerActiveSound]
  );
 
  /**
   * Play bone crack sound
   * 
   * **Korean**: 뼈 부러짐 소리 (ppyeo bureojim sori)
   * 
   * Plays for joint locks, throws, and breaking techniques
   * 
   * WARNING: High-impact sound, use sparingly
   * 
   * @param severity - Severity of technique (0-1)
   */
  const playBoneCrack = useCallback(
    (severity = 0.8) => {
      if (!canPlaySound(GRAPPLING_SOUND_TYPES.BONE_CRACK)) return;
 
      const placeholderId = GRAPPLING_AUDIO_PLACEHOLDERS[GRAPPLING_SOUND_TYPES.BONE_CRACK];
 
      // AudioManager automatically selects from registered variations
      audio.playSFX(placeholderId, severity * 0.85);
 
      lastPlayTime.current[GRAPPLING_SOUND_TYPES.BONE_CRACK] = Date.now();
      registerActiveSound(300);
    },
    [audio, canPlaySound, registerActiveSound]
  );
 
  /**
   * Play counter-attack sound
   * 
   * **Korean**: 반격 소리 (bangyeok sori)
   * 
   * Plays when defender successfully counters from grapple
   * 
   * @param volume - Volume multiplier (0-1)
   */
  const playCounterAttack = useCallback(
    (volume = 1.0) => {
      Iif (!canPlaySound(GRAPPLING_SOUND_TYPES.COUNTER_ATTACK)) return;
 
      const placeholderId = GRAPPLING_AUDIO_PLACEHOLDERS[GRAPPLING_SOUND_TYPES.COUNTER_ATTACK];
 
      // AudioManager automatically selects from registered variations
      audio.playSFX(placeholderId, volume * 0.9);
 
      lastPlayTime.current[GRAPPLING_SOUND_TYPES.COUNTER_ATTACK] = Date.now();
      registerActiveSound(350);
    },
    [audio, canPlaySound, registerActiveSound]
  );
 
  /**
   * Play limb exposure warning sound
   * 
   * **Korean**: 사지 노출 경고 소리 (saji nochul gyeonggo sori)
   * 
   * Plays when limb becomes exposed during grapple
   * 
   * @param volume - Volume multiplier (0-1)
   */
  const playLimbExposureWarning = useCallback(
    (volume = 0.6) => {
      Iif (!canPlaySound(GRAPPLING_SOUND_TYPES.LIMB_EXPOSURE_WARNING)) return;
 
      const placeholderId = GRAPPLING_AUDIO_PLACEHOLDERS[GRAPPLING_SOUND_TYPES.LIMB_EXPOSURE_WARNING];
 
      // AudioManager automatically selects from registered variations
      audio.playSFX(placeholderId, volume * 0.5);
 
      lastPlayTime.current[GRAPPLING_SOUND_TYPES.LIMB_EXPOSURE_WARNING] = Date.now();
      registerActiveSound(150);
    },
    [audio, canPlaySound, registerActiveSound]
  );
 
  /**
   * Play state transition sound
   * 
   * Automatically plays appropriate sound for grapple state change
   * 
   * @param newState - New grapple state
   * @param previousState - Previous grapple state
   * @param target - Body part being grappled
   */
  const playStateTransition = useCallback(
    (newState: GrappleState, previousState: GrappleState | null, target: GrappleTarget) => {
      // GRABBING -> CONTROLLING: Connection established
      if (previousState === GrappleState.GRABBING && newState === GrappleState.CONTROLLING) {
        playGrappleConnect(target);
      }
 
      // Any -> ESCAPING: Escape attempt
      if (newState === GrappleState.ESCAPING && previousState !== GrappleState.ESCAPING) {
        playGrappleStruggle(0.9);
      }
 
      // ESCAPING -> any other state: State change (potentially successful)
      Iif (previousState === GrappleState.ESCAPING && newState !== GrappleState.ESCAPING && newState !== GrappleState.CONTROLLING) {
        playGrappleEscape();
      }
    },
    [playGrappleConnect, playGrappleStruggle, playGrappleEscape]
  );
 
  return {
    playGrappleConnect,
    playGrappleStruggle,
    playGrappleEscape,
    playBoneCrack,
    playCounterAttack,
    playLimbExposureWarning,
    playStateTransition,
  };
};
 
export default useGrapplingAudio;