feat: optimized state handling x2

This commit is contained in:
TrèsAbhi committed 2024-01-13 16:48:20 -06:00
1 parent 230b2df5fd
commit 586cd84349
13 files changed
+138 -149

No files matched your search

@@ -1,6 +1,7 @@
import { Card, Flex, Tabs, Theme } from '@radix-ui/themes';
import { Html } from '@react-three/drei';
import { Canvas } from '@react-three/fiber';
import { useRouter } from 'next/navigation';
import { Suspense, use, useEffect, useRef, useState } from 'react';
import { applyPitchYawLimits } from '../../../../../../core/blitz/applyPitchYawLimits';
import { modelDefinitions } from '../../../../../../core/blitzkrieg/modelDefinitions';
@@ -19,6 +20,7 @@ import { TankArmor } from './components/TankArmor';
import { TankModel } from './components/TankModel';
export function TankDisplay() {
const router = useRouter();
const canvas = useRef<HTMLCanvasElement>(null);
const awaitedModelDefinitions = use(modelDefinitions);
const canvasWrapper = useRef<HTMLDivElement>(null);
@@ -64,15 +66,24 @@ export function TankDisplay() {
const gunModelDefinition = turretModelDefinition.guns[protagonist.gun.id];
mutateTankopedia((draft) => {
[draft.model.gunPitch, draft.model.turretYaw] = applyPitchYawLimits(
draft.model.gunPitch,
draft.model.turretYaw,
gunModelDefinition.pitch,
turretModelDefinition.yaw,
);
[draft.model.physical.gunPitch, draft.model.physical.turretYaw] =
applyPitchYawLimits(
draft.model.physical.gunPitch,
draft.model.physical.turretYaw,
gunModelDefinition.pitch,
turretModelDefinition.yaw,
);
});
}, [protagonist?.gun, protagonist?.turret]);
useEffect(() => {
if (location.hash.startsWith('#')) {
mutateTankopedia((draft) => {
draft.mode = location.hash.match(/#(.+)/)![1] as TankopediaMode;
});
}
}, []);
if (!protagonist) return null;
return (
@@ -96,6 +107,8 @@ export function TankDisplay() {
<Tabs.Root
value={mode}
onValueChange={(mode) => {
router.push(`#${mode}`);
mutateTankopedia((draft) => {
draft.mode = mode as TankopediaMode;
});