All files / components/training TrainingScreen3D.tsx

35.15% Statements 45/128
15.62% Branches 5/32
37.14% Functions 13/35
36.52% Lines 42/115

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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551                                                                                                                    1x             9x                     9x 9x 9x 9x 9x 9x 9x     9x                 9x     9x     9x 9x   9x 9x 9x 9x                 9x   9x 9x 9x               9x 9x                   9x 9x               9x                     9x 9x         9x                           9x             9x                                                                                                                                                                             9x 9x                                                                                             9x 9x       9x 9x             9x         9x 9x             9x                                                                                                                                                                                                                                                                                                                                                                                                                        
/**
 * TrainingScreen3D - Three.js-based training screen
 * 
 * Provides 3D training dummy with vital point targeting and Html UI overlays
 */
 
import { Html } from "@react-three/drei";
import { Canvas } from "@react-three/fiber";
import React, {
  useCallback,
  useEffect,
  useMemo,
  useState,
} from "react";
import { useWebGLContextLossHandler } from "../../hooks/useWebGLContextLossHandler";
import { PlayerState } from "../../systems";
import { useAudio } from "../../audio/AudioProvider";
import { Position, TrigramStance } from "../../types/common";
import { KOREAN_COLORS } from "../../types/constants";
import { usePlayerMovement } from "../../utils/inputSystem";
import { VolumeControl } from "../ui/VolumeControl";
import TrainingArena3D from "./components/TrainingArena3D";
import TrainingDummy3D from "./components/TrainingDummy3D";
import TrainingHitEffects3D from "./components/TrainingHitEffects3D";
import TrainingControlsHTML from "./components/TrainingControlsHTML";
import TrainingStatsHTML, { type TrainingStats } from "./components/TrainingStatsHTML";
import VitalPointTrainingHTML from "./components/VitalPointTrainingHTML";
import TrainingModeSelectorHTML, { type TrainingMode } from "./components/TrainingModeSelectorHTML";
import TrainingFeedbackHTML from "./components/TrainingFeedbackHTML";
 
/**
 * Props for the TrainingScreen3D component
 */
export interface TrainingScreen3DProps {
  /** Callback to update player state */
  readonly onPlayerUpdate: (updates: Partial<PlayerState>) => void;
  /** Callback when returning to menu */
  readonly onReturnToMenu: () => void;
  /** Canvas width in pixels. Defaults to 1200 */
  readonly width?: number;
  /** Canvas height in pixels. Defaults to 800 */
  readonly height?: number;
}
 
/**
 * Hit effect state
 */
interface HitEffect {
  readonly id: number;
  readonly position: [number, number, number];
  readonly type: "success" | "perfect" | "miss";
  readonly visible: boolean;
}
 
/**
 * TrainingScreen3D Component
 * Three.js-based training screen with 3D dummy and Html UI
 */
