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

3.22% Statements 3/93
0% Branches 0/36
0% Functions 0/12
3.44% Lines 3/87

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                                                                                                                                                  3x                                                                       3x                                                                                                                                                     3x                                                                                                                                                                                                                                                                                                                                                
/**
 * BloodParticles3D - Realistic blood splatter particle system
 *
 * Creates physics-based blood particles that spray from impact points with gravity,
 * creating pools on the arena floor. Optimized for 60fps with instanced rendering.
 *
 * Features:
 * - Gravity-based particle physics
 * - Blood pool accumulation on floor
 * - 10-second fade-out for pools
 * - Instanced rendering for performance
 * - Mobile-optimized particle counts
 *
 * @module components/combat/BloodParticles3D
 * @category Combat Effects
 * @korean 피입자3D
 */
 
import { Points, PointMaterial } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import React, { useRef, useMemo } from "react";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../../../../types/constants";
 
/**
 * Blood particle data structure for efficient simulation
 */
interface BloodParticle {
  /** Current position [x, y, z] */
  position: THREE.Vector3;
  /** Current velocity [x, y, z] */
  velocity: THREE.Vector3;
  /** Particle lifetime in seconds */
  lifetime: number;
  /** Time elapsed since creation */
  age: number;
  /** Whether particle has settled on floor */
  settled: boolean;
}
 
/**
 * Blood splatter effect configuration
 */
export interface BloodSplatterEffect {
  /** Unique identifier */
  readonly id: string;
  /** Origin position in 3D world space */
  readonly position: [number, number, number];
  /** Impact direction for splatter */
  readonly direction: [number, number, number];
  /** Intensity of effect (0.0 to 1.0) */
  readonly intensity: number;
  /** Timestamp when effect was created */
  readonly startTime: number;
}
 
/**
 * Props for BloodParticles3D component
 */
export interface BloodParticles3DProps {
  /** Active blood splatter effects to render */
  readonly effects: readonly BloodSplatterEffect[];
  /** Whether to enable blood effects (violence settings) */
  readonly enabled?: boolean;
  /** Mobile device mode (reduced particle count) */
  readonly isMobile?: boolean;
  /** Callback when effect completes */
  readonly onEffectComplete?: (effectId: string) => void;
}
 
/**
 * Performance and physics constants
 */
const BLOOD_CONSTANTS = {
  /** Gravity acceleration (m/s²) */
  GRAVITY: -9.8,
  /** Maximum particles per splatter effect */
  MAX_PARTICLES_DESKTOP: 300,
  MAX_PARTICLES_MOBILE: 100,
  /** Particle lifetime in seconds */
  PARTICLE_LIFETIME: 2.0,
  /** Blood pool fade duration in seconds */
  POOL_FADE_DURATION: 10.0,
  /** Initial velocity range */
  VELOCITY_MIN: 2.0,
  VELOCITY_MAX: 5.0,
  /** Spread angle in radians */
  SPREAD_ANGLE: Math.PI / 3,
  /** Floor Y position */
  FLOOR_Y: 0.0,
  /** Particle size */
  PARTICLE_SIZE: 0.05,
  /**
   * Maximum per-frame delta time (seconds) used to clamp the blood physics update.
   * 
   * We cap the simulation step at ~33ms (1/30) to avoid the classic
   * "spiral of death" when the frame rate drops: very large timesteps can
   * cause unstable motion, particles tunneling through the floor, or
   * visually exaggerated splatter. Treating anything below 30fps as
   * "slow motion" for this effect keeps the blood behavior stable even
   * on slower devices. If the engine's minimum target frame rate changes,
   * adjust this threshold accordingly.
   */
  MAX_DELTA: 1 / 30,
} as const;
 
/**
 * Generate initial particles for a blood splatter effect
 */
