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

4.04% Statements 4/99
0% Branches 0/30
0% Functions 0/11
4.3% Lines 4/93

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                                                                                                                                  1x                             1x                                                                                                                           1x                                                                                                                                                                                                                                                                                                                                                                                           1x  
/**
 * BoneCrackParticles3D - Bone fracture particle effects for brutal combat realism
 * 
 * Displays white/ivory bone fragments when bone-breaking techniques land successfully.
 * Supports different fracture types (hairline, compound, shatter) with varying severity.
 * 
 * 골절 입자 효과 (Bone Fracture Particle Effects)
 * 
 * @module BoneCrackParticles3D
 */
 
import { useEffect, useState } from 'react';
import { useFrame } from '@react-three/fiber';
import * as THREE from 'three';
 
/**
 * Fracture types based on Korean martial arts bone-breaking techniques
 * 골절 유형
 */
export type FractureType = 'hairline' | 'compound' | 'shatter';
 
/**
 * Bone types that can be fractured in combat
 * 골 유형
 */
export type BoneType = 'rib' | 'limb' | 'skull' | 'spine';
 
/**
 * Bone crack particle effect data
 * 골절 입자 효과 데이터
 */
export interface BoneCrackEffect {
  readonly id: string;
  readonly position: [number, number, number];
  readonly fractureType: FractureType;
  readonly boneType: BoneType;
  readonly impactDirection: [number, number, number];
  readonly startTime: number;
}
 
/**
 * Props for BoneCrackParticles3D component
 */
export interface BoneCrackParticles3DProps {
  readonly effects: readonly BoneCrackEffect[];
  readonly enabled?: boolean;
  readonly isMobile?: boolean;
  readonly onEffectComplete?: (id: string) => void;
}
 
/**
 * Particle instance for bone fragments
 */
interface BoneParticleInstance {
  readonly effectId: string;
  readonly points: THREE.Points;
  readonly velocities: Float32Array;
  readonly rotations: Float32Array;
  readonly lifetimes: Float32Array;
  readonly startTime: number;
}
 
/**
 * Physics constants for bone fragments
 */
const BONE_PHYSICS = {
  GRAVITY: -9.8,
  AIR_RESISTANCE: 0.96, // Heavier than blood
  ROTATION_SPEED: 3.0,
  FRAGMENT_SIZE_HAIRLINE: 0.05,
  FRAGMENT_SIZE_COMPOUND: 0.08,
  FRAGMENT_SIZE_SHATTER: 0.12,
  LIFETIME_ACTIVE: 2.0, // Fragments fall for 2s
  LIFETIME_GROUND: 8.0, // Persist on ground for 8s
  MAX_DELTA: 1 / 30,
} as const;
 
/**
 * Particle counts based on fracture severity
 */
const PARTICLE_COUNTS = {
  hairline: { desktop: 15, mobile: 8 },
  compound: { desktop: 35, mobile: 18 },
  shatter: { desktop: 60, mobile: 30 },
} as const;
 
/**
 * Get particle count based on fracture type and device
 */
function getParticleCount(
  fractureType: FractureType,
  isMobile: boolean
): number {
  const counts = PARTICLE_COUNTS[fractureType];
  return isMobile ? counts.mobile : counts.desktop;
}
 
/**
 * Get fragment size based on fracture type
 */
function getFragmentSize(fractureType: FractureType): number {
  switch (fractureType) {
    case 'hairline':
      return BONE_PHYSICS.FRAGMENT_SIZE_HAIRLINE;
    case 'compound':
      return BONE_PHYSICS.FRAGMENT_SIZE_COMPOUND;
    case 'shatter':
      return BONE_PHYSICS.FRAGMENT_SIZE_SHATTER;
  }
}
 
/**
 * Get velocity spread based on fracture type
 */
function getVelocitySpread(fractureType: FractureType): number {
  switch (fractureType) {
    case 'hairline':
      return 1.0; // Minimal spread
    case 'compound':
      return 2.5; // Moderate spread
    case 'shatter':
      return 4.5; // Wide explosion
  }
}
 
/**
 * BoneCrackParticles3D - Renders bone fracture particle effects
 * 
 * Features:
 * - Angular white/ivory fragments with realistic physics
 * - Three fracture types: hairline, compound, shatter
 * - Slower fall rate than blood (heavier fragments)
 * - Ground persistence (8 seconds)
 * - Mobile optimization (50% particle reduction)
 * 
 * 기능:
 * - 현실적인 물리 법칙을 가진 각진 흰색/상아색 파편
 * - 세 가지 골절 유형: 미세, 복합, 분쇄
 * - 혈액보다 느린 낙하 속도 (무거운 파편)
 * - 바닥 지속성 (8초)
 * - 모바일 최적화 (입자 50% 감소)
 */
