All files / components/screens/combat/components/effects CombatParticleEffects3D.tsx

23.33% Statements 21/90
13.46% Branches 7/52
10.52% Functions 2/19
29.16% Lines 21/72

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                                                                                                                                                                                                                                      2x 2x                                                   2x   60x     60x 60x 60x         60x 30x 30x 30x   30x                                                                                                             30x         30x         30x         30x             60x       60x       60x       60x   60x                                                      
/**
 * CombatParticleEffects3D - Particle effects coordinator for combat
 * 전투 입자 효과 통합 관리
 *
 * Maps HitEffect events to advanced particle effects:
 * - BloodViscosity3D for blood physics on hits
 * - InternalDamage3D for organ damage visualization on vital point strikes
 * - ParticleAudio3D for synchronized combat audio
 *
 * @module components/effects
 * @category Combat Effects
 * @korean 전투입자효과
 */
 
import { useCallback, useEffect, useRef, useState } from "react";
import type { HitEffect } from "../../../../../systems";
import { HitEffectType } from "../../../../../systems/effects";
import {
  BloodViscosity3D,
  type BloodViscosityEffect,
  type ViscosityType,
} from "./BloodViscosity3D";
import {
  InternalDamage3D,
  type InternalDamageEffect,
  type OrganType,
  type PenetrationDepth,
} from "./InternalDamage3D";
import {
  ParticleAudio3D,
  type ParticleAudioTrigger,
  type ParticleEffectType,
} from "./ParticleAudio3D";
 
/**
 * Props for CombatParticleEffects3D
 */
export interface CombatParticleEffects3DProps {
  /** Active hit effects from the combat state */
  readonly hitEffects: readonly HitEffect[];
  /** Whether effects are enabled */
  readonly enabled?: boolean;
  /** Mobile optimization flag */
  readonly isMobile?: boolean;
}
 
/**
 * Map HitEffectType → blood viscosity type
 * 타격 유형 → 혈액 점도 매핑
 */
function getViscosityForHitType(type: HitEffectType): ViscosityType | null {
  switch (type) {
    case HitEffectType.HIT:
      return "thin";
    case HitEffectType.COUNTER:
      return "medium";
    case HitEffectType.CRITICAL_HIT:
      return "thick";
    case HitEffectType.VITAL_POINT_STRIKE:
      return "gout";
    default:
      return null;
  }
}
 
/**
 * Map HitEffectType → organ type based on strike position
 * 급소 유형 → 장기 매핑 (타격 위치 기반)
 */
function getOrganForPosition(
  position: { x: number; y: number } | undefined,
): OrganType {
  if (!position) return "stomach";
  // Map y-position to organ (based on typical character model height)
  const y = position.y;
  if (y > 1.4) return "heart"; // 심장 - upper chest
  if (y > 1.1) return "stomach"; // 명치 - solar plexus
  if (y > 0.8) return "liver"; // 간 - right side mid-torso
  if (y > 0.5) return "spleen"; // 비장 - left side mid-torso
  return "kidney"; // 신장 - lower back
}
 
/**
 * Map HitEffect intensity → penetration depth
 * 타격 강도 → 관통 깊이 매핑
 */
function getPenetrationDepth(intensity: number): PenetrationDepth {
  if (intensity >= 1.5) return "critical";
  if (intensity >= 1.0) return "deep";
  if (intensity >= 0.5) return "shallow";
  return "surface";
}
 
/**
 * Map HitEffectType → audio effect type
 * 타격 유형 → 오디오 효과 매핑
 */
function getAudioEffectType(type: HitEffectType): ParticleEffectType | null {
  switch (type) {
    case HitEffectType.HIT:
    case HitEffectType.COUNTER:
      return "viscosity";
    case HitEffectType.CRITICAL_HIT:
      return "bone";
    case HitEffectType.VITAL_POINT_STRIKE:
      return "organ";
    case HitEffectType.BLOCK:
    case HitEffectType.PARRY:
      return "bone";
    default:
      return null;
  }
}
 
/** Maximum concurrent effects to prevent performance issues */
const MAX_BLOOD_EFFECTS = 8;
const MAX_ORGAN_EFFECTS = 4;
 
/**
 * Simple deterministic hash from string → [0, 1)
 * Used for deterministic direction calculations inside useMemo
 */
function hashToFloat(str: string): number {
  let hash = 0;
  for (let i = 0; i < str.length; i++) {
    hash = (hash * 31 + str.charCodeAt(i)) | 0;
  }
  return Math.abs(hash % 10000) / 10000;
}
 
/**
 * CombatParticleEffects3D - Coordinates particle effects for combat
 *
 * Converts HitEffect events into:
 * - Blood viscosity particles (thin/medium/thick/gout based on strike type)
 * - Internal organ damage pulses (for vital point strikes)
 * - Synchronized audio triggers
 *
 * @korean 전투 입자 효과 통합 (타격→혈액물리+장기손상+오디오)
 */
