// --- Constants for our tiles ---
const WALL = "⬛";
const EMPTY = "⬜";
const PLAYER = "🟦";
const GOAL = "🟩";
// --- ASCII Art for the Title (adapted for p5.js) ---
const TITLE_ART_LINES = [
" ___ _ ___",
" / __\\ __ __ ___ _| | ___ _ __ / __\\___ _ __ ___",
" / / | '__/ _` \\ \\ /\\ / / |/ _ \\ '__| / / / _ \\| '__/ _ \\",
"/ /__| | | (_| |\\ V V /| | __/ | / /__| (_) | | | __/ ",
"\\____/_| \\__,_| \\_/\_/ |_|\\___|_| \\____/\\___/|_| \\___|"
];
// --- Map Database ---
const MAPS = {
"Map 1: The Classic": [
[WALL, WALL, WALL, WALL, WALL, WALL],
[WALL, PLAYER, WALL, EMPTY, GOAL, WALL],
[WALL, EMPTY, WALL, EMPTY, WALL, WALL],
[WALL, EMPTY, WALL, EMPTY, EMPTY, WALL],
[WALL, EMPTY, WALL, WALL, EMPTY, WALL],
[WALL, EMPTY, EMPTY, EMPTY, EMPTY, WALL],
[WALL, WALL, WALL, WALL, WALL, WALL],
],
"Map 2: Open Field": [
[WALL, WALL, WALL, WALL, WALL, WALL, WALL],
[WALL, PLAYER, EMPTY, EMPTY, EMPTY, EMPTY, WALL],
[WALL, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL],
[WALL, EMPTY, EMPTY, GOAL, EMPTY, EMPTY, WALL],
[WALL, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL],
[WALL, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL],
[WALL, WALL, WALL, WALL, WALL, WALL, WALL],
],
"Map 3: The Corridor": [
[WALL, WALL, WALL, WALL, WALL, WALL],
[WALL, PLAYER, EMPTY, EMPTY, EMPTY, WALL],
[WALL, WALL, WALL, WALL, EMPTY, WALL],
[WALL, GOAL, EMPTY, EMPTY, EMPTY, WALL],
[WALL, WALL, WALL, WALL, WALL, WALL],
],
};
// --- Game State Variables ---
let currentMapData = [];
let originalMapData = []; // To store the pristine version of the selected map for resets
let playerPos = { r: 0, c: 0 };
let selectedMapName = "";
const TILE_SIZE = 40;
let canvasWidth, canvasHeight;
// Game States
const STATE_MENU = "MENU";
const STATE_PLAYING = "PLAYING";
const STATE_VICTORY = "VICTORY";
const STATE_CONFIRM = "CONFIRM"; // For Reset/Menu confirmation during gameplay
let gameState = STATE_MENU;
let message = "";
let messageEndTime = 0;
// For menu interaction
let menuItems = [];
let mapNames = [];
// For confirmation state
let confirmAction = null; // { type: "RESET" or "MENU_RETURN", message: "..." }
let confirmYesAction = null;
let confirmNoAction = null;
function setup() {
// Determine max map dimensions for canvas size
let maxRows = 0;
let maxCols = 0;
for (const mapName in MAPS) {
maxRows = Math.max(maxRows, MAPS[mapName].length);
maxCols = Math.max(maxCols, MAPS[mapName][0].length);
}
canvasWidth = Math.max(500, maxCols * TILE_SIZE + TILE_SIZE * 2); // Add padding
canvasHeight = Math.max(400, maxRows * TILE_SIZE + TILE_SIZE * 6); // More padding for title/messages
createCanvas(canvasWidth, canvasHeight);
textFont("monospace"); // Good for blocky text
mapNames = Object.keys(MAPS);
prepareMenuItems();
}
function deepCopyArray(arr) {
return arr.map(row => [...row]);
}
function loadMap(mapName) {
if (MAPS[mapName]) {
selectedMapName = mapName;
originalMapData = deepCopyArray(MAPS[mapName]);
currentMapData = deepCopyArray(originalMapData);
findPlayer();
if (playerPos.r === null) {
console.error("Player not found in map:", mapName);
// Fallback or error state
gameState = STATE_MENU;
displayMessage("Error: Player not found in map.", 5000);
return;
}
gameState = STATE_PLAYING;
clearMessage();
}
}
function findPlayer() {
for (let r = 0; r < currentMapData.length; r++) {
for (let c = 0; c < currentMapData[r].length; c++) {
if (currentMapData[r][c] === PLAYER) {
playerPos = { r: r, c: c };
return;
}
}
}
playerPos = { r: null, c: null }; // Player not found (e.g., after winning)
}
function displayMessage(msg, duration = 2000) {
message = msg;
messageEndTime = millis() + duration;
}
function clearMessage() {
message = "";
messageEndTime = 0;
}
function prepareMenuItems() {
menuItems = [];
let startY = TITLE_ART_LINES.length * 20 + 80; // Adjusted for title art
let itemHeight = 30;
mapNames.forEach((name, index) => {
menuItems.push({
text: `${index + 1}: ${name}`,
mapName: name,
x: 50,
y: startY + index * itemHeight,
w: canvasWidth - 100,
h: itemHeight
});
});
// Add Quit option (though less functional in browser)
menuItems.push({
text: `${mapNames.length + 1}: Quit (closes tab if allowed)`,
mapName: null, // Special marker for quit
x: 50,
y: startY + mapNames.length * itemHeight,
w: canvasWidth - 100,
h: itemHeight
});
}
function draw() {
background(50); // Dark gray background
if (gameState === STATE_MENU) {
drawMenu();
} else if (gameState === STATE_PLAYING) {
drawGame();
} else if (gameState === STATE_VICTORY) {
drawVictoryScreen();
} else if (gameState === STATE_CONFIRM) {
drawConfirmScreen();
}
// Display persistent messages
if (message && millis() < messageEndTime) {
fill(255, 255, 0); // Yellow for messages
textAlign(CENTER, BOTTOM);
textSize(18);
text(message, canvasWidth / 2, canvasHeight - 10);
} else if (message) {
clearMessage(); // Clear expired message
}
}
function drawMenu() {
// Title Art
fill(100, 255, 100); // Greenish title
textSize(16);
textAlign(CENTER, TOP);
let titleY = 30;
for (let i = 0; i < TITLE_ART_LINES.length; i++) {
text(TITLE_ART_LINES[i], canvasWidth / 2, titleY + i * 20);
}
// Menu Items
textSize(20);
textAlign(LEFT, CENTER);
fill(220); // Light gray for menu text
menuItems.forEach(item => {
// Basic hover effect
if (mouseX > item.x && mouseX < item.x + item.w && mouseY > item.y - item.h/2 && mouseY < item.y + item.h/2) {
fill(255, 200, 0); // Highlight yellow
} else {
fill(220);
}
text(item.text, item.x, item.y);
});
fill(200);
textAlign(CENTER, TOP);
text("Click to select a map or press the number key.", canvasWidth/2, menuItems[menuItems.length-1].y + 40);
}
function drawGame() {
// Draw the map
if (!currentMapData || currentMapData.length === 0) return;
let mapRows = currentMapData.length;
let mapCols = currentMapData[0].length;
// Center the map
let mapPixelWidth = mapCols * TILE_SIZE;
let mapPixelHeight = mapRows * TILE_SIZE;
let offsetX = (canvasWidth - mapPixelWidth) / 2;
let offsetY = (canvasHeight - mapPixelHeight) / 2 - 30; // Shift up a bit for messages
textAlign(CENTER, CENTER);
textSize(TILE_SIZE * 0.75); // Adjust emoji size
for (let r = 0; r < mapRows; r++) {
for (let c = 0; c < mapCols; c++) {
let tile = currentMapData[r][c];
let x = offsetX + c * TILE_SIZE;
let y = offsetY + r * TILE_SIZE;
// Optional: draw a background for empty tiles if preferred
// if (tile === EMPTY) {
// fill(180);
// noStroke();
// rect(x, y, TILE_SIZE, TILE_SIZE);
// }
text(tile, x + TILE_SIZE / 2, y + TILE_SIZE / 2);
}
}
// Instructions
fill(200);
textAlign(CENTER, TOP);
textSize(16);
text("Use W/A/S/D to move. R to Reset, M for Menu.", canvasWidth / 2, 20);
}
function drawVictoryScreen() {
fill(255, 215, 0); // Gold
textAlign(CENTER, CENTER);
textSize(48);
text("Victory!", canvasWidth / 2, canvasHeight / 2 - 50);
textSize(24);
fill(220);
text("You made it!", canvasWidth / 2, canvasHeight / 2);
textSize(20);
text("Press M for Menu", canvasWidth / 2, canvasHeight / 2 + 50);
text("Press R to Reset this map", canvasWidth / 2, canvasHeight / 2 + 80);
}
function drawConfirmScreen() {
// Draw current game slightly dimmed
push();
tint(255, 150); // Apply transparency
drawGame(); // Draw the game behind the confirmation
pop();
// Confirmation dialog
let dialogWidth = canvasWidth * 0.6;
let dialogHeight = 150;
let dialogX = (canvasWidth - dialogWidth) / 2;
let dialogY = (canvasHeight - dialogHeight) / 2;
fill(30, 30, 30, 230); // Dark semi-transparent background
stroke(200);
rect(dialogX, dialogY, dialogWidth, dialogHeight, 10); // Rounded corners
fill(220);
noStroke();
textAlign(CENTER, CENTER);
textSize(20);
if (confirmAction) {
text(confirmAction.message, dialogX + dialogWidth / 2, dialogY + dialogHeight / 3);
}
textSize(18);
text("Press Y for Yes, N for No", dialogX + dialogWidth / 2, dialogY + dialogHeight * 0.66);
}
function keyPressed() {
if (gameState === STATE_PLAYING) {
let newR = playerPos.r;
let newC = playerPos.c;
let moved = false;
if (key === 'W' || keyCode === UP_ARROW) {
newR--;
moved = true;
} else if (key === 'S' || keyCode === DOWN_ARROW) {
newR++;
moved = true;
} else if (key === 'A' || keyCode === LEFT_ARROW) {
newC--;
moved = true;
} else if (key === 'D' || keyCode === RIGHT_ARROW) {
newC++;
moved = true;
} else if (key === 'R') {
confirmAction = { type: "RESET", message: "Reset the map? [Y/N]" };
confirmYesAction = () => {
loadMap(selectedMapName); // Reloads the current map
gameState = STATE_PLAYING;
};
confirmNoAction = () => { gameState = STATE_PLAYING; };
gameState = STATE_CONFIRM;
return;
} else if (key === 'M') {
confirmAction = { type: "MENU_RETURN", message: "Return to Menu? [Y/N]" };
confirmYesAction = () => { gameState = STATE_MENU; };
confirmNoAction = () => { gameState = STATE_PLAYING; };
gameState = STATE_CONFIRM;
return;
}
if (moved) {
// Check boundaries
if (newR < 0 || newR >= currentMapData.length || newC < 0 || newC >= currentMapData[0].length) {
displayMessage("You can't move off the map!");
return;
}
let destTile = currentMapData[newR][newC];
if (destTile === WALL) {
displayMessage("You can't move into a wall!");
} else if (destTile === GOAL) {
currentMapData[playerPos.r][playerPos.c] = EMPTY; // Old player spot becomes empty
currentMapData[newR][newC] = EMPTY; // Goal spot becomes empty (player "absorbs" it)
playerPos.r = null; // Signal player is gone (won)
playerPos.c = null;
gameState = STATE_VICTORY;
} else { // EMPTY
currentMapData[newR][newC] = PLAYER;
currentMapData[playerPos.r][playerPos.c] = EMPTY;
playerPos = { r: newR, c: newC };
clearMessage();
}
}
} else if (gameState === STATE_MENU) {
let choiceNum = parseInt(key);
if (!isNaN(choiceNum) && choiceNum >= 1 && choiceNum <= menuItems.length) {
let selectedItem = menuItems[choiceNum - 1];
if (selectedItem.mapName) {
loadMap(selectedItem.mapName);
} else {
// "Quit" selected
displayMessage("Quitting... (You might need to close the tab manually)", 3000);
// In a browser, we can't reliably close the window/tab via script
// window.close(); // Often blocked by browsers
noLoop(); // Stops p5 draw loop
}
}
} else if (gameState === STATE_VICTORY) {
if (key === 'M') {
gameState = STATE_MENU;
} else if (key === 'R') {
loadMap(selectedMapName); // Reloads the current map
}
} else if (gameState === STATE_CONFIRM) {
if (key === 'Y') {
if (confirmYesAction) confirmYesAction();
confirmAction = null;
confirmYesAction = null;
confirmNoAction = null;
} else if (key === 'N') {
if (confirmNoAction) confirmNoAction();
gameState = STATE_PLAYING; // Always return to playing if 'N'
confirmAction = null;
confirmYesAction = null;
confirmNoAction = null;
}
}
}
function mousePressed() {
if (gameState === STATE_MENU) {
menuItems.forEach(item => {
if (mouseX > item.x && mouseX < item.x + item.w &&
mouseY > item.y - item.h / 2 && mouseY < item.y + item.h / 2) {
if (item.mapName) {
loadMap(item.mapName);
} else {
// "Quit" selected
displayMessage("Quitting... (You might need to close the tab manually)", 3000);
noLoop();
}
}
});
}
// No mouse interaction needed for PLAYING, VICTORY, or CONFIRM based on current design
// (those use keyboard)
}