const generateBloodParticles = (
  effect: BloodSplatterEffect,
  maxParticles: number
): BloodParticle[] => {
  const particles: BloodParticle[] = [];
  const particleCount = Math.floor(maxParticles * effect.intensity);
 
  const baseDir = new THREE.Vector3(...effect.direction).normalize();
  const origin = new THREE.Vector3(...effect.position);
 
  for (let i = 0; i < particleCount; i++) {
    // Random spread around impact direction
    const spreadAngle = BLOOD_CONSTANTS.SPREAD_ANGLE;
    const phi = (Math.random() - 0.5) * spreadAngle;
    const theta = Math.random() * Math.PI * 2;
 
    // Rotate direction vector
    const direction = baseDir.clone();
    direction.applyAxisAngle(
      new THREE.Vector3(0, 1, 0),
      phi
    );
    direction.applyAxisAngle(
      new THREE.Vector3(0, 0, 1),
      theta
    );
 
    // Random velocity magnitude
    const speed =
      BLOOD_CONSTANTS.VELOCITY_MIN +
      Math.random() * (BLOOD_CONSTANTS.VELOCITY_MAX - BLOOD_CONSTANTS.VELOCITY_MIN);
 
    particles.push({
      position: origin.clone(),
      velocity: direction.multiplyScalar(speed),
      lifetime: BLOOD_CONSTANTS.PARTICLE_LIFETIME,
      age: 0,
      settled: false,
    });
  }
 
  return particles;
};
 
/**
 * BloodParticles3D Component
 *
 * Renders realistic blood splatter effects using physics-based particle simulation.
 * Optimized for performance with instanced rendering and efficient physics updates.
 *
 * @example
 * ```tsx
 * const [bloodEffects, setBloodEffects] = useState<BloodSplatterEffect[]>([]);
 *
 * // On hit event
 * const handleHit = (position: [number, number, number], direction: [number, number, number]) => {
 *   setBloodEffects([...bloodEffects, {
 *     id: generateId(),
 *     position,
 *     direction,
 *     intensity: 0.8,
 *     startTime: Date.now(),
 *   }]);
 * };
 *
 * <BloodParticles3D
 *   effects={bloodEffects}
 *   enabled={violenceSettings.blood}
 *   isMobile={isMobile}
 *   onEffectComplete={(id) => {
 *     setBloodEffects(prev => prev.filter(e => e.id !== id));
 *   }}
 * />
 * ```
 */
