Files
blitzkit/src/app/tools/tankopedia/[id]/components/Model/TankSandbox.tsx
T

274 lines
8.9 KiB
TypeScript
Raw Normal View History

2024-01-26 23:28:17 -06:00
import { LightningBoltIcon } from '@radix-ui/react-icons';
2024-04-08 21:24:24 -05:00
import { Button, Card, Flex, Tabs, Text, Theme } from '@radix-ui/themes';
2024-01-24 16:05:59 -06:00
import { PerspectiveCamera } from '@react-three/drei';
2024-01-06 18:17:04 -06:00
import { Canvas } from '@react-three/fiber';
2024-01-07 13:13:27 -06:00
import { Suspense, use, useEffect, useRef, useState } from 'react';
2024-02-16 21:07:48 -06:00
import { isMobile } from 'react-device-detect';
2024-02-27 11:52:36 -06:00
import { Armor } from '../../../../../../components/Armor';
2024-02-28 23:06:37 -06:00
import { ShotDisplay } from '../../../../../../components/Armor/components/ShotDisplay';
2024-01-07 13:13:27 -06:00
import { applyPitchYawLimits } from '../../../../../../core/blitz/applyPitchYawLimits';
2024-04-20 12:00:48 -05:00
import { modelDefinitions } from '../../../../../../core/blitzrinth/modelDefinitions';
import { modelTransformEvent } from '../../../../../../core/blitzrinth/modelTransform';
import { Pose, poseEvent } from '../../../../../../core/blitzrinth/pose';
import { tankIcon } from '../../../../../../core/blitzrinth/tankIcon';
import { useEquipment } from '../../../../../../hooks/useEquipment';
2024-04-01 21:33:47 -05:00
import { useFullScreen } from '../../../../../../hooks/useFullScreen';
2024-02-16 21:07:48 -06:00
import { useWideFormat } from '../../../../../../hooks/useWideFormat';
2024-04-20 12:00:48 -05:00
import { BlitzrinthWormWide } from '../../../../../../icons/BlitzrinthWormWide';
2024-02-22 16:41:42 -06:00
import { mutateDuel, useDuel } from '../../../../../../stores/duel';
import mutateTankopediaPersistent, {
2024-01-06 18:17:04 -06:00
TankopediaMode,
2024-01-21 10:34:52 -06:00
mutateTankopediaTemporary,
2024-02-22 16:41:42 -06:00
useTankopediaPersistent,
2024-01-06 18:17:04 -06:00
} from '../../../../../../stores/tankopedia';
2024-02-22 19:04:44 -06:00
import { AntagonistBar } from '../AntagonistBar';
2024-01-06 18:17:04 -06:00
import { Controls } from '../Control';
2024-02-27 11:52:36 -06:00
import { Lighting } from '../Lighting';
2024-01-22 17:06:06 -06:00
import { RotationInputs } from '../QuickInputs';
2024-02-27 11:52:36 -06:00
import { SceneProps } from '../SceneProps';
2024-01-22 15:50:41 -06:00
import { ModelLoader } from './components/ModelLoader';
2024-01-13 13:44:27 -06:00
import { Options } from './components/Options';
2024-02-27 11:52:36 -06:00
import { TankModel } from './components/TankModel';
2024-01-06 18:17:04 -06:00
2024-01-22 18:04:13 -06:00
export function TankSandbox() {
2024-01-06 18:17:04 -06:00
const canvas = useRef<HTMLCanvasElement>(null);
const hasImprovedVerticalStabilizer = useEquipment(122);
2024-01-07 13:13:27 -06:00
const awaitedModelDefinitions = use(modelDefinitions);
2024-01-06 18:17:04 -06:00
const canvasWrapper = useRef<HTMLDivElement>(null);
2024-02-22 16:41:42 -06:00
const mode = useTankopediaPersistent((state) => state.mode);
2024-01-22 18:04:13 -06:00
const protagonist = useDuel((state) => state.protagonist!);
const tankModelDefinition = awaitedModelDefinitions[protagonist.tank.id];
2024-01-19 22:46:10 -06:00
const turretModelDefinition =
2024-01-22 18:04:13 -06:00
tankModelDefinition.turrets[protagonist.turret.id];
const gunModelDefinition = turretModelDefinition.guns[protagonist.gun.id];
2024-02-16 21:07:48 -06:00
const wideFormat = useWideFormat();
const [loadModel, setLoadModel] = useState(wideFormat || !isMobile);
2024-01-26 23:28:17 -06:00
const duel = useDuel();
2024-04-01 21:33:47 -05:00
const isFullScreen = useFullScreen();
2024-01-07 13:13:27 -06:00
2024-01-06 18:17:04 -06:00
function handlePointerDown() {
window.addEventListener('pointermove', handlePointerMove);
window.addEventListener('pointerup', handlePointerUp);
}
function handlePointerMove(event: PointerEvent) {
event.preventDefault();
}
function handlePointerUp(event: PointerEvent) {
event.preventDefault();
window.removeEventListener('pointermove', handlePointerMove);
window.removeEventListener('pointerup', handlePointerUp);
}
useEffect(() => {
2024-01-19 22:46:10 -06:00
function handlePoseEvent(pose: Pose) {
switch (pose) {
case Pose.HullDown: {
2024-02-22 16:41:42 -06:00
mutateDuel((draft) => {
2024-01-19 22:46:10 -06:00
const [pitch, yaw] = applyPitchYawLimits(
-Infinity,
0,
gunModelDefinition.pitch,
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
2024-01-19 22:46:10 -06:00
);
modelTransformEvent.emit({ pitch, yaw });
2024-02-22 16:41:42 -06:00
draft.protagonist!.pitch = pitch;
draft.protagonist!.yaw = yaw;
2024-01-19 22:46:10 -06:00
});
2024-01-20 16:59:18 -06:00
break;
2024-01-19 22:46:10 -06:00
}
2024-01-20 16:59:18 -06:00
case Pose.FaceHug: {
2024-02-22 16:41:42 -06:00
mutateDuel((draft) => {
2024-01-20 16:59:18 -06:00
const [pitch, yaw] = applyPitchYawLimits(
2024-01-21 10:20:48 -06:00
Infinity,
2024-01-20 16:59:18 -06:00
0,
gunModelDefinition.pitch,
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
2024-01-20 16:59:18 -06:00
);
modelTransformEvent.emit({ pitch, yaw });
2024-02-22 16:41:42 -06:00
draft.protagonist!.pitch = pitch;
draft.protagonist!.yaw = yaw;
2024-01-20 16:59:18 -06:00
});
break;
}
case Pose.Default:
2024-02-22 16:41:42 -06:00
mutateDuel((draft) => {
2024-01-22 16:55:09 -06:00
const [pitch, yaw] = applyPitchYawLimits(
0,
0,
gunModelDefinition.pitch,
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
2024-01-22 16:55:09 -06:00
);
modelTransformEvent.emit({ pitch, yaw });
2024-02-22 16:41:42 -06:00
draft.protagonist!.pitch = pitch;
draft.protagonist!.yaw = yaw;
2024-01-22 16:55:09 -06:00
});
2024-01-20 16:59:18 -06:00
break;
2024-01-19 22:46:10 -06:00
}
}
poseEvent.on(handlePoseEvent);
2024-01-06 18:17:04 -06:00
return () => {
2024-01-19 22:46:10 -06:00
poseEvent.off(handlePoseEvent);
2024-01-06 18:17:04 -06:00
};
});
2024-01-07 13:13:27 -06:00
useEffect(() => {
2024-02-22 16:41:42 -06:00
mutateDuel((draft) => {
[draft.protagonist!.pitch, draft.protagonist!.yaw] = applyPitchYawLimits(
draft.protagonist!.pitch,
draft.protagonist!.yaw,
2024-01-21 22:11:01 -06:00
gunModelDefinition.pitch,
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
2024-01-21 22:11:01 -06:00
);
2024-01-07 13:13:27 -06:00
});
2024-01-22 18:04:13 -06:00
}, [protagonist.gun, protagonist.turret]);
2024-01-07 13:13:27 -06:00
2024-01-06 18:17:04 -06:00
return (
<Theme radius={isFullScreen ? 'none' : undefined}>
<Card
style={{
2024-04-01 21:33:47 -05:00
position: isFullScreen ? 'fixed' : 'relative',
2024-01-06 18:17:04 -06:00
border: isFullScreen ? 'none' : 'unset',
2024-04-01 21:33:47 -05:00
width: isFullScreen ? '100vw' : 'unset',
height: isFullScreen ? 'calc(100vh)' : 'unset',
top: isFullScreen ? 0 : 'unset',
left: isFullScreen ? 0 : 'unset',
zIndex: isFullScreen ? 2 : 'unset',
2024-01-06 18:17:04 -06:00
}}
ref={canvasWrapper}
>
2024-04-08 21:24:24 -05:00
<Flex
style={{
height: isFullScreen ? '100%' : '75vh',
maxHeight: isFullScreen ? 'unset' : 576,
}}
direction="column"
gap="2"
>
<Tabs.Root
value={mode}
onValueChange={(mode) => {
mutateTankopediaPersistent((draft) => {
draft.mode = mode as TankopediaMode;
});
2024-01-06 18:17:04 -06:00
}}
>
2024-04-08 21:24:24 -05:00
<Tabs.List>
<Tabs.Trigger value="model">
<Flex gap="2" align="center">
Model
2024-01-26 23:28:17 -06:00
</Flex>
2024-04-08 21:24:24 -05:00
</Tabs.Trigger>
<Tabs.Trigger value="armor">Armor</Tabs.Trigger>
</Tabs.List>
</Tabs.Root>
2024-01-06 18:17:04 -06:00
2024-04-16 09:35:10 -05:00
{wideFormat && (
<Text
color="gray"
style={{
position: 'absolute',
left: '50%',
top: 11,
}}
>
2024-04-20 12:00:48 -05:00
<BlitzrinthWormWide height={32} />
2024-04-16 09:35:10 -05:00
</Text>
)}
2024-01-26 23:28:17 -06:00
2024-04-08 21:24:24 -05:00
<div
style={{
height: 'calc(100% - 64px)',
position: 'absolute',
width: '100%',
bottom: 0,
}}
>
{loadModel ? (
<div style={{ width: '100%', height: '100%' }}>
<Canvas
2024-04-13 10:36:13 -05:00
gl={{
clippingPlanes: Object.freeze([]),
localClippingEnabled: true,
}}
2024-04-08 21:24:24 -05:00
shadows
ref={canvas}
onPointerDown={handlePointerDown}
onPointerMissed={() => {
mutateTankopediaTemporary((draft) => {
draft.shot = undefined;
});
}}
>
<PerspectiveCamera makeDefault fov={25} far={32} />
<Controls />
<SceneProps />
2024-02-22 19:04:44 -06:00
2024-04-08 21:24:24 -05:00
<Suspense fallback={<ModelLoader />}>
<Lighting />
<TankModel />
<Armor />
<ShotDisplay />
</Suspense>
</Canvas>
</div>
) : (
<Flex
align="center"
justify="center"
style={{ height: '100%', position: 'relative' }}
>
<Button
style={{
zIndex: 1,
}}
onClick={() => {
setLoadModel(true);
}}
>
<LightningBoltIcon /> Load model
</Button>
<Flex
style={{
filter: 'blur(16px)',
width: 256,
height: 128,
display: 'flex',
position: 'absolute',
top: '50%',
left: '50%',
transformOrigin: 'center center',
transform: 'translate(25%, -25%) scale(300%)',
}}
>
<img src={tankIcon(duel.antagonist!.tank.id, 'big')} />
</Flex>
</Flex>
)}
</div>
<Options canvasWrapper={canvasWrapper} isFullScreen={isFullScreen} />
{isFullScreen && <AntagonistBar floating />}
{loadModel && <RotationInputs isFullScreen={isFullScreen} />}
</Flex>
2024-01-06 18:17:04 -06:00
</Card>
</Theme>
);
}