export const TrainingScreen3D: React.FC<TrainingScreen3DProps> = ({
  onPlayerUpdate,
  onReturnToMenu,
  width = 1200,
  height = 800,
}) => {
  // Handle WebGL context loss and restoration
  useWebGLContextLossHandler({
    onContextLost: () => {
      console.warn('⚠️ WebGL context lost in TrainingScreen');
    },
    onContextRestored: () => {
      console.log('✅ WebGL context restored in TrainingScreen');
    },
    autoRestore: true,
  });
 
  // Training state
  const [trainingMode, setTrainingMode] = useState<TrainingMode>("basics");
  const [isTraining, setIsTraining] = useState(false);
  const [selectedVitalPoint, setSelectedVitalPoint] = useState<string | null>(null);
  const [feedback, setFeedback] = useState("");
  const [showFeedback, setShowFeedback] = useState(false);
  const [hitEffects, setHitEffects] = useState<HitEffect[]>([]);
  const [nextEffectId, setNextEffectId] = useState(0);
  
  // Training statistics
  const [stats, setStats] = useState<TrainingStats>({
    score: 0,
    combo: 0,
    hits: 0,
    misses: 0,
    accuracy: 0,
  });
 
  // Responsive detection
  const isMobile = useMemo(() => width < 768, [width]);
 
  // Audio context
  const audio = useAudio();
 
  // Audio lifecycle management
  useEffect(() => {
    let audioStarted = false;
    
    const startMusic = async () => {
      try {
        await audio.fadeIn("cyberpunk_fusion", 2000);
        audioStarted = true;
      } catch (err) {
        console.warn("Failed to start training music:", err);
        // Show user-visible feedback that audio failed
        setFeedback("오디오 초기화 실패 | Audio initialization failed");
        setShowFeedback(true);
      }
    };
    
    void startMusic();
 
    return () => {
      Eif (audioStarted) {
        void audio.fadeOut(2000).then(() => audio.stopMusic()).catch((err) => 
          console.warn("Failed to stop training music:", err)
        );
      }
    };
  }, [audio]);
 
  // Arena bounds for player movement
  const arenaBounds = useMemo(
    () => ({
      x: -8,
      y: -6,
      width: 16,
      height: 12,
    }),
    []
  );
 
  // Initial player position in 3D space
  const initialPosition = useMemo<Position>(
    () => ({
      x: -5,
      y: 0,
    }),
    []
  );
 
  // Player movement with input system
  const { playerPosition } = usePlayerMovement({
    enabled: isTraining,
    bounds: arenaBounds,
    onPositionChange: (newPosition: Position) => {
      onPlayerUpdate({ position: newPosition });
    },
    initialPosition,
    moveSpeed: 300,
  });
 
  // Convert 2D position to 3D
  const player3DPosition = useMemo<[number, number, number]>(
    () => [playerPosition.x, 0, playerPosition.y],
    [playerPosition]
  );
 
  // Training handlers
  const handleStartTraining = useCallback(() => {
    setIsTraining(true);
    setStats({
      score: 0,
      combo: 0,
      hits: 0,
      misses: 0,
      accuracy: 0,
    });
    setFeedback("훈련 시작! | Training Start!");
    setShowFeedback(true);
    audio.playSFX("menu_select");
  }, [audio]);
 
  const handleStopTraining = useCallback(() => {
    setIsTraining(false);
    setFeedback("훈련 종료 | Training End");
    setShowFeedback(true);
    audio.playSFX("menu_back");
  }, [audio]);
 
  const handleDummyHit = useCallback(
    (_vitalPointId: string, accuracy: number): boolean => {
      if (!isTraining) return false;
 
      // Determine hit position (dummy is at [5, 0, 0])
      const hitPosition: [number, number, number] = [5, 1.5, 0];
      
      let effectType: "success" | "perfect" | "miss";
 
      if (accuracy > 0.5) {
        const points = Math.round(accuracy * 100);
        
        setStats((prev) => {
          const newHits = prev.hits + 1;
          const totalAttempts = newHits + prev.misses;
          return {
            score: prev.score + points,
            combo: prev.combo + 1,
            hits: newHits,
            misses: prev.misses,
            accuracy: totalAttempts > 0 ? (newHits / totalAttempts) * 100 : 0,
          };
        });
 
        if (accuracy > 0.9) {
          setFeedback("완벽한 타격! | Perfect Strike!");
          audio.playSFX("ki_release");
          effectType = "perfect";
        } else if (accuracy > 0.7) {
          setFeedback("좋은 타격! | Good Strike!");
          audio.playSFX("ki_charge");
          effectType = "success";
        } else {
          setFeedback("타격 성공 | Strike Success");
          audio.playSFX("menu_click");
          effectType = "success";
        }
        
        setShowFeedback(true);
        
        // Add hit effect
        setHitEffects((prev) => [
          ...prev,
          {
            id: nextEffectId,
            position: hitPosition,
            type: effectType,
            visible: true,
          },
        ]);
        setNextEffectId((prev) => prev + 1);
        
        return true;
      } else {
        setStats((prev) => {
          const newMisses = prev.misses + 1;
          const totalAttempts = prev.hits + newMisses;
          return {
            ...prev,
            combo: 0,
            misses: newMisses,
            accuracy: totalAttempts > 0 ? (prev.hits / totalAttempts) * 100 : 0,
          };
        });
        setFeedback("빗나감 | Miss");
        setShowFeedback(true);
        audio.playSFX("menu_navigate");
        
        // Add miss effect
        setHitEffects((prev) => [
          ...prev,
          {
            id: nextEffectId,
            position: hitPosition,
            type: "miss",
            visible: true,
          },
        ]);
        setNextEffectId((prev) => prev + 1);
        
        return false;
      }
    },
    [isTraining, audio, nextEffectId]
  );
 
  // Consolidated keyboard input handling
  useEffect(() => {
    const handleKeyDown = (event: KeyboardEvent) => {
      const key = event.key.toLowerCase();
 
      // ESC key - return to menu
      if (key === "escape") {
        onReturnToMenu();
        return;
      }
 
      // Training mode controls only work when training is active
      if (!isTraining) return;
 
      // Handle stance changes (1-8)
      if (key >= "1" && key <= "8") {
        const stanceIndex = parseInt(key) - 1;
        const stances: readonly TrigramStance[] = [
          TrigramStance.GEON,
          TrigramStance.TAE,
          TrigramStance.LI,
          TrigramStance.JIN,
          TrigramStance.SON,
          TrigramStance.GAM,
          TrigramStance.GAN,
          TrigramStance.GON,
        ];
        onPlayerUpdate({
          currentStance: stances[stanceIndex],
          lastActionTime: Date.now(),
        });
        audio.playSFX("stance_change_1");
        event.preventDefault();
      }
 
      // Handle attacks (Space key)
      if (key === " ") {
        // Calculate distance to dummy (at position [5, 0, 0])
        // Use squared distance to avoid expensive Math.sqrt
        const dx = player3DPosition[0] - 5;
        const dz = player3DPosition[2] - 0;
        const squaredDistance = dx * dx + dz * dz;
        const accuracy = Math.max(0, 1 - squaredDistance / 64);
        
        handleDummyHit(selectedVitalPoint ?? "generic", accuracy);
        event.preventDefault();
      }
    };
 
    window.addEventListener("keydown", handleKeyDown);
    return () => window.removeEventListener("keydown", handleKeyDown);
  }, [isTraining, player3DPosition, selectedVitalPoint, onPlayerUpdate, handleDummyHit, audio, onReturnToMenu]);
 
  // Hide feedback after delay
  useEffect(() => {
    Iif (showFeedback) {
      const timer = setTimeout(() => setShowFeedback(false), 2000);
      return () => clearTimeout(timer);
    }
  }, [showFeedback]);
 
  // Handle hit effect completion
  const handleEffectComplete = useCallback((effectId: number) => {
    setHitEffects((prev) => prev.filter((effect) => effect.id !== effectId));
  }, []);
 
  // Memoize camera configuration for stable reference
  const cameraConfig = useMemo(
    () => ({
      position: [0, 8, 12] as [number, number, number],
      fov: 60,
    }),
    []
  );
 
  return (
    <div
      style={{
        width: `${width}px`,
        height: `${height}px`,
        position: "relative",
      }}
      data-testid="training-screen-3d"
    >
      <Canvas
        style={{ width, height }}
        gl={{
          antialias: true,
          alpha: false,
          powerPreference: "high-performance",
        }}
        dpr={[1, 2]}
        shadows
        onCreated={({ gl }) => {
          gl.setClearColor(KOREAN_COLORS.UI_BACKGROUND_DARK, 1);
        }}
        camera={cameraConfig}
      >
        {/* Korean-themed lighting (오방색 - Five Cardinal Colors) */}
        <ambientLight intensity={0.4} color={KOREAN_COLORS.PRIMARY_CYAN} />
        
        {/* Main directional light - Center (황색/Yellow) */}
        <directionalLight
          position={[0, 10, 5]}
          intensity={1}
          color={KOREAN_COLORS.SECONDARY_YELLOW}
          castShadow
          shadow-mapSize={[2048, 2048]}
        />
        
        {/* East (청색/Blue-Green) */}
        <pointLight
          position={[10, 5, 0]}
          intensity={0.6}
          color={KOREAN_COLORS.ACCENT_GREEN}
          distance={20}
        />
        
        {/* West (백색/White) */}
        <pointLight
          position={[-10, 5, 0]}
          intensity={0.6}
          color={KOREAN_COLORS.WHITE_SOLID}
          distance={20}
        />
        
        {/* South (적색/Red) */}
        <pointLight
          position={[0, 5, 10]}
          intensity={0.5}
          color={KOREAN_COLORS.ACCENT_RED}
          distance={20}
        />
        
        {/* North (흑색/Black) - dim for depth */}
        <pointLight
          position={[0, 5, -10]}
          intensity={0.3}
          color={KOREAN_COLORS.UI_BACKGROUND_MEDIUM}
          distance={20}
        />
 
        {/* Training arena ground */}
        <TrainingArena3D />
 
        {/* Training dummy at fixed position */}
        <TrainingDummy3D
          position={[5, 0, 0]}
          selectedVitalPoint={selectedVitalPoint}
          isTraining={isTraining}
        />
 
        {/* Player model (simplified for now) */}
        <group position={player3DPosition}>
          <mesh castShadow receiveShadow>
            <capsuleGeometry args={[0.4, 1.2, 16, 32]} />
            <meshStandardMaterial
              color={KOREAN_COLORS.ACCENT_GOLD}
              metalness={0.3}
              roughness={0.7}
            />
          </mesh>
        </group>
 
        {/* Hit effects */}
        {hitEffects.map((effect) => (
          <TrainingHitEffects3D
            key={effect.id}
            position={effect.position}
            type={effect.type}
            visible={effect.visible}
            onComplete={() => handleEffectComplete(effect.id)}
          />
        ))}
 
        {/* Html UI Overlays */}
        <Html fullscreen>
          <div
            style={{
              width: "100%",
              height: "100%",
              pointerEvents: "none",
              position: "relative",
            }}
          >
            {/* Top Left - Training Controls */}
            <div
              style={{
                position: "absolute",
                top: 20,
                left: 20,
                pointerEvents: "all",
              }}
            >
              <TrainingControlsHTML
                isTraining={isTraining}
                onStartTraining={handleStartTraining}
                onStopTraining={handleStopTraining}
                isMobile={isMobile}
              />
            </div>
 
            {/* Top Right - Training Stats */}
            <div
              style={{
                position: "absolute",
                top: 20,
                right: 20,
                pointerEvents: "all",
                display: "flex",
                flexDirection: "column",
                gap: "10px",
                alignItems: "flex-end",
              }}
            >
              <VolumeControl position="custom" compact={isMobile} />
              <TrainingStatsHTML
                stats={stats}
                isMobile={isMobile}
              />
            </div>
 
            {/* Bottom Left - Mode Selector */}
            <div
              style={{
                position: "absolute",
                bottom: 20,
                left: 20,
                pointerEvents: "all",
              }}
            >
              <TrainingModeSelectorHTML
                currentMode={trainingMode}
                onModeChange={setTrainingMode}
                isMobile={isMobile}
              />
            </div>
 
            {/* Bottom Right - Vital Point Panel */}
            <div
              style={{
                position: "absolute",
                bottom: 20,
                right: 20,
                pointerEvents: "all",
              }}
            >
              <VitalPointTrainingHTML
                selectedVitalPoint={selectedVitalPoint}
                onVitalPointSelect={setSelectedVitalPoint}
                isMobile={isMobile}
              />
            </div>
 
            {/* Center - Feedback Message */}
            {showFeedback && (
              <div
                style={{
                  position: "absolute",
                  top: "50%",
                  left: "50%",
                  transform: "translate(-50%, -50%)",
                  pointerEvents: "none",
                }}
              >
                <TrainingFeedbackHTML
                  message={feedback}
                  isMobile={isMobile}
                />
              </div>
            )}
          </div>
        </Html>
      </Canvas>
    </div>
  );
};
 
export default TrainingScreen3D;