From 7af5d5f4650fe2f4e5b702e612486fc1ec18183a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sat, 4 May 2024 11:00:54 -0500 Subject: [PATCH] feat: module tree --- .../[id]/components/Model/ModulesSection.tsx | 207 +++++++++++++++--- .../[id]/components/Model/TechTreeSection.tsx | 10 +- src/app/tools/tankopedia/[id]/page.tsx | 2 +- src/core/blitzkit/tankDefinitions/index.ts | 5 + 4 files changed, 192 insertions(+), 32 deletions(-) diff --git a/src/app/tools/tankopedia/[id]/components/Model/ModulesSection.tsx b/src/app/tools/tankopedia/[id]/components/Model/ModulesSection.tsx index de04ef3c7..86f1f8784 100644 --- a/src/app/tools/tankopedia/[id]/components/Model/ModulesSection.tsx +++ b/src/app/tools/tankopedia/[id]/components/Model/ModulesSection.tsx @@ -1,31 +1,84 @@ -import { Heading } from '@radix-ui/themes'; +import { Flex, Heading } from '@radix-ui/themes'; import { use } from 'react'; +import { ModuleButton } from '../../../../../../components/ModuleButtons/ModuleButton'; import PageWrapper from '../../../../../../components/PageWrapper'; import { - EngineDefinition, - GunDefinition, + ModuleDefinition, ModuleType, + TankDefinition, tankDefinitions, - TrackDefinition, - TurretDefinition, Unlock, } from '../../../../../../core/blitzkit/tankDefinitions'; -import { useDuel } from '../../../../../../stores/duel'; +import { TIER_ROMAN_NUMERALS } from '../../../../../../core/blitzkit/tankDefinitions/constants'; +import { mutateDuel, useDuel } from '../../../../../../stores/duel'; +import { TreeArrow } from './TechTreeSection'; export function ModulesSection() { const awaitedTankDefinitions = use(tankDefinitions); const tank = useDuel((state) => state.protagonist!.tank); + const turret = useDuel((state) => state.protagonist!.turret); + const gun = useDuel((state) => state.protagonist!.gun); + const engine = useDuel((state) => state.protagonist!.engine); + const track = useDuel((state) => state.protagonist!.track); + const turret0 = tank.turrets[0]; + const gun0 = turret0.guns[0]; + const engine0 = tank.engines[0]; + const track0 = tank.tracks[0]; + + function setByUnlock(unlock: Unlock) { + mutateDuel((draft) => { + if (unlock.type === 'turret') { + draft.protagonist!.turret = draft.protagonist!.tank.turrets.find( + (turret) => turret.id === unlock.id, + )!; + + if ( + !draft.protagonist!.turret.guns.some( + (gun) => gun.id === draft.protagonist!.gun.id, + ) + ) { + draft.protagonist!.gun = draft.protagonist!.turret.guns.at(-1)!; + draft.protagonist!.shell = draft.protagonist!.gun.shells.at(-1)!; + } + } else if (unlock.type === 'gun') { + const gunInTurret = draft.protagonist!.turret.guns.find( + (gun) => gun.id === unlock.id, + ); + if (gunInTurret) { + draft.protagonist!.gun = gunInTurret; + draft.protagonist!.shell = gunInTurret.shells.at(-1)!; + } else { + // TODO: warn somehow? + const suitableTurret = draft.protagonist!.tank.turrets.find( + (turret) => turret.guns.some((gun) => gun.id === unlock.id), + )!; + const gunInSuitableTurret = suitableTurret.guns.find( + (gun) => gun.id === unlock.id, + )!; + + draft.protagonist!.turret = suitableTurret; + draft.protagonist!.gun = gunInSuitableTurret; + draft.protagonist!.shell = gunInSuitableTurret.shells.at(-1)!; + } + } else if (unlock.type === 'engine') { + draft.protagonist!.engine = draft.protagonist!.tank.engines.find( + (engine) => engine.id === unlock.id, + )!; + } else if (unlock.type === 'chassis') { + draft.protagonist!.track = draft.protagonist!.tank.tracks.find( + (track) => track.id === unlock.id, + )!; + } + }); + } function tree(type: ModuleType, unlocks: Unlock[]) { return ( - + ); } return ( - turret0 - {tank.turrets[0].unlocks && tree('turret', tank.turrets[0].unlocks)} + Modules - gun0 - {tank.turrets[0].guns[0].unlocks && - tree('gun', tank.turrets[0].guns[0].unlocks)} + + + setByUnlock({ id: turret0.id, type: 'turret' })} + /> - engine0 - {tank.engines[0].unlocks && tree('engine', tank.engines[0].unlocks)} + {turret0.unlocks && ( + <> + + {tree('turret', turret0.unlocks)} + + )} + - track0 - {tank.tracks[0].unlocks && tree('chassis', tank.tracks[0].unlocks)} + + setByUnlock({ id: gun0.id, type: 'gun' })} + /> + + {gun0.unlocks && ( + <> + + {tree('gun', gun0.unlocks)} + + )} + + + + setByUnlock({ id: engine0.id, type: 'engine' })} + /> + + {engine0.unlocks && ( + <> + + {tree('engine', engine0.unlocks)} + + )} + + + + setByUnlock({ id: track0.id, type: 'chassis' })} + /> + + {track0.unlocks && ( + <> + + {tree('chassis', track0.unlocks)} + + )} + + + {/* gun0 + {tank.turrets[0].guns[0].unlocks && + tree('gun', tank.turrets[0].guns[0].unlocks)} + + engine0 + {tank.engines[0].unlocks && tree('engine', tank.engines[0].unlocks)} + + track0 + {tank.tracks[0].unlocks && tree('chassis', tank.tracks[0].unlocks)} */} + ); } diff --git a/src/app/tools/tankopedia/[id]/components/Model/TechTreeSection.tsx b/src/app/tools/tankopedia/[id]/components/Model/TechTreeSection.tsx index 30c2cad10..2c7401638 100644 --- a/src/app/tools/tankopedia/[id]/components/Model/TechTreeSection.tsx +++ b/src/app/tools/tankopedia/[id]/components/Model/TechTreeSection.tsx @@ -1,20 +1,22 @@ import { ChevronRightIcon } from '@radix-ui/react-icons'; import { Flex, Heading, Text } from '@radix-ui/themes'; import Link from 'next/link'; -import { use } from 'react'; +import { ComponentProps, use } from 'react'; import PageWrapper from '../../../../../../components/PageWrapper'; import { asset } from '../../../../../../core/blitzkit/asset'; import { tankDefinitions } from '../../../../../../core/blitzkit/tankDefinitions'; import { TIER_ROMAN_NUMERALS } from '../../../../../../core/blitzkit/tankDefinitions/constants'; import { useDuel } from '../../../../../../stores/duel'; -function Arrow() { +export function TreeArrow({ style, ...props }: ComponentProps) { return ( - + )} @@ -160,7 +162,7 @@ export function TechTreeSection() { {tank.successors && ( <> - + {/* */} - + {/* */} diff --git a/src/core/blitzkit/tankDefinitions/index.ts b/src/core/blitzkit/tankDefinitions/index.ts index 1df044345..b6b6bc0cb 100644 --- a/src/core/blitzkit/tankDefinitions/index.ts +++ b/src/core/blitzkit/tankDefinitions/index.ts @@ -24,6 +24,11 @@ export interface Crew { count?: number; substitute?: CrewMember[]; } +export type ModuleDefinition = + | TrackDefinition + | EngineDefinition + | TurretDefinition + | GunDefinition; export interface TankDefinition { id: number; description?: string;