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 | 1x 1x 1x | /**
* LimbExposureIndicator3D - Visual indicator for exposed limbs during combat
*
* **Korean**: 사지 노출 표시기 3D (Saji Nochul Pyosigi 3D)
*
* Displays a glowing effect on exposed limbs during attack execution,
* indicating vulnerability windows for counter-attacks. The glow intensity
* is based on the vulnerability multiplier (1.5x-2.5x).
*
* Features:
* - Red/orange glow effect using pointLight
* - Intensity scales with vulnerability multiplier
* - Smooth fade in/out during exposure window
* - Positioned at limb location on character model
* - Performance optimized for 60fps
*
* @module components/shared/three/effects/LimbExposureIndicator3D
* @category Combat Effects
* @korean 사지노출표시기3D
*/
import { useFrame } from "@react-three/fiber";
import React, { useMemo, useRef, useState } from "react";
import { KOREAN_COLORS } from "../../../../types/constants";
import type {
CounterOpportunity,
ExposedLimbType,
Position3D,
} from "../../../../types/physics";
import * as THREE from "three";
/**
* Props for LimbExposureIndicator3D component
*/
export interface LimbExposureIndicator3DProps {
/** Counter-attack opportunity with exposure data */
readonly opportunity: CounterOpportunity | undefined;
/** Character position in 3D space (meters) */
readonly characterPosition: Position3D;
/** Whether the character is facing left */
readonly facingLeft?: boolean;
/** Whether to use mobile-optimized rendering */
readonly isMobile?: boolean;
/** Current time in technique execution (ms) */
readonly currentTime: number;
/** Test ID for component testing */
readonly "data-testid"?: string;
}
/**
* Calculate limb position offset from character center
*
* Returns position offset in meters for the exposed limb.
* Adjusts for character facing direction.
*
* @param limb - Type of exposed limb
* @param facingLeft - Whether character is facing left
* @returns Position offset [x, y, z] in meters
*/
function getLimbPositionOffset(
limb: ExposedLimbType,
facingLeft: boolean
): [number, number, number] {
// Base offsets in meters (approximate human proportions)
const leftMult = facingLeft ? -1 : 1;
switch (limb) {
// Arms (상체)
case "left_arm":
return [-0.3 * leftMult, 1.2, 0]; // Shoulder height
case "right_arm":
return [0.3 * leftMult, 1.2, 0];
case "left_elbow":
return [-0.4 * leftMult, 1.0, 0.1]; // Slightly forward
case "right_elbow":
return [0.4 * leftMult, 1.0, 0.1];
case "left_wrist":
return [-0.5 * leftMult, 0.9, 0.2]; // Extended forward
case "right_wrist":
return [0.5 * leftMult, 0.9, 0.2];
// Legs (하체)
case "left_leg":
return [-0.15 * leftMult, 0.8, 0]; // Mid-thigh
case "right_leg":
return [0.15 * leftMult, 0.8, 0];
case "left_knee":
return [-0.2 * leftMult, 0.5, 0.15]; // Forward when kicking
case "right_knee":
return [0.2 * leftMult, 0.5, 0.15];
case "left_ankle":
return [-0.2 * leftMult, 0.1, 0.3]; // Foot position
case "right_ankle":
return [0.2 * leftMult, 0.1, 0.3];
default:
return [0, 1.0, 0]; // Default chest height
}
}
/**
* Calculate glow intensity from vulnerability multiplier
*
* Maps vulnerability multiplier (1.0-3.0) to light intensity (0-10).
* Higher vulnerability = brighter glow.
*
* @param vulnerabilityMultiplier - Damage multiplier (1.0-3.0)
* @param isMobile - Whether to use reduced intensity for mobile
* @returns Light intensity (0-10)
*/
function calculateGlowIntensity(
vulnerabilityMultiplier: number,
isMobile: boolean
): number {
// Clamp multiplier to valid range
const clamped = Math.max(1.0, Math.min(3.0, vulnerabilityMultiplier));
// Map to intensity range (1.0 -> 3, 3.0 -> 10)
const baseIntensity = 3 + ((clamped - 1.0) / 2.0) * 7;
// Reduce intensity on mobile for performance
return isMobile ? baseIntensity * 0.7 : baseIntensity;
}
/**
* Calculate fade factor based on current time and window timing
*
* Provides smooth fade in/out at window boundaries:
* - Fade in: First 20% of window
* - Sustained: Middle 60% of window
* - Fade out: Last 20% of window
*
* @param currentTime - Current time in technique (ms)
* @param windowStart - Window start time (ms)
* @param windowDuration - Window duration (ms)
* @returns Fade factor (0-1)
*/
function calculateFadeFactor(
currentTime: number,
windowStart: number,
windowDuration: number
): number {
// Guard against invalid window duration
if (windowDuration <= 0) return 0;
const elapsed = currentTime - windowStart;
// Before window starts
if (elapsed < 0) return 0;
// After window ends
if (elapsed > windowDuration) return 0;
// Calculate progress through window (0-1)
const progress = elapsed / windowDuration;
// Fade in during first 20%
if (progress < 0.2) {
return progress / 0.2;
}
// Fade out during last 20%
if (progress > 0.8) {
return (1.0 - progress) / 0.2;
}
// Sustained at full intensity (20%-80%)
return 1.0;
}
/**
* Get glow color based on vulnerability level
*
* @param vulnerabilityMultiplier - Damage multiplier
* @param allowsBreaking - Whether breaking techniques are allowed
* @returns THREE.Color instance (reused, not recreated)
*/
const colorCache = {
breakingRed: new THREE.Color(KOREAN_COLORS.NEGATIVE_RED),
highRed: new THREE.Color(KOREAN_COLORS.ACCENT_RED),
mediumOrange: new THREE.Color(KOREAN_COLORS.SECONDARY_ORANGE),
lowGold: new THREE.Color(KOREAN_COLORS.ACCENT_GOLD),
};
function getGlowColor(
vulnerabilityMultiplier: number,
allowsBreaking: boolean
): THREE.Color {
// Breaking opportunities: bright red (NEGATIVE_RED for high danger)
if (allowsBreaking) {
return colorCache.breakingRed;
}
// High vulnerability (>2.0): red
if (vulnerabilityMultiplier >= 2.0) {
return colorCache.highRed;
}
// Medium vulnerability (1.5-2.0): orange
if (vulnerabilityMultiplier >= 1.5) {
return colorCache.mediumOrange;
}
// Low vulnerability (<1.5): yellow
return colorCache.lowGold;
}
/**
* LimbExposureIndicator3D Component
*
* Renders a glowing point light at the exposed limb position during
* vulnerability windows. The light intensity and color reflect the
* degree of vulnerability.
*
* Performance:
* - Uses single point light (low overhead)
* - Calculates fade factor once per frame
* - Memoized offset calculations
* - Early return if no opportunity
*
* @example
* ```tsx
* <LimbExposureIndicator3D
* opportunity={counterOpportunity}
* characterPosition={{ x: 2, y: 0, z: 0 }}
* facingLeft={false}
* isMobile={false}
* currentTime={450}
* />
* ```
*/
export const LimbExposureIndicator3D: React.FC<
LimbExposureIndicator3DProps
> = ({
opportunity,
characterPosition,
facingLeft = false,
isMobile = false,
currentTime,
"data-testid": testId = "limb-exposure-indicator",
}) => {
// Track animated intensity for smooth transitions
const [animatedIntensity, setAnimatedIntensity] = useState(0);
const intensityRef = useRef(0);
// Calculate limb position offset (memoized, changes only when opportunity or facing changes)
const limbOffset = useMemo(
() =>
opportunity
? getLimbPositionOffset(opportunity.exposedLimb, facingLeft)
: [0, 0, 0],
[opportunity, facingLeft]
);
// Calculate final 3D position
const position3D: [number, number, number] = useMemo(
() => [
characterPosition.x + limbOffset[0],
characterPosition.y + limbOffset[1],
characterPosition.z + limbOffset[2],
],
[characterPosition, limbOffset]
);
// Calculate target intensity based on opportunity
const targetIntensity = useMemo(() => {
if (!opportunity) return 0;
const fadeFactor = calculateFadeFactor(
currentTime,
opportunity.windowStart,
opportunity.windowDuration
);
if (fadeFactor === 0) return 0;
const baseIntensity = calculateGlowIntensity(
opportunity.vulnerabilityMultiplier,
isMobile
);
return baseIntensity * fadeFactor;
}, [opportunity, currentTime, isMobile]);
// Get glow color (returns cached color instance, not a new one)
const glowColor = useMemo(
() =>
opportunity
? getGlowColor(
opportunity.vulnerabilityMultiplier,
opportunity.allowsBreaking
)
: colorCache.highRed,
[opportunity]
);
// Smooth intensity transitions using useFrame
useFrame((_, delta) => {
// Lerp towards target intensity
const lerpSpeed = 5.0; // Speed of intensity transition
intensityRef.current = THREE.MathUtils.lerp(
intensityRef.current,
targetIntensity,
delta * lerpSpeed
);
// Update state only if changed significantly (avoid unnecessary renders)
if (Math.abs(intensityRef.current - animatedIntensity) > 0.01) {
setAnimatedIntensity(intensityRef.current);
}
});
// Don't render if no opportunity or intensity is near zero
if (!opportunity || animatedIntensity < 0.01) {
return null;
}
return (
<group data-testid={testId} position={position3D}>
{/* Point light for glow effect */}
<pointLight
color={glowColor}
intensity={animatedIntensity}
distance={2} // Light radius in meters
decay={2} // Natural light falloff
castShadow={false} // Don't cast shadows for performance
/>
{/* Visual indicator sphere (optional, for debugging) */}
{process.env.NODE_ENV === "development" && (
<mesh>
<sphereGeometry args={[0.05, 8, 8]} />
<meshBasicMaterial
color={glowColor}
transparent
opacity={animatedIntensity / 10}
/>
</mesh>
)}
</group>
);
};
// Display name for debugging
LimbExposureIndicator3D.displayName = "LimbExposureIndicator3D";
// Memoize component to prevent unnecessary re-renders
export default React.memo(
LimbExposureIndicator3D,
(prevProps, nextProps) => {
// Re-render only if relevant props change
return (
prevProps.opportunity?.exposedLimb === nextProps.opportunity?.exposedLimb &&
prevProps.opportunity?.windowStart === nextProps.opportunity?.windowStart &&
prevProps.opportunity?.windowDuration === nextProps.opportunity?.windowDuration &&
prevProps.opportunity?.vulnerabilityMultiplier === nextProps.opportunity?.vulnerabilityMultiplier &&
prevProps.opportunity?.allowsBreaking === nextProps.opportunity?.allowsBreaking &&
prevProps.characterPosition.x === nextProps.characterPosition.x &&
prevProps.characterPosition.y === nextProps.characterPosition.y &&
prevProps.characterPosition.z === nextProps.characterPosition.z &&
prevProps.facingLeft === nextProps.facingLeft &&
prevProps.isMobile === nextProps.isMobile &&
prevProps.currentTime === nextProps.currentTime
);
}
);
|