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 | 2x 2x 2x 2x | /**
* HitFeedbackEffect3D - Visual hit confirmation with damage numbers
*
* Provides particle effects, color flashes, and floating damage numbers
* for training hit feedback
*/
import { Html } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import * as THREE from "three";
import { FONT_FAMILY, KOREAN_COLORS } from "../../../../types/constants";
import { ThreeObjectPools } from "../../../../utils/threeObjectPool";
/**
* Props for HitFeedbackEffect3D component
*/
export interface HitFeedbackEffect3DProps {
/** 3D position where hit occurred */
readonly position: [number, number, number];
/** Type of hit (affects visual style) */
readonly type: "success" | "perfect" | "miss";
/** Damage dealt (if applicable) */
readonly damage?: number;
/** Whether effect is visible */
readonly visible?: boolean;
/** Callback when effect completes */
readonly onComplete?: () => void;
/** Duration in milliseconds */
readonly duration?: number;
/** Whether on mobile device */
readonly isMobile?: boolean;
}
/**
* Impact particle system
*/
const ImpactParticles: React.FC<{
position: [number, number, number];
color: number;
count: number;
}> = ({ position, color, count }) => {
const pointsRef = useRef<THREE.Points>(null);
const velocitiesRef = useRef<Float32Array | null>(null);
const [initialPosition] = useState(position);
const { positions, velocities } = useMemo(() => {
const pos = new Float32Array(count * 3);
const vel = new Float32Array(count * 3);
const seed =
initialPosition[0] + initialPosition[1] * 10 + initialPosition[2] * 100;
function seededRandom(index: number): number {
const x = Math.sin(seed + index) * 10000;
return x - Math.floor(x);
}
const tempVel = ThreeObjectPools.vector3.acquire();
try {
for (let i = 0; i < count; i++) {
const i3 = i * 3;
pos[i3] = 0;
pos[i3 + 1] = 0;
pos[i3 + 2] = 0;
const theta = seededRandom(i * 3) * Math.PI * 2;
const phi = seededRandom(i * 3 + 1) * Math.PI;
const speed = 0.5 + seededRandom(i * 3 + 2) * 1.5;
tempVel.set(
Math.sin(phi) * Math.cos(theta),
Math.cos(phi),
Math.sin(phi) * Math.sin(theta)
);
tempVel.normalize().multiplyScalar(speed);
vel[i3] = tempVel.x;
vel[i3 + 1] = tempVel.y + 1;
vel[i3 + 2] = tempVel.z;
}
} finally {
ThreeObjectPools.vector3.release(tempVel);
}
return { positions: pos, velocities: vel };
}, [count, initialPosition]); // initialPosition is captured at mount and won't change
useEffect(() => {
velocitiesRef.current = velocities;
}, [velocities]);
useFrame((_, delta) => {
if (!pointsRef.current || !velocitiesRef.current) return;
const attr = pointsRef.current.geometry.attributes.position;
const array = attr.array as Float32Array;
const vel = velocitiesRef.current;
for (let i = 0; i < count; i++) {
const i3 = i * 3;
array[i3] += vel[i3] * delta * 10;
array[i3 + 1] += vel[i3 + 1] * delta * 10;
array[i3 + 2] += vel[i3 + 2] * delta * 10;
vel[i3 + 1] -= 9.8 * delta;
if (array[i3 + 1] < -2) {
array[i3 + 1] = -2;
}
}
attr.needsUpdate = true;
});
return (
<points ref={pointsRef} position={position}>
<bufferGeometry>
<bufferAttribute attach="attributes-position" args={[positions, 3]} />
</bufferGeometry>
<pointsMaterial
size={0.08}
color={color}
transparent
opacity={1.0}
sizeAttenuation
depthWrite={false}
blending={THREE.AdditiveBlending}
/>
</points>
);
};
/**
* Expanding ring effect
*/
const RingEffect: React.FC<{
position: [number, number, number];
color: number;
maxRadius: number;
}> = ({ position, color, maxRadius }) => {
const meshRef = useRef<THREE.Mesh>(null);
const startTime = useRef<number>(0);
useEffect(() => {
if (startTime.current === 0) {
startTime.current = Date.now();
}
}, []);
useFrame(() => {
if (!meshRef.current) return;
const elapsed = (Date.now() - startTime.current) / 1000;
const progress = Math.min(elapsed / 0.5, 1); // 0.5 second duration
const radius = progress * maxRadius;
const tempScale = ThreeObjectPools.vector3.acquire();
try {
tempScale.setScalar(radius);
meshRef.current.scale.copy(tempScale);
} finally {
ThreeObjectPools.vector3.release(tempScale);
}
const material = meshRef.current.material as THREE.MeshBasicMaterial;
material.opacity = 1 - progress;
});
return (
<mesh ref={meshRef} position={position} rotation={[Math.PI / 2, 0, 0]}>
<ringGeometry args={[0.9, 1.0, 32]} />
<meshBasicMaterial
color={color}
transparent
opacity={1}
side={THREE.DoubleSide}
/>
</mesh>
);
};
/**
* Floating damage number
*/
const DamageNumber: React.FC<{
position: [number, number, number];
damage: number;
type: "perfect" | "normal" | "miss";
isMobile: boolean;
onComplete: () => void;
}> = ({ position, damage, type, isMobile, onComplete }) => {
const [offset, setOffset] = useState(0);
const [opacity, setOpacity] = useState(1);
const startTime = useRef<number>(0);
const completedRef = useRef(false);
useEffect(() => {
if (startTime.current === 0) {
startTime.current = Date.now();
}
}, []);
useFrame(() => {
const elapsed = (Date.now() - startTime.current) / 1000;
const progress = Math.min(elapsed / 1.5, 1); // 1.5 second duration
setOffset(progress * 1);
setOpacity(1 - progress);
if (progress >= 1 && !completedRef.current && onComplete) {
completedRef.current = true;
onComplete();
}
});
const color =
type === "perfect" ? "#ffd700" : type === "normal" ? "#00ffff" : "#ff4444";
const text = type === "miss" ? "빗나감 | MISS" : `${damage}`; // Korean: 빗나감 = miss/deflected
return (
<Html
position={[position[0], position[1] + offset, position[2]]}
center
distanceFactor={10}
style={{ pointerEvents: "none", opacity }}
>
<div
style={{
fontSize: isMobile ? "20px" : "28px",
fontWeight: "bold",
fontFamily: FONT_FAMILY.KOREAN,
color,
textShadow: "0 0 10px rgba(0, 0, 0, 0.8)",
whiteSpace: "nowrap",
}}
data-testid="damage-number"
>
{text}
</div>
</Html>
);
};
/**
* HitFeedbackEffect3D Component
* Main hit feedback visualization
*/
export const HitFeedbackEffect3D: React.FC<HitFeedbackEffect3DProps> = ({
position,
type,
damage,
visible = true,
onComplete,
duration = 1500,
isMobile = false,
}) => {
const [showEffect, setShowEffect] = useState(visible);
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const effectColor = useMemo(() => {
switch (type) {
case "perfect":
return KOREAN_COLORS.ACCENT_GOLD;
case "success":
return KOREAN_COLORS.PRIMARY_CYAN;
case "miss":
return KOREAN_COLORS.TEXT_SECONDARY;
default:
return KOREAN_COLORS.PRIMARY_CYAN;
}
}, [type]);
const particleCount = useMemo(() => {
const isPerfect = type === "perfect";
const isSuccess = type === "success";
if (isMobile) {
return isPerfect ? 30 : isSuccess ? 20 : 10;
}
return isPerfect ? 80 : isSuccess ? 50 : 25;
}, [type, isMobile]);
const ringRadius = type === "perfect" ? 1.5 : 1.0;
const completedRef = useRef(false);
const handleComplete = useCallback(() => {
if (completedRef.current) return;
completedRef.current = true;
setShowEffect(false);
onComplete?.();
}, [onComplete]);
useEffect(() => {
if (visible && showEffect) {
timeoutRef.current = setTimeout(handleComplete, duration);
}
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, [visible, showEffect, duration, handleComplete]);
if (!showEffect) return null;
return (
<group name={`hit-feedback-${type}`}>
{/* Particle burst */}
<ImpactParticles
position={position}
color={effectColor}
count={particleCount}
/>
{/* Expanding ring */}
<RingEffect
position={position}
color={effectColor}
maxRadius={ringRadius}
/>
{/* Floating damage number */}
{damage !== undefined && type !== "miss" && (
<DamageNumber
position={position}
damage={damage}
type={type === "perfect" ? "perfect" : "normal"}
isMobile={isMobile}
onComplete={handleComplete}
/>
)}
{/* Miss indicator */}
{type === "miss" && (
<DamageNumber
position={position}
damage={0}
type="miss"
isMobile={isMobile}
onComplete={handleComplete}
/>
)}
</group>
);
};
export default HitFeedbackEffect3D;
|