feat: loading bar

This commit is contained in:
TrèsAbhi committed 2024-01-04 13:37:36 -06:00
1 parent 0a4e696bc1
commit 6ca9e637dc
5 files changed
+97 -120

No files matched your search

@@ -1,25 +1,18 @@
import { OrbitControls } from '@react-three/drei';
import { useThree } from '@react-three/fiber';
import { RefObject, useEffect, useRef } from 'react';
import { Box3, Object3D } from 'three';
import { useEffect, useRef } from 'react';
import { OrbitControls as OrbitControlsClass } from 'three-stdlib';
import { useTankopedia } from '../../../../../stores/tankopedia';
interface ControlsProps {
fit: RefObject<Object3D>;
}
export function Controls({ fit }: ControlsProps) {
export function Controls() {
const camera = useThree((state) => state.camera);
const orbitControls = useRef<OrbitControlsClass>(null);
const enabled = useTankopedia((state) => state.model.controlsEnabled);
useEffect(() => {
const box = new Box3().setFromObject(fit.current!);
orbitControls.current?.target.set(0, (box.min.y + box.max.y) / 2, 0);
camera.position.set(-5, (box.min.y + box.max.y) / 2, -15);
}, [fit.current]);
camera.position.set(-5, 4, -20);
orbitControls.current?.target.set(0, 1.5, 0);
}, []);
return <OrbitControls ref={orbitControls} enabled={enabled} />;
}
@@ -0,0 +1,29 @@
import { Html, useProgress } from '@react-three/drei';
import { theme } from '../../../../../stitches.config';
export function Loader() {
const { progress } = useProgress();
return (
<Html center>
<div
style={{
width: 160,
height: 16,
backgroundColor: theme.colors.appBackground1_light,
borderRadius: 8,
overflow: 'hidden',
}}
>
<div
style={{
width: `${Math.round(progress)}%`,
height: '100%',
backgroundColor: theme.colors.solidBackground_purple,
transition: 'width 0.1s',
}}
></div>
</div>
</Html>
);
}
@@ -1,9 +1,18 @@
import { useLoader } from '@react-three/fiber';
import { forwardRef, use, useImperativeHandle, useRef } from 'react';
import {
forwardRef,
useEffect,
useImperativeHandle,
useRef,
useState,
} from 'react';
import { Group, Vector3 } from 'three';
import { GLTFLoader } from 'three-stdlib';
import { X_AXIS } from '../../../../../constants/axis';
import { modelDefinitions } from '../../../../../core/blitzkrieg/modelDefinitions';
import {
ModelDefinitions,
modelDefinitions,
} from '../../../../../core/blitzkrieg/modelDefinitions';
import mutateTankopedia, {
useTankopedia,
} from '../../../../../stores/tankopedia';
@@ -11,22 +20,33 @@ import { GunContainer } from './GunContainer';
import { HullContainer } from './HullContainer';
import { TurretContainer } from './TurretContainer';
interface ModelProps {
interface TankModelProps {
tankId: number;
gunId: number;
turretId: number;
gunId: number;
}
export const Model = forwardRef<Group, ModelProps>(
({ gunId, tankId, turretId }, ref) => {
const awaitedModelDefinitions = use(modelDefinitions);
const model = useTankopedia((state) => state.model);
export const TankModel = forwardRef<Group, TankModelProps>(
({ tankId, turretId, gunId }, ref) => {
// "TypeError: dispatcher.use is not a function"
// const awaitedModelDefinitions = use(modelDefinitions);
const [awaitedModelDefinitions, setAwaitedModelDefinitions] = useState<
ModelDefinitions | undefined
>(undefined);
const gltf = useLoader(GLTFLoader, `/test/${tankId}.glb`);
const gunContainer = useRef<Group>(null);
const model = useTankopedia((state) => state.model);
const hullContainer = useRef<Group>(null);
const turretContainer = useRef<Group>(null);
const gunContainer = useRef<Group>(null);
useImperativeHandle(ref, () => hullContainer.current!);
useEffect(() => {
(async () => {
setAwaitedModelDefinitions(await modelDefinitions);
})();
}, []);
if (!awaitedModelDefinitions) return null;
const tankModelDefinition = awaitedModelDefinitions[tankId];
const turretModelDefinition = tankModelDefinition.turrets[turretId];
@@ -48,24 +68,24 @@ export const Model = forwardRef<Group, ModelProps>(
yaw={model.hullYaw}
ref={hullContainer}
onYawStart={() =>
mutateTankopedia((state) => {
state.model.controlsEnabled = false;
mutateTankopedia((draft) => {
draft.model.controlsEnabled = false;
})
}
onYawEnd={(yaw) => {
mutateTankopedia((state) => {
state.model.controlsEnabled = true;
state.model.hullYaw = yaw;
mutateTankopedia((draft) => {
draft.model.controlsEnabled = true;
draft.model.hullYaw = yaw;
});
}}
onTrackStart={() => {
mutateTankopedia((state) => {
state.model.controlsEnabled = false;
mutateTankopedia((draft) => {
draft.model.controlsEnabled = false;
});
}}
onTrackEnd={() => {
mutateTankopedia((state) => {
state.model.controlsEnabled = true;
mutateTankopedia((draft) => {
draft.model.controlsEnabled = true;
});
}}
>