From 66c749f4ff09c0258183bc0847c752f852857d09 Mon Sep 17 00:00:00 2001 From: Aaron Delasy Date: Sat, 19 Oct 2024 19:25:43 +0300 Subject: [PATCH] Fix reset on play page --- app/play/[level]/page.tsx | 9 ++++++++- components/Visualizer.tsx | 17 +++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/play/[level]/page.tsx b/app/play/[level]/page.tsx index cdb183f..2078ce4 100644 --- a/app/play/[level]/page.tsx +++ b/app/play/[level]/page.tsx @@ -11,6 +11,7 @@ import { ChevronLeftIcon } from "@radix-ui/react-icons"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import TestMode from "./test-mode"; import { useRouter } from "next/navigation"; +import { ZombieSurvival } from "@/simulators/zombie-survival"; export default function PlayLevelPage({ params, @@ -119,7 +120,7 @@ export default function PlayLevelPage({ }; const runSimulation = () => { - if (!playerMap.some((row) => row.includes("P"))) { + if (!ZombieSurvival.mapHasPlayer(playerMap)) { alert( "Please place a player (P) on the map before running the simulation.", ); @@ -140,6 +141,11 @@ export default function PlayLevelPage({ } }; + const handleReset = () => { + setIsSimulating(false); + setGameResult(null); + }; + const handleClearMap = () => { setPlayerMap(map.grid.map((row) => [...row])); setBlockCount(0); @@ -195,6 +201,7 @@ export default function PlayLevelPage({ {gameResult && ( diff --git a/components/Visualizer.tsx b/components/Visualizer.tsx index fc144a7..4f8bcef 100644 --- a/components/Visualizer.tsx +++ b/components/Visualizer.tsx @@ -13,6 +13,7 @@ export function Visualizer({ controls = true, cellSize = "64", map, + onReset, onSimulationEnd, }: { autoReplay?: boolean; @@ -20,6 +21,7 @@ export function Visualizer({ controls?: boolean; cellSize?: string; map: string[][]; + onReset?: () => unknown; onSimulationEnd?: (isWin: boolean) => unknown; }) { const visualizer = useVisualizer(); @@ -54,6 +56,7 @@ export function Visualizer({ function startSimulation() { simulator.current = new ZombieSurvival(map); + renderer.current?.render(simulator.current.getAllAliveEntities()); setRunning(true); interval.current = setInterval(() => { @@ -63,11 +66,7 @@ export function Visualizer({ if (!simulator.current.finished()) { simulator.current.step(); - - if (renderer.current !== null) { - renderer.current.render(simulator.current.getAllAliveEntities()); - } - + renderer.current?.render(simulator.current.getAllAliveEntities()); return; } @@ -133,13 +132,7 @@ export function Visualizer({ -