Files
blitzkit/packages/website/src/components/StaticArmor.tsx
T

390 lines
15 KiB
TypeScript
Raw Normal View History

2025-09-01 22:56:39 -05:00
import { useThree, type ThreeEvent } from "@react-three/fiber";
import { memo, useRef } from "react";
import { Group, Plane, Vector2, Vector3 } from "three";
2026-09-27 16:46:22 -05:00
import { applyPitchYawLimits } from "../core/blitz/applyPitchYawLimits";
import { correctZYTuple } from "../core/blitz/correctZYTuple";
import { hasEquipment } from "../core/blitzkit/hasEquipment";
import { modelTransformEvent } from "../core/blitzkit/modelTransform";
import { nameToArmorId } from "../core/blitzkit/nameToArmorId";
import { resolveArmor } from "../core/blitzkit/resolveThickness";
import { controlsEnabledEvent } from "../core/controlsEnabled";
import { useArmor } from "../hooks/useArmor";
import { useModel } from "../hooks/useModel";
import { useTankModelDefinition } from "../hooks/useTankModelDefinition";
import { useTankTransform } from "../hooks/useTankTransform";
import { Duel } from "../stores/duel";
import { Tankopedia } from "../stores/tankopedia";
import { TankopediaPersistent } from "../stores/tankopediaPersistent";
2025-09-01 22:56:39 -05:00
import { ModelTankWrapper } from "./ModelTankWrapper";
import { ArmorType } from "./SpacedArmorScene";
import { StaticArmorSceneComponent } from "./StaticArmorSceneComponent";
2024-07-24 20:27:16 -05:00
2024-07-31 13:36:56 -05:00
interface ArmorSceneProps {
2024-07-29 14:08:52 -05:00
thicknessRange: ThicknessRange;
}
2024-07-26 13:24:18 -05:00
2024-07-31 13:36:56 -05:00
export interface ThicknessRange {
value: number;
2024-07-24 20:09:38 -05:00
}
2024-07-31 13:36:56 -05:00
export const StaticArmor = memo<ArmorSceneProps>(({ thicknessRange }) => {
const wrapper = useRef<Group>(null);
2025-09-27 16:07:45 -05:00
const turretContainer = useRef<Group>(null!);
const gunContainer = useRef<Group>(null!);
2024-07-31 13:36:56 -05:00
const tank = Duel.use((state) => state.protagonist.tank);
const track = Duel.use((state) => state.protagonist.track);
const turret = Duel.use((state) => state.protagonist.turret);
const gun = Duel.use((state) => state.protagonist.gun);
const { gltf: armorGltf } = useArmor(tank.id);
2024-07-31 13:36:56 -05:00
const { gltf: modelGltf } = useModel(tank.id);
const armorNodes = Object.values(armorGltf.nodes);
const modelNodes = Object.values(modelGltf.nodes);
const tankModelDefinition = useTankModelDefinition();
const trackModelDefinition = tankModelDefinition.tracks[track.id];
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
2025-08-18 12:06:14 -05:00
const gunModelDefinition = turretModelDefinition.guns[gun.id];
2026-03-31 14:11:45 -05:00
const hullOrigin = correctZYTuple(trackModelDefinition.origin!);
const turretOrigin = correctZYTuple(tankModelDefinition.turret_origin!);
const gunOrigin = correctZYTuple(turretModelDefinition.gun_origin!);
2024-08-01 20:38:37 -05:00
const canvas = useThree((state) => state.gl.domElement);
2025-09-02 16:24:13 -05:00
2024-07-31 13:36:56 -05:00
const maskOrigin =
gunModelDefinition.mask === undefined
? undefined
: gunModelDefinition.mask + hullOrigin.y + turretOrigin.y + gunOrigin.y;
const showPrimaryArmor = TankopediaPersistent.use(
2026-03-31 14:11:45 -05:00
(state) => state.showPrimaryArmor,
2024-07-31 13:36:56 -05:00
);
const showSpacedArmor = TankopediaPersistent.use(
2026-03-31 14:11:45 -05:00
(state) => state.showSpacedArmor,
2024-07-31 13:36:56 -05:00
);
const showExternalModules = TankopediaPersistent.use(
2026-03-31 14:11:45 -05:00
(state) => state.showExternalModules,
2024-07-31 13:36:56 -05:00
);
const isDynamicArmorActive = Duel.use((state) =>
2026-03-31 14:11:45 -05:00
state.protagonist.consumables.includes(73),
2024-08-28 21:16:25 -05:00
);
2024-07-31 13:36:56 -05:00
useTankTransform(track, turret, turretContainer, gunContainer);
2024-07-31 13:36:56 -05:00
return (
<ModelTankWrapper ref={wrapper}>
<group position={hullOrigin}>
{armorNodes.map((node) => {
2025-09-01 22:56:39 -05:00
const isHull = node.name.startsWith("hull_");
2024-07-31 13:36:56 -05:00
const isVisible = isHull;
const armorId = nameToArmorId(node.name);
const { spaced, thickness } = resolveArmor(
2026-03-31 14:11:45 -05:00
tankModelDefinition.armor!,
armorId,
2024-07-31 13:36:56 -05:00
);
if (
!isVisible ||
thickness === undefined ||
(!showPrimaryArmor && !spaced) ||
2024-08-28 21:16:25 -05:00
(!showSpacedArmor && spaced) ||
2025-09-01 22:56:39 -05:00
(isDynamicArmorActive && node.name.includes("state_00")) ||
(!isDynamicArmorActive && node.name.includes("state_01"))
2024-07-31 13:36:56 -05:00
)
return null;
return (
<StaticArmorSceneComponent
name={node.name}
thicknessRange={thicknessRange}
key={node.uuid}
type={spaced ? ArmorType.Spaced : ArmorType.Primary}
thickness={thickness}
node={node}
onPointerDown={(event) => event.stopPropagation()}
2024-07-31 13:36:56 -05:00
/>
);
})}
</group>
{modelNodes.map((node) => {
2025-09-01 22:56:39 -05:00
const isWheel = node.name.startsWith("chassis_wheel_");
const isTrack = node.name.startsWith("chassis_track_");
2024-07-31 13:36:56 -05:00
const isVisible = isWheel || isTrack;
if (!isVisible || !showExternalModules) return null;
return (
<StaticArmorSceneComponent
name={node.name}
thicknessRange={thicknessRange}
key={node.uuid}
type={ArmorType.External}
thickness={trackModelDefinition.thickness}
variant="track"
node={node}
onPointerDown={(event) => event.stopPropagation()}
2024-07-31 13:36:56 -05:00
/>
);
})}
<group ref={turretContainer}>
{armorNodes.map((node) => {
const isCurrentTurret = node.name.startsWith(
2026-03-31 14:11:45 -05:00
`turret_${turretModelDefinition.model_id.toString().padStart(2, "0")}`,
2024-07-31 13:36:56 -05:00
);
const isVisible = isCurrentTurret;
const armorId = nameToArmorId(node.name);
const { spaced, thickness } = resolveArmor(
2026-03-31 14:11:45 -05:00
turretModelDefinition.armor!,
armorId,
2024-07-31 13:36:56 -05:00
);
if (
!isVisible ||
thickness === undefined ||
(!showPrimaryArmor && !spaced) ||
2024-08-28 21:16:25 -05:00
(!showSpacedArmor && spaced) ||
2025-09-01 22:56:39 -05:00
(isDynamicArmorActive && node.name.includes("state_00")) ||
(!isDynamicArmorActive && node.name.includes("state_01"))
2024-08-01 20:38:37 -05:00
) {
2024-07-31 13:36:56 -05:00
return null;
2024-08-01 20:38:37 -05:00
}
const position = new Vector2();
const delta = new Vector2();
function onPointerDown(event: ThreeEvent<PointerEvent>) {
event.stopPropagation();
position.set(event.clientX, event.clientY);
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(false);
2025-09-01 22:56:39 -05:00
Tankopedia.mutate((draft) => {
2024-08-01 20:38:37 -05:00
draft.shot = undefined;
draft.highlightArmor = undefined;
});
2025-09-01 22:56:39 -05:00
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
2024-08-01 20:38:37 -05:00
}
2025-08-29 13:42:44 -05:00
function handlePointerMove(event: PointerEvent) {
const hasImprovedVerticalStabilizer = hasEquipment(
2024-08-01 20:38:37 -05:00
122,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2024-08-01 20:38:37 -05:00
);
2025-08-29 13:42:44 -05:00
const hasDownImprovedVerticalStabilizer = hasEquipment(
2025-03-11 10:13:02 -05:00
124,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2025-03-11 10:13:02 -05:00
);
2024-08-01 20:38:37 -05:00
const boundingRect = canvas.getBoundingClientRect();
delta.set(event.clientX, event.clientY).sub(position);
position.set(event.clientX, event.clientY);
2025-08-12 13:43:11 -05:00
const [pitch, yaw] = applyPitchYawLimits(
modelTransformEvent.last!.pitch,
modelTransformEvent.last!.yaw +
delta.x * (Math.PI / boundingRect.width),
2026-03-31 14:11:45 -05:00
gunModelDefinition.pitch!,
2024-08-01 20:38:37 -05:00
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
2026-03-31 14:11:45 -05:00
hasDownImprovedVerticalStabilizer,
2024-08-01 20:38:37 -05:00
);
2025-08-10 20:36:48 -05:00
modelTransformEvent.dispatch({ pitch, yaw });
2024-08-01 20:38:37 -05:00
}
function handlePointerUp() {
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(true);
2025-09-01 22:56:39 -05:00
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
2024-08-01 20:38:37 -05:00
}
2024-07-31 13:36:56 -05:00
return (
2024-10-06 00:12:36 -05:00
<group key={node.uuid} position={hullOrigin}>
<group position={turretOrigin}>
2024-07-31 13:36:56 -05:00
<StaticArmorSceneComponent
name={node.name}
thicknessRange={thicknessRange}
type={spaced ? ArmorType.Spaced : ArmorType.Primary}
thickness={thickness}
node={node}
2024-08-02 13:48:24 -05:00
onPointerDown={onPointerDown}
2024-07-31 13:36:56 -05:00
/>
</group>
</group>
);
})}
<group ref={gunContainer}>
{armorNodes.map((node) => {
const isCurrentGun = node.name.startsWith(
2026-03-31 14:11:45 -05:00
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}`,
2024-07-31 13:36:56 -05:00
);
const isVisible = isCurrentGun;
const armorId = nameToArmorId(node.name);
const { spaced, thickness } = resolveArmor(
2026-03-31 14:11:45 -05:00
gunModelDefinition.armor!,
armorId,
2024-07-31 13:36:56 -05:00
);
if (
!isVisible ||
thickness === undefined ||
(!showPrimaryArmor && !spaced) ||
2024-08-28 21:16:25 -05:00
(!showSpacedArmor && spaced) ||
2025-09-01 22:56:39 -05:00
(isDynamicArmorActive && node.name.includes("state_00")) ||
(!isDynamicArmorActive && node.name.includes("state_01"))
2024-08-28 21:16:25 -05:00
)
2024-07-31 13:36:56 -05:00
return null;
2024-08-01 20:38:37 -05:00
const position = new Vector2();
const delta = new Vector2();
function onPointerDown(event: ThreeEvent<PointerEvent>) {
event.stopPropagation();
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(false);
2025-09-01 22:56:39 -05:00
Tankopedia.mutate((draft) => {
2024-08-01 20:38:37 -05:00
draft.shot = undefined;
draft.highlightArmor = undefined;
});
position.set(event.clientX, event.clientY);
2025-09-01 22:56:39 -05:00
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
2024-08-01 20:38:37 -05:00
}
2025-08-29 13:42:44 -05:00
function handlePointerMove(event: PointerEvent) {
const hasImprovedVerticalStabilizer = hasEquipment(
2024-08-01 20:38:37 -05:00
122,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2024-08-01 20:38:37 -05:00
);
2025-08-29 13:42:44 -05:00
const hasDownImprovedVerticalStabilizer = hasEquipment(
2025-03-11 10:13:02 -05:00
124,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2025-03-11 10:13:02 -05:00
);
2024-08-01 20:38:37 -05:00
const boundingRect = canvas.getBoundingClientRect();
delta.set(event.clientX, event.clientY).sub(position);
position.set(event.clientX, event.clientY);
2025-08-12 13:43:11 -05:00
const [pitch, yaw] = applyPitchYawLimits(
modelTransformEvent.last!.pitch -
delta.y * (Math.PI / boundingRect.height),
modelTransformEvent.last!.yaw +
delta.x * (Math.PI / boundingRect.width),
2026-03-31 14:11:45 -05:00
gunModelDefinition.pitch!,
2024-08-01 20:38:37 -05:00
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
2026-03-31 14:11:45 -05:00
hasDownImprovedVerticalStabilizer,
2024-08-01 20:38:37 -05:00
);
2025-08-10 20:36:48 -05:00
modelTransformEvent.dispatch({ pitch, yaw });
2024-08-01 20:38:37 -05:00
}
function handlePointerUp() {
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(true);
2025-09-01 22:56:39 -05:00
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
2024-08-01 20:38:37 -05:00
}
2024-07-31 13:36:56 -05:00
return (
2024-10-06 00:12:36 -05:00
<group key={node.uuid} position={hullOrigin}>
<group position={turretOrigin.clone().add(gunOrigin)}>
2024-07-31 13:36:56 -05:00
<StaticArmorSceneComponent
name={node.name}
thicknessRange={thicknessRange}
type={spaced ? ArmorType.Spaced : ArmorType.Primary}
thickness={thickness}
node={node}
2024-08-02 13:48:24 -05:00
onPointerDown={onPointerDown}
2024-07-31 13:36:56 -05:00
/>
</group>
</group>
);
})}
{modelNodes.map((node) => {
2024-10-07 18:45:11 -05:00
const gunString = `gun_${gunModelDefinition.model_id
2024-09-06 23:17:53 -05:00
.toString()
2025-09-01 22:56:39 -05:00
.padStart(2, "0")}`;
2024-07-31 13:36:56 -05:00
const isCurrentGun = gunModelDefinition.mask
? node.name.startsWith(gunString)
: node.name === gunString;
const isVisible = isCurrentGun;
if (!isVisible || !showExternalModules) return null;
2024-08-02 13:48:24 -05:00
const position = new Vector2();
const delta = new Vector2();
function onPointerDown(event: ThreeEvent<PointerEvent>) {
event.stopPropagation();
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(false);
2025-09-01 22:56:39 -05:00
Tankopedia.mutate((draft) => {
2024-08-02 13:48:24 -05:00
draft.shot = undefined;
draft.highlightArmor = undefined;
});
position.set(event.clientX, event.clientY);
2025-09-01 22:56:39 -05:00
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
2024-08-02 13:48:24 -05:00
}
2025-08-29 13:42:44 -05:00
function handlePointerMove(event: PointerEvent) {
const hasImprovedVerticalStabilizer = hasEquipment(
2024-08-02 13:48:24 -05:00
122,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2024-08-02 13:48:24 -05:00
);
2025-08-29 13:42:44 -05:00
const hasDownImprovedVerticalStabilizer = hasEquipment(
2025-03-11 10:13:02 -05:00
124,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2025-03-11 10:13:02 -05:00
);
2024-08-02 13:48:24 -05:00
const boundingRect = canvas.getBoundingClientRect();
delta.set(event.clientX, event.clientY).sub(position);
position.set(event.clientX, event.clientY);
2025-08-12 13:43:11 -05:00
const [pitch, yaw] = applyPitchYawLimits(
modelTransformEvent.last!.pitch -
delta.y * (Math.PI / boundingRect.height),
modelTransformEvent.last!.yaw +
delta.x * (Math.PI / boundingRect.width),
2026-03-31 14:11:45 -05:00
gunModelDefinition.pitch!,
2024-08-02 13:48:24 -05:00
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
2026-03-31 14:11:45 -05:00
hasDownImprovedVerticalStabilizer,
2024-08-02 13:48:24 -05:00
);
2025-08-10 20:36:48 -05:00
modelTransformEvent.dispatch({ pitch, yaw });
2024-08-02 13:48:24 -05:00
}
function handlePointerUp() {
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(true);
2025-08-12 13:43:11 -05:00
2025-09-01 22:56:39 -05:00
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
2024-08-02 13:48:24 -05:00
}
2024-07-31 13:36:56 -05:00
return (
<StaticArmorSceneComponent
name={node.name}
thicknessRange={thicknessRange}
key={node.uuid}
type={ArmorType.External}
thickness={gunModelDefinition.thickness}
variant="gun"
node={node}
clip={
maskOrigin === undefined
? undefined
: new Plane(new Vector3(0, 0, -1), -maskOrigin)
}
2024-08-02 13:48:24 -05:00
hullOrigin={hullOrigin}
turretOrigin={turretOrigin}
gunOrigin={gunOrigin}
onPointerDown={onPointerDown}
2024-07-31 13:36:56 -05:00
/>
);
})}
</group>
</group>
</ModelTankWrapper>
);
});