From befcd186e4799fafc23966c10f805cac62da04a7 Mon Sep 17 00:00:00 2001 From: Alexander Mangel Date: Thu, 12 Oct 2023 17:49:44 -0500 Subject: [PATCH] refactor: cleaning up some code --- .../loadingScreen/loadingScreen.tsx | 15 +++++++------- .../client/src/components/ui/inventory.tsx | 5 +++-- packages/client/src/game/systems/gameLoop.tsx | 20 +++++++++---------- .../src/game/systems/useConstruction.tsx | 7 +------ packages/client/src/lib/useOnce.ts | 15 ++++++++++++++ 5 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 packages/client/src/lib/useOnce.ts diff --git a/packages/client/src/components/loadingScreen/loadingScreen.tsx b/packages/client/src/components/loadingScreen/loadingScreen.tsx index 01c6364..c49ed5f 100644 --- a/packages/client/src/components/loadingScreen/loadingScreen.tsx +++ b/packages/client/src/components/loadingScreen/loadingScreen.tsx @@ -7,14 +7,6 @@ import "./loadingScreen.css"; function LoadingScreen() { const [hide, setHide] = useState(false); const [loading, setLoading] = useState(true); - - const fadeOut = () => { - setLoading(false); - setTimeout(() => { - setHide(true); - }, 5000); - }; - const { progress } = useNProgress({ isAnimating: loading, animationDuration: 300, @@ -23,6 +15,13 @@ function LoadingScreen() { }); useEffect(() => { + const fadeOut = () => { + setLoading(false); + setTimeout(() => { + setHide(true); + }, 5000); + }; + document.addEventListener("gameLoaded", () => { fadeOut(); }); diff --git a/packages/client/src/components/ui/inventory.tsx b/packages/client/src/components/ui/inventory.tsx index 360a948..3360167 100644 --- a/packages/client/src/components/ui/inventory.tsx +++ b/packages/client/src/components/ui/inventory.tsx @@ -10,6 +10,7 @@ import { useTransition, type SpringValue, } from "@react-spring/web"; +import { useOnce } from "@/lib/useOnce"; function Inventory() { const { @@ -19,7 +20,7 @@ function Inventory() { const [cardsLoaded, setcardsLoaded] = useState(false); const [facilities, setFacilities] = useState([]); - useEffect(() => { + useOnce(() => { const f = Object.entries(EntityData.facilities) .map(([, entityData]) => entityData) .filter((entityData) => entityData.tags.includes("startingItem")); @@ -30,7 +31,7 @@ function Inventory() { return () => { document.removeEventListener("gameLoaded", () => {}); }; - }, []); + }); useEffect(() => { // TODO: Remove hack to only show gravityhill at startup diff --git a/packages/client/src/game/systems/gameLoop.tsx b/packages/client/src/game/systems/gameLoop.tsx index 21bba9d..6915b9e 100644 --- a/packages/client/src/game/systems/gameLoop.tsx +++ b/packages/client/src/game/systems/gameLoop.tsx @@ -7,18 +7,27 @@ import { buildFacility } from "./constructionSystem"; import { useEntityQuery } from "@latticexyz/react"; import { Has, getComponentValueStrict } from "@latticexyz/recs"; import { useStore } from "../store"; +import { useOnce } from "@/lib/useOnce"; let loaded = false; function GameLoop() { const { - // systemCalls: { mudGetAllFacilityEntityMetadatas }, components: { Position, EntityType }, } = useMUD(); const { world: { getEntityByPosition }, } = useStore(); + // Startup + useOnce(() => { + for (let i = 0; i < 10; i++) { + const { createResource, randomEmptyPosition } = resourceFactory(); + const position = randomEmptyPosition(); + createResource(EntityData.resources.crystalFloat, position!); + } + }); + const facilities = useEntityQuery([Has(Position), Has(EntityType)]).map( (entity) => { const pos = getComponentValueStrict(Position, entity); @@ -56,15 +65,6 @@ function GameLoop() { } }, [facilities, getEntityByPosition]); - // // Startup - useEffect(() => { - for (let i = 0; i < 10; i++) { - const { createResource, randomEmptyPosition } = resourceFactory(); - const position = randomEmptyPosition(); - createResource(EntityData.resources.crystalFloat, position!); - } - }, []); - return <>; } diff --git a/packages/client/src/game/systems/useConstruction.tsx b/packages/client/src/game/systems/useConstruction.tsx index b4408de..d393cf8 100644 --- a/packages/client/src/game/systems/useConstruction.tsx +++ b/packages/client/src/game/systems/useConstruction.tsx @@ -6,11 +6,7 @@ import { queueAsyncCall } from "../utils/asyncQueue"; function useConstruction() { const { - systemCalls: { - // mudIsPositionEmpty, - mudBuildFacility, - // mudGetEntityMetadataAtPosition, - }, + systemCalls: { mudBuildFacility }, } = useMUD(); const { input: { building }, @@ -29,7 +25,6 @@ function useConstruction() { queueAsyncCall(async () => { console.trace("buildFacility hook", position); console.log(build); - try { const result = await mudBuildFacility(...build); console.log("mudBuildFacility result", result); diff --git a/packages/client/src/lib/useOnce.ts b/packages/client/src/lib/useOnce.ts new file mode 100644 index 0000000..c8191fb --- /dev/null +++ b/packages/client/src/lib/useOnce.ts @@ -0,0 +1,15 @@ +import { useEffect, useRef } from "react"; + +function useOnce(fn: () => void) { + const started = useRef(false); + useEffect(() => { + fn(); + + return () => { + started.current = true; + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); +} + +export { useOnce };