All files / components/shared/three/effects GrapplingIndicator3D.tsx

3.73% Statements 4/107
0% Branches 0/50
0% Functions 0/10
3.92% Lines 4/102

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                                                                                                    1x                                                                                                                                                                                                                       1x                                                                                                                                                                                                                                                                             1x                                                                                                                                                       1x                                                                                                          
/**
 * GrapplingIndicator3D - Visual indicator for grappling state and struggle
 *
 * **Korean**: 잡기 표시기 3D (Japgi Pyosigi 3D)
 *
 * Displays visual feedback during grappling interactions including:
 * - Struggle particle effects
 * - Grip strength meter (UI overlay)
 * - Bilingual state text (Korean-English)
 * - Shake effects during escape attempts
 *
 * Features:
 * - Particle system for struggle visualization
 * - HTML overlay for grip strength UI
 * - Smooth transitions between grapple states
 * - Performance optimized for 60fps
 * - Korean-English bilingual text support
 *
 * @module components/shared/three/effects/GrapplingIndicator3D
 * @category Combat Effects
 * @korean 잡기표시기3D
 */
 
import { Html } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import React, { useRef, useEffect, useState } from "react";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../../../types/constants";
import type { Position3D } from "../../../../types/physics";
import { GrappleControl, GrappleState } from "../../../../types/common";
 
/**
 * Props for GrapplingIndicator3D component
 */
export interface GrapplingIndicator3DProps {
  /** Current grapple control state (undefined if not grappling) */
  readonly grappleControl: GrappleControl | undefined;
  /** Character position in 3D space (meters) */
  readonly characterPosition: Position3D;
  /** Whether the character is the controller (attacker) */
  readonly isController: boolean;
  /** Whether to use mobile-optimized rendering */
  readonly isMobile?: boolean;
  /** Test ID for component testing */
  readonly "data-testid"?: string;
}
 
/**
 * Particle count based on grapple state
 */
const PARTICLE_COUNT_MAP: Record<string, number> = {
  [GrappleState.NONE]: 0,
  [GrappleState.GRABBING]: 20,
  [GrappleState.CONTROLLING]: 30,
  [GrappleState.ESCAPING]: 50,
  [GrappleState.THROWING]: 40,
  [GrappleState.LOCKING]: 35,
};
 
/**
 * Get bilingual text for grapple state
 *
 * @param state - Current grapple state
 * @param isController - Whether viewing as controller
 * @returns Korean and English text for the state
 */
function getGrappleStateText(
  state: GrappleState,
  isController: boolean
): { korean: string; english: string } {
  if (isController) {
    switch (state) {
      case GrappleState.NONE:
        return { korean: "없음", english: "None" };
      case GrappleState.GRABBING:
        return { korean: "잡기 중", english: "Grabbing" };
      case GrappleState.CONTROLLING:
        return { korean: "잡기 유지", english: "Controlling" };
      case GrappleState.ESCAPING:
        return { korean: "제어 상실", english: "Losing Control" };
      case GrappleState.THROWING:
        return { korean: "던지기", english: "Throwing" };
      case GrappleState.LOCKING:
        return { korean: "관절기", english: "Joint Lock" };
    }
  } else {
    switch (state) {
      case GrappleState.NONE:
        return { korean: "없음", english: "None" };
      case GrappleState.GRABBING:
        return { korean: "잡힘", english: "Grabbed" };
      case GrappleState.CONTROLLING:
        return { korean: "잡힘 상태", english: "Grappled" };
      case GrappleState.ESCAPING:
        return { korean: "탈출 중", english: "Escaping" };
      case GrappleState.THROWING:
        return { korean: "던져짐", english: "Being Thrown" };
      case GrappleState.LOCKING:
        return { korean: "관절기당함", english: "Locked" };
    }
  }
}
 
/**
 * Get color for grapple state visualization
 *
 * @param state - Current grapple state
 * @param isController - Whether viewing as controller
 * @returns Hex color value
 */
function getGrappleStateColor(
  state: GrappleState,
  isController: boolean
): number {
  if (isController) {
    switch (state) {
      case GrappleState.NONE:
        return KOREAN_COLORS.UI_GRAY;
      case GrappleState.GRABBING:
        return KOREAN_COLORS.ACCENT_CYAN;
      case GrappleState.CONTROLLING:
        return KOREAN_COLORS.ACCENT_GOLD;
      case GrappleState.ESCAPING:
        return KOREAN_COLORS.WARNING_ORANGE;
      case GrappleState.THROWING:
        return KOREAN_COLORS.ACCENT_PURPLE;
      case GrappleState.LOCKING:
        return KOREAN_COLORS.CRITICAL_HIT;
    }
  } else {
    switch (state) {
      case GrappleState.NONE:
        return KOREAN_COLORS.UI_GRAY;
      case GrappleState.GRABBING:
        return KOREAN_COLORS.WARNING_ORANGE;
      case GrappleState.CONTROLLING:
        return KOREAN_COLORS.NEGATIVE_RED;
      case GrappleState.ESCAPING:
        return KOREAN_COLORS.ACCENT_GREEN;
      case GrappleState.THROWING:
        return KOREAN_COLORS.WARNING_YELLOW;
      case GrappleState.LOCKING:
        return KOREAN_COLORS.NEGATIVE_RED;
    }
  }
}
 
