All files / components/shared/three/indicators GuardIndicator.tsx

100% Statements 31/31
100% Branches 26/26
100% Functions 11/11
100% Lines 30/30

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                                                                                          98x 98x   98x 76x 22x 17x   5x                           98x                   98x             98x   70x   9x   19x                                                               4x             176x 98x       176x 98x       176x 98x       176x 98x       176x 98x         176x 97x                     176x 97x                                   176x   33x                                                                                                                                                                                                                                                                          
/**
 * GuardIndicator - Visual indicator showing current fighting stance guard position
 * Displays Korean traditional stance name, English translation, and guard characteristics
 *
 * @module components/shared/three/indicators/GuardIndicator
 * @category Combat UI
 * @korean 방어자세표시기
 */
 
import React, { useMemo } from "react";
import { STANCE_GUARD_CONFIGS } from "../../../../systems/animation";
import { TRIGRAM_DATA } from "../../../../systems/trigram/types";
import { TrigramStance } from "../../../../types/common";
import { KOREAN_COLORS } from "../../../../types/constants";
import { hexToRgbaString } from "../../../../utils/colorUtils";
 
/**
 * Props for GuardIndicator component
 */
export interface GuardIndicatorProps {
  /** Current trigram stance */
  readonly currentStance: TrigramStance;
  /** Whether player is in guard animation state */
  readonly isInGuard: boolean;
  /** Player position: 'left' for player 1, 'right' for player 2 */
  readonly position: "left" | "right";
  /** Whether to use mobile-optimized sizing */
  readonly isMobile?: boolean;
}
 
/**
 * Get guard height label based on arm positions
 *
 * Guard heights based on shoulder.x (vertical angle in radians):
 * - High guard (고위): shoulder at chin level (x <= -0.8)
 *   GEON (-1.0), LI (-1.6), GAN (-1.0)
 * - Mid guard (중위): shoulder at chest level (-0.8 < x < -0.4)
 *   TAE (-0.7), JIN (-0.6), SON (-0.8), GAM (-0.8)
 * - Low guard (저위): shoulder low (x >= -0.4)
 *   GON (-0.4)
 */
function getGuardHeight(stance: TrigramStance): {
  korean: string;
  english: string;
} {
  const config = STANCE_GUARD_CONFIGS[stance];
  const shoulderY = config.guardPose.leftArm.shoulder.x; // x is vertical in Euler
 
  if (shoulderY <= -0.8) {
    return { korean: "고위", english: "High" };
  } else if (shoulderY < -0.4) {
    return { korean: "중위", english: "Mid" };
  } else {
    return { korean: "저위", english: "Low" };
  }
}
 
/**
 * Get traditional Korean stance name from guard config
 */
function getTraditionalStanceName(stance: TrigramStance): {
  korean: string;
  romanized: string;
} {
  const stanceNames: Record<
    TrigramStance,
    { korean: string; romanized: string }
  > = {
    [TrigramStance.GEON]: { korean: "앞서기", romanized: "Ap Seogi" },
    [TrigramStance.TAE]: { korean: "범서기", romanized: "Beom Seogi" },
    [TrigramStance.LI]: { korean: "겨루기 준비", romanized: "Gyeorugi Junbi" },
    [TrigramStance.JIN]: { korean: "주춤서기", romanized: "Juchum Seogi" },
    [TrigramStance.SON]: { korean: "학다리서기", romanized: "Hakdari Seogi" },
    [TrigramStance.GAM]: { korean: "뒷발서기", romanized: "Dwitbal Seogi" },
    [TrigramStance.GAN]: { korean: "모아서기", romanized: "Moa Seogi" },
    [TrigramStance.GON]: { korean: "중하", romanized: "Joong Ha Seogi" },
  };
  return stanceNames[stance];
}
 
/**
 * Get weight distribution icon
 */
function getWeightIcon(weight: "forward" | "neutral" | "back"): string {
  switch (weight) {
    case "forward":
      return "▲"; // Forward lean
    case "back":
      return "▼"; // Back lean
    case "neutral":
      return "●"; // Centered
  }
}
 
/**
 * GuardIndicator Component
 *
 * Displays current guard position information with Korean martial arts terminology.
 * Shows only when player is in a stance guard animation state.
 *
 * Features:
 * - Traditional Korean stance name (앞서기, 앞굽이, etc.)
 * - Romanized pronunciation
 * - Guard height indicator (High/Mid/Low)
 * - Weight distribution visualization
 * - Korean cyberpunk styling with glow effects
 * - Responsive mobile layout
 * - Accessible with proper ARIA labels
 *
 * @example
 * ```tsx
 * <GuardIndicator
 *   currentStance={TrigramStance.GEON}
 *   isInGuard={true}
 *   position="left"
 *   isMobile={false}
 * />
 * ```
 *
 * @public
 * @korean 방어자세표시기
 */
