All files / components/screens/training/components TrainingAICharacter3D.tsx

50% Statements 10/20
60% Branches 12/20
75% Functions 3/4
50% Lines 9/18

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                                                            6x                     6x             1x           6x 6x   6x     6x     6x                                                   6x                                                                                                                                                      
/**
 * TrainingAICharacter3D - 3D character model for AI opponent in training
 *
 * Displays the AI opponent with stance-based materials and animations
 */
 
import { useFrame } from "@react-three/fiber";
import React, { useMemo, useRef, useState } from "react";
import * as THREE from "three";
import { TrigramStance } from "../../../../types/common";
import { KOREAN_COLORS } from "../../../../types/constants";
 
/**
 * Props for TrainingAICharacter3D
 */
export interface TrainingAICharacter3DProps {
  /** Position in 3D space */
  readonly position: [number, number, number];
  /** Current trigram stance */
  readonly stance: TrigramStance;
  /** Whether AI is currently attacking */
  readonly isAttacking?: boolean;
  /** Health percentage (0-1) */
  readonly healthPercent?: number;
}
 
/**
 * Get color based on trigram stance
 */
function getStanceColor(stance: TrigramStance): number {
  const stanceColors: Record<TrigramStance, number> = {
    [TrigramStance.GEON]: KOREAN_COLORS.SECONDARY_YELLOW, // Heaven
    [TrigramStance.TAE]: KOREAN_COLORS.ACCENT_BLUE, // Lake
    [TrigramStance.LI]: KOREAN_COLORS.ACCENT_RED, // Fire
    [TrigramStance.JIN]: KOREAN_COLORS.ACCENT_GOLD, // Thunder
    [TrigramStance.SON]: KOREAN_COLORS.ACCENT_GREEN, // Wind
    [TrigramStance.GAM]: KOREAN_COLORS.PRIMARY_CYAN, // Water
    [TrigramStance.GAN]: KOREAN_COLORS.UI_BACKGROUND_LIGHT, // Mountain
    [TrigramStance.GON]: KOREAN_COLORS.UI_BACKGROUND_MEDIUM, // Earth
  };
 
  return stanceColors[stance] ?? KOREAN_COLORS.PRIMARY_CYAN;
}
 
/**
 * TrainingAICharacter3D Component
 * Renders the AI opponent with stance-based visual effects
 */
export const TrainingAICharacter3D: React.FC<TrainingAICharacter3DProps> = ({
  position,
  stance,
  isAttacking = false,
  healthPercent = 1.0,
}) => {
  const groupRef = useRef<THREE.Group>(null);
  const stanceAuraRef = useRef<THREE.Mesh>(null);
  // Use state for attack offset since it's used in render
  const [attackOffset, setAttackOffset] = useState(0);
 
  // Memoize stance color
  const stanceColor = useMemo(() => getStanceColor(stance), [stance]);
 
  // Animation loop
  useFrame((state) => {
    if (!groupRef.current || !stanceAuraRef.current) return;
 
    const time = state.clock.elapsedTime;
 
    // Breathing animation
    // NOTE: This per-character animation is acceptable for training mode (single AI opponent).
    // If planning to display multiple AI characters simultaneously (e.g., multiplayer),
    // batch breathing animations using InstancedMesh or limit animation to active combat participants only.
    // 한 명의 AI 상대만 표시되는 훈련 모드에서는 성능 문제가 없으나,
    // 다수의 AI가 동시에 표시되는 경우에는 InstancedMesh 또는 배치 애니메이션으로 최적화 필요.
    const breathScale = Math.sin(time * 2) * 0.02 + 1;
    groupRef.current.scale.y = breathScale;
 
    // Stance aura pulsing
    const auraPulse = Math.sin(time * 3) * 0.1 + 0.9;
    stanceAuraRef.current.scale.setScalar(auraPulse);
 
    // Attack animation with offset
    if (isAttacking) {
      setAttackOffset(Math.sin(time * 10) * 0.1);
    } else {
      setAttackOffset(0);
    }
  });
 
  return (
    <group
      ref={groupRef}
      position={[position[0], position[1], position[2] + attackOffset]}
      name="training-ai-character-3d"
    >
      {/* Main body - capsule representing the AI fighter */}
      <mesh castShadow receiveShadow>
        <capsuleGeometry args={[0.4, 1.2, 16, 32]} />
        <meshStandardMaterial
          color={stanceColor}
          emissive={stanceColor}
          emissiveIntensity={isAttacking ? 0.6 : 0.2}
          metalness={0.4}
          roughness={0.6}
        />
      </mesh>
 
      {/* Head marker */}
      <mesh position={[0, 1.2, 0]} castShadow>
        <sphereGeometry args={[0.3, 16, 16]} />
        <meshStandardMaterial
          color={KOREAN_COLORS.ACCENT_GOLD}
          metalness={0.5}
          roughness={0.5}
        />
      </mesh>
 
      {/* Stance aura effect */}
      <mesh ref={stanceAuraRef} position={[0, 0, 0]}>
        <ringGeometry args={[0.6, 0.8, 32]} />
        <meshBasicMaterial
          color={stanceColor}
          transparent
          opacity={0.4}
          side={THREE.DoubleSide}
        />
      </mesh>
 
      {/* Health indicator (rings around character) */}
      {healthPercent < 1.0 && (
        <mesh position={[0, 2, 0]} rotation={[Math.PI / 2, 0, 0]}>
          <ringGeometry
            args={[0.4, 0.5, 32, 1, 0, Math.PI * 2 * healthPercent]}
          />
          <meshBasicMaterial
            color={
              healthPercent > 0.6
                ? KOREAN_COLORS.ACCENT_GREEN
                : healthPercent > 0.3
                ? KOREAN_COLORS.SECONDARY_YELLOW
                : KOREAN_COLORS.ACCENT_RED
            }
            transparent
            opacity={0.8}
            side={THREE.DoubleSide}
          />
        </mesh>
      )}
 
      {/* Attack effect */}
      {isAttacking && (
        <pointLight
          position={[0, 0.6, 0.5]}
          intensity={1.5}
          distance={3}
          color={stanceColor}
          decay={2}
        />
      )}
    </group>
  );
};
 
export default TrainingAICharacter3D;