All files / components/screens/endscreen/components DefeatAnimation3D.tsx

52.38% Statements 33/63
23.52% Branches 8/34
66.66% Functions 6/9
50% Lines 30/60

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                      2x 14x 14x 14x       14x 14x     14x 12x 12x   12x 1200x 1200x 1200x 1200x   1200x 1200x 1200x     12x       14x                                                       14x   12x 12x 12x   12x   12x 12x 12x       12x                     12x                                             14x                                                                                                                                                                                  
import { useFrame } from "@react-three/fiber";
import React, { useEffect, useRef, useState } from "react";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../../../types/constants";
 
/**
 * Defeat Animation 3D Component
 * Displays somber 3D particle effects for defeat screen
 * Uses blue/cyan tones to contrast with gold victory effects
 * Optimized for 60fps performance with object reuse
 */
export const DefeatAnimation3D: React.FC = () => {
  const groupRef = useRef<THREE.Group>(null);
  const particlesRef = useRef<THREE.Points>(null);
  const spiralRef = useRef<THREE.Group>(null);
 
  // Reusable objects for animation calculations to avoid per-frame allocations
  // These are reused across all animation frames for scale and position updates
  const [reusableScale] = useState(() => new THREE.Vector3());
  const [reusablePosition] = useState(() => new THREE.Vector3());
 
  // Create defeat particles - use useState with lazy initializer
  const [particlePositions] = useState(() => {
    const count = 100; // Fewer particles than victory for subdued effect
    const positions = new Float32Array(count * 3);
 
    for (let i = 0; i < count; i++) {
      const i3 = i * 3;
      const radius = 2 + Math.random() * 3;
      const theta = Math.random() * Math.PI * 2;
      const phi = Math.random() * Math.PI;
 
      positions[i3] = radius * Math.sin(phi) * Math.cos(theta);
      positions[i3 + 1] = radius * Math.cos(phi) - 1; // Lower starting position
      positions[i3 + 2] = radius * Math.sin(phi) * Math.sin(theta);
    }
 
    return positions;
  });
 
  // Animate defeat effects with slower, descending motion - optimized for 60fps
  useFrame((state) => {
    const time = state.clock.elapsedTime;
 
    // Slow rotation
    if (groupRef.current) {
      groupRef.current.rotation.y = time * 0.15; // Slower than victory
    }
 
    // Fade particles downward - use reusable objects
    if (particlesRef.current) {
      const scale = 1 + Math.sin(time * 1.5) * 0.1; // Subtle pulsing
      reusableScale.setScalar(scale);
      particlesRef.current.scale.copy(reusableScale);
      
      // Slowly descend
      const yPos = Math.sin(time * 0.5) * 0.3 - 0.2;
      reusablePosition.set(0, yPos, 0);
      particlesRef.current.position.copy(reusablePosition);
    }
 
    // Spiral effect - slower, descending
    if (spiralRef.current) {
      spiralRef.current.rotation.y = -time * 0.2; // Counter-rotation
      spiralRef.current.rotation.z = Math.sin(time * 0.3) * 0.2;
    }
  });
 
  // Cleanup Three.js resources on unmount
  useEffect(() => {
    // Capture ref values at effect setup time to avoid stale references in cleanup
    const group = groupRef.current;
    const particles = particlesRef.current;
    const spiral = spiralRef.current;
 
    return () => {
      // Dispose geometries and materials to prevent memory leaks
      Eif (particles) {
        particles.geometry?.dispose();
        Iif (particles.material) {
          (particles.material as THREE.Material).dispose();
        }
      }
      Iif (spiral?.children && Array.isArray(spiral.children)) {
        spiral.children.forEach((child) => {
          if (child instanceof THREE.Mesh) {
            child.geometry?.dispose();
            if (child.material) {
              (child.material as THREE.Material).dispose();
            }
          }
        });
      }
      // Additionally iterate over all group children to catch meshes/points without explicit refs
      Iif (group?.children && Array.isArray(group.children)) {
        group.children.forEach((child) => {
          // Skip already-handled refs
          if (child === particles || child === spiral) {
            return;
          }
 
          if (child instanceof THREE.Mesh) {
            child.geometry?.dispose();
            if (child.material) {
              (child.material as THREE.Material).dispose();
            }
          } else if (child instanceof THREE.Points) {
            child.geometry?.dispose();
            if (child.material) {
              (child.material as THREE.Material).dispose();
            }
          }
        });
      }
    };
  }, []);
 
  return (
    <group
      ref={groupRef}
      position={[0, 1, 0]}
      data-testid="defeat-animation-3d"
    >
      {/* Defeat particles - blue/cyan theme */}
      <points ref={particlesRef}>
        <bufferGeometry>
          <bufferAttribute
            attach="attributes-position"
            count={100}
            itemSize={3}
            args={[particlePositions, 3]}
          />
        </bufferGeometry>
        <pointsMaterial
          size={0.15}
          color={new THREE.Color(KOREAN_COLORS.ACCENT_BLUE)}
          transparent
          opacity={0.6}
          sizeAttenuation
          depthWrite={false}
        />
      </points>
 
      {/* Descending spiral rings */}
      <group ref={spiralRef}>
        <mesh rotation={[Math.PI / 2, 0, 0]}>
          <torusGeometry args={[1.5, 0.03, 12, 64]} />
          <meshBasicMaterial
            color={KOREAN_COLORS.PRIMARY_CYAN}
            transparent
            opacity={0.4}
          />
        </mesh>
 
        <mesh rotation={[Math.PI / 2, Math.PI / 4, 0]} position={[0, -0.3, 0]}>
          <torusGeometry args={[2, 0.03, 12, 64]} />
          <meshBasicMaterial
            color={KOREAN_COLORS.ACCENT_BLUE}
            transparent
            opacity={0.3}
          />
        </mesh>
 
        <mesh rotation={[Math.PI / 2, Math.PI / 2, 0]} position={[0, -0.6, 0]}>
          <torusGeometry args={[2.5, 0.03, 12, 64]} />
          <meshBasicMaterial
            color={KOREAN_COLORS.PRIMARY_CYAN}
            transparent
            opacity={0.2}
          />
        </mesh>
      </group>
 
      {/* Central dimmed sphere */}
      <mesh>
        <sphereGeometry args={[0.4, 32, 32]} />
        <meshStandardMaterial
          color={KOREAN_COLORS.ACCENT_BLUE}
          emissive={KOREAN_COLORS.ACCENT_BLUE}
          emissiveIntensity={0.8}
          transparent
          opacity={0.6}
        />
      </mesh>
 
      {/* Outer faint glow */}
      <mesh>
        <sphereGeometry args={[0.7, 32, 32]} />
        <meshBasicMaterial
          color={KOREAN_COLORS.PRIMARY_CYAN}
          transparent
          opacity={0.15}
          side={THREE.BackSide}
        />
      </mesh>
 
      {/* Point light for subdued glow effect */}
      <pointLight
        position={[0, 0, 0]}
        intensity={1.5}
        distance={8}
        color={KOREAN_COLORS.ACCENT_BLUE}
      />
    </group>
  );
};