export const BloodParticles3D: React.FC<BloodParticles3DProps> = ({
  effects,
  enabled = true,
  isMobile = false,
  onEffectComplete,
}) => {
  // Particle system state
  const particlesRef = useRef<Map<string, BloodParticle[]>>(new Map());
  const poolParticlesRef = useRef<BloodParticle[]>([]);
  const pointsRef = useRef<THREE.Points>(null);
  const completedEffectsRef = useRef<Set<string>>(new Set());
 
  // Performance configuration
  const maxParticles = isMobile
    ? BLOOD_CONSTANTS.MAX_PARTICLES_MOBILE
    : BLOOD_CONSTANTS.MAX_PARTICLES_DESKTOP;
 
  // Blood color from Korean theme
  const bloodColor = useMemo(() => KOREAN_COLORS.BLOODLOSS_INDICATOR, []);
 
  // Initialize particles for new effects
  React.useEffect(() => {
    if (!enabled) return;
 
    effects.forEach((effect) => {
      if (!particlesRef.current.has(effect.id)) {
        const particles = generateBloodParticles(effect, maxParticles);
        particlesRef.current.set(effect.id, particles);
      }
    });
 
    // Clean up removed effects
    const effectIds = new Set(effects.map((e) => e.id));
    particlesRef.current.forEach((_, id) => {
      if (!effectIds.has(id)) {
        particlesRef.current.delete(id);
        completedEffectsRef.current.delete(id);
      }
    });
  }, [effects, enabled, maxParticles]);
 
  // Initial particle positions for first render
  // Actual positions are updated in useFrame
  const initialPositions = useMemo(() => {
    const positions = new Float32Array(maxParticles * 3);
    // Initialize with zeros - actual positions set in useFrame
    return positions;
  }, [maxParticles]);
 
  // Physics update loop
  useFrame((_, delta) => {
    if (!enabled || !pointsRef.current) return;
 
    const safeDelta = Math.min(delta, BLOOD_CONSTANTS.MAX_DELTA);
 
    let totalParticleIndex = 0;
    const attr = pointsRef.current.geometry.attributes.position;
    const posArray = attr.array as Float32Array;
 
    // Update active splatter particles
    particlesRef.current.forEach((particles, effectId) => {
      let hasActiveParticles = false;
 
      for (let i = 0; i < particles.length; i++) {
        const p = particles[i];
        p.age += safeDelta;
 
        if (!p.settled) {
          // Apply gravity
          p.velocity.y += BLOOD_CONSTANTS.GRAVITY * safeDelta;
 
          // Update position
          p.position.addScaledVector(p.velocity, safeDelta);
 
          // Check floor collision
          if (p.position.y <= BLOOD_CONSTANTS.FLOOR_Y) {
            p.position.y = BLOOD_CONSTANTS.FLOOR_Y;
            p.velocity.set(0, 0, 0);
            p.settled = true;
            p.lifetime = BLOOD_CONSTANTS.POOL_FADE_DURATION;
            p.age = 0;
 
            // Add to pool particles
            poolParticlesRef.current.push(p);
          } else {
            hasActiveParticles = true;
          }
        }
 
        // Update render position
        if (totalParticleIndex < posArray.length / 3) {
          posArray[totalParticleIndex * 3] = p.position.x;
          posArray[totalParticleIndex * 3 + 1] = p.position.y;
          posArray[totalParticleIndex * 3 + 2] = p.position.z;
          totalParticleIndex++;
        } else if (process.env.NODE_ENV === "development") {
          // Warn in development when particles are being dropped due to buffer overflow
          console.warn(
            `BloodParticles3D: Particle buffer full (${posArray.length / 3} particles). ` +
            `Additional particles are not rendered. Consider reducing particle counts or ` +
            `increasing maxParticles if this happens frequently.`
          );
        }
      }
 
      // Check if effect is complete
      if (!hasActiveParticles && !completedEffectsRef.current.has(effectId)) {
        completedEffectsRef.current.add(effectId);
        onEffectComplete?.(effectId);
      }
    });
 
    // Determine the maximum number of particles we can safely render this frame.
    // This explicitly ties the visible particle cap to both the buffer size and maxParticles.
    const maxVisibleParticles = Math.min(maxParticles, posArray.length / 3);
 
    // Update blood pool particles (fade out)
    if (totalParticleIndex >= maxVisibleParticles) {
      // Buffer is full: continue aging and culling pool particles, but do not write positions.
      poolParticlesRef.current = poolParticlesRef.current.filter((p) => {
        p.age += safeDelta;
        return p.age < p.lifetime;
      });
    } else {
      // There is still room in the buffer: write pool particle positions up to the cap.
      poolParticlesRef.current = poolParticlesRef.current.filter((p) => {
        p.age += safeDelta;
        const isAlive = p.age < p.lifetime;
 
        if (isAlive && totalParticleIndex < maxVisibleParticles) {
          posArray[totalParticleIndex * 3] = p.position.x;
          posArray[totalParticleIndex * 3 + 1] = p.position.y;
          posArray[totalParticleIndex * 3 + 2] = p.position.z;
          totalParticleIndex++;
        }
 
        return isAlive;
      });
    }
 
    attr.needsUpdate = true;
  });
 
  // Don't render if disabled or no effects
  if (!enabled || effects.length === 0) {
    return null;
  }
 
  return (
    <Points
      ref={pointsRef}
      positions={initialPositions}
      data-testid="blood-particles-3d"
    >
      <PointMaterial
        color={bloodColor}
        size={BLOOD_CONSTANTS.PARTICLE_SIZE}
        sizeAttenuation
        transparent
        opacity={0.9}
        depthWrite={false}
        blending={THREE.NormalBlending}
      />
    </Points>
  );
};
 
export default BloodParticles3D;