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

262 lines
7.2 KiB
TypeScript
Raw Normal View History

2024-06-28 20:41:24 -05:00
import { Box, Flex, Text } from '@radix-ui/themes';
2024-02-28 22:29:21 -06:00
import { Html } from '@react-three/drei';
2024-05-26 13:42:51 -05:00
import { useFrame } from '@react-three/fiber';
2024-07-23 21:01:32 -05:00
import { clamp } from 'lodash';
import { ComponentProps, useEffect, useRef } from 'react';
import {
BufferGeometry,
DoubleSide,
Euler,
Group,
Line,
LineDashedMaterial,
Mesh,
MeshBasicMaterial,
Path,
Quaternion,
} from 'three';
import { J_HAT, K_HAT } from '../../../constants/axis';
2024-07-09 14:43:33 -05:00
import * as TankopediaEphemeral 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-07-09 14:43:33 -05:00
TankopediaEphemeral.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;
useEffect(() => {
if (shot && shot.splashRadius !== undefined) {
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
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);
}
}
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-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-07-09 14:43:33 -05:00
? (
shot.in.layers.at(-1) as TankopediaEphemeral.ShotLayerNonExternal
).point.distanceTo(
(shot.out.layers.at(-1) as TankopediaEphemeral.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-07-09 14:43:33 -05:00
) as TankopediaEphemeral.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-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-02-29 13:17:41 -06:00
<Html
2024-05-26 13:42:51 -05:00
position={inLast.point}
2024-02-29 13:17:41 -06:00
style={{
pointerEvents: 'none',
2024-05-25 14:21:36 -05:00
transform: 'translateY(-50%)',
2024-02-29 13:17:41 -06:00
}}
>
2024-06-28 20:41:24 -05:00
<Box
pl="9"
display={{
initial: 'none',
sm: 'block',
}}
>
2024-07-13 19:49:00 -05:00
<ShotDisplayCard shot={shot} />
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-06-03 13:57:15 -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-05-25 14:21:36 -05:00
</group>
2024-02-28 22:29:21 -06:00
);
}