All files / components/training/components HitFeedbackEffect3D.tsx

3.84% Statements 4/104
0% Branches 0/55
0% Functions 0/16
4.08% Lines 4/98

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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356                                                                                1x                                                                                                                                                                                                                         1x                                                                                               1x                                                                                                                         1x                                                                                                                                                                                                  
/**
 * HitFeedbackEffect3D - Visual hit confirmation with damage numbers
 * 
 * Provides particle effects, color flashes, and floating damage numbers
 * for training hit feedback
 */
 
import { Html } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import * as THREE from "three";
import { KOREAN_COLORS, FONT_FAMILY } from "../../../types/constants";
 
/**
 * Props for HitFeedbackEffect3D component
 */
export interface HitFeedbackEffect3DProps {
  /** 3D position where hit occurred */
  readonly position: [number, number, number];
  /** Type of hit (affects visual style) */
  readonly type: "success" | "perfect" | "miss";
  /** Damage dealt (if applicable) */
  readonly damage?: number;
  /** Whether effect is visible */
  readonly visible?: boolean;
  /** Callback when effect completes */
  readonly onComplete?: () => void;
  /** Duration in milliseconds */
  readonly duration?: number;
  /** Whether on mobile device */
  readonly isMobile?: boolean;
}
 
/**
 * Impact particle system
 */
const ImpactParticles: React.FC<{
  position: [number, number, number];
  color: number;
  count: number;
}> = ({ position, color, count }) => {
  const pointsRef = useRef<THREE.Points>(null);
  
  // Store velocities in a ref that persists across renders
  const velocitiesRef = useRef<Float32Array | null>(null);
  
  // Store initial position for seeded random - use useState to capture at mount
  // Note: This intentionally ignores position prop changes to maintain consistent
  // particle behavior throughout the effect's lifetime. To update particles when
  // position changes, add a key prop to the parent component to force remount.
  const [initialPosition] = useState(position);
 
  // Initialize particle positions and velocities - use seed based on initial position
  const { positions, velocities } = useMemo(() => {
    const pos = new Float32Array(count * 3);
    const vel = new Float32Array(count * 3);
 
    // Use initial position as seed for deterministic but varying particles
    const seed = initialPosition[0] + initialPosition[1] * 10 + initialPosition[2] * 100;
    
    // Simple seeded random using position
    // Large multiplier (10000) ensures sufficient entropy for randomness while keeping values deterministic
    function seededRandom(index: number): number {
      const x = Math.sin(seed + index) * 10000;
      return x - Math.floor(x);
    }
 
    for (let i = 0; i < count; i++) {
      const i3 = i * 3;
      // Start at center
      pos[i3] = 0;
      pos[i3 + 1] = 0;
      pos[i3 + 2] = 0;
 
      // Random outward velocities using seeded random
      const theta = seededRandom(i * 3) * Math.PI * 2;
      const phi = seededRandom(i * 3 + 1) * Math.PI;
      const speed = 0.5 + seededRandom(i * 3 + 2) * 1.5;
 
      vel[i3] = Math.sin(phi) * Math.cos(theta) * speed;
      vel[i3 + 1] = Math.cos(phi) * speed + 1; // Upward bias
      vel[i3 + 2] = Math.sin(phi) * Math.sin(theta) * speed;
    }
 
    return { positions: pos, velocities: vel };
  }, [count, initialPosition]); // initialPosition is captured at mount and won't change
  
  // Update velocities ref in useEffect to avoid ref access during render
  useEffect(() => {
    velocitiesRef.current = velocities;
  }, [velocities]);
 
  // Animate particles
  useFrame((_, delta) => {
    if (!pointsRef.current || !velocitiesRef.current) return;
 
    const attr = pointsRef.current.geometry.attributes.position;
    const array = attr.array as Float32Array;
    const vel = velocitiesRef.current;
 
    for (let i = 0; i < count; i++) {
      const i3 = i * 3;
 
      // Update positions
      array[i3] += vel[i3] * delta * 10;
      array[i3 + 1] += vel[i3 + 1] * delta * 10;
      array[i3 + 2] += vel[i3 + 2] * delta * 10;
 
      // Apply gravity
      // Note: Intentionally mutating velocitiesRef.current (Float32Array) for performance.
      // This is safe and won't trigger React re-renders since it's a ref.
      vel[i3 + 1] -= 9.8 * delta;
 
      // Fade out particles that go too far
      if (array[i3 + 1] < -2) {
        array[i3 + 1] = -2;
      }
    }
 
    attr.needsUpdate = true;
  });
 
  return (
    <points ref={pointsRef} position={position}>
      <bufferGeometry>
        <bufferAttribute
          attach="attributes-position"
          args={[positions, 3]}
        />
      </bufferGeometry>
      <pointsMaterial
        size={0.05}
        color={color}
        transparent
        opacity={0.8}
        sizeAttenuation
        depthWrite={false}
      />
    </points>
  );
};
 
/**
 * Expanding ring effect
 */
