import { useThree, type ThreeEvent } from "@react-three/fiber"; import { memo, useRef } from "react"; import { Group, Plane, Vector2, Vector3 } from "three"; import { applyPitchYawLimits } from "../core/blitz/applyPitchYawLimits"; 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 { useConsumable } from "../hooks/useConsumable"; import { useDuelModels } from "../hooks/useDuelModels"; import { getDuelModules, useDuelModules } from "../hooks/useDuelModules"; import { hasEquipment } from "../hooks/useEquipment"; import { useModel } from "../hooks/useModel"; import { useTankTransform } from "../hooks/useTankTransform"; import { Tankopedia } from "../stores/tankopedia"; import { TankopediaPersistent } from "../stores/tankopediaPersistent"; import { toThreeVector } from "../three/toThreeVector"; import { ArmorType } from "./SpacedArmorScene"; import { StaticArmorSceneComponent } from "./StaticArmorSceneComponent"; interface ArmorSceneProps { thicknessRange: ThicknessRange; } export interface ThicknessRange { value: number; } export const TankStaticArmor = memo(({ thicknessRange }) => { const turretContainer = useRef(null!); const gunContainer = useRef(null!); const protagonist = useDuelModules("protagonist"); const { gltf: armorGltf } = useArmor(protagonist.tank.id); const { gltf: modelGltf } = useModel(protagonist.tank.id); const armorNodes = Object.values(armorGltf.nodes); const modelNodes = Object.values(modelGltf.nodes); const models = useDuelModels("protagonist"); const hullOrigin = toThreeVector(models.track.origin); const turretOrigin = toThreeVector(models.tank.turret_origin); const gunOrigin = toThreeVector(models.turret.gun_origin); const canvas = useThree((state) => state.gl.domElement); const maskOrigin = models.gun.mask === undefined ? undefined : models.gun.mask + hullOrigin.y + turretOrigin.y + gunOrigin.y; const showPrimaryArmor = TankopediaPersistent.use( (state) => state.showPrimaryArmor, ); const showSpacedArmor = TankopediaPersistent.use( (state) => state.showSpacedArmor, ); const showExternalModules = TankopediaPersistent.use( (state) => state.showExternalModules, ); const isDynamicArmorActive = useConsumable("protagonist", 73); useTankTransform( protagonist.track, protagonist.turret, turretContainer, gunContainer, ); return ( <> {armorNodes.map((node) => { const isHull = node.name.startsWith("hull_"); const isVisible = isHull; const armorId = nameToArmorId(node.name); const { spaced, thickness } = resolveArmor( models.tank.armor!, armorId, ); if ( !isVisible || thickness === undefined || (!showPrimaryArmor && !spaced) || (!showSpacedArmor && spaced) || (isDynamicArmorActive && node.name.includes("state_00")) || (!isDynamicArmorActive && node.name.includes("state_01")) ) return null; return ( event.stopPropagation()} /> ); })} {modelNodes.map((node) => { const isWheel = node.name.startsWith("chassis_wheel_"); const isTrack = node.name.startsWith("chassis_track_"); const isVisible = isWheel || isTrack; if (!isVisible || !showExternalModules) return null; return ( event.stopPropagation()} /> ); })} {armorNodes.map((node) => { const isCurrentTurret = node.name.startsWith( `turret_${models.turret.model_id.toString().padStart(2, "0")}`, ); const isVisible = isCurrentTurret; const armorId = nameToArmorId(node.name); const { spaced, thickness } = resolveArmor( models.turret.armor!, armorId, ); if ( !isVisible || thickness === undefined || (!showPrimaryArmor && !spaced) || (!showSpacedArmor && spaced) || (isDynamicArmorActive && node.name.includes("state_00")) || (!isDynamicArmorActive && node.name.includes("state_01")) ) { return null; } const position = new Vector2(); const delta = new Vector2(); function onPointerDown(event: ThreeEvent) { event.stopPropagation(); position.set(event.clientX, event.clientY); controlsEnabledEvent.dispatch(false); Tankopedia.mutate((draft) => { draft.shot = undefined; draft.highlightArmor = undefined; }); window.addEventListener("pointermove", handlePointerMove); window.addEventListener("pointerup", handlePointerUp); } function handlePointerMove(event: PointerEvent) { const protagonist = getDuelModules("protagonist").tank; const hasImprovedVerticalStabilizer = hasEquipment( 122, protagonist.equipment_preset, Tankopedia.state.protagonist.equipment, ); const hasDownImprovedVerticalStabilizer = hasEquipment( 124, protagonist.equipment_preset, Tankopedia.state.protagonist.equipment, ); const boundingRect = canvas.getBoundingClientRect(); delta.set(event.clientX, event.clientY).sub(position); position.set(event.clientX, event.clientY); const [pitch, yaw] = applyPitchYawLimits( modelTransformEvent.last!.pitch, modelTransformEvent.last!.yaw + delta.x * (Math.PI / boundingRect.width), models.gun.pitch!, models.turret.yaw, hasImprovedVerticalStabilizer, hasDownImprovedVerticalStabilizer, ); modelTransformEvent.dispatch({ pitch, yaw }); } function handlePointerUp() { controlsEnabledEvent.dispatch(true); window.removeEventListener("pointermove", handlePointerMove); window.removeEventListener("pointerup", handlePointerUp); } return ( ); })} {armorNodes.map((node) => { const isCurrentGun = node.name.startsWith( `gun_${models.gun.model_id.toString().padStart(2, "0")}`, ); const isVisible = isCurrentGun; const armorId = nameToArmorId(node.name); const { spaced, thickness } = resolveArmor( models.gun.armor!, armorId, ); if ( !isVisible || thickness === undefined || (!showPrimaryArmor && !spaced) || (!showSpacedArmor && spaced) || (isDynamicArmorActive && node.name.includes("state_00")) || (!isDynamicArmorActive && node.name.includes("state_01")) ) return null; const position = new Vector2(); const delta = new Vector2(); function onPointerDown(event: ThreeEvent) { event.stopPropagation(); controlsEnabledEvent.dispatch(false); Tankopedia.mutate((draft) => { draft.shot = undefined; draft.highlightArmor = undefined; }); position.set(event.clientX, event.clientY); window.addEventListener("pointermove", handlePointerMove); window.addEventListener("pointerup", handlePointerUp); } function handlePointerMove(event: PointerEvent) { const protagonist = getDuelModules("protagonist").tank; const hasImprovedVerticalStabilizer = hasEquipment( 122, protagonist.equipment_preset, Tankopedia.state.protagonist.equipment, ); const hasDownImprovedVerticalStabilizer = hasEquipment( 124, protagonist.equipment_preset, Tankopedia.state.protagonist.equipment, ); const boundingRect = canvas.getBoundingClientRect(); delta.set(event.clientX, event.clientY).sub(position); position.set(event.clientX, event.clientY); const [pitch, yaw] = applyPitchYawLimits( modelTransformEvent.last!.pitch - delta.y * (Math.PI / boundingRect.height), modelTransformEvent.last!.yaw + delta.x * (Math.PI / boundingRect.width), models.gun.pitch!, models.turret.yaw, hasImprovedVerticalStabilizer, hasDownImprovedVerticalStabilizer, ); modelTransformEvent.dispatch({ pitch, yaw }); } function handlePointerUp() { controlsEnabledEvent.dispatch(true); window.removeEventListener("pointermove", handlePointerMove); window.removeEventListener("pointerup", handlePointerUp); } return ( ); })} {modelNodes.map((node) => { const gunString = `gun_${models.gun.model_id .toString() .padStart(2, "0")}`; const isCurrentGun = models.gun.mask ? node.name.startsWith(gunString) : node.name === gunString; const isVisible = isCurrentGun; if (!isVisible || !showExternalModules) return null; const position = new Vector2(); const delta = new Vector2(); function onPointerDown(event: ThreeEvent) { event.stopPropagation(); controlsEnabledEvent.dispatch(false); Tankopedia.mutate((draft) => { draft.shot = undefined; draft.highlightArmor = undefined; }); position.set(event.clientX, event.clientY); window.addEventListener("pointermove", handlePointerMove); window.addEventListener("pointerup", handlePointerUp); } function handlePointerMove(event: PointerEvent) { const protagonist = getDuelModules("protagonist").tank; const hasImprovedVerticalStabilizer = hasEquipment( 122, protagonist.equipment_preset, Tankopedia.state.protagonist.equipment, ); const hasDownImprovedVerticalStabilizer = hasEquipment( 124, protagonist.equipment_preset, Tankopedia.state.protagonist.equipment, ); const boundingRect = canvas.getBoundingClientRect(); delta.set(event.clientX, event.clientY).sub(position); position.set(event.clientX, event.clientY); const [pitch, yaw] = applyPitchYawLimits( modelTransformEvent.last!.pitch - delta.y * (Math.PI / boundingRect.height), modelTransformEvent.last!.yaw + delta.x * (Math.PI / boundingRect.width), models.gun.pitch!, models.turret.yaw, hasImprovedVerticalStabilizer, hasDownImprovedVerticalStabilizer, ); modelTransformEvent.dispatch({ pitch, yaw }); } function handlePointerUp() { controlsEnabledEvent.dispatch(true); window.removeEventListener("pointermove", handlePointerMove); window.removeEventListener("pointerup", handlePointerUp); } return ( ); })} ); });