Files
blitzkit/packages/website/src/components/Armor/components/ShotDisplay.tsx
T

317 lines
8.5 KiB
TypeScript
Raw Normal View History

2025-09-01 22:56:39 -05:00
import { J_HAT, K_HAT } from "@blitzkit/core";
import { Box, Flex, Text } from "@radix-ui/themes";
import { Html } from "@react-three/drei";
2026-06-26 13:31:40 -05:00
import { useFrame } from "@react-three/fiber";
2025-09-01 22:56:39 -05:00
import { clamp } from "lodash-es";
2026-06-26 13:31:40 -05:00
import { useEffect, useRef, type ComponentProps, type RefObject } from "react";
2024-07-23 21:01:32 -05:00
import {
BufferGeometry,
DoubleSide,
Euler,
Group,
Line,
LineDashedMaterial,
Mesh,
MeshBasicMaterial,
Path,
Quaternion,
2025-09-01 22:56:39 -05:00
} from "three";
import { isHalloween } from "../../../core/blitzkit/isHalloween";
import { LocaleProvider, useLocale } from "../../../hooks/useLocale";
2024-09-22 21:12:18 -05:00
import {
2025-09-01 22:56:39 -05:00
Tankopedia,
2024-09-22 21:12:18 -05:00
type ShotLayerNonExternal,
type ShotStatus,
2025-09-01 22:56:39 -05:00
} from "../../../stores/tankopedia";
import { ShotDisplayCard } from "./ShotDisplayCard";
2024-02-28 22:29:21 -06:00
2024-06-28 20:41:24 -05:00
export const shotStatusColors: Record<
2024-09-22 21:12:18 -05:00
ShotStatus,
2025-09-01 22:56:39 -05:00
ComponentProps<typeof Text>["color"]
2024-05-25 14:21:36 -05:00
> = {
2025-09-01 22:56:39 -05:00
blocked: "red",
penetration: "green",
ricochet: "yellow",
splash: "orange",
2024-05-25 14:21:36 -05:00
};
2024-06-03 13:57:15 -05:00
const SURFACE_POINT_SIZE = 12;
2024-06-28 20:41:24 -05:00
export const SHOT_DISPLAY_LENGTH_INFINITY = 4;
2024-05-27 20:42:48 -05:00
const TRACER_THICK = 1 / 32;
2024-02-29 15:30:53 -06:00
const TRACER_THIN = TRACER_THICK / 2;
2024-02-29 10:30:48 -06:00
2024-02-28 23:06:37 -06:00
export function ShotDisplay() {
2025-09-01 22:56:39 -05:00
const shot = Tankopedia.use((state) => state.shot);
2026-06-26 13:31:40 -05:00
const inTracer = useRef<Mesh>(null!);
const outTracer = useRef<Mesh>(null!);
const splashRadiusWrapper = useRef<Group>(null!);
const splashRadiusMaterial = useRef<MeshBasicMaterial>(null!);
2025-01-13 20:56:30 -06:00
const { locale } = useLocale();
2024-07-23 21:01:32 -05:00
useEffect(() => {
2024-07-24 13:27:46 -05:00
if (shot?.splashRadius !== undefined) {
2024-07-23 21:01:32 -05:00
const outlineGeometry = new BufferGeometry().setFromPoints(
2026-04-03 12:31:46 -05:00
new Path().absarc(0, 0, 1, 0, Math.PI * 2).getSpacedPoints(50),
2024-07-23 21:01:32 -05:00
);
const outlineMaterial = new LineDashedMaterial({
2025-09-01 22:56:39 -05:00
color: "orange",
2024-07-23 21:01:32 -05:00
dashSize: 0.05,
gapSize: 0.025,
});
const outline = new Line(
outlineGeometry,
2026-04-03 12:31:46 -05:00
outlineMaterial,
2024-07-23 21:01:32 -05:00
).computeLineDistances();
splashRadiusWrapper.current?.add(outline);
2024-07-23 21:01:32 -05:00
return () => {
splashRadiusWrapper.current?.remove(outline);
2024-07-23 21:01:32 -05:00
};
}
});
2024-05-26 13:42:51 -05:00
2024-07-24 13:27:46 -05:00
useEffect(() => {
2024-07-24 13:30:50 -05:00
if (
2025-08-29 13:21:41 -05:00
isHalloween() &&
2024-07-24 13:30:50 -05:00
shot?.splashRadius !== undefined &&
2025-09-01 22:56:39 -05:00
shot.in.status === "penetration"
2024-07-24 13:30:50 -05:00
) {
2025-09-01 22:56:39 -05:00
const audio = new Audio("/assets/audio/lotta-damage.mp3");
2024-07-24 13:27:46 -05:00
audio.currentTime = 0.7;
audio.volume = 0.25;
audio.play();
}
}, [shot]);
2024-02-29 13:17:41 -06:00
if (!shot) return null;
2024-02-29 10:30:48 -06:00
2024-05-26 13:42:51 -05:00
const inLength =
shot.in.layers.reduce((accumulator, layer) => {
if (layer.type === null) return accumulator + layer.distance;
return accumulator;
2024-06-28 20:41:24 -05:00
}, 0) + SHOT_DISPLAY_LENGTH_INFINITY;
2024-05-27 20:42:48 -05:00
const outLength =
shot.out && shot.out.layers.length > 0
2024-09-22 21:12:18 -05:00
? (shot.in.layers.at(-1) as ShotLayerNonExternal).point.distanceTo(
2026-04-03 12:31:46 -05:00
(shot.out.layers.at(-1) as ShotLayerNonExternal).point,
2024-05-27 20:42:48 -05:00
)
2024-06-28 20:41:24 -05:00
: SHOT_DISPLAY_LENGTH_INFINITY;
2024-05-27 21:08:05 -05:00
const inLast = shot.in.layers.findLast(
2026-04-03 12:31:46 -05:00
(layer) => layer.type !== null,
2024-09-22 21:12:18 -05:00
) as ShotLayerNonExternal;
2024-05-27 20:57:04 -05:00
const outTitleColor = shot.out
? shotStatusColors[shot.out.status]
: undefined;
const inTitleColor = shotStatusColors[shot.in.status];
2024-07-29 20:18:31 -05:00
const tracerGoingUp =
shot.in.surfaceNormal.y + (shot.out?.surfaceNormal.y ?? 0) > 0;
2024-05-26 13:42:51 -05:00
2024-02-28 22:29:21 -06:00
return (
2024-05-25 14:21:36 -05:00
<group>
2026-06-26 13:31:40 -05:00
<Animator
inTracer={inTracer}
outTracer={outTracer}
inLength={inLength}
outLength={outLength}
splashRadiusWrapper={splashRadiusWrapper}
splashRadiusMaterial={splashRadiusMaterial}
/>
2024-07-23 21:01:32 -05:00
{shot.splashRadius && (
<group
position={inLast.point}
ref={splashRadiusWrapper}
rotation={new Euler().setFromQuaternion(
2026-04-03 12:31:46 -05:00
new Quaternion().setFromUnitVectors(K_HAT, shot.in.surfaceNormal),
2024-07-23 21:01:32 -05:00
)}
>
<mesh>
<circleGeometry />
<meshBasicMaterial
side={DoubleSide}
color="orange"
transparent
ref={splashRadiusMaterial}
/>
</mesh>
</group>
)}
2024-07-29 20:18:31 -05:00
<Html position={inLast.point} center>
2024-06-28 20:41:24 -05:00
<Box
2024-07-29 20:18:31 -05:00
style={{
2025-09-01 22:56:39 -05:00
pointerEvents: "none",
2024-07-29 20:18:31 -05:00
transform: `translateY(${tracerGoingUp ? 50 : -50}%)`,
2024-06-28 20:41:24 -05:00
}}
2025-09-01 22:56:39 -05:00
mb={tracerGoingUp ? "0" : "9"}
pb={tracerGoingUp ? "0" : "4"}
mt={tracerGoingUp ? "9" : "0"}
pt={tracerGoingUp ? "4" : "0"}
2024-06-28 20:41:24 -05:00
>
2025-01-13 20:56:30 -06:00
<LocaleProvider locale={locale}>
<ShotDisplayCard shot={shot} />
</LocaleProvider>
2024-06-28 20:41:24 -05:00
</Box>
2024-02-29 13:17:41 -06:00
</Html>
2024-05-26 13:42:51 -05:00
2024-09-22 21:27:46 -05:00
{[...shot.in.layers, ...(shot.out?.layers ?? [])].map((layer, index) => {
2024-05-26 13:42:51 -05:00
if (layer.type === null) return null;
const shotStatusColor =
2025-09-01 22:56:39 -05:00
shot.in.status === "splash"
2024-05-26 13:42:51 -05:00
? shotStatusColors.splash
: shotStatusColors[layer.status];
return (
2024-05-27 20:42:48 -05:00
<Html
position={layer.point}
2024-06-03 13:57:15 -05:00
key={index}
2024-05-27 20:42:48 -05:00
style={{
2025-09-01 22:56:39 -05:00
pointerEvents: "none",
transform: "translateY(-50%)",
2024-05-27 20:42:48 -05:00
}}
>
2024-06-03 13:57:15 -05:00
<Flex
style={{
marginLeft: -SURFACE_POINT_SIZE / 2,
}}
>
<div
style={{
width: SURFACE_POINT_SIZE,
height: SURFACE_POINT_SIZE,
backgroundColor: shotStatusColor,
opacity: 0.75,
borderRadius: SURFACE_POINT_SIZE / 2,
}}
/>
</Flex>
2024-05-26 13:42:51 -05:00
</Html>
);
})}
<group
position={inLast.point}
rotation={new Euler().setFromQuaternion(
2026-04-03 12:31:46 -05:00
new Quaternion().setFromUnitVectors(J_HAT, shot.in.surfaceNormal),
2024-05-26 13:42:51 -05:00
)}
>
2024-05-27 20:57:04 -05:00
<mesh position={[0, inLength / 2, 0]} renderOrder={2}>
2024-05-26 13:42:51 -05:00
<cylinderGeometry
args={[TRACER_THIN / 2, TRACER_THIN / 2, inLength]}
/>
2024-05-27 20:57:04 -05:00
<meshBasicMaterial depthTest={false} transparent />
2024-05-26 13:42:51 -05:00
</mesh>
2024-05-27 20:57:04 -05:00
<mesh ref={inTracer} position={[0, inLength / 2, 0]} renderOrder={3}>
2024-05-26 13:42:51 -05:00
<cylinderGeometry
args={[TRACER_THICK / 2, TRACER_THICK / 2, inLength]}
/>
2024-05-27 20:57:04 -05:00
<meshBasicMaterial
color={inTitleColor}
depthTest={false}
transparent
/>
2024-05-26 13:42:51 -05:00
</mesh>
</group>
2024-05-26 14:39:16 -05:00
{shot.out && (
<group
position={inLast.point}
rotation={new Euler().setFromQuaternion(
2024-05-27 20:42:48 -05:00
new Quaternion().setFromUnitVectors(
J_HAT,
2026-04-03 12:31:46 -05:00
shot.out.surfaceNormal.clone(),
),
2024-05-26 14:39:16 -05:00
)}
>
2024-05-27 20:57:04 -05:00
<mesh position={[0, outLength / 2, 0]} renderOrder={2}>
2024-05-26 14:39:16 -05:00
<cylinderGeometry
2024-05-27 20:42:48 -05:00
args={[TRACER_THIN / 2, TRACER_THIN / 2, outLength]}
2024-05-26 14:39:16 -05:00
/>
2024-05-27 20:57:04 -05:00
<meshBasicMaterial depthTest={false} transparent />
2024-05-26 14:39:16 -05:00
</mesh>
2024-05-27 20:57:04 -05:00
<mesh
ref={outTracer}
position={[0, outLength / 2, 0]}
renderOrder={3}
>
2024-05-26 14:39:16 -05:00
<cylinderGeometry
2024-05-27 20:42:48 -05:00
args={[TRACER_THICK / 2, TRACER_THICK / 2, outLength]}
2024-05-26 14:39:16 -05:00
/>
2024-05-27 20:57:04 -05:00
<meshBasicMaterial
color={outTitleColor}
depthTest={false}
transparent
/>
2024-05-26 14:39:16 -05:00
</mesh>
</group>
2024-09-22 21:27:46 -05:00
)}
2024-05-25 14:21:36 -05:00
</group>
2024-02-28 22:29:21 -06:00
);
}
2026-06-26 13:31:40 -05:00
interface AnimatorProps {
inTracer: RefObject<Mesh>;
outTracer: RefObject<Mesh>;
splashRadiusWrapper: RefObject<Group>;
splashRadiusMaterial: RefObject<MeshBasicMaterial>;
inLength: number;
outLength: number;
}
function Animator({
inTracer,
outTracer,
splashRadiusWrapper,
splashRadiusMaterial,
inLength,
outLength,
}: AnimatorProps) {
const shot = Tankopedia.use((state) => state.shot!);
let animationStartTime: number | null = null;
useFrame(({ clock, invalidate }) => {
const t = clock.elapsedTime;
const tracerT1 = t % 1;
const tracerT2 = (t + 0.5) % 1;
if (inTracer.current) {
inTracer.current.scale.set(1, 1 - 2 * Math.abs(tracerT1 - 0.5), 1);
inTracer.current.position.set(0, (1 - tracerT1) * inLength, 0);
2026-06-26 13:31:40 -05:00
if (outTracer.current) {
outTracer.current.scale.set(1, 1 - 2 * Math.abs(tracerT2 - 0.5), 1);
outTracer.current.position.set(0, tracerT2 * outLength, 0);
}
invalidate();
2026-06-26 13:31:40 -05:00
}
if (
shot.splashRadius !== undefined &&
splashRadiusWrapper.current &&
splashRadiusMaterial.current
) {
2026-06-26 13:31:40 -05:00
if (animationStartTime === null) animationStartTime = t;
const scale =
clamp(4 * (t - animationStartTime), 0, 1) * shot.splashRadius;
const opacity = 1 - clamp(2 * (t - animationStartTime), 0, 1);
splashRadiusWrapper.current.scale.set(scale, scale, scale);
splashRadiusMaterial.current.opacity = opacity;
invalidate();
}
});
return null;
}