/**
 * Struggle particle system component
 *
 * Displays particles during grappling to visualize struggle and resistance.
 * Particle count and movement intensity scale with grapple state.
 */
const StruggleParticles: React.FC<{
  readonly particleCount: number;
  readonly color: number;
  readonly intensity: number;
  readonly position: Position3D;
}> = ({ particleCount, color, intensity, position }) => {
  const pointsRef = useRef<THREE.Points>(null);
 
  // Positions for initial geometry (useState to allow access during render)
  const [positions, setPositions] = useState<Float32Array>(
    () => new Float32Array(particleCount * 3)
  );
  
  // Velocities are mutable and only accessed in useFrame (useRef is fine)
  const velocitiesRef = useRef<Float32Array>(new Float32Array(particleCount * 3));
 
  // Initialize particle positions and velocities when count or intensity changes
  useEffect(() => {
    const pos = new Float32Array(particleCount * 3);
    const vel = new Float32Array(particleCount * 3);
 
    for (let i = 0; i < particleCount; i++) {
      const i3 = i * 3;
      const radius = 0.3 + Math.random() * 0.4; // 0.3-0.7m from center
      const theta = Math.random() * Math.PI * 2;
      const phi = Math.random() * Math.PI;
 
      // Spherical distribution around character
      pos[i3] = radius * Math.sin(phi) * Math.cos(theta);
      pos[i3 + 1] = 1.2 + radius * Math.cos(phi); // Center at chest height
      pos[i3 + 2] = radius * Math.sin(phi) * Math.sin(theta);
 
      // Outward velocities with some randomness
      vel[i3] = (Math.random() - 0.5) * intensity * 2;
      vel[i3 + 1] = Math.random() * intensity;
      vel[i3 + 2] = (Math.random() - 0.5) * intensity * 2;
    }
 
    setPositions(pos);
    velocitiesRef.current = vel;
 
    // Update geometry if it exists
    if (pointsRef.current) {
      const attr = pointsRef.current.geometry.attributes.position;
      (attr.array as Float32Array).set(pos);
      attr.needsUpdate = true;
    }
  }, [particleCount, intensity]);
 
  // Cleanup geometry and material on unmount
  useEffect(() => {
    const points = pointsRef.current;
    return () => {
      if (points) {
        // Dispose geometry
        points.geometry.dispose();
        // Dispose material
        if (points.material instanceof THREE.Material) {
          points.material.dispose();
        }
      }
    };
  }, []);
 
  // Animate particles
  useFrame((_state, delta) => {
    if (!pointsRef.current) return;
 
    const attr = pointsRef.current.geometry.attributes.position;
    const array = attr.array as Float32Array;
    const velocities = velocitiesRef.current;
 
    for (let i = 0; i < particleCount; i++) {
      const i3 = i * 3;
 
      // Update positions with velocities
      array[i3] += velocities[i3] * delta;
      array[i3 + 1] += velocities[i3 + 1] * delta;
      array[i3 + 2] += velocities[i3 + 2] * delta;
 
      // Decay upward velocity (gravity simulation)
      velocities[i3 + 1] -= 2.0 * delta;
 
      // Reset particles that fall too low or go too far
      const y = array[i3 + 1];
      const distFromCenter = Math.sqrt(
        array[i3] * array[i3] + array[i3 + 2] * array[i3 + 2]
      );
 
      if (y < 0.5 || distFromCenter > 1.5) {
        const radius = 0.3 + Math.random() * 0.2;
        const theta = Math.random() * Math.PI * 2;
        const phi = Math.random() * Math.PI;
 
        array[i3] = radius * Math.sin(phi) * Math.cos(theta);
        array[i3 + 1] = 1.2 + radius * Math.cos(phi);
        array[i3 + 2] = radius * Math.sin(phi) * Math.sin(theta);
 
        velocities[i3] = (Math.random() - 0.5) * intensity * 2;
        velocities[i3 + 1] = Math.random() * intensity;
        velocities[i3 + 2] = (Math.random() - 0.5) * intensity * 2;
      }
    }
 
    attr.needsUpdate = true;
  });
 
  if (particleCount === 0) return null;
 
  return (
    <points ref={pointsRef} position={[position.x, position.y, position.z]}>
      <bufferGeometry>
        <bufferAttribute
          attach="attributes-position"
          args={[positions, 3]}
        />
      </bufferGeometry>
      <pointsMaterial
        color={color}
        size={0.04}
        sizeAttenuation
        transparent
        opacity={0.7}
        depthWrite={false}
        blending={THREE.AdditiveBlending}
      />
    </points>
  );
};
 
