All files / components/combat/components PlayerHUD.tsx

78.57% Statements 11/14
90% Branches 27/30
66.66% Functions 2/3
78.57% Lines 11/14

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                                                                            2x         145x 145x     145x 145x 145x     145x 83x   83x 83x     145x                                                                                                                                                                                                                                                                
/**
 * PlayerHUD Component - Combined combat readiness, health and stamina display
 *
 * Displays a complete player HUD with:
 * - Archetype icon/image
 * - Player name (Korean/English)
 * - Combat Readiness bar (10-segment, multi-factor)
 * - Health bar (segmented, color-coded)
 * - Stamina bar (segmented, cyan-themed)
 * - Current stance indicator
 * - Responsive positioning (top-left for player 1, top-right for player 2)
 */
 
import React, { useMemo } from "react";
import { PlayerState } from "../../../systems/player";
import {
  ARCHETYPE_ASSETS,
  FALLBACK_ARCHETYPE_IMAGE,
  FONT_FAMILY,
  KOREAN_COLORS,
} from "../../../types/constants";
import { hexToRgbaString } from "../../../utils/colorUtils";
import { HealthBar } from "./HealthBar";
import { StaminaBar } from "./StaminaBar";
import { CombatReadinessBar } from "./CombatReadinessBar";
 
export interface PlayerHUDProps {
  /** Player state with health, stamina, and other data */
  readonly player: PlayerState;
  /** Player position: 'left' for player 1, 'right' for player 2 */
  readonly position: "left" | "right";
  /** Whether to use mobile-optimized sizing */
  readonly isMobile: boolean;
}
 
/**
 * PlayerHUD - Complete player status display with health and stamina bars
 */
export const PlayerHUD: React.FC<PlayerHUDProps> = ({
  player,
  position,
  isMobile,
}) => {
  const playerId = player.id;
  const isLeft = position === "left";
 
  // Responsive sizing
  const fontSize = isMobile ? 11 : 13;
  const gap = isMobile ? "6px" : "8px";
  const iconSize = isMobile ? 40 : 50;
 
  // Get archetype image path
  const archetypeImagePath = useMemo(() => {
    const archetypeKey = player.archetype.toLowerCase();
    const assets =
      ARCHETYPE_ASSETS[archetypeKey as keyof typeof ARCHETYPE_ASSETS];
    return assets?.image ?? FALLBACK_ARCHETYPE_IMAGE;
  }, [player.archetype]);
 
  return (
    <div
      data-testid={`player-hud-${playerId}`}
      style={{
        position: "absolute",
        top: isMobile ? "8px" : "10px",
        left: isLeft ? (isMobile ? "8px" : "12px") : "auto",
        right: isLeft ? "auto" : isMobile ? "8px" : "12px",
        display: "flex",
        flexDirection: "column",
        gap,
        pointerEvents: "none",
        zIndex: 100,
        maxWidth: isMobile ? "220px" : "300px",
      }}
    >
      {/* Player Name with Archetype Icon */}
      <div
        data-testid={`player-name-${playerId}`}
        style={{
          display: "flex",
          alignItems: "center",
          gap: "8px",
          flexDirection: isLeft ? "row" : "row-reverse",
        }}
      >
        {/* Archetype Icon */}
        <div
          data-testid={`archetype-icon-${playerId}`}
          style={{
            width: `${iconSize}px`,
            height: `${iconSize}px`,
            borderRadius: "8px",
            overflow: "hidden",
            border: `2px solid ${hexToRgbaString(
              KOREAN_COLORS.ACCENT_GOLD,
              1
            )}`,
            boxShadow: `0 0 10px ${hexToRgbaString(
              KOREAN_COLORS.ACCENT_GOLD,
              0.5
            )}`,
            flexShrink: 0,
          }}
        >
          <img
            src={archetypeImagePath}
            alt={`${player.name.english} archetype`}
            style={{
              width: "100%",
              height: "100%",
              objectFit: "cover",
            }}
            onError={(e) => {
              const target = e.target as HTMLImageElement;
              if (!target.src.endsWith(FALLBACK_ARCHETYPE_IMAGE)) {
                target.src = FALLBACK_ARCHETYPE_IMAGE;
              }
            }}
          />
        </div>
        {/* Player Name */}
        <div
          style={{
            fontSize: `${fontSize}px`,
            fontWeight: "bold",
            fontFamily: FONT_FAMILY.KOREAN,
            color: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 1),
            textAlign: isLeft ? "left" : "right",
            textShadow: "0 0 4px rgba(0,0,0,0.8), 0 0 8px rgba(0,0,0,0.6)",
            padding: "2px 6px",
            background: hexToRgbaString(KOREAN_COLORS.UI_BACKGROUND_DARK, 0.7),
            borderRadius: "4px",
            whiteSpace: "nowrap",
          }}
        >
          {player.name.korean} | {player.name.english}
        </div>
      </div>
 
      {/* Combat Readiness Bar - shows overall combat capability */}
      <CombatReadinessBar
        player={player}
        playerId={playerId}
        isMobile={isMobile}
      />
 
      {/* Health Bar - shows aggregate body health */}
      <HealthBar
        current={player.health}
        max={player.maxHealth}
        playerId={playerId}
        isMobile={isMobile}
      />
 
      {/* Stamina Bar */}
      <StaminaBar
        current={player.stamina}
        max={player.maxStamina}
        playerId={playerId}
        isMobile={isMobile}
      />
 
      {/* Current Stance Indicator */}
      <div
        data-testid={`stance-indicator-${playerId}`}
        style={{
          fontSize: isMobile ? "10px" : "12px",
          fontFamily: FONT_FAMILY.KOREAN,
          color: hexToRgbaString(KOREAN_COLORS.ACCENT_CYAN, 1),
          textAlign: isLeft ? "left" : "right",
          textShadow: "0 0 4px rgba(0,0,0,0.8)",
          padding: "4px 8px",
          backgroundColor: hexToRgbaString(
            KOREAN_COLORS.UI_BACKGROUND_DARK,
            0.8
          ),
          borderRadius: "4px",
          marginTop: "2px",
        }}
      >
        자세 | Stance: {player.currentStance}
      </div>
    </div>
  );
};
 
export default PlayerHUD;