diff --git a/app/play/[level]/page.tsx b/app/play/[level]/page.tsx index da758af..3e5c20a 100644 --- a/app/play/[level]/page.tsx +++ b/app/play/[level]/page.tsx @@ -14,6 +14,8 @@ import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { api } from "@/convex/_generated/api"; import { ZombieSurvival } from "@/simulators/zombie-survival"; +type PlacementMode = "player" | "block" | "landmine"; + export default function PlayLevelPage({ params, }: { @@ -24,10 +26,9 @@ export default function PlayLevelPage({ const [playerMap, setPlayerMap] = useState([]); const [isSimulating, setIsSimulating] = useState(false); const [gameResult, setGameResult] = useState<"WON" | "LOST" | null>(null); - const [placementMode, setPlacementMode] = useState<"player" | "block">( - "player", - ); + const [placementMode, setPlacementMode] = useState("player"); const [blockCount, setBlockCount] = useState(0); + const [landmineCount, setLandmineCount] = useState(0); const flags = useQuery(api.flags.getFlags); const [mode, setMode] = useState<"play" | "test">("play"); @@ -83,6 +84,7 @@ export default function PlayLevelPage({ setIsSimulating(false); setGameResult(null); setBlockCount(0); + setLandmineCount(0); setPlayerMap(map.grid.map((row) => [...row])); setPlacementMode("player"); }; @@ -92,16 +94,16 @@ export default function PlayLevelPage({ const newMap = [...playerMap]; if (placementMode === "player") { - // Remove existing player if any - for (let i = 0; i < newMap.length; i++) { - for (let j = 0; j < newMap[i].length; j++) { - if (newMap[i][j] === "P") { - newMap[i][j] = " "; + if (newMap[y][x] === " ") { + // Remove existing player if any + for (let i = 0; i < newMap.length; i++) { + for (let j = 0; j < newMap[i].length; j++) { + if (newMap[i][j] === "P") { + newMap[i][j] = " "; + } } } - } - // Place new player - if (newMap[y][x] === " ") { + newMap[y][x] = "P"; } } else if (placementMode === "block") { @@ -115,13 +117,24 @@ export default function PlayLevelPage({ setBlockCount(blockCount + 1); } } + } else if (placementMode === "landmine") { + // Place new landmine + if (newMap[y][x] === "L") { + newMap[y][x] = " "; + setLandmineCount(landmineCount - 1); + } else if (landmineCount < 2) { + if (newMap[y][x] === " ") { + newMap[y][x] = "L"; + setLandmineCount(landmineCount + 1); + } + } } setPlayerMap(newMap); }; - const handlePlacementModeChange = async (mode: "player" | "block") => { + function handlePlacementModeChange(mode: PlacementMode) { setPlacementMode(mode); - }; + } const runSimulation = () => { if (!ZombieSurvival.mapHasPlayer(playerMap)) { @@ -153,6 +166,7 @@ export default function PlayLevelPage({ const handleClearMap = () => { setPlayerMap(map.grid.map((row) => [...row])); setBlockCount(0); + setLandmineCount(0); setPlacementMode("player"); }; @@ -198,6 +212,13 @@ export default function PlayLevelPage({ > Place Block ({2 - blockCount} left) +

diff --git a/app/playground/page.tsx b/app/playground/page.tsx index 0d9cf3b..1082534 100644 --- a/app/playground/page.tsx +++ b/app/playground/page.tsx @@ -146,8 +146,11 @@ export default function PlaygroundPage() { // Remove players and blocks from the map const cleanedMap = map.map((row) => - row.map((cell) => (cell === "P" || cell === "B" ? " " : cell)), + row.map((cell) => + cell === "P" || cell === "B" || cell === "L" ? " " : cell, + ), ); + setMap(cleanedMap); window.localStorage.setItem(STORAGE_MAP_KEY, JSON.stringify(cleanedMap)); } @@ -318,49 +321,52 @@ export default function PlaygroundPage() { row.map((cell, x) => (
{ const newMap = userPlaying ? [...userSolution] : [...map]; + if (userPlaying) { - // Count existing players and blocks const playerCount = newMap .flat() .filter((c) => c === "P").length; const blockCount = newMap .flat() .filter((c) => c === "B").length; + const landmineCount = newMap + .flat() + .filter((c) => c === "L").length; - // Toggle logic for play mode if (cell === " ") { if (playerCount === 0) { newMap[y][x] = "P"; } else if (blockCount < 2) { newMap[y][x] = "B"; + } else if (landmineCount === 0) { + newMap[y][x] = "L"; } } else if (cell === "P") { newMap[y][x] = " "; } else if (cell === "B") { newMap[y][x] = " "; + } else if (cell === "L") { + newMap[y][x] = " "; } + userPlaying ? setUserSolution(newMap) : handleChangeMap(newMap); } else { - // Toggle between empty, zombie, and rock for edit mode - if (cell === " ") newMap[y][x] = "Z"; - else if (cell === "Z") newMap[y][x] = "R"; - else newMap[y][x] = " "; + if (cell === " ") { + newMap[y][x] = "Z"; + } else if (cell === "Z") { + newMap[y][x] = "R"; + } else { + newMap[y][x] = " "; + } } + userPlaying ? setUserSolution(newMap) : handleChangeMap(newMap); diff --git a/components/Map.tsx b/components/Map.tsx index a1d27ca..47863d1 100644 --- a/components/Map.tsx +++ b/components/Map.tsx @@ -32,14 +32,9 @@ export function getCellImage(cell: string) { if (cell === "L") { return ( Block ); }