/**
 * Grip strength meter UI overlay
 *
 * Displays grip strength as a horizontal bar with bilingual labels.
 */
const GripStrengthMeter: React.FC<{
  readonly gripStrength: number;
  readonly maxGripStrength: number;
  readonly stateText: { korean: string; english: string };
  readonly color: number;
}> = ({ gripStrength, maxGripStrength, stateText, color }) => {
  const percentage = (gripStrength / maxGripStrength) * 100;
  const hexColor = `#${color.toString(16).padStart(6, "0")}`;
 
  return (
    <div
      style={{
        backgroundColor: "rgba(10, 10, 10, 0.85)",
        border: `2px solid ${hexColor}`,
        borderRadius: "8px",
        padding: "8px 12px",
        minWidth: "200px",
        fontFamily: "'Noto Sans KR', 'Roboto', sans-serif",
        boxShadow: `0 4px 12px rgba(0, 0, 0, 0.6), 0 0 20px ${hexColor}40`,
      }}
      data-testid="grip-strength-meter"
    >
      {/* State text */}
      <div
        style={{
          display: "flex",
          justifyContent: "space-between",
          marginBottom: "6px",
          fontSize: "14px",
          fontWeight: 600,
        }}
      >
        <span style={{ color: hexColor }}>{stateText.korean}</span>
        <span style={{ color: hexColor }}>{stateText.english}</span>
      </div>
 
      {/* Grip strength bar */}
      <div
        style={{
          width: "100%",
          height: "20px",
          backgroundColor: "rgba(90, 101, 120, 0.3)",
          borderRadius: "4px",
          overflow: "hidden",
          border: "1px solid rgba(255, 255, 255, 0.2)",
        }}
      >
        <div
          style={{
            width: `${percentage}%`,
            height: "100%",
            backgroundColor: hexColor,
            transition: "width 0.3s ease-out",
            boxShadow: `0 0 10px ${hexColor}`,
          }}
          data-testid="grip-bar-fill"
        />
      </div>
 
      {/* Grip strength value */}
      <div
        style={{
          textAlign: "center",
          marginTop: "4px",
          fontSize: "12px",
          color: "#cccccc",
          fontWeight: 500,
        }}
      >
        {Math.round(gripStrength)} / {maxGripStrength}
      </div>
    </div>
  );
};
 
/**
 * Main GrapplingIndicator3D component
 *
 * Renders struggle particles and grip strength UI during grappling.
 * Automatically adjusts visualization based on grapple state.
 */
export const GrapplingIndicator3D: React.FC<GrapplingIndicator3DProps> = ({
  grappleControl,
  characterPosition,
  isController,
  isMobile = false,
  "data-testid": testId = "grappling-indicator-3d",
}) => {
  // Don't render if not grappling
  if (!grappleControl) return null;
 
  const { state, gripStrength } = grappleControl;
  const maxGripStrength = 100; // TODO: Get from config
 
  // Get visual properties for current state
  const particleCount = isMobile
    ? Math.floor(PARTICLE_COUNT_MAP[state] * 0.6) // Reduce particles on mobile
    : PARTICLE_COUNT_MAP[state];
 
  const color = getGrappleStateColor(state, isController);
  const stateText = getGrappleStateText(state, isController);
 
  // Struggle intensity scales with grip strength and state
  const intensity = state === GrappleState.ESCAPING ? 1.5 : 0.8;
 
  return (
    <group data-testid={testId}>
      {/* Struggle particle effect */}
      <StruggleParticles
        particleCount={particleCount}
        color={color}
        intensity={intensity}
        position={characterPosition}
      />
 
      {/* Grip strength meter overlay */}
      <Html
        position={[characterPosition.x, characterPosition.y + 2.2, characterPosition.z]}
        center
        distanceFactor={8}
        zIndexRange={[100, 0]}
      >
        <GripStrengthMeter
          gripStrength={gripStrength}
          maxGripStrength={maxGripStrength}
          stateText={stateText}
          color={color}
        />
      </Html>
    </group>
  );
};
 
export default GrapplingIndicator3D;