setup()
setup() runs once when the game starts. It initializes all global variables, creates the low-res graphics buffer, spawns the player and world entities, and defines the quest list. This is where game parameters (world dimensions, physics constants, initial positions) are set.
function setup() {
pixelDensity(1);
createCanvas(windowWidth, windowHeight);
pg = createGraphics(worldWidth, worldHeight);
calculateScaleFactor();
player = null;
worldHouses = [];
worldObstacles = [];
roomObstacles = [];
roomFish = [];
quests = [];
currentQuestIndex = 0;
showQuestCompleteMessage = false;
questCompleteTimer = 0;
showDialogueMessage = false;
dialogueMessage = "";
dialogueTimer = 0;
currentInteriorHouse = null;
// Initialize player in the water in the world
player = new Human(worldWidth / 4, surfaceY + 20);
// Spawn houses on land and assign IDs, ensuring no overlap
let currentHouseX = 50; // Starting X position for the first house, giving more space
const minHouseGap = 10; // Minimum pixel gap between houses
for (let i = 0; i < 10; i++) {
const tempHouse = new House(0, surfaceY, i);
const houseWidth = tempHouse.width;
let safeX = currentHouseX + houseWidth / 2 + random(0, minHouseGap);
safeX = constrain(safeX, houseWidth / 2, worldScrollableWidth - houseWidth / 2);
const newHouse = new House(safeX, surfaceY, i);
worldHouses.push(newHouse);
worldObstacles.push(newHouse); // Houses are also world obstacles
// Lock all houses except House #1 initially
if (i !== 1) {
newHouse.isLocked = true;
}
currentHouseX = safeX + houseWidth / 2 + minHouseGap;
if (currentHouseX > worldScrollableWidth - houseWidth / 2) {
break;
}
}
// Define quests
if (worldHouses.length > 1) quests.push({ description: "Flood House #1 and scare the residents!", targetHouseId: 1, isCompleted: false });
if (worldHouses.length > 2) quests.push({ description: "Use the key to unlock House #2!", targetHouseId: 2, isCompleted: false, requiresKey: true });
if (worldHouses.length > 2) quests.push({ description: "Flood House #2 and reclaim more land!", targetHouseId: 2, isCompleted: false });
if (worldHouses.length > 5) quests.push({ description: "Flood House #5 for the glory of the deep!", targetHouseId: 5, isCompleted: false });
if (worldHouses.length > 8) quests.push({ description: "Flood House #8, one last push for the sea!", targetHouseId: 8, isCompleted: false });
// Spawn world underwater obstacles
for (let i = 0; i < 15; i++) {
worldObstacles.push(new Obstacle(random(worldScrollableWidth), random(surfaceY + 10, worldHeight - 10)));
}
gameState = "playing";
enterRoom("world"); // Start in the world room
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
for (let i = 0; i < 10; i++) {
Creates up to 10 randomly-positioned houses along the world, ensuring they don't overlap by tracking the previous house position
if (worldHouses.length > 1) quests.push(...)
Populates the quest array with story objectives, checking that enough houses exist before defining quests
for (let i = 0; i < 15; i++) {
Scatters 15 random rocks and objects underwater to fill the world with visual interest and obstacles
pixelDensity(1);- Disables high-DPI scaling so pixels stay crisp and blocky on retina screens—essential for pixel-art games.
createCanvas(windowWidth, windowHeight);- Creates a full-screen canvas that fills the browser window.
pg = createGraphics(worldWidth, worldHeight);- Creates a low-resolution off-screen graphics buffer (320x180) where all game visuals are drawn before scaling up.
calculateScaleFactor();- Calculates how much to scale the 320x180 buffer to fill the screen, maintaining aspect ratio.
player = new Human(worldWidth / 4, surfaceY + 20);- Spawns the player one-quarter across the world width, slightly below the water surface to start submerged.
let safeX = currentHouseX + houseWidth / 2 + random(0, minHouseGap);- Calculates a new house X position, adding randomness so houses are spaced unpredictably but never overlap.
if (i !== 1) { newHouse.isLocked = true; }- Locks all houses except House #1 so the player must unlock them via quests or keys.
enterRoom("world");- Initializes the first room as 'world', populating room-specific obstacles and fish arrays.