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

171 lines
5.0 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 {
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-11 16:49:54 -05:00
import { Code } from "../Code";
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}
alt=""
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
label={strings.tanks.characteristics.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
label={strings.tanks.characteristics.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
label={strings.tanks.characteristics.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
label={strings.tanks.characteristics.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"]}>
{strings.tanks.characteristics.toys.ricochet.no_ricochet}
</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
);
}