shot card

This commit is contained in:
tresabhi committed 2026-11-09 22:46:59 -06:00
1 parent 7ff386cecc
commit e6158dec8d
17 files changed
+373 -1158

No files matched your search

@@ -0,0 +1,85 @@
.cardWrapper {
pointer-events: none;
/* keeps the card clear of the tracer, on the side it isn't heading toward */
&[data-going-up="true"] {
transform: translateY(50%);
margin-top: 4rem;
padding-top: 1rem;
}
&[data-going-up="false"] {
transform: translateY(-50%);
margin-bottom: 4rem;
padding-bottom: 1rem;
}
}
.point {
opacity: 0.75;
border-radius: 50%;
}
.card {
display: flex;
flex-direction: column;
gap: var(--space-4);
padding: var(--space-4);
border-radius: var(--space-3);
border: 1px solid var(--gray-a4);
background-color: var(--gray-2);
white-space: nowrap;
}
.section {
display: flex;
flex-direction: column;
gap: var(--space-3);
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
gap: var(--space-4);
}
.layers {
display: table;
/* column-gap doesn't apply to tables; border-spacing also pads the outer
edges, so pull them back in */
border-spacing: var(--space-3) var(--space-2);
margin: 0 calc(-1 * var(--space-3));
}
.row {
display: table-row;
}
.cell {
display: table-cell;
&[data-align="right"] {
text-align: right;
}
}
.divider {
display: flex;
align-items: center;
gap: var(--space-3);
/* bleed to the card edges */
margin: 0 calc(-1 * var(--space-4));
&::before,
&::after {
content: "";
flex: 1;
height: 1px;
background-color: var(--gray-a4);
}
}
@@ -0,0 +1,501 @@
import { J_HAT, K_HAT } from "@blitzkit/core";
import { literals } from "@blitzkit/i18n/src/literals";
import { Html } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import { clamp } from "lodash-es";
import { useEffect, useRef, type ComponentProps, type RefObject } from "react";
import {
BufferGeometry,
DoubleSide,
Euler,
Group,
Line,
LineDashedMaterial,
Mesh,
MeshBasicMaterial,
Path,
Quaternion,
} from "three";
import { radToDeg } from "three/src/math/MathUtils.js";
import { isHalloween } from "../../core/blitzkit/isHalloween";
import { LocaleProvider, useLocale } from "../../hooks/useLocale";
import { useStrings } from "../../hooks/useStrings";
import {
Tankopedia,
type Shot,
type ShotLayer,
type ShotLayerNonExternal,
type ShotStatus,
} from "../../stores/tankopedia";
import { classNames } from "../../ui/classNames";
import { ArmorType } from "../SpacedArmorScene";
import { Text, type TextProps } from "../Text";
import styles from "./index.module.css";
export const shotStatusColors: Record<ShotStatus, TextProps["color"]> = {
blocked: "red",
penetration: "green",
ricochet: "yellow",
splash: "orange",
};
export const layerTypeNames = {
[ArmorType.Primary]: "primary",
[ArmorType.Spaced]: "spaced",
[ArmorType.External]: "external",
null: "gap",
} as const;
interface LayerEntryProps {
layerIndex?: string;
color: TextProps["color"];
layerName: string;
layer: ShotLayer;
angle?: number;
}
function LayerEntry({
layerIndex,
color,
layerName,
layer,
angle,
}: LayerEntryProps) {
const locale = useLocale();
const strings = useStrings();
const { stats } = strings.tanks.shot_card;
const format = (value: number) => Math.round(value).toLocaleString(locale);
const mm = (value: number) =>
literals(strings.common.units.mm, { value: format(value) });
return (
<div className={styles.row}>
<div className={styles.cell}>
<Text size="minor" lowContrast>
{layerIndex}
</Text>
</div>
<div className={styles.cell}>
<Text color={color}>{layerName}</Text>
</div>
<div className={styles.cell}>
<Text color={color}>
{layer.type === null && mm(layer.distance * 1000)}
{layer.type === ArmorType.External && mm(layer.thickness)}
{(layer.type === ArmorType.Primary ||
layer.type === ArmorType.Spaced) &&
(angle === undefined
? mm(layer.thicknessAngled)
: literals(stats.thickness_and_angle, {
thickness: format(layer.thicknessAngled),
angle: radToDeg(angle).toFixed(0),
}))}
</Text>
</div>
<div className={styles.cell} data-align="right">
{layer.type === null && (
<Text lowContrast>
{literals(stats.ricochet_loss, {
loss: Math.max(-100, -50 * layer.distance).toFixed(0),
})}
</Text>
)}
{(layer.type === ArmorType.Primary ||
layer.type === ArmorType.Spaced) && (
<Text lowContrast>
{literals(stats.nominal, { thickness: format(layer.thickness) })}
</Text>
)}
</div>
</div>
);
}
interface LayersProps {
layers: ShotLayer[];
status: Shot["in"]["status"];
containsGaps: boolean;
/** Offset applied to displayed layer numbers (for post-ricochet layers). */
offset?: number;
}
function Layers({ layers, status, containsGaps, offset = 0 }: LayersProps) {
const strings = useStrings();
return (
<div className={styles.layers}>
{layers.map((layer, index) => {
const shiftedIndex = index + offset;
return (
<LayerEntry
key={index}
layer={layer}
layerName={
strings.tanks.shot_card.element[
layer.type === ArmorType.External
? layer.variant
: layerTypeNames[`${layer.type}`]
]
}
layerIndex={
layer.type === null
? undefined
: `${(containsGaps ? shiftedIndex / 2 : shiftedIndex) + 1}.`
}
color={
status === "splash"
? shotStatusColors.splash
: shotStatusColors[layer.status]
}
angle={
layer.type === ArmorType.Primary ||
layer.type === ArmorType.Spaced
? layer.angle
: undefined
}
/>
);
})}
</div>
);
}
interface ShotCardHtmlProps extends ComponentProps<"div"> {
shot: Shot;
}
function ShotCardHtml({ shot, className, ...props }: ShotCardHtmlProps) {
const locale = useLocale();
const strings = useStrings();
if (!shot) return null;
const { status, stats } = strings.tanks.shot_card;
const damage = Math.round(shot.damage).toLocaleString(locale);
const hasOutLayers = shot.out !== undefined && shot.out.layers.length > 0;
const penetrationChance = shot.penetrationChance !== undefined && (
<Text lowContrast weight="bold">
{literals(strings.common.units.percentage, {
value: (shot.penetrationChance * 100).toFixed(0),
})}
</Text>
);
return (
<div className={classNames(styles.card, className)} {...props}>
<div className={styles.section}>
<div className={styles.header}>
<Text color={shotStatusColors[shot.in.status]} weight="bold">
{literals(status[shot.in.status], { damage })}
</Text>
{!hasOutLayers && penetrationChance}
</div>
<Layers
layers={shot.in.layers}
status={shot.in.status}
containsGaps={shot.containsGaps}
/>
</div>
{shot.out && hasOutLayers && (
<>
<div className={styles.divider}>
<Text size="minor" lowContrast>
{stats.ricochet_divider}
</Text>
</div>
<div className={styles.section}>
<div className={styles.header}>
<Text color={shotStatusColors[shot.out.status]} weight="bold">
{literals(status[shot.out.status], { damage })}
</Text>
{penetrationChance}
</div>
<Layers
layers={shot.out.layers}
status={shot.out.status}
containsGaps={shot.containsGaps}
offset={shot.in.layers.length}
/>
</div>
</>
)}
</div>
);
}
const SURFACE_POINT_SIZE = 12;
const SHOT_DISPLAY_LENGTH_INFINITY = 4;
const TRACER_THICK = 1 / 32;
const TRACER_THIN = TRACER_THICK / 2;
export function ShotCard() {
const shot = Tankopedia.use((state) => state.shot);
const inTracer = useRef<Mesh>(null);
const outTracer = useRef<Mesh>(null);
const splashRadiusWrapper = useRef<Group>(null);
const splashRadiusMaterial = useRef<MeshBasicMaterial>(null);
const locale = useLocale();
useEffect(() => {
if (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);
};
}
});
useEffect(() => {
if (
isHalloween() &&
shot?.splashRadius !== undefined &&
shot.in.status === "penetration"
) {
const audio = new Audio("/assets/audio/lotta-damage.mp3");
audio.currentTime = 0.7;
audio.volume = 0.25;
audio.play();
}
}, [shot]);
if (!shot) return null;
const inLength =
shot.in.layers.reduce((accumulator, layer) => {
if (layer.type === null) return accumulator + layer.distance;
return accumulator;
}, 0) + SHOT_DISPLAY_LENGTH_INFINITY;
const outLength =
shot.out && shot.out.layers.length > 0
? (shot.in.layers.at(-1) as ShotLayerNonExternal).point.distanceTo(
(shot.out.layers.at(-1) as ShotLayerNonExternal).point,
)
: SHOT_DISPLAY_LENGTH_INFINITY;
const inLast = shot.in.layers.findLast(
(layer) => layer.type !== null,
) as ShotLayerNonExternal;
const outTitleColor = shot.out
? shotStatusColors[shot.out.status]
: undefined;
const inTitleColor = shotStatusColors[shot.in.status];
const tracerGoingUp =
shot.in.surfaceNormal.y + (shot.out?.surfaceNormal.y ?? 0) > 0;
return (
<group>
<Animator
inTracer={inTracer}
outTracer={outTracer}
inLength={inLength}
outLength={outLength}
splashRadiusWrapper={splashRadiusWrapper}
splashRadiusMaterial={splashRadiusMaterial}
/>
{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>
)}
<Html position={inLast.point} center>
<div className={styles.cardWrapper} data-going-up={tracerGoingUp}>
<LocaleProvider locale={locale}>
<ShotCardHtml shot={shot} />
</LocaleProvider>
</div>
</Html>
{[...shot.in.layers, ...(shot.out?.layers ?? [])].map((layer, index) => {
if (layer.type === null) return null;
const shotStatusColor =
shot.in.status === "splash"
? shotStatusColors.splash
: shotStatusColors[layer.status];
return (
<Html
position={layer.point}
key={index}
style={{
pointerEvents: "none",
transform: "translateY(-50%)",
}}
>
<div
className={styles.point}
style={{
width: SURFACE_POINT_SIZE,
height: SURFACE_POINT_SIZE,
marginLeft: -SURFACE_POINT_SIZE / 2,
backgroundColor: shotStatusColor,
}}
/>
</Html>
);
})}
<group
position={inLast.point}
rotation={new Euler().setFromQuaternion(
new Quaternion().setFromUnitVectors(J_HAT, shot.in.surfaceNormal),
)}
>
<mesh position={[0, inLength / 2, 0]} renderOrder={2}>
<cylinderGeometry
args={[TRACER_THIN / 2, TRACER_THIN / 2, inLength]}
/>
<meshBasicMaterial depthTest={false} transparent />
</mesh>
<mesh ref={inTracer} position={[0, inLength / 2, 0]} renderOrder={3}>
<cylinderGeometry
args={[TRACER_THICK / 2, TRACER_THICK / 2, inLength]}
/>
<meshBasicMaterial
color={inTitleColor}
depthTest={false}
transparent
/>
</mesh>
</group>
{shot.out && (
<group
position={inLast.point}
rotation={new Euler().setFromQuaternion(
new Quaternion().setFromUnitVectors(
J_HAT,
shot.out.surfaceNormal.clone(),
),
)}
>
<mesh position={[0, outLength / 2, 0]} renderOrder={2}>
<cylinderGeometry
args={[TRACER_THIN / 2, TRACER_THIN / 2, outLength]}
/>
<meshBasicMaterial depthTest={false} transparent />
</mesh>
<mesh
ref={outTracer}
position={[0, outLength / 2, 0]}
renderOrder={3}
>
<cylinderGeometry
args={[TRACER_THICK / 2, TRACER_THICK / 2, outLength]}
/>
<meshBasicMaterial
color={outTitleColor}
depthTest={false}
transparent
/>
</mesh>
</group>
)}
</group>
);
}
interface AnimatorProps {
inTracer: RefObject<Mesh | null>;
outTracer: RefObject<Mesh | null>;
splashRadiusWrapper: RefObject<Group | null>;
splashRadiusMaterial: RefObject<MeshBasicMaterial | null>;
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);
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();
}
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;
invalidate();
}
});
return null;
}