const RingEffect: React.FC<{
  position: [number, number, number];
  color: number;
  maxRadius: number;
}> = ({ position, color, maxRadius }) => {
  const meshRef = useRef<THREE.Mesh>(null);
  const startTime = useRef<number>(0);
  
  // Initialize start time on mount using useEffect
  useEffect(() => {
    if (startTime.current === 0) {
      startTime.current = Date.now();
    }
  }, []);
 
  useFrame(() => {
    if (!meshRef.current) return;
 
    const elapsed = (Date.now() - startTime.current) / 1000;
    const progress = Math.min(elapsed / 0.5, 1); // 0.5 second duration
 
    // Expand ring
    const radius = progress * maxRadius;
    meshRef.current.scale.setScalar(radius);
 
    // Fade out
    const material = meshRef.current.material as THREE.MeshBasicMaterial;
    material.opacity = 1 - progress;
  });
 
  return (
    <mesh ref={meshRef} position={position} rotation={[Math.PI / 2, 0, 0]}>
      <ringGeometry args={[0.9, 1.0, 32]} />
      <meshBasicMaterial
        color={color}
        transparent
        opacity={1}
        side={THREE.DoubleSide}
      />
    </mesh>
  );
};
 
/**
 * Floating damage number
 */
const DamageNumber: React.FC<{
  position: [number, number, number];
  damage: number;
  type: "perfect" | "normal" | "miss";
  isMobile: boolean;
  onComplete: () => void;
}> = ({ position, damage, type, isMobile, onComplete }) => {
  const [offset, setOffset] = useState(0);
  const [opacity, setOpacity] = useState(1);
  const startTime = useRef<number>(0);
  const completedRef = useRef(false);
  
  // Initialize start time on mount using useEffect
  useEffect(() => {
    if (startTime.current === 0) {
      startTime.current = Date.now();
    }
  }, []);
 
  useFrame(() => {
    const elapsed = (Date.now() - startTime.current) / 1000;
    const progress = Math.min(elapsed / 1.5, 1); // 1.5 second duration
 
    // Float upward
    setOffset(progress * 1);
 
    // Fade out
    setOpacity(1 - progress);
 
    // Complete when done (only once)
    if (progress >= 1 && !completedRef.current && onComplete) {
      completedRef.current = true;
      onComplete();
    }
  });
 
  const color = type === "perfect" ? "#ffd700" : type === "normal" ? "#00ffff" : "#ff4444";
  const text = type === "miss" ? "빗나감 | MISS" : `${damage}`; // Korean: 빗나감 = miss/deflected
 
  return (
    <Html
      position={[position[0], position[1] + offset, position[2]]}
      center
      distanceFactor={10}
      style={{ pointerEvents: "none", opacity }}
    >
      <div
        style={{
          fontSize: isMobile ? "20px" : "28px",
          fontWeight: "bold",
          fontFamily: FONT_FAMILY.KOREAN,
          color,
          textShadow: "0 0 10px rgba(0, 0, 0, 0.8)",
          whiteSpace: "nowrap",
        }}
        data-testid="damage-number"
      >
        {text}
      </div>
    </Html>
  );
};
 
/**
 * HitFeedbackEffect3D Component
 * Main hit feedback visualization
 */
export const HitFeedbackEffect3D: React.FC<HitFeedbackEffect3DProps> = ({
  position,
  type,
  damage,
  visible = true,
  onComplete,
  duration = 1500,
  isMobile = false,
}) => {
  const [showEffect, setShowEffect] = useState(visible);
  const timeoutRef = useRef<NodeJS.Timeout | null>(null);
 
  // Get colors based on hit type
  const effectColor = useMemo(() => {
    switch (type) {
      case "perfect":
        return KOREAN_COLORS.ACCENT_GOLD;
      case "success":
        return KOREAN_COLORS.PRIMARY_CYAN;
      case "miss":
        return KOREAN_COLORS.TEXT_SECONDARY;
      default:
        return KOREAN_COLORS.PRIMARY_CYAN;
    }
  }, [type]);
 
  const particleCount = type === "perfect" ? 30 : type === "success" ? 20 : 10;
  const ringRadius = type === "perfect" ? 1.5 : 1.0;
 
  // Track completion to prevent multiple calls
  const completedRef = useRef(false);
 
  // Handle effect completion (only once)
  const handleComplete = useCallback(() => {
    if (completedRef.current) return;
    completedRef.current = true;
    setShowEffect(false);
    onComplete?.();
  }, [onComplete]);
 
  // Auto-complete after duration
  useEffect(() => {
    if (visible && showEffect) {
      timeoutRef.current = setTimeout(handleComplete, duration);
    }
 
    return () => {
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
      }
    };
  }, [visible, showEffect, duration, handleComplete]);
 
  if (!showEffect) return null;
 
  return (
    <group data-testid={`hit-feedback-${type}`}>
      {/* Particle burst */}
      <ImpactParticles
        position={position}
        color={effectColor}
        count={particleCount}
      />
 
      {/* Expanding ring */}
      <RingEffect
        position={position}
        color={effectColor}
        maxRadius={ringRadius}
      />
 
      {/* Floating damage number */}
      {damage !== undefined && type !== "miss" && (
        <DamageNumber
          position={position}
          damage={damage}
          type={type === "perfect" ? "perfect" : "normal"}
          isMobile={isMobile}
          onComplete={handleComplete}
        />
      )}
 
      {/* Miss indicator */}
      {type === "miss" && (
        <DamageNumber
          position={position}
          damage={0}
          type="miss"
          isMobile={isMobile}
          onComplete={handleComplete}
        />
      )}
    </group>
  );
};
 
export default HitFeedbackEffect3D;