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 | 1x 1x 1x | /**
* VulnerabilityWindowHUD - Timing indicator for counter-attack opportunities
*
* **Korean**: 취약성 타이밍 표시기 (Chwiyakseong Timing Pyosigi)
*
* Displays a bilingual timing indicator when the opponent is vulnerable,
* showing the remaining window duration for executing counter-attacks.
*
* Features:
* - Korean-English bilingual text ("반격 기회" / "Counter Opportunity")
* - Circular progress timer showing remaining window
* - Only visible during vulnerability windows (300-400ms)
* - Gold accent with Korean cyberpunk aesthetic
* - Mobile and desktop optimized layouts
*
* @module components/shared/three/ui/VulnerabilityWindowHUD
* @category Combat UI
* @korean 취약성창표시기
*/
import { Html } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import React, { useMemo, useState, useRef } from "react";
import { FONT_FAMILY, KOREAN_COLORS } from "../../../../types/constants";
import type { CounterOpportunity } from "../../../../types/physics";
import { hexColorToCSS, hexToRgbaString } from "../../../../utils/colorUtils";
import { withGPUAcceleration } from "../../../../utils/performanceOptimization";
/**
* Props for VulnerabilityWindowHUD component
*/
export interface VulnerabilityWindowHUDProps {
/** Counter-attack opportunity with timing data */
readonly opportunity: CounterOpportunity | undefined;
/** Current time in technique execution (ms) */
readonly currentTime: number;
/** Whether to use mobile-optimized layout */
readonly isMobile?: boolean;
/** Test ID for component testing */
readonly "data-testid"?: string;
}
/**
* Calculate remaining time in vulnerability window
*
* @param currentTime - Current time in technique (ms)
* @param windowStart - Window start time (ms)
* @param windowDuration - Window duration (ms)
* @returns Remaining time in ms, or 0 if outside window
*/
function calculateRemainingTime(
currentTime: number,
windowStart: number,
windowDuration: number
): number {
const elapsed = currentTime - windowStart;
// Before window
if (elapsed < 0) return 0;
// After window
if (elapsed > windowDuration) return 0;
// Remaining time
return windowDuration - elapsed;
}
/**
* Calculate progress through vulnerability window (0-1)
*
* @param currentTime - Current time in technique (ms)
* @param windowStart - Window start time (ms)
* @param windowDuration - Window duration (ms)
* @returns Progress value (0-1), or 0 if outside window
*/
function calculateWindowProgress(
currentTime: number,
windowStart: number,
windowDuration: number
): number {
// Guard against invalid window duration
if (windowDuration <= 0) return 0;
const elapsed = currentTime - windowStart;
// Before or after window
if (elapsed < 0 || elapsed > windowDuration) return 0;
// Progress through window
return elapsed / windowDuration;
}
/**
* Get urgency color based on remaining time
*
* Changes color from gold -> orange -> red as time runs out.
*
* @param progress - Progress through window (0-1)
* @returns CSS color string
*/
function getUrgencyColor(progress: number): string {
// Early window (0-50%): Gold
if (progress < 0.5) {
return hexColorToCSS(KOREAN_COLORS.ACCENT_GOLD);
}
// Mid window (50-75%): Orange
if (progress < 0.75) {
return hexColorToCSS(KOREAN_COLORS.SECONDARY_ORANGE);
}
// Late window (75-100%): Red
return hexColorToCSS(KOREAN_COLORS.ACCENT_RED);
}
/**
* Format milliseconds to decimal seconds
*
* @param ms - Time in milliseconds
* @returns Formatted string (e.g., "0.35s")
*/
function formatTime(ms: number): string {
return `${(ms / 1000).toFixed(2)}s`;
}
/**
* Circular progress indicator component
*/
interface CircularProgressProps {
readonly progress: number;
readonly size: number;
readonly strokeWidth: number;
readonly color: string;
}
const CircularProgress: React.FC<CircularProgressProps> = ({
progress,
size,
strokeWidth,
color,
}) => {
const radius = (size - strokeWidth) / 2;
const circumference = radius * 2 * Math.PI;
const offset = circumference - progress * circumference;
return (
<svg
width={size}
height={size}
style={{
transform: "rotate(-90deg)", // Start from top
}}
>
{/* Background circle */}
<circle
cx={size / 2}
cy={size / 2}
r={radius}
fill="none"
stroke={hexToRgbaString(KOREAN_COLORS.UI_BORDER, 0.3)}
strokeWidth={strokeWidth}
/>
{/* Progress circle */}
<circle
cx={size / 2}
cy={size / 2}
r={radius}
fill="none"
stroke={color}
strokeWidth={strokeWidth}
strokeDasharray={circumference}
strokeDashoffset={offset}
strokeLinecap="round"
style={{
transition: "stroke-dashoffset 0.1s linear",
}}
/>
</svg>
);
};
/**
* VulnerabilityWindowHUD Component
*
* Displays a timing indicator at the top-center of the screen during
* vulnerability windows, showing the player when to execute counter-attacks.
*
* Layout:
* - Mobile: Compact design, smaller text, 60px circle
* - Desktop: Full size, larger text, 80px circle
*
* Performance:
* - Uses GPU-accelerated transforms
* - Memoized calculations
* - Updates only during active windows
* - Minimal DOM updates
*
* @example
* ```tsx
* <VulnerabilityWindowHUD
* opportunity={counterOpportunity}
* currentTime={450}
* isMobile={false}
* />
* ```
*/
export const VulnerabilityWindowHUD: React.FC<
VulnerabilityWindowHUDProps
> = ({
opportunity,
currentTime,
isMobile = false,
"data-testid": testId = "vulnerability-window-hud",
}) => {
// Track animation state
const [progress, setProgress] = useState(0);
const [remainingMs, setRemainingMs] = useState(0);
// Calculate position (top-center of view)
const position3D: [number, number, number] = useMemo(() => {
return [0, 4.5, 0]; // Top center in 3D space
}, []);
// Track previous values to avoid unnecessary re-renders
const prevProgressRef = useRef(0);
const prevRemainingRef = useRef(0);
// Update progress and remaining time each frame (only if changed)
useFrame(() => {
if (!opportunity) {
if (prevProgressRef.current !== 0 || prevRemainingRef.current !== 0) {
setProgress(0);
setRemainingMs(0);
prevProgressRef.current = 0;
prevRemainingRef.current = 0;
}
return;
}
const newProgress = calculateWindowProgress(
currentTime,
opportunity.windowStart,
opportunity.windowDuration
);
const newRemaining = calculateRemainingTime(
currentTime,
opportunity.windowStart,
opportunity.windowDuration
);
// Only update state if values changed significantly (avoid 60fps churn)
const progressChanged = Math.abs(newProgress - prevProgressRef.current) > 0.001;
const remainingChanged = Math.abs(newRemaining - prevRemainingRef.current) > 1; // 1ms threshold
if (progressChanged) {
setProgress(newProgress);
prevProgressRef.current = newProgress;
}
if (remainingChanged) {
setRemainingMs(newRemaining);
prevRemainingRef.current = newRemaining;
}
});
// Don't render if no opportunity
if (!opportunity) {
return null;
}
// Check if we're within the vulnerability window time range
const windowStart = opportunity.windowStart;
const windowEnd = windowStart + opportunity.windowDuration;
// Only hide if we're completely outside the window time range
if (currentTime < windowStart || currentTime > windowEnd) {
return null;
}
// Responsive sizing
const circleSize = isMobile ? 60 : 80;
const strokeWidth = isMobile ? 4 : 6;
const fontSize = isMobile ? 14 : 18;
const titleSize = isMobile ? 18 : 24;
// Get color based on urgency
const urgencyColor = getUrgencyColor(progress);
return (
<Html
position={position3D}
center
distanceFactor={10}
style={{ pointerEvents: "none" }}
>
<div
data-testid={testId}
style={withGPUAcceleration({
display: "flex",
flexDirection: "column",
alignItems: "center",
gap: isMobile ? "8px" : "12px",
padding: isMobile ? "12px 20px" : "16px 32px",
background: hexToRgbaString(
KOREAN_COLORS.UI_BACKGROUND_DARK,
0.85
),
borderRadius: "12px",
border: `2px solid ${urgencyColor}`,
boxShadow: `
0 0 20px ${hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.4)},
0 4px 12px rgba(0, 0, 0, 0.6)
`,
backdropFilter: "blur(8px)",
userSelect: "none",
minWidth: isMobile ? "200px" : "280px",
})}
>
{/* Title */}
<div
style={{
fontSize: `${titleSize}px`,
fontWeight: "bold",
fontFamily: FONT_FAMILY.KOREAN,
color: urgencyColor,
textAlign: "center",
textShadow: `
0 0 10px ${hexToRgbaString(KOREAN_COLORS.ACCENT_GOLD, 0.6)},
2px 2px 4px rgba(0, 0, 0, 0.8)
`,
lineHeight: 1.2,
}}
>
<div>반격 기회</div>
<div
style={{
fontSize: `${fontSize}px`,
fontWeight: "normal",
marginTop: "4px",
opacity: 0.9,
}}
>
Counter Opportunity
</div>
</div>
{/* Circular timer */}
<div
style={{
position: "relative",
width: `${circleSize}px`,
height: `${circleSize}px`,
}}
>
<CircularProgress
progress={1 - progress} // Invert so it counts down
size={circleSize}
strokeWidth={strokeWidth}
color={urgencyColor}
/>
{/* Time remaining text */}
<div
style={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
fontSize: `${isMobile ? 16 : 20}px`,
fontWeight: "bold",
fontFamily: FONT_FAMILY.KOREAN,
color: hexColorToCSS(KOREAN_COLORS.TEXT_PRIMARY),
textShadow: "1px 1px 2px rgba(0, 0, 0, 0.8)",
}}
>
{formatTime(remainingMs)}
</div>
</div>
{/* Additional info (optional) */}
{!isMobile && opportunity.recommendedCounters && (
<div
style={{
fontSize: `${fontSize - 2}px`,
fontFamily: FONT_FAMILY.KOREAN,
color: hexColorToCSS(KOREAN_COLORS.TEXT_SECONDARY),
textAlign: "center",
opacity: 0.8,
}}
>
추천 반격 기술
<br />
<span style={{ fontSize: `${fontSize - 4}px`, opacity: 0.7 }}>
Recommended Counters
</span>
</div>
)}
</div>
</Html>
);
};
// Display name for debugging
VulnerabilityWindowHUD.displayName = "VulnerabilityWindowHUD";
// Memoize component to prevent unnecessary re-renders
export default React.memo(
VulnerabilityWindowHUD,
(prevProps, nextProps) => {
// Re-render only if relevant props change
return (
prevProps.opportunity?.windowStart === nextProps.opportunity?.windowStart &&
prevProps.opportunity?.windowDuration === nextProps.opportunity?.windowDuration &&
prevProps.currentTime === nextProps.currentTime &&
prevProps.isMobile === nextProps.isMobile
);
}
);
|