export const CombatParticleEffects3D: React.FC<
  CombatParticleEffects3DProps
> = ({ hitEffects, enabled = true, isMobile = false }) => {
  // Track processed hit effect IDs to avoid duplicates
  const processedIds = useRef<Set<string>>(new Set());
 
  // Effect state arrays
  const [bloodEffects, setBloodEffects] = useState<BloodViscosityEffect[]>([]);
  const [organEffects, setOrganEffects] = useState<InternalDamageEffect[]>([]);
  const [audioTriggers, setAudioTriggers] = useState<ParticleAudioTrigger[]>(
    [],
  );
 
  // Process new HitEffects in useEffect (not during render)
  useEffect(() => {
    const newBlood: BloodViscosityEffect[] = [];
    const newOrgan: InternalDamageEffect[] = [];
    const newAudio: ParticleAudioTrigger[] = [];
 
    for (const hit of hitEffects) {
      if (processedIds.current.has(hit.id)) continue;
      processedIds.current.add(hit.id);
 
      const pos: [number, number, number] = [
        hit.position?.x ?? 0,
        hit.position?.y ?? 1.0,
        0,
      ];
 
      // Blood viscosity effect
      const viscosity = getViscosityForHitType(hit.type);
      if (viscosity) {
        // Direction: deterministic spray based on hit ID
        const angle = hashToFloat(hit.id) * Math.PI * 2;
        const ySpread = 0.3 + hashToFloat(hit.id + "_y") * 0.4;
        const dir: [number, number, number] = [
          Math.cos(angle) * 0.5,
          ySpread,
          Math.sin(angle) * 0.5,
        ];
 
        newBlood.push({
          id: `blood_${hit.id}`,
          position: pos,
          direction: dir,
          viscosityType: viscosity,
          intensity: Math.min(1, hit.intensity),
          startTime: hit.startTime,
        });
      }
 
      // Internal damage for vital point strikes
      if (hit.type === HitEffectType.VITAL_POINT_STRIKE) {
        newOrgan.push({
          id: `organ_${hit.id}`,
          position: pos,
          organType: getOrganForPosition(hit.position),
          penetrationDepth: getPenetrationDepth(hit.intensity),
          startTime: hit.startTime,
        });
      }
 
      // Audio trigger
      const audioType = getAudioEffectType(hit.type);
      if (audioType) {
        newAudio.push({
          effectType: audioType,
          intensity: Math.min(1, hit.intensity),
          timestamp: hit.timestamp,
        });
      }
    }
 
    // Batch merge with existing effects (respecting limits)
    Iif (newBlood.length > 0) {
      setBloodEffects((prev) =>
        [...prev, ...newBlood].slice(-MAX_BLOOD_EFFECTS),
      );
    }
    Iif (newOrgan.length > 0) {
      setOrganEffects((prev) =>
        [...prev, ...newOrgan].slice(-MAX_ORGAN_EFFECTS),
      );
    }
    Iif (newAudio.length > 0) {
      setAudioTriggers((prev) => [...prev, ...newAudio]);
    }
 
    // Clean up old processed IDs periodically
    Iif (processedIds.current.size > 500) {
      const arr = Array.from(processedIds.current);
      processedIds.current = new Set(arr.slice(-250));
    }
  }, [hitEffects]);
 
  // Effect completion handlers
  const handleBloodComplete = useCallback((id: string) => {
    setBloodEffects((prev) => prev.filter((e) => e.id !== id));
  }, []);
 
  const handleOrganComplete = useCallback((id: string) => {
    setOrganEffects((prev) => prev.filter((e) => e.id !== id));
  }, []);
 
  const handleAudioProcessed = useCallback((timestamp: number) => {
    setAudioTriggers((prev) => prev.filter((t) => t.timestamp !== timestamp));
  }, []);
 
  Iif (!enabled) return null;
 
  return (
    <>
      {/* Blood Viscosity Particles - 혈액 점도 입자 */}
      <BloodViscosity3D
        effects={bloodEffects}
        enabled={enabled}
        isMobile={isMobile}
        onEffectComplete={handleBloodComplete}
      />
 
      {/* Internal Organ Damage Pulses - 장기 손상 시각화 */}
      <InternalDamage3D
        effects={organEffects}
        enabled={enabled}
        isMobile={isMobile}
        onEffectComplete={handleOrganComplete}
      />
 
      {/* Audio Coordination - 입자 효과 오디오 동기화 */}
      <ParticleAudio3D
        triggers={audioTriggers}
        enabled={enabled}
        onTriggerProcessed={handleAudioProcessed}
      />
    </>
  );
};