All files / blacktrigram/src/components/ui Player.tsx

0% Statements 0/364
100% Branches 1/1
100% Functions 1/1
0% Lines 0/364

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
// Complete Player UI component with Korean martial arts character rendering
 
import { PLAYER_ARCHETYPES_DATA, PlayerState } from "@/systems";
import * as PIXI from "pixi.js";
import React, { useCallback, useMemo } from "react";
import { KOREAN_COLORS } from "../../types/constants";
import { getArchetypeColors } from "../../utils/colorUtils";
import { usePixiExtensions } from "../../utils/pixiExtensions";
import { BaseComponentProps } from "./types";
 
// Define PlayerRenderMode type
export type PlayerRenderMode = "full" | "compact" | "training" | "combat";
 
export interface PlayerProps extends BaseComponentProps {
  readonly playerState: PlayerState;
  readonly playerIndex: number;
  readonly onClick?: () => void;
  readonly renderMode?: PlayerRenderMode;
  readonly showHealthBar?: boolean;
  readonly showStatusEffects?: boolean;
  readonly showArchetype?: boolean;
}
 
/**
 * Korean Martial Arts Player Visual Component
 * Renders a player with full Korean martial arts aesthetics and combat information
 */
export const Player: React.FC<PlayerProps> = ({
  playerState,
  playerIndex,
  onClick,
  renderMode = "full", // Add default value and destructure the prop
  x = 0,
  y = 0,
  width = 80,
  height = 140,
  visible = true,
  alpha = 1.0,
}) => {
  usePixiExtensions();
 
  // Get archetype-specific data and colors
  const archetypeData = useMemo(() => {
    return PLAYER_ARCHETYPES_DATA[playerState.archetype];
  }, [playerState.archetype]);
 
  const archetypeColors = useMemo(() => {
    return getArchetypeColors(playerState.archetype);
  }, [playerState.archetype]);
 
  // Calculate health-based visual states
  const healthPercentage = useMemo(() => {
    return playerState.health / playerState.maxHealth;
  }, [playerState.health, playerState.maxHealth]);
 
  const kiPercentage = useMemo(() => {
    return playerState.ki / playerState.maxKi;
  }, [playerState.ki, playerState.maxKi]);
 
  const staminaPercentage = useMemo(() => {
    return playerState.stamina / playerState.maxStamina;
  }, [playerState.stamina, playerState.maxStamina]);
 
  // Determine player body color based on health and status
  const playerBodyColor = useMemo(() => {
    if (playerState.isStunned) return KOREAN_COLORS.WARNING_YELLOW;
    if (healthPercentage > 0.7) return archetypeColors.primary;
    if (healthPercentage > 0.4) return KOREAN_COLORS.WARNING_ORANGE;
    if (healthPercentage > 0.15) return KOREAN_COLORS.WARNING_YELLOW;
    return KOREAN_COLORS.NEGATIVE_RED;
  }, [healthPercentage, playerState.isStunned, archetypeColors.primary]);
 
  // Player body outline color based on combat state
  const outlineColor = useMemo(() => {
    if (playerState.isBlocking) return KOREAN_COLORS.PRIMARY_BLUE;
    if (playerState.isCountering) return KOREAN_COLORS.ACCENT_PURPLE;
    if (playerState.isStunned) return KOREAN_COLORS.NEGATIVE_RED;
    return KOREAN_COLORS.TEXT_PRIMARY;
  }, [playerState.isBlocking, playerState.isCountering, playerState.isStunned]);
 
  // Combat status glow effect
  const shouldGlow = useMemo(() => {
    return (
      playerState.isBlocking ||
      playerState.isCountering ||
      playerState.statusEffects.length > 0
    );
  }, [
    playerState.isBlocking,
    playerState.isCountering,
    playerState.statusEffects,
  ]);
 
  // Main graphics drawing callback
  const drawPlayerBody = useCallback(
    (g: PIXI.Graphics) => {
      g.clear();
 
      // Body background
      g.beginFill(playerBodyColor, 0.9);
      g.drawRoundedRect(0, 0, width, height, 8);
      g.endFill();
 
      // Player outline with combat state indication
      const lineWidth = shouldGlow ? 3 : 2;
      g.lineStyle(lineWidth, outlineColor, 1);
      g.drawRoundedRect(0, 0, width, height, 8);
 
      // Archetype symbol background
      const symbolSize = 24;
      const symbolX = width / 2 - symbolSize / 2;
      const symbolY = 10;
 
      g.beginFill(archetypeColors.secondary, 0.8);
      g.drawCircle(
        symbolX + symbolSize / 2,
        symbolY + symbolSize / 2,
        symbolSize / 2
      );
      g.endFill();
 
      // Combat state indicators
      if (playerState.isBlocking) {
        g.lineStyle(2, KOREAN_COLORS.PRIMARY_BLUE, 0.8);
        g.drawRect(5, 5, width - 10, height - 10);
      }
 
      if (playerState.isStunned) {
        // Draw stun effect
        for (let i = 0; i < 3; i++) {
          g.lineStyle(1, KOREAN_COLORS.WARNING_YELLOW, 0.6);
          g.drawCircle(width / 2, height / 2, 15 + i * 8);
        }
      }
    },
    [
      width,
      height,
      playerBodyColor,
      outlineColor,
      shouldGlow,
      archetypeColors,
      playerState.isBlocking,
      playerState.isStunned,
    ]
  );
 
  // Health bar drawing
  const drawHealthBar = useCallback(
    (g: PIXI.Graphics) => {
      g.clear();
      const barWidth = width - 8;
      const barHeight = 8;
      const barX = 4;
      const barY = height + 10;
 
      // Background
      g.beginFill(KOREAN_COLORS.UI_BACKGROUND_DARK, 0.8);
      g.drawRoundedRect(barX, barY, barWidth, barHeight, 3);
      g.endFill();
 
      // Health fill
      const healthWidth = barWidth * healthPercentage;
      const healthColor =
        healthPercentage > 0.6
          ? KOREAN_COLORS.POSITIVE_GREEN
          : healthPercentage > 0.3
          ? KOREAN_COLORS.WARNING_YELLOW
          : KOREAN_COLORS.NEGATIVE_RED;
 
      g.beginFill(healthColor, 0.9);
      g.drawRoundedRect(barX, barY, healthWidth, barHeight, 3);
      g.endFill();
 
      // Border
      g.lineStyle(1, KOREAN_COLORS.UI_BORDER, 0.8);
      g.drawRoundedRect(barX, barY, barWidth, barHeight, 3);
    },
    [width, height, healthPercentage]
  );
 
  // Ki bar drawing
  const drawKiBar = useCallback(
    (g: PIXI.Graphics) => {
      g.clear();
      const barWidth = width - 8;
      const barHeight = 6;
      const barX = 4;
      const barY = height + 22;
 
      // Background
      g.beginFill(KOREAN_COLORS.UI_BACKGROUND_DARK, 0.6);
      g.drawRoundedRect(barX, barY, barWidth, barHeight, 2);
      g.endFill();
 
      // Ki fill
      const kiWidth = barWidth * kiPercentage;
      g.beginFill(KOREAN_COLORS.PRIMARY_CYAN, 0.8);
      g.drawRoundedRect(barX, barY, kiWidth, barHeight, 2);
      g.endFill();
    },
    [width, height, kiPercentage]
  );
 
  // Stamina bar drawing
  const drawStaminaBar = useCallback(
    (g: PIXI.Graphics) => {
      g.clear();
      const barWidth = width - 8;
      const barHeight = 4;
      const barX = 4;
      const barY = height + 32;
 
      // Background
      g.beginFill(KOREAN_COLORS.UI_BACKGROUND_DARK, 0.4);
      g.drawRoundedRect(barX, barY, barWidth, barHeight, 1);
      g.endFill();
 
      // Stamina fill
      const staminaWidth = barWidth * staminaPercentage;
      g.beginFill(KOREAN_COLORS.SECONDARY_YELLOW, 0.7);
      g.drawRoundedRect(barX, barY, staminaWidth, barHeight, 1);
      g.endFill();
    },
    [width, height, staminaPercentage]
  );
 
  // Status effects drawing
  const drawStatusEffects = useCallback(
    (g: PIXI.Graphics) => {
      g.clear();
 
      if (playerState.statusEffects.length === 0) return;
 
      // Draw status effect indicators
      playerState.statusEffects.forEach((effect, index) => {
        const effectX = 5 + index * 12;
        const effectY = height - 15;
 
        // Fix: Use PIXI.ColorSource type for all effect colors
        let effectColor: PIXI.ColorSource = KOREAN_COLORS.NEUTRAL_GRAY;
        switch (effect.type) {
          case "stun":
            effectColor = KOREAN_COLORS.WARNING_YELLOW as PIXI.ColorSource;
            break;
          case "poison":
            effectColor = KOREAN_COLORS.POSITIVE_GREEN as PIXI.ColorSource;
            break;
          case "burn":
            effectColor = KOREAN_COLORS.ACCENT_RED as PIXI.ColorSource;
            break;
          case "bleed":
            effectColor = KOREAN_COLORS.NEGATIVE_RED as PIXI.ColorSource;
            break;
          case "strengthened":
            effectColor = KOREAN_COLORS.ACCENT_GOLD as PIXI.ColorSource;
            break;
          case "weakened":
            effectColor = KOREAN_COLORS.UI_GRAY as PIXI.ColorSource;
            break;
        }
 
        g.beginFill(effectColor, 0.8);
        g.drawCircle(effectX, effectY, 4);
        g.endFill();
      });
    },
    [height, playerState.statusEffects]
  );
 
  // Text styles with proper PIXI color typing
  const nameTextStyle = useMemo(
    () =>
      new PIXI.TextStyle({
        fontSize: 12,
        fill: KOREAN_COLORS.TEXT_PRIMARY as PIXI.ColorSource,
        fontFamily: '"Noto Sans KR", Arial, sans-serif',
        align: "center",
        fontWeight: "bold",
      }),
    []
  );
 
  const stanceTextStyle = useMemo(
    () =>
      new PIXI.TextStyle({
        fontSize: 10,
        fill: KOREAN_COLORS.ACCENT_GOLD as PIXI.ColorSource,
        fontFamily: '"Noto Sans KR", Arial, sans-serif',
        align: "center",
      }),
    []
  );
 
  const archetypeTextStyle = useMemo(
    () =>
      new PIXI.TextStyle({
        fontSize: 8,
        fill: archetypeColors.primary as PIXI.ColorSource,
        fontFamily: '"Noto Sans KR", Arial, sans-serif',
        align: "center",
      }),
    [archetypeColors.primary]
  );
 
  const healthTextStyle = useMemo(
    () =>
      new PIXI.TextStyle({
        fontSize: 8,
        fill: KOREAN_COLORS.TEXT_PRIMARY as PIXI.ColorSource,
        fontFamily: "Arial, sans-serif",
        align: "center",
      }),
    []
  );
 
  // Render mode specific settings
  const getRenderModeSettings = useCallback((mode: PlayerRenderMode) => {
    switch (mode) {
      case "training":
        return {
          showHealthBar: false,
          showStatusEffects: false,
          showArchetype: true,
          scale: 1.2,
        };
      case "combat":
        return {
          showHealthBar: true,
          showStatusEffects: true,
          showArchetype: false,
          scale: 1.0,
        };
      case "compact":
        return {
          showHealthBar: true,
          showStatusEffects: false,
          showArchetype: false,
          scale: 0.8,
        };
      default: // 'full'
        return {
          showHealthBar: true,
          showStatusEffects: true,
          showArchetype: true,
          scale: 1.0,
        };
    }
  }, []);
 
  const { showHealthBar, showStatusEffects, showArchetype, scale } =
    getRenderModeSettings(renderMode);
 
  return (
    <pixiContainer
      x={x}
      y={y}
      visible={visible}
      alpha={alpha}
      interactive={true}
      onPointerDown={onClick}
      data-testid={`player-${playerIndex}`}
      scale={scale}
    >
      {/* Main player body */}
      <pixiGraphics draw={drawPlayerBody} />
 
      {/* Player name (Korean) */}
      <pixiText
        text={playerState.name.korean}
        style={nameTextStyle}
        x={width / 2}
        y={-25}
        anchor={0.5}
      />
 
      {/* Archetype name */}
      {showArchetype && (
        <pixiText
          text={archetypeData.name.korean}
          style={archetypeTextStyle}
          x={width / 2}
          y={-12}
          anchor={0.5}
        />
      )}
 
      {/* Current stance indicator */}
      <pixiText
        text={`${playerState.currentStance} 자세`}
        style={stanceTextStyle}
        x={width / 2}
        y={height + 45}
        anchor={0.5}
      />
 
      {/* Health bar */}
      {showHealthBar && <pixiGraphics draw={drawHealthBar} />}
 
      {/* Ki bar */}
      <pixiGraphics draw={drawKiBar} />
 
      {/* Stamina bar */}
      <pixiGraphics draw={drawStaminaBar} />
 
      {/* Health value text */}
      <pixiText
        text={`${Math.round(playerState.health)}/${playerState.maxHealth}`}
        style={healthTextStyle}
        x={width / 2}
        y={height + 14}
        anchor={0.5}
      />
 
      {/* Status effects */}
      {showStatusEffects && <pixiGraphics draw={drawStatusEffects} />}
 
      {/* Combat state text */}
      {(playerState.isBlocking ||
        playerState.isStunned ||
        playerState.isCountering) && (
        <pixiText
          text={
            playerState.isBlocking
              ? "방어"
              : playerState.isStunned
              ? "기절"
              : playerState.isCountering
              ? "반격"
              : ""
          }
          style={
            new PIXI.TextStyle({
              fontSize: 10,
              fill: KOREAN_COLORS.WARNING_YELLOW as PIXI.ColorSource,
              fontFamily: '"Noto Sans KR", Arial, sans-serif',
              align: "center",
              fontWeight: "bold",
            })
          }
          x={width / 2}
          y={height / 2 + 20}
          anchor={0.5}
        />
      )}
 
      {/* Player index indicator (for debugging/development) */}
      <pixiText
        text={`P${playerIndex + 1}`}
        style={
          new PIXI.TextStyle({
            fontSize: 8,
            fill: KOREAN_COLORS.TEXT_TERTIARY as PIXI.ColorSource,
            fontFamily: "Arial, sans-serif",
          })
        }
        x={2}
        y={2}
      />
 
      {/* Consciousness indicator (if unconscious) */}
      {playerState.consciousness <= 0 && (
        <pixiText
          text="의식잃음"
          style={
            new PIXI.TextStyle({
              fontSize: 12,
              fill: KOREAN_COLORS.NEGATIVE_RED as PIXI.ColorSource,
              fontFamily: '"Noto Sans KR", Arial, sans-serif',
              align: "center",
              fontWeight: "bold",
            })
          }
          x={width / 2}
          y={height / 2}
          anchor={0.5}
        />
      )}
    </pixiContainer>
  );
};
 
export default Player;