export const GuardIndicator: React.FC<GuardIndicatorProps> = ({
  currentStance,
  isInGuard,
  position: _position, // Currently unused - indicator always positioned bottom-right
  isMobile = false,
}) => {
  // Get guard configuration
  const config = useMemo(
    () => STANCE_GUARD_CONFIGS[currentStance],
    [currentStance],
  );
 
  const trigramData = useMemo(
    () => TRIGRAM_DATA[currentStance],
    [currentStance],
  );
 
  const stanceName = useMemo(
    () => getTraditionalStanceName(currentStance),
    [currentStance],
  );
 
  const guardHeight = useMemo(
    () => getGuardHeight(currentStance),
    [currentStance],
  );
 
  const weightIcon = useMemo(
    () => getWeightIcon(config.guardPose.weight),
    [config.guardPose.weight],
  );
 
  // Memoize responsive sizing
  const layout = useMemo(
    () => ({
      fontSize: isMobile ? 10 : 12,
      titleSize: isMobile ? 13 : 16,
      iconSize: isMobile ? 14 : 18,
      padding: isMobile ? "6px 10px" : "8px 12px",
      gap: isMobile ? "3px" : "4px",
    }),
    [isMobile],
  );
 
  // Container style - uses relative positioning for embedding in container HUDs
  const containerStyle = useMemo(
    () => ({
      position: "relative" as const,
      display: "flex",
      flexDirection: "column" as const,
      gap: layout.gap,
      backgroundColor: hexToRgbaString(KOREAN_COLORS.UI_BACKGROUND_DARK, 0.85),
      border: `1px solid ${hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.6)}`,
      borderRadius: "6px",
      padding: layout.padding,
      pointerEvents: "none" as const,
      width: "100%",
      boxSizing: "border-box" as const,
      boxShadow: `0 0 15px ${hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.3)}`,
    }),
    [layout],
  );
 
  // Don't render if not in guard state
  if (!isInGuard) return null;
 
  return (
    <div
      data-testid="guard-indicator"
      role="status"
      aria-live="polite"
      aria-label={`Guard position: ${stanceName.romanized}, ${guardHeight.english} guard, ${config.guardPose.weight} weight`}
      style={containerStyle}
    >
      {/* Title: GUARD with trigram symbol */}
      <div
        style={{
          fontSize: layout.titleSize,
          color: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 1),
          fontWeight: "bold",
          textAlign: "center",
          textShadow: `0 0 10px ${hexToRgbaString(
            KOREAN_COLORS.ACCENT_GOLD,
            0.6,
          )}`,
          borderBottom: `1px solid ${hexToRgbaString(
            KOREAN_COLORS.PRIMARY_CYAN,
            0.4,
          )}`,
          paddingBottom: layout.gap,
        }}
      >
        {trigramData.symbol} GUARD
      </div>
 
      {/* Traditional stance name (Korean) */}
      <div
        style={{
          fontSize: layout.fontSize,
          color: hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 1),
          textAlign: "center",
          fontWeight: "600",
        }}
      >
        {stanceName.korean}
      </div>
 
      {/* Romanized name */}
      <div
        style={{
          fontSize: layout.fontSize,
          color: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.8),
          textAlign: "center",
          fontStyle: "italic",
        }}
      >
        {stanceName.romanized}
      </div>
 
      {/* Guard characteristics row */}
      <div
        style={{
          display: "flex",
          justifyContent: "space-around",
          marginTop: layout.gap,
          paddingTop: layout.gap,
          borderTop: `1px solid ${hexToRgbaString(
            KOREAN_COLORS.PRIMARY_CYAN,
            0.3,
          )}`,
        }}
      >
        {/* Guard height */}
        <div style={{ textAlign: "center" }}>
          <div
            style={{
              fontSize: layout.iconSize,
              color: hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 1),
            }}
          >
            ⚔️
          </div>
          <div
            style={{
              fontSize: layout.fontSize - 1,
              color: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.9),
            }}
          >
            {guardHeight.korean}
          </div>
          <div
            style={{
              fontSize: layout.fontSize - 2,
              color: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.6),
            }}
          >
            {guardHeight.english}
          </div>
        </div>
 
        {/* Weight distribution */}
        <div style={{ textAlign: "center" }}>
          <div
            style={{
              fontSize: layout.iconSize,
              color: hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 1),
            }}
          >
            {weightIcon}
          </div>
          <div
            style={{
              fontSize: layout.fontSize - 1,
              color: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.9),
            }}
          >
            {config.guardPose.weight === "forward" && "전방"}
            {config.guardPose.weight === "neutral" && "중립"}
            {config.guardPose.weight === "back" && "후방"}
          </div>
          <div
            style={{
              fontSize: layout.fontSize - 2,
              color: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.6),
              textTransform: "capitalize" as const,
            }}
          >
            {config.guardPose.weight}
          </div>
        </div>
      </div>
    </div>
  );
};
 
/**
 * Memoized GuardIndicator to prevent unnecessary re-renders
 */
export default React.memo(GuardIndicator);