2024-01-26 23:28:17 -06:00
|
|
|
import { LightningBoltIcon } from '@radix-ui/react-icons';
|
2024-02-18 16:19:25 -06:00
|
|
|
import { Button, Card, Flex, Tabs, 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-02 16:08:41 -06:00
|
|
|
import { ShotDisplay } from '../../../../../../components/ShotDisplay';
|
2024-01-07 13:13:27 -06:00
|
|
|
import { applyPitchYawLimits } from '../../../../../../core/blitz/applyPitchYawLimits';
|
|
|
|
|
import { modelDefinitions } from '../../../../../../core/blitzkrieg/modelDefinitions';
|
2024-01-19 22:46:10 -06:00
|
|
|
import { modelTransformEvent } from '../../../../../../core/blitzkrieg/modelTransform';
|
|
|
|
|
import { Pose, poseEvent } from '../../../../../../core/blitzkrieg/pose';
|
2024-01-26 23:28:17 -06:00
|
|
|
import { tankIcon } from '../../../../../../core/blitzkrieg/tankIcon';
|
2024-02-16 21:07:48 -06:00
|
|
|
import { useWideFormat } from '../../../../../../hooks/useWideFormat';
|
2024-01-22 18:04:13 -06:00
|
|
|
import { useDuel } from '../../../../../../stores/duel';
|
2024-01-21 10:34:52 -06:00
|
|
|
import {
|
2024-01-06 18:17:04 -06:00
|
|
|
TankopediaMode,
|
2024-01-21 10:34:52 -06:00
|
|
|
mutateTankopediaTemporary,
|
|
|
|
|
useTankopediaTemporary,
|
2024-01-06 18:17:04 -06:00
|
|
|
} from '../../../../../../stores/tankopedia';
|
|
|
|
|
import { Controls } from '../Control';
|
2024-01-09 22:31:34 -06:00
|
|
|
import { Lighting } from '../Lighting';
|
2024-01-22 17:06:06 -06:00
|
|
|
import { RotationInputs } from '../QuickInputs';
|
2024-01-06 18:17:04 -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-01-10 13:00:37 -06:00
|
|
|
import { TankArmor } from './components/TankArmor';
|
2024-02-02 10:31:40 -06:00
|
|
|
import { ExternalModuleMask } from './components/TankArmor/components/ExternalModuleMask';
|
|
|
|
|
import { SpacedArmorDepth } from './components/TankArmor/components/SpacedArmorDepth';
|
2024-01-06 18:17:04 -06:00
|
|
|
import { TankModel } from './components/TankModel';
|
|
|
|
|
|
2024-01-22 18:04:13 -06:00
|
|
|
export function TankSandbox() {
|
2024-01-06 18:17:04 -06:00
|
|
|
const canvas = useRef<HTMLCanvasElement>(null);
|
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);
|
|
|
|
|
const [isFullScreen, setIsFullScreen] = useState(false);
|
2024-01-21 10:34:52 -06:00
|
|
|
const mode = useTankopediaTemporary((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-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(() => {
|
|
|
|
|
function handleFullScreenChange() {
|
|
|
|
|
setIsFullScreen(document.fullscreenElement !== null);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 22:46:10 -06:00
|
|
|
function handlePoseEvent(pose: Pose) {
|
|
|
|
|
switch (pose) {
|
|
|
|
|
case Pose.HullDown: {
|
2024-01-21 10:34:52 -06:00
|
|
|
mutateTankopediaTemporary((draft) => {
|
2024-01-19 22:46:10 -06:00
|
|
|
const [pitch, yaw] = applyPitchYawLimits(
|
|
|
|
|
-Infinity,
|
|
|
|
|
0,
|
|
|
|
|
gunModelDefinition.pitch,
|
|
|
|
|
turretModelDefinition.yaw,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
modelTransformEvent.emit({ pitch, yaw });
|
2024-01-21 22:11:01 -06:00
|
|
|
draft.model.pose.pitch = pitch;
|
|
|
|
|
draft.model.pose.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-01-21 10:34:52 -06:00
|
|
|
mutateTankopediaTemporary((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,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
modelTransformEvent.emit({ pitch, yaw });
|
2024-01-21 22:11:01 -06:00
|
|
|
draft.model.pose.pitch = pitch;
|
|
|
|
|
draft.model.pose.yaw = yaw;
|
2024-01-20 16:59:18 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Pose.Default:
|
2024-01-22 16:55:09 -06:00
|
|
|
mutateTankopediaTemporary((draft) => {
|
|
|
|
|
const [pitch, yaw] = applyPitchYawLimits(
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
gunModelDefinition.pitch,
|
|
|
|
|
turretModelDefinition.yaw,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
modelTransformEvent.emit({ pitch, yaw });
|
|
|
|
|
draft.model.pose.pitch = pitch;
|
|
|
|
|
draft.model.pose.yaw = yaw;
|
|
|
|
|
});
|
|
|
|
|
|
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
|
|
|
document.addEventListener('fullscreenchange', handleFullScreenChange);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener('fullscreenchange', handleFullScreenChange);
|
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-01-21 10:34:52 -06:00
|
|
|
mutateTankopediaTemporary((draft) => {
|
2024-01-21 22:11:01 -06:00
|
|
|
[draft.model.pose.pitch, draft.model.pose.yaw] = applyPitchYawLimits(
|
|
|
|
|
draft.model.pose.pitch,
|
|
|
|
|
draft.model.pose.yaw,
|
|
|
|
|
gunModelDefinition.pitch,
|
|
|
|
|
turretModelDefinition.yaw,
|
|
|
|
|
);
|
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={{
|
|
|
|
|
position: 'relative',
|
|
|
|
|
border: isFullScreen ? 'none' : 'unset',
|
|
|
|
|
}}
|
|
|
|
|
ref={canvasWrapper}
|
|
|
|
|
>
|
|
|
|
|
<Theme radius="full" style={{ height: '100%' }}>
|
|
|
|
|
<Flex
|
|
|
|
|
style={{
|
|
|
|
|
height: isFullScreen ? '100%' : '75vh',
|
|
|
|
|
maxHeight: isFullScreen ? 'unset' : 576,
|
|
|
|
|
}}
|
|
|
|
|
direction="column"
|
|
|
|
|
gap="2"
|
|
|
|
|
>
|
|
|
|
|
<Tabs.Root
|
|
|
|
|
value={mode}
|
|
|
|
|
onValueChange={(mode) => {
|
2024-01-21 10:34:52 -06:00
|
|
|
mutateTankopediaTemporary((draft) => {
|
2024-01-06 18:17:04 -06:00
|
|
|
draft.mode = mode as TankopediaMode;
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Tabs.List>
|
2024-02-03 15:13:19 -06:00
|
|
|
<Tabs.Trigger value="model">
|
|
|
|
|
<Flex gap="2" align="center">
|
|
|
|
|
Model
|
|
|
|
|
</Flex>
|
|
|
|
|
</Tabs.Trigger>
|
2024-01-06 18:17:04 -06:00
|
|
|
<Tabs.Trigger value="armor">Armor</Tabs.Trigger>
|
|
|
|
|
</Tabs.List>
|
|
|
|
|
</Tabs.Root>
|
|
|
|
|
|
|
|
|
|
<div style={{ height: '100%' }}>
|
2024-01-26 23:28:17 -06:00
|
|
|
{loadModel ? (
|
2024-02-02 16:08:41 -06:00
|
|
|
<Canvas
|
|
|
|
|
shadows
|
|
|
|
|
ref={canvas}
|
|
|
|
|
onPointerDown={handlePointerDown}
|
|
|
|
|
onPointerMissed={() => {
|
|
|
|
|
mutateTankopediaTemporary((draft) => {
|
|
|
|
|
draft.shot = undefined;
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-01-26 23:28:17 -06:00
|
|
|
<PerspectiveCamera makeDefault fov={25} far={32} />
|
|
|
|
|
<Controls />
|
|
|
|
|
<SceneProps />
|
2024-01-06 18:17:04 -06:00
|
|
|
|
2024-01-26 23:28:17 -06:00
|
|
|
<Suspense fallback={<ModelLoader />}>
|
2024-02-02 16:08:41 -06:00
|
|
|
<ShotDisplay />
|
2024-01-26 23:28:17 -06:00
|
|
|
<Lighting />
|
|
|
|
|
<TankModel />
|
|
|
|
|
<TankArmor />
|
2024-02-02 10:31:40 -06:00
|
|
|
<ExternalModuleMask ornamental />
|
|
|
|
|
<SpacedArmorDepth ornamental />
|
2024-01-26 23:28:17 -06:00
|
|
|
</Suspense>
|
|
|
|
|
</Canvas>
|
|
|
|
|
) : (
|
|
|
|
|
<Flex
|
|
|
|
|
align="center"
|
|
|
|
|
justify="center"
|
|
|
|
|
style={{ height: '100%', position: 'relative' }}
|
|
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
style={{
|
|
|
|
|
zIndex: 1,
|
|
|
|
|
}}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setLoadModel(true);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<LightningBoltIcon /> Load model
|
|
|
|
|
</Button>
|
|
|
|
|
|
2024-02-16 21:07:48 -06:00
|
|
|
<Flex
|
2024-01-26 23:28:17 -06:00
|
|
|
style={{
|
|
|
|
|
filter: 'blur(16px)',
|
|
|
|
|
width: 256,
|
|
|
|
|
height: 128,
|
|
|
|
|
display: 'flex',
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
top: '50%',
|
|
|
|
|
left: '50%',
|
2024-02-16 21:07:48 -06:00
|
|
|
transformOrigin: 'center center',
|
2024-01-26 23:28:17 -06:00
|
|
|
transform: 'translate(25%, -25%) scale(300%)',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<img src={tankIcon(duel.antagonist!.tank.id, 'big')} />
|
2024-02-16 21:07:48 -06:00
|
|
|
</Flex>
|
2024-01-26 23:28:17 -06:00
|
|
|
</Flex>
|
|
|
|
|
)}
|
2024-01-06 18:17:04 -06:00
|
|
|
</div>
|
|
|
|
|
|
2024-01-13 13:44:27 -06:00
|
|
|
<Options canvas={canvasWrapper} isFullScreen={isFullScreen} />
|
2024-01-26 23:28:17 -06:00
|
|
|
|
|
|
|
|
{loadModel && <RotationInputs />}
|
2024-01-06 18:17:04 -06:00
|
|
|
</Flex>
|
|
|
|
|
</Theme>
|
|
|
|
|
</Card>
|
|
|
|
|
</Theme>
|
|
|
|
|
);
|
|
|
|
|
}
|