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

287 lines
8.0 KiB
TypeScript
Raw Normal View History

2024-09-22 21:27:46 -05:00
import { J_HAT, K_HAT } from '@blitzkit/core';
import { Box, Flex, Text } from '@radix-ui/themes';
2024-02-28 22:29:21 -06:00
import { Html } from '@react-three/drei';
2024-11-10 22:19:05 -06:00
import { invalidate, useFrame } from '@react-three/fiber';
2024-09-14 19:43:14 -05:00
import { clamp } from 'lodash-es';
2024-09-22 21:12:18 -05:00
import { useEffect, useRef, type ComponentProps } from 'react';
2024-07-23 21:01:32 -05:00
import {
BufferGeometry,
DoubleSide,
Euler,
Group,
Line,
LineDashedMaterial,
Mesh,
MeshBasicMaterial,
Path,
Quaternion,
} from 'three';
2025-01-13 20:56:30 -06:00
import { LocaleProvider, useLocale } from '../../../hooks/useLocale';
2024-09-22 21:12:18 -05:00
import {
TankopediaEphemeral,
type ShotLayerNonExternal,
type ShotStatus,
} from '../../../stores/tankopediaEphemeral';
2024-06-28 20:41:24 -05:00
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,
2024-05-25 14:21:36 -05:00
ComponentProps<typeof Text>['color']
> = {
blocked: 'red',
penetration: 'green',
ricochet: 'yellow',
splash: 'orange',
};
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() {
2024-07-09 14:43:33 -05:00
const shot = TankopediaEphemeral.use((state) => state.shot);
2024-05-26 13:42:51 -05:00
const inTracer = useRef<Mesh>(null);
2024-05-26 14:39:16 -05:00
const outTracer = useRef<Mesh>(null);
2024-07-23 21:01:32 -05:00
const splashRadiusWrapper = useRef<Group>(null);
const splashRadiusMaterial = useRef<MeshBasicMaterial>(null);
let animationStartTime: number | null = 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(
new Path().absarc(0, 0, 1, 0, Math.PI * 2).getSpacedPoints(50),
);
const outlineMaterial = new LineDashedMaterial({
color: 'orange',
dashSize: 0.05,
gapSize: 0.025,
});
const outline = new Line(
outlineGeometry,
outlineMaterial,
).computeLineDistances();
splashRadiusWrapper.current?.add(outline);
return () => {
splashRadiusWrapper.current?.remove(outline);
};
}
});
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
const data = new Date();
const isHalloween = data.getMonth() === 9 && data.getDate() === 31;
if (
isHalloween &&
shot?.splashRadius !== undefined &&
shot.in.status === 'penetration'
) {
2024-07-24 13:27:46 -05:00
const audio = new Audio('/assets/audio/lotta-damage.mp3');
audio.currentTime = 0.7;
audio.volume = 0.25;
audio.play();
}
}, [shot]);
2024-05-26 13:42:51 -05:00
useFrame(({ clock }) => {
2024-07-23 21:01:32 -05:00
if (!shot) return;
2024-05-26 13:42:51 -05:00
2024-07-23 21:01:32 -05:00
const t = clock.elapsedTime;
2024-05-26 13:42:51 -05:00
2024-07-23 21:01:32 -05:00
if (inTracer.current) {
const tracerT1 = t % 1;
const tracerT2 = (t + 0.5) % 1;
2024-05-26 14:39:16 -05:00
2024-07-23 21:01:32 -05:00
inTracer.current.scale.set(1, 1 - 2 * Math.abs(tracerT1 - 0.5), 1);
inTracer.current.position.set(0, (1 - tracerT1) * inLength, 0);
2024-05-26 14:39:16 -05:00
2024-07-23 21:01:32 -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);
}
2024-10-24 15:14:34 -05:00
2024-11-10 22:19:05 -06:00
invalidate();
2024-07-23 21:01:32 -05:00
}
if (
shot.splashRadius !== undefined &&
splashRadiusWrapper.current &&
splashRadiusMaterial.current
) {
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;
2024-10-24 15:14:34 -05:00
2024-11-10 22:19:05 -06:00
invalidate();
2024-07-23 21:01:32 -05:00
}
2024-05-26 13:42:51 -05:00
});
2024-02-29 15:58:17 -06:00
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(
(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(
(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>
2024-07-23 21:01:32 -05:00
{shot.splashRadius && (
<group
position={inLast.point}
ref={splashRadiusWrapper}
rotation={new Euler().setFromQuaternion(
new Quaternion().setFromUnitVectors(K_HAT, shot.in.surfaceNormal),
)}
>
<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={{
pointerEvents: 'none',
transform: `translateY(${tracerGoingUp ? 50 : -50}%)`,
2024-06-28 20:41:24 -05:00
}}
2024-07-29 20:18:31 -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 =
shot.in.status === 'splash'
? 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={{
2024-06-03 15:56:02 -05:00
pointerEvents: 'none',
2024-06-03 13:57:15 -05:00
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(
2024-05-26 14:39:16 -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,
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
);
}