Files
blitzkit/packages/website/src/components/TankopediaToys/index.tsx
T

306 lines
8.8 KiB
TypeScript
Raw Normal View History

2026-10-11 16:49:54 -05:00
import { alias, isExplosive } from "@blitzkit/core";
import { literals } from "@blitzkit/i18n";
import {
2026-10-12 00:04:47 -05:00
useEffect,
2026-10-11 16:49:54 -05:00
useRef,
useState,
type CSSProperties,
type PointerEvent,
} from "react";
import { clamp, degToRad, radToDeg } from "three/src/math/MathUtils.js";
2026-10-11 16:27:53 -05:00
import type { ToyProps } from "../../config/toys";
import { DuelModule, useDuel } from "../../hooks/useDuel";
import { useStrings } from "../../hooks/useStrings";
2026-10-12 00:04:47 -05:00
import type { ComputedCharacteristics } from "../../types/characteristics";
import { Checkbox } from "../Checkbox";
2026-10-11 16:49:54 -05:00
import { Code } from "../Code";
2026-10-12 00:04:47 -05:00
import { SegmentedControl } from "../SegmentedControl";
2026-10-11 16:49:54 -05:00
import styles from "./index.module.css";
2026-10-11 16:27:53 -05:00
export function RicochetToy({ characteristics }: ToyProps) {
const strings = useStrings();
const shell = useDuel("protagonist", DuelModule.Shell);
const container = useRef<HTMLDivElement>(null);
2026-10-11 16:49:54 -05:00
const normalizationValue = characteristics.get("normalization");
const ricochetValue = characteristics.get("ricochet");
2026-10-11 16:27:53 -05:00
const normalization = degToRad(
2026-10-11 16:49:54 -05:00
typeof normalizationValue === "number" ? normalizationValue : 0,
);
const ricochet = degToRad(
typeof ricochetValue === "number" ? ricochetValue : 90,
2026-10-11 16:27:53 -05:00
);
const explodes = isExplosive(shell.type);
const [rawAngle, setAngle] = useState(degToRad(-25));
const angle = explodes ? degToRad(-70) : rawAngle;
const doesRicochet = Math.abs(angle) >= ricochet;
const effectiveAngle = doesRicochet
? -angle + Math.PI
: Math.sign(angle) * Math.max(0, Math.abs(angle) - normalization);
const armorPercentage = Math.min(999, 100 / Math.cos(effectiveAngle));
2026-10-11 16:49:54 -05:00
function updateAngle(event: PointerEvent<HTMLDivElement>) {
event.preventDefault();
if (!container.current || explodes) return;
const rect = container.current.getBoundingClientRect();
const x = event.clientX - rect.left - rect.width / 3;
const y = event.clientY - rect.top - rect.height / 2;
setAngle(clamp(Math.atan2(-y, x), -Math.PI / 2, Math.PI / 2));
}
2026-10-11 16:27:53 -05:00
return (
2026-10-11 16:49:54 -05:00
<div
ref={container}
className={styles.visualizer}
onPointerMove={updateAngle}
2026-10-11 16:27:53 -05:00
>
2026-10-11 16:49:54 -05:00
<div className={styles.armor}>
<div className={styles["armor-texture"]} />
</div>
2026-10-11 16:27:53 -05:00
2026-10-11 16:49:54 -05:00
<div
className={styles.zones}
style={
{
"--normalization-angle": `${normalization}rad`,
"--ricochet-angle": `${ricochet}rad`,
} as CSSProperties
}
2026-10-11 16:27:53 -05:00
/>
2026-10-11 16:49:54 -05:00
<div
className={styles["shell-path"]}
style={
{
"--rotation-angle": `${-Math.PI / 2 - angle}rad`,
} as CSSProperties
}
2026-10-11 16:27:53 -05:00
>
2026-10-11 16:49:54 -05:00
<div className={styles["shell-path-extension"]} />
<div className={styles["shell-path-line"]} />
2026-10-11 16:27:53 -05:00
<img
2026-10-11 16:49:54 -05:00
className={styles.shell}
2026-10-11 16:27:53 -05:00
src={alias("api", `/icons/shells/${shell.icon}.webp`)}
/>
2026-10-11 16:49:54 -05:00
</div>
2026-10-11 16:27:53 -05:00
2026-10-11 16:49:54 -05:00
<div
className={styles["effective-path"]}
style={
{
"--rotation-angle": `${-Math.PI / 2 - effectiveAngle}rad`,
} as CSSProperties
}
2026-10-11 16:27:53 -05:00
>
2026-10-11 16:49:54 -05:00
<div className={styles["effective-path-line"]} />
</div>
2026-10-11 16:27:53 -05:00
2026-10-11 16:49:54 -05:00
<CornerStat
2026-10-12 00:04:47 -05:00
label={strings.tanks.toys.ricochet.angle}
2026-10-11 16:27:53 -05:00
value={literals(strings.common.units.deg, {
value: radToDeg(Math.abs(angle)).toFixed(0),
})}
2026-10-11 16:49:54 -05:00
position={CornerStatPosition.TopLeft}
2026-10-11 16:27:53 -05:00
/>
2026-10-11 16:49:54 -05:00
<CornerStat
2026-10-12 00:04:47 -05:00
label={strings.tanks.toys.ricochet.normalization}
2026-10-11 16:27:53 -05:00
value={
doesRicochet
? "-"
: literals(strings.common.units.deg, {
value: radToDeg(
Math.min(normalization, Math.abs(angle)),
).toFixed(0),
})
}
2026-10-11 16:49:54 -05:00
position={CornerStatPosition.TopRight}
2026-10-11 16:27:53 -05:00
/>
2026-10-11 16:49:54 -05:00
<CornerStat
2026-10-12 00:04:47 -05:00
label={strings.tanks.toys.ricochet.effective}
2026-10-11 16:27:53 -05:00
value={
doesRicochet
? "-"
: literals(strings.common.units.deg, {
value: radToDeg(Math.abs(effectiveAngle)).toFixed(0),
})
}
2026-10-11 16:49:54 -05:00
position={CornerStatPosition.BottomLeft}
2026-10-11 16:27:53 -05:00
/>
2026-10-11 16:49:54 -05:00
<CornerStat
2026-10-12 00:04:47 -05:00
label={strings.tanks.toys.ricochet.armor}
2026-10-11 16:27:53 -05:00
value={
doesRicochet
? "-"
: literals(strings.common.units.percentage, {
value: armorPercentage.toFixed(0),
})
}
2026-10-11 16:49:54 -05:00
position={CornerStatPosition.BottomRight}
2026-10-11 16:27:53 -05:00
/>
{explodes && (
2026-10-11 16:49:54 -05:00
<div className={styles["explosive-overlay"]}>
2026-10-12 00:04:47 -05:00
{strings.tanks.toys.ricochet.no_ricochet}
2026-10-11 16:49:54 -05:00
</div>
2026-10-11 16:27:53 -05:00
)}
2026-10-11 16:49:54 -05:00
</div>
);
}
enum CornerStatPosition {
TopLeft = "top-left",
TopRight = "top-right",
BottomLeft = "bottom-left",
BottomRight = "bottom-right",
}
interface CornerStatProps {
label: string;
value: string;
position: CornerStatPosition;
}
function CornerStat({ label, value, position }: CornerStatProps) {
return (
<div className={styles["corner-stat"]} data-position={position}>
<span>{label}</span>
<Code className={styles["corner-stat-value"]} size="minor">
{value}
</Code>
</div>
2026-10-11 16:27:53 -05:00
);
}
2026-10-12 00:04:47 -05:00
const terrains = ["hard", "medium", "soft"] as const;
type Terrain = (typeof terrains)[number];
interface TraverseToyProps {
characteristics: ComputedCharacteristics;
}
export function TraverseToy({ characteristics }: TraverseToyProps) {
const hull = useRef<HTMLDivElement>(null);
const turret = useRef<HTMLImageElement>(null);
const [terrain, setTerrain] = useState<Terrain>("hard");
const [rotateHull, setRotateHull] = useState(true);
const [rotateTurret, setRotateTurret] = useState(true);
const turretAngle = useRef(0);
const hullAngle = useRef(0);
const strings = useStrings();
const hullSpeed = characteristics.get(
`hull_traverse_speed_${terrain}`,
) as number;
const turretTraverseSpeed = characteristics.get(
"turret_traverse_speed",
) as number;
const turretSpeed = rotateTurret ? turretTraverseSpeed : 0;
const hullSpeedEffective = rotateHull ? hullSpeed : 0;
const isTurretless =
characteristics.has("azimuth_left") || characteristics.has("azimuth_right");
useEffect(() => {
let cancel = false;
const turretSpeedRad = (turretSpeed * Math.PI) / 180;
const hullSpeedRad = (hullSpeedEffective * Math.PI) / 180;
let lastT = Date.now() / 1000;
function frame() {
const t = Date.now() / 1000;
const dt = t - lastT;
lastT = t;
if (turret.current) {
turretAngle.current =
(turretAngle.current + turretSpeedRad * dt) % (2 * Math.PI);
turret.current.style.transform = `translate(-50%, -50%) rotate(${-turretAngle.current}rad)`;
}
if (hull.current) {
hullAngle.current =
(hullAngle.current + hullSpeedRad * dt) % (2 * Math.PI);
hull.current.style.transform = `translate(-50%, -50%) rotate(${-hullAngle.current}rad)`;
}
if (!cancel) requestAnimationFrame(frame);
}
frame();
return () => {
cancel = true;
};
}, [hullSpeedEffective, turretSpeed]);
const terrainOptions = terrains.map((terrain) => ({
value: terrain,
label:
strings.tanks.characteristics.items[`hull_traverse_speed_${terrain}`],
}));
return (
<div className={styles["traverse-visualizer"]}>
<div
ref={hull}
className={styles["traverse-hull"]}
data-turretless={isTurretless}
>
<img
draggable="false"
className={styles["traverse-hull-image"]}
src={`/media/toys/hull${isTurretless ? "-only" : ""}.png`}
/>
{!isTurretless && (
<img
draggable="false"
ref={turret}
className={styles["traverse-turret"]}
src="/media/toys/turret.png"
/>
)}
</div>
<div className={styles["traverse-toggles"]}>
{!isTurretless && (
<>
<Checkbox checked={rotateHull} onCheckedChange={setRotateHull}>
{strings.tanks.toys.traverse.hull}
</Checkbox>
<Checkbox checked={rotateTurret} onCheckedChange={setRotateTurret}>
{strings.tanks.toys.traverse.turret}
</Checkbox>
</>
)}
</div>
<div className={styles["traverse-terrain"]}>
<SegmentedControl
options={terrainOptions}
value={terrain}
onValueChange={(value) => {
const nextTerrain = terrains.find((terrain) => terrain === value);
if (nextTerrain) setTerrain(nextTerrain);
}}
variant="underline"
/>
</div>
<div className={styles["traverse-effective"]}>
<span>{strings.tanks.toys.traverse.effective}</span>
<Code size="minor">
{literals(strings.units.deg_s, {
value: (turretSpeed + hullSpeedEffective).toFixed(1),
})}
</Code>
</div>
</div>
);
}