export const BoneCrackParticles3D: React.FC<BoneCrackParticles3DProps> = ({
  effects,
  enabled = true,
  isMobile = false,
  onEffectComplete,
}) => {
  const [particleInstances, setParticleInstances] = useState<
    Map<string, BoneParticleInstance>
  >(new Map());
 
  // Create particle instances for new effects
  useEffect(() => {
    if (!enabled) return;
 
    effects.forEach((effect) => {
      if (!particleInstances.has(effect.id)) {
        const particleCount = getParticleCount(effect.fractureType, isMobile);
        const fragmentSize = getFragmentSize(effect.fractureType);
        const velocitySpread = getVelocitySpread(effect.fractureType);
 
        // Create positions buffer
        const positions = new Float32Array(particleCount * 3);
        const velocities = new Float32Array(particleCount * 3);
        const rotations = new Float32Array(particleCount * 3);
        const lifetimes = new Float32Array(particleCount);
 
        // Initialize particles at impact position
        for (let i = 0; i < particleCount; i++) {
          const i3 = i * 3;
 
          // Start at impact position
          positions[i3] = effect.position[0];
          positions[i3 + 1] = effect.position[1];
          positions[i3 + 2] = effect.position[2];
 
          // Velocity: explosion pattern based on impact direction
          const angle = (Math.PI * 2 * i) / particleCount;
          const elevation = Math.random() * Math.PI * 0.5; // 0-90 degrees
 
          const speed = Math.random() * velocitySpread + 1.0;
          velocities[i3] =
            effect.impactDirection[0] +
            Math.cos(angle) * Math.cos(elevation) * speed;
          velocities[i3 + 1] = Math.sin(elevation) * speed + 1.0; // Upward bias
          velocities[i3 + 2] =
            effect.impactDirection[2] +
            Math.sin(angle) * Math.cos(elevation) * speed;
 
          // Random rotation velocities for tumbling
          rotations[i3] = (Math.random() - 0.5) * BONE_PHYSICS.ROTATION_SPEED;
          rotations[i3 + 1] =
            (Math.random() - 0.5) * BONE_PHYSICS.ROTATION_SPEED;
          rotations[i3 + 2] =
            (Math.random() - 0.5) * BONE_PHYSICS.ROTATION_SPEED;
 
          // Lifetime (active + ground persistence)
          lifetimes[i] =
            BONE_PHYSICS.LIFETIME_ACTIVE + BONE_PHYSICS.LIFETIME_GROUND;
        }
 
        // Create geometry
        const geometry = new THREE.BufferGeometry();
        geometry.setAttribute(
          'position',
          new THREE.BufferAttribute(positions, 3)
        );
 
        // White/ivory material for bone fragments
        const material = new THREE.PointsMaterial({
          color: 0xf5f5dc, // Ivory/bone color
          size: fragmentSize,
          sizeAttenuation: true,
          transparent: true,
          opacity: 1.0,
          blending: THREE.NormalBlending,
          depthWrite: false,
        });
 
        const points = new THREE.Points(geometry, material);
 
        const instance: BoneParticleInstance = {
          effectId: effect.id,
          points,
          velocities,
          rotations,
          lifetimes,
          startTime: effect.startTime,
        };
 
        setParticleInstances((prev) => new Map(prev).set(effect.id, instance));
      }
    });
  }, [effects, enabled, isMobile, particleInstances]);
 
  // Animate bone fragments with physics
  useFrame((_state, delta) => {
    if (!enabled) return;
 
    const safeDelta = Math.min(delta, BONE_PHYSICS.MAX_DELTA);
    const currentTime = Date.now();
 
    particleInstances.forEach((instance, effectId) => {
      const positions = instance.points.geometry.attributes.position
        .array as Float32Array;
      const particleCount = positions.length / 3;
      let allExpired = true;
 
      for (let i = 0; i < particleCount; i++) {
        const i3 = i * 3;
        const lifetime = instance.lifetimes[i];
 
        if (lifetime > 0) {
          allExpired = false;
 
          const elapsed =
            (currentTime - instance.startTime) / 1000 - i * 0.01; // Stagger
 
          // Active falling phase
          if (elapsed < BONE_PHYSICS.LIFETIME_ACTIVE) {
            // Apply velocity
            positions[i3] += instance.velocities[i3] * safeDelta;
            positions[i3 + 1] += instance.velocities[i3 + 1] * safeDelta;
            positions[i3 + 2] += instance.velocities[i3 + 2] * safeDelta;
 
            // Apply gravity
            instance.velocities[i3 + 1] += BONE_PHYSICS.GRAVITY * safeDelta;
 
            // Apply air resistance
            instance.velocities[i3] *= BONE_PHYSICS.AIR_RESISTANCE;
            instance.velocities[i3 + 1] *= BONE_PHYSICS.AIR_RESISTANCE;
            instance.velocities[i3 + 2] *= BONE_PHYSICS.AIR_RESISTANCE;
 
            // Ground collision
            if (positions[i3 + 1] <= 0) {
              positions[i3 + 1] = 0;
              instance.velocities[i3 + 1] = 0;
              instance.velocities[i3] *= 0.3; // Bounce damping
              instance.velocities[i3 + 2] *= 0.3;
            }
          }
          // Ground persistence phase - fragments remain visible
          else {
            // Ensure fragments stay on ground
            if (positions[i3 + 1] > 0) {
              positions[i3 + 1] = 0;
            }
 
            // Fade out in last second
            const timeRemaining = lifetime - elapsed;
            if (timeRemaining < 1.0) {
              const opacity = timeRemaining;
              (instance.points.material as THREE.PointsMaterial).opacity =
                opacity;
            }
          }
 
          // Decrease lifetime
          instance.lifetimes[i] -= safeDelta;
        }
      }
 
      instance.points.geometry.attributes.position.needsUpdate = true;
 
      // Clean up expired effect
      if (allExpired) {
        instance.points.geometry.dispose();
        (instance.points.material as THREE.Material).dispose();
 
        setParticleInstances((prev) => {
          const next = new Map(prev);
          next.delete(effectId);
          return next;
        });
 
        onEffectComplete?.(effectId);
      }
    });
  });
 
  if (!enabled) return null;
 
  return (
    <group>
      {Array.from(particleInstances.values()).map((instance) => (
        <primitive key={instance.effectId} object={instance.points} />
      ))}
    </group>
  );
};
 
BoneCrackParticles3D.displayName = 'BoneCrackParticles3D';