All files / components/shared/three/effects TrigramParticles3D.tsx

5.76% Statements 3/52
0% Branches 0/21
0% Functions 0/12
6.12% Lines 3/49

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                                                                                                                  1x                                 1x                                                                                                               1x                                                                                                                                                                                                                                                          
/**
 * TrigramParticles3D - Korean trigram symbol particle effects for stance transitions
 *
 * Creates spiraling trigram symbols (☰☱☲☳☴☵☶☷) that emanate from the player
 * during stance changes. Provides visual feedback for the Eight Trigram system
 * with Korean cultural authenticity.
 *
 * Features:
 * - Eight authentic trigram symbols from I Ching
 * - Spiral expansion pattern
 * - Korean-themed colors per trigram
 * - Additive blending for glowing effect
 * - Fade-out animation
 * - Stance-specific visual identity
 *
 * @module components/combat/TrigramParticles3D
 * @category Combat Effects
 * @korean 팔괘입자3D
 */
 
import { Text } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import React, { useRef, useState, useEffect } from "react";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../../../types/constants";
import { TrigramStance } from "../../../../types/common";
import { TRIGRAM_DATA } from "../../../../systems/trigram/types";
 
/**
 * Trigram symbol particle effect data
 */
export interface TrigramParticleEffect {
  /** Unique identifier */
  readonly id: string;
  /** Player position in 3D world space */
  readonly position: [number, number, number];
  /** Current trigram stance */
  readonly stance: TrigramStance;
  /** Timestamp when effect was created */
  readonly startTime: number;
}
 
/**
 * Props for TrigramParticles3D component
 */
export interface TrigramParticles3DProps {
  /** Active trigram effects to render */
  readonly effects: readonly TrigramParticleEffect[];
  /** Whether to enable trigram effects */
  readonly enabled?: boolean;
  /** Callback when effect completes */
  readonly onEffectComplete?: (effectId: string) => void;
}
 
/**
 * Get color for trigram stance
 */
const getTrigramColor = (stance: TrigramStance): number => {
  const colorMap: Record<string, number> = {
    [TrigramStance.GEON]: KOREAN_COLORS.TRIGRAM_GEON_PRIMARY,
    [TrigramStance.TAE]: KOREAN_COLORS.TRIGRAM_TAE_PRIMARY,
    [TrigramStance.LI]: KOREAN_COLORS.TRIGRAM_LI_PRIMARY,
    [TrigramStance.JIN]: KOREAN_COLORS.TRIGRAM_JIN_PRIMARY,
    [TrigramStance.SON]: KOREAN_COLORS.TRIGRAM_SON_PRIMARY,
    [TrigramStance.GAM]: KOREAN_COLORS.TRIGRAM_GAM_PRIMARY,
    [TrigramStance.GAN]: KOREAN_COLORS.TRIGRAM_GAN_PRIMARY,
    [TrigramStance.GON]: KOREAN_COLORS.TRIGRAM_GON_PRIMARY,
  };
  return colorMap[stance] ?? KOREAN_COLORS.PRIMARY_CYAN;
};
 
/**
 * Effect constants
 */
const TRIGRAM_CONSTANTS = {
  /** Number of trigram symbols to spawn */
  SYMBOL_COUNT: 8,
  /** Effect lifetime in seconds */
  LIFETIME: 2.0,
  /** Spiral expansion radius */
  SPIRAL_RADIUS: 1.5,
  /** Rotation speed (radians/second) */
  ROTATION_SPEED: 2.0,
  /** Symbol font size */
  FONT_SIZE: 0.3,
  /** Rise speed (m/s) */
  RISE_SPEED: 0.5,
} as const;
 
/**
 * Individual trigram effect instance with animation state
 */
interface TrigramEffectInstance {
  id: string;
  position: THREE.Vector3;
  stance: TrigramStance;
  age: number;
  rotation: number;
}
 
/**
 * TrigramParticles3D Component
 *
 * Renders Korean trigram symbols that spiral outward during stance transitions.
 * Each of the eight trigrams has its own distinct color and represents a different
 * martial arts principle.
 *
 * @example
 * ```tsx
 * const [trigramEffects, setTrigramEffects] = useState<TrigramParticleEffect[]>([]);
 *
 * // On stance change
 * const handleStanceChange = (newStance: TrigramStance, position: [number, number, number]) => {
 *   setTrigramEffects([...trigramEffects, {
 *     id: generateId(),
 *     position,
 *     stance: newStance,
 *     startTime: Date.now(),
 *   }]);
 * };
 *
 * <TrigramParticles3D
 *   effects={trigramEffects}
 *   enabled={visualEffects.trigrams}
 *   onEffectComplete={(id) => {
 *     setTrigramEffects(prev => prev.filter(e => e.id !== id));
 *   }}
 * />
 * ```
 */
export const TrigramParticles3D: React.FC<TrigramParticles3DProps> = ({
  effects,
  enabled = true,
  onEffectComplete,
}) => {
  const [effectInstances, setEffectInstances] = useState<Map<string, TrigramEffectInstance>>(new Map());
  const completedEffectsRef = useRef<Set<string>>(new Set());
 
  // Initialize effect instances
  useEffect(() => {
    if (!enabled) return;
 
    setEffectInstances((prevInstances) => {
      const newInstances = new Map(prevInstances);
      
      effects.forEach((effect) => {
        if (!newInstances.has(effect.id)) {
          newInstances.set(effect.id, {
            id: effect.id,
            position: new THREE.Vector3(...effect.position),
            stance: effect.stance,
            age: 0,
            rotation: 0,
          });
        }
      });
 
      // Clean up removed effects
      const effectIds = new Set(effects.map((e) => e.id));
      newInstances.forEach((_, id) => {
        if (!effectIds.has(id)) {
          newInstances.delete(id);
          completedEffectsRef.current.delete(id);
        }
      });
      
      return newInstances;
    });
  }, [effects, enabled]);
 
  // Animation loop
  useFrame((_, delta) => {
    if (!enabled) return;
 
    setEffectInstances((prevInstances) => {
      const newInstances = new Map(prevInstances);
      let hasChanges = false;
 
      newInstances.forEach((instance, effectId) => {
        instance.age += delta;
        instance.rotation += TRIGRAM_CONSTANTS.ROTATION_SPEED * delta;
        hasChanges = true;
 
        // Check if effect is complete
        if (
          instance.age >= TRIGRAM_CONSTANTS.LIFETIME &&
          !completedEffectsRef.current.has(effectId)
        ) {
          completedEffectsRef.current.add(effectId);
          onEffectComplete?.(effectId);
        }
      });
 
      return hasChanges ? newInstances : prevInstances;
    });
  });
 
  // Don't render if disabled or no effects
  if (!enabled || effects.length === 0) {
    return null;
  }
 
  return (
    <group>
      {Array.from(effectInstances.values()).map((instance) => {
        const progress = Math.min(instance.age / TRIGRAM_CONSTANTS.LIFETIME, 1);
        const opacity = 1 - progress; // Fade out
        const scale = 0.5 + progress * 0.5; // Slight growth
        const rise = progress * TRIGRAM_CONSTANTS.RISE_SPEED * TRIGRAM_CONSTANTS.LIFETIME;
 
        // Get trigram symbol and color
        const trigramData = TRIGRAM_DATA[instance.stance as unknown as TrigramStance];
        const symbol = trigramData.symbol;
        const color = getTrigramColor(instance.stance);
 
        return (
          <group
            key={instance.id}
            position={[instance.position.x, instance.position.y + rise, instance.position.z]}
            rotation={[0, instance.rotation, 0]}
            scale={[scale, scale, scale]}
          >
            {/* Render multiple symbols in spiral pattern */}
            {Array.from({ length: TRIGRAM_CONSTANTS.SYMBOL_COUNT }).map((_, i) => {
              const angle = (Math.PI * 2 * i) / TRIGRAM_CONSTANTS.SYMBOL_COUNT;
              const radius = TRIGRAM_CONSTANTS.SPIRAL_RADIUS * progress;
              const x = Math.cos(angle) * radius;
              const z = Math.sin(angle) * radius;
 
              return (
                <Text
                  key={i}
                  position={[x, 0, z]}
                  rotation={[0, -angle, 0]}
                  fontSize={TRIGRAM_CONSTANTS.FONT_SIZE}
                  color={color}
                  outlineColor={KOREAN_COLORS.ACCENT_GOLD}
                  outlineWidth={0.015}
                  material-transparent
                  material-opacity={opacity}
                  material-blending={THREE.AdditiveBlending}
                  data-testid={`trigram-symbol-${i}`}
                >
                  {symbol}
                </Text>
              );
            })}
          </group>
        );
      })}
    </group>
  );
};
 
export default TrigramParticles3D;