reload visualizer

This commit is contained in:
tresabhi committed 2026-10-12 10:34:01 -05:00
1 parent 1c4ef7c129
commit 3d8cfc3aeb
10 files changed
+450 -9

No files matched your search

@@ -1,7 +1,11 @@
import { alias, isExplosive } from "@blitzkit/core";
import { literals } from "@blitzkit/i18n";
import { DoubleArrowRightIcon } from "@radix-ui/react-icons";
import { times } from "lodash-es";
import {
useCallback,
useEffect,
useMemo,
useRef,
useState,
type CSSProperties,
@@ -12,9 +16,11 @@ import type { ToyProps } from "../../config/toys";
import { DuelModule, useDuel } from "../../hooks/useDuel";
import { useStrings } from "../../hooks/useStrings";
import type { ComputedCharacteristics } from "../../types/characteristics";
import { Button } from "../Button";
import { Checkbox } from "../Checkbox";
import { Code } from "../Code";
import { SegmentedControl } from "../SegmentedControl";
import { ShellBorder } from "../Tankopedia/CharacteristicsSection/components/Characteristics/components/ReloadVisualizer/components/ShellBorder";
import styles from "./index.module.css";
export function RicochetToy({ characteristics }: ToyProps) {
@@ -247,11 +253,7 @@ export function TraverseToy({ characteristics }: TraverseToyProps) {
return (
<div className={styles.traverse}>
<div
ref={hull}
className={styles.hull}
data-turretless={isTurretless}
>
<div ref={hull} className={styles.hull} data-turretless={isTurretless}>
<img
draggable="false"
className={styles.image}
@@ -304,3 +306,263 @@ export function TraverseToy({ characteristics }: TraverseToyProps) {
</div>
);
}
const RELOAD_PRECISION = 1;
interface ReloadData {
reloads: number[];
shells: number;
intraClip: number;
burstShells: number;
burstInterShell?: number;
}
export function ReloadToy({ characteristics }: ToyProps) {
const gun = useDuel("protagonist", DuelModule.Gun);
const strings = useStrings();
const reloadData = useMemo<ReloadData>(() => {
const reloadCoefficient = characteristics.get(
"_reload_coefficient",
) as number;
const intraClip =
gun.gun_type!.$case === "regular"
? 0
: (characteristics.get("intra_clip") as number);
let reloads: number[];
let shells: number;
switch (gun.gun_type!.$case) {
case "regular":
reloads = [characteristics.get("reload") as number];
shells = 1;
break;
case "auto_loader":
reloads = [characteristics.get("reload") as number];
shells = gun.gun_type!.value.shell_count;
break;
case "auto_reloader":
reloads = gun.gun_type!.value.shell_reloads.map(
(reload) => reload * reloadCoefficient,
);
shells = gun.gun_type!.value.shell_count;
break;
}
return {
reloads,
shells,
intraClip,
burstShells: gun.burst?.count ?? 1,
burstInterShell: gun.burst?.interval,
};
}, [characteristics, gun]);
const glow = useRef<HTMLDivElement>(null);
const ring = useRef<HTMLDivElement>(null);
const core = useRef<HTMLDivElement>(null);
const remainingTime = useRef<HTMLSpanElement>(null);
const totalTime = useRef<HTMLSpanElement>(null);
const flash = useRef<HTMLDivElement>(null);
const shellBars = useRef<Array<HTMLDivElement | null>>([]);
const state = useRef(0);
const shoot = useCallback(() => {
for (const element of [flash.current, ring.current, glow.current]) {
if (!element) continue;
element.dataset.shot = "true";
void element.offsetWidth;
delete element.dataset.shot;
}
}, []);
useEffect(() => {
let cancel = false;
let lastTime = performance.now() / 1000;
let lastState = state.current;
state.current = 0;
function frame() {
const reloadThreshold = reloadData.reloads.length;
const now = performance.now() / 1000;
const deltaTime = now - lastTime;
let reload = 0;
let shells: number[];
let interval = 0;
if (state.current < reloadThreshold) {
const currentReload = reloadData.reloads[Math.floor(state.current)];
if (currentReload === undefined) {
throw new Error("Missing reload duration for current reload stage");
}
interval = currentReload;
state.current = Math.min(
reloadThreshold,
state.current + deltaTime / interval,
);
reload = state.current % 1;
shells = Array(reloadData.shells).fill(reload);
if (reloadThreshold > 1) {
shells = shells.map((_, index) =>
Math.max(0, state.current - reloadData.shells + index + 1),
);
}
if (state.current >= reloadThreshold) shoot();
} else if (state.current < reloadData.shells + reloadThreshold - 1) {
if (
reloadData.burstShells === 1 ||
Math.floor(state.current) % reloadData.burstShells === 0
) {
interval = reloadData.intraClip;
} else {
if (reloadData.burstInterShell === undefined) {
throw new Error("Missing burst interval for firing sequence");
}
interval = reloadData.burstInterShell;
}
state.current = Math.min(
reloadData.shells + reloadThreshold,
state.current + deltaTime / interval,
);
reload = state.current % 1;
shells =
reloadThreshold > 1
? times(reloadData.shells, (index) =>
Math.max(
0,
Math.floor(index + 1 + reloadThreshold - state.current),
),
)
: times(reloadData.shells, (index) =>
Number(index > state.current - 1),
);
if (lastState % 1 > state.current % 1) shoot();
} else {
state.current = 0;
shells = Array(reloadData.shells).fill(0);
}
if (interval > 0 && totalTime.current) {
totalTime.current.textContent = interval.toFixed(RELOAD_PRECISION);
}
if (interval > 0 && remainingTime.current) {
remainingTime.current.textContent = ((1 - reload) * interval).toFixed(
RELOAD_PRECISION,
);
}
for (const [index, bar] of shellBars.current.entries()) {
if (bar) bar.style.width = `${(shells[index] ?? 0) * 100}%`;
}
if (ring.current && glow.current && core.current) {
const angle = reload * 2 * Math.PI;
ring.current.style.background = `conic-gradient(var(--amber-9) 0 ${angle}rad, var(--orange-7) ${angle}rad)`;
core.current.style.background = `conic-gradient(var(--amber-6) 0 ${angle}rad, var(--orange-3) ${angle}rad)`;
glow.current.style.background = `conic-gradient(var(--amber-9) 0 ${angle}rad, transparent ${angle}rad)`;
}
if (!cancel) requestAnimationFrame(frame);
lastTime = now;
lastState = state.current;
}
frame();
return () => {
cancel = true;
};
}, [reloadData, shoot]);
return (
<div className={styles.reload}>
<div className={styles.backdrop} />
<div className={styles.vignette} />
<div className={styles.glow} ref={glow} />
<div className={styles.ring} ref={ring}>
<div className={styles.core}>
<div className={styles.target} />
<div className={styles.fill} ref={core} />
</div>
</div>
<img
className={styles.caret}
src="/assets/images/tankopedia/visualizers/reload/caret.png"
/>
<Code
variant="ghost"
className={styles["remaining-time"]}
ref={remainingTime}
>
{(0).toFixed(RELOAD_PRECISION)}
</Code>
<Code variant="ghost" className={styles["total-time"]} ref={totalTime}>
{(0).toFixed(RELOAD_PRECISION)}
</Code>
{reloadData.shells > 1 && (
<div className={styles.shells} aria-hidden="true">
{Array.from({ length: reloadData.shells }, (_, index) => (
<div
className={styles["shell-track"]}
key={index}
style={{
transform: `rotate(${135 - 7 * (index - (reloadData.shells - 1) / 2)}deg)`,
}}
>
<div className={styles.shell}>
<ShellBorder className={styles.border} />
<div className={styles.mask}>
<div
className={styles["shell-fill"]}
ref={(element) => {
shellBars.current[index] = element;
}}
/>
</div>
</div>
</div>
))}
</div>
)}
<div className={styles.flash} ref={flash} />
<Button
className={styles.skip}
size="minor"
variant="outline"
color="gray"
highContrast
aria-label={strings.tanks.toys.reload.skip}
onClick={() => {
state.current = Math.floor(state.current + 1);
}}
>
{strings.tanks.toys.reload.skip}
<DoubleArrowRightIcon />
</Button>
</div>
);
}