All files / components/screens/combat/components/hud CombatPortraitStatusStrip.tsx

100% Statements 16/16
88.88% Branches 16/18
100% Functions 3/3
100% Lines 16/16

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                                                                                                                2x                   14x             14x               14x           14x                                                                                                                                                                                                   2x     7x     5x 5x 5x       5x 5x             5x 5x   5x     7x                                                                                  
/**
 * CombatPortraitStatusStrip - Compact two-player HP/stamina strip for portrait mobile.
 *
 * When the viewport is portrait mobile we hide `CombatLeftHUD` and
 * `CombatRightHUD` (they occlude the 3D arena on narrow screens). This strip
 * replaces them with a minimal, **always visible** health+stamina bar for
 * both players, rendered as a single horizontal band directly under the
 * top HUD.
 *
 * UX goals (game-UI best practice for narrow portrait):
 * - Both bars fit on a single row on viewports as narrow as 320 px.
 * - Clear left/right grouping (P1 on the left, P2 on the right) mirrors the
 *   fighters' on-screen positions.
 * - Low visual weight (no side HUD backgrounds, no icons) so the strip does
 *   not re-introduce the occlusion problem it was meant to solve.
 * - Korean + English bilingual micro-labels (체 / ST).
 *
 * 세로 모드 휴대폰 전용 상/하단 상태 바
 *
 * @module components/screens/combat/components/hud/CombatPortraitStatusStrip
 */
 
import React, { useMemo } from "react";
import { PlayerState } from "../../../../../systems";
import {
  BORDERS,
  FONT_SIZE_MULTIPLIERS,
  GRADIENTS,
  HIERARCHY,
  HUD_STYLE,
  TYPOGRAPHY,
  TYPOGRAPHY_NUMERIC,
} from "../../../../../types/constants/designSystem";
import { KOREAN_COLORS } from "../../../../../types/constants/colors";
import { hexToRgbaString } from "../../../../../utils/colorUtils";
import {
  getResponsiveFontSize,
  getResponsivePadding,
} from "../../../../../utils/responsiveLayout";
 
export interface CombatPortraitStatusStripProps {
  /** Screen width for layout calculations */
  readonly width: number;
  /** Screen height for layout calculations */
  readonly height: number;
  /** Player 1 state */
  readonly player1: PlayerState;
  /** Player 2 (AI) state */
  readonly player2: PlayerState;
  /** Position scale multiplier for large displays */
  readonly positionScale: number;
  /** Y offset from top of screen (should equal top HUD height) */
  readonly topOffset: number;
}
 
/** Single compact HP + stamina pill for one fighter. */
const PlayerPill: React.FC<{
  readonly player: PlayerState;
  readonly side: "left" | "right";
  readonly fontSize: number;
  readonly barHeight: number;
  readonly testId: string;
}> = ({ player, side, fontSize, barHeight, testId }) => {
  // Guard against non-positive maxima (0 or negative) which would
  // otherwise yield Infinity/NaN percentages and render invalid widths.
  const healthPercent =
    player.maxHealth > 0
      ? Math.max(
          0,
          Math.min(100, (player.health / player.maxHealth) * 100),
        )
      : 0;
  const staminaPercent =
    player.maxStamina > 0
      ? Math.max(
          0,
          Math.min(100, (player.stamina / player.maxStamina) * 100),
        )
      : 0;
 
  const healthColor =
    healthPercent > 50
      ? KOREAN_COLORS.HEALTH_FULL
      : healthPercent > 25
        ? KOREAN_COLORS.HEALTH_MEDIUM
        : KOREAN_COLORS.HEALTH_LOW;
 
  return (
    <div
      data-testid={testId}
      style={{
        display: "flex",
        flex: 1,
        flexDirection: "column",
        alignItems: side === "left" ? "flex-start" : "flex-end",
        gap: "2px",
        minWidth: 0,
      }}
    >
      {/* Name + side indicator */}
      <div
        style={{
          fontSize: `${fontSize}px`,
          color: HIERARCHY.accent.color,
          fontFamily: TYPOGRAPHY.body.fontFamily,
          fontWeight: 600,
          whiteSpace: "nowrap",
          overflow: "hidden",
          textOverflow: "ellipsis",
          maxWidth: "100%",
        }}
      >
        {side === "left" ? "P1" : "P2"}
      </div>
      {/* HP bar */}
      <div
        role="progressbar"
        aria-label={`Player ${side === "left" ? 1 : 2} health`}
        aria-valuenow={Math.max(
          0,
          Math.min(player.maxHealth, Math.ceil(player.health)),
        )}
        aria-valuemin={0}
        aria-valuemax={player.maxHealth}
        style={{
          width: "100%",
          height: `${barHeight}px`,
          backgroundColor: hexToRgbaString(
            KOREAN_COLORS.UI_BACKGROUND_MEDIUM,
            1,
          ),
          borderRadius: "2px",
          overflow: "hidden",
          border: `1px solid ${hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.5)}`,
        }}
      >
        <div
          data-testid={`${testId}-hp-fill`}
          style={{
            width: `${healthPercent}%`,
            height: "100%",
            backgroundColor: hexToRgbaString(healthColor, 1),
            transition: "width 0.2s ease-out, background-color 0.3s ease-out",
          }}
        />
      </div>
      {/* Stamina bar (thinner) */}
      <div
        role="progressbar"
        aria-label={`Player ${side === "left" ? 1 : 2} stamina`}
        aria-valuenow={Math.max(
          0,
          Math.min(player.maxStamina, Math.ceil(player.stamina)),
        )}
        aria-valuemin={0}
        aria-valuemax={player.maxStamina}
        style={{
          width: "100%",
          height: `${Math.max(3, Math.floor(barHeight * 0.5))}px`,
          backgroundColor: hexToRgbaString(
            KOREAN_COLORS.UI_BACKGROUND_MEDIUM,
            1,
          ),
          borderRadius: "2px",
          overflow: "hidden",
          border: `1px solid ${hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.4)}`,
        }}
      >
        <div
          data-testid={`${testId}-sp-fill`}
          style={{
            width: `${staminaPercent}%`,
            height: "100%",
            backgroundColor: hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 1),
            transition: "width 0.2s ease-out",
          }}
        />
      </div>
    </div>
  );
};
 
/**
 * Compact two-player HP/stamina strip, only rendered for portrait mobile.
 */
export const CombatPortraitStatusStrip: React.FC<
  CombatPortraitStatusStripProps
> = ({ width, height, player1, player2, positionScale, topOffset }) => {
  const layout = useMemo(() => {
    // Tighter strip on extra-small phones (<380 px) so the arena retains
    // usable vertical space.
    const isExtraSmall = width < 380;
    const minStripHeight = isExtraSmall ? 28 : 36;
    const stripHeight = Math.max(
      minStripHeight,
      Math.round(height * 0.055 * positionScale),
    );
    const padding = getResponsivePadding(width) * positionScale;
    const fontSize = Math.max(
      TYPOGRAPHY_NUMERIC.caption,
      getResponsiveFontSize(width) *
        positionScale *
        FONT_SIZE_MULTIPLIERS.bodySmall,
    );
    // Reserve a narrow center gap between the two pills.
    const centerGap = Math.max(6, padding);
    const barHeight = Math.max(6, Math.floor(stripHeight * 0.28));
 
    return { stripHeight, padding, fontSize, centerGap, barHeight };
  }, [width, height, positionScale]);
 
  return (
    <div
      data-testid="combat-portrait-status-strip"
      style={{
        position: "absolute",
        top: `${topOffset}px`,
        left: 0,
        width: "100%",
        height: `${layout.stripHeight}px`,
        display: "flex",
        flexDirection: "row",
        alignItems: "center",
        justifyContent: "space-between",
        gap: `${layout.centerGap}px`,
        padding: `4px ${layout.padding}px`,
        pointerEvents: "none",
        boxSizing: "border-box",
        borderBottom: BORDERS.muted,
        background: GRADIENTS.vertical(0.85),
        backdropFilter: HUD_STYLE.backdropFilter,
      }}
    >
      <PlayerPill
        player={player1}
        side="left"
        fontSize={layout.fontSize}
        barHeight={layout.barHeight}
        testId="combat-portrait-p1"
      />
      <PlayerPill
        player={player2}
        side="right"
        fontSize={layout.fontSize}
        barHeight={layout.barHeight}
        testId="combat-portrait-p2"
      />
    </div>
  );
};
 
export default CombatPortraitStatusStrip;