Skip to content

Commit

Permalink
Fix reset on play page
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Oct 19, 2024
1 parent d18fcbb commit 66c749f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
9 changes: 8 additions & 1 deletion app/play/[level]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.",
);
Expand All @@ -140,6 +141,11 @@ export default function PlayLevelPage({
}
};

const handleReset = () => {
setIsSimulating(false);
setGameResult(null);
};

const handleClearMap = () => {
setPlayerMap(map.grid.map((row) => [...row]));
setBlockCount(0);
Expand Down Expand Up @@ -195,6 +201,7 @@ export default function PlayLevelPage({
<Visualizer
map={playerMap}
autoStart={true}
onReset={handleReset}
onSimulationEnd={handleSimulationEnd}
/>
{gameResult && (
Expand Down
17 changes: 5 additions & 12 deletions components/Visualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export function Visualizer({
controls = true,
cellSize = "64",
map,
onReset,
onSimulationEnd,
}: {
autoReplay?: boolean;
autoStart?: boolean;
controls?: boolean;
cellSize?: string;
map: string[][];
onReset?: () => unknown;
onSimulationEnd?: (isWin: boolean) => unknown;
}) {
const visualizer = useVisualizer();
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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;
}

Expand Down Expand Up @@ -133,13 +132,7 @@ export function Visualizer({
<Button onClick={startSimulation} disabled={running}>
Replay
</Button>
<Button
disabled={running}
onClick={() => {
simulator.current = new ZombieSurvival(map);
setRunning(false);
}}
>
<Button disabled={running} onClick={onReset}>
Reset
</Button>
</div>
Expand Down

0 comments on commit 66c749f

Please sign in to comment.