Skip to content

Commit

Permalink
Add landmine to pages
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Oct 27, 2024
1 parent 21f6695 commit 20e6ab2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
47 changes: 34 additions & 13 deletions app/play/[level]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}: {
Expand All @@ -24,10 +26,9 @@ export default function PlayLevelPage({
const [playerMap, setPlayerMap] = useState<string[][]>([]);
const [isSimulating, setIsSimulating] = useState(false);
const [gameResult, setGameResult] = useState<"WON" | "LOST" | null>(null);
const [placementMode, setPlacementMode] = useState<"player" | "block">(
"player",
);
const [placementMode, setPlacementMode] = useState<PlacementMode>("player");
const [blockCount, setBlockCount] = useState(0);
const [landmineCount, setLandmineCount] = useState(0);
const flags = useQuery(api.flags.getFlags);
const [mode, setMode] = useState<"play" | "test">("play");

Expand Down Expand Up @@ -83,6 +84,7 @@ export default function PlayLevelPage({
setIsSimulating(false);
setGameResult(null);
setBlockCount(0);
setLandmineCount(0);
setPlayerMap(map.grid.map((row) => [...row]));
setPlacementMode("player");
};
Expand All @@ -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") {
Expand All @@ -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)) {
Expand Down Expand Up @@ -153,6 +166,7 @@ export default function PlayLevelPage({
const handleClearMap = () => {
setPlayerMap(map.grid.map((row) => [...row]));
setBlockCount(0);
setLandmineCount(0);
setPlacementMode("player");
};

Expand Down Expand Up @@ -198,6 +212,13 @@ export default function PlayLevelPage({
>
Place Block ({2 - blockCount} left)
</Button>
<Button
onClick={() => handlePlacementModeChange("landmine")}
variant={placementMode === "landmine" ? "default" : "outline"}
className="h-10"
>
Place Landmine ({1 - landmineCount} left)
</Button>
</div>
<div className="mb-8 flex flex-col items-center">
<h2 className="mb-4 text-xl font-semibold">
Expand Down
38 changes: 22 additions & 16 deletions app/playground/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -318,49 +321,52 @@ export default function PlaygroundPage() {
row.map((cell, x) => (
<div
key={`${x}-${y}`}
className={`${
cell === " " ||
cell === "Z" ||
cell === "R" ||
cell === "P" ||
cell === "B"
? "z-10 cursor-pointer hover:border-2 hover:border-dashed hover:border-slate-300"
: ""
} border border-transparent`}
className="z-10 cursor-pointer border border-transparent hover:border-2 hover:border-dashed hover:border-slate-300"
onClick={() => {
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);
Expand Down
9 changes: 2 additions & 7 deletions components/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@ export function getCellImage(cell: string) {
if (cell === "L") {
return (
<img
src="/entities/block.svg"
alt="Block"
src="/entities/landmine.svg"
alt="Landmine"
className="h-full w-full"
style={{
position: "relative",
top: "0px",
left: "0px",
}}
/>
);
}
Expand Down

0 comments on commit 20e6ab2

Please sign in to comment.