setup()
setup() runs once when the sketch starts. It initializes the canvas, collects player customization, and spawns all the initial characters into the world. The prompt() calls pause execution and wait for player input, making setup() interactive.
function setup() {
createCanvas(windowWidth, windowHeight);
textFont('Arial');
textAlign(CENTER, CENTER);
rectMode(CENTER);
houseArea.height = windowHeight;
houseEntrance.y = windowHeight / 2;
let colorInput = prompt("Choose your character's color (e.g., 'red', '#00FF00', or RGB like '255,100,0'):", playerCustomColor);
if (colorInput) playerCustomColor = colorInput;
let sizeInput = prompt("Choose your character's size (e.g., '30', '50'):", playerCustomSize);
if (sizeInput && !isNaN(sizeInput)) playerCustomSize = float(sizeInput);
let iconInput = prompt("Choose your character's emoji icon (e.g., '😊', '🚀', '👑'):", playerCustomIcon);
if (iconInput) playerCustomIcon = iconInput;
initializeTables();
player = new PlayerControlledCustomer(houseEntrance.x, houseEntrance.y, playerCustomColor, playerCustomSize, playerCustomIcon);
customers.push(player);
for (let i = 0; i < numInitialAICustomers; i++) {
customers.push(new Customer(random(-100, -50), random(height)));
}
for (let i = 0; i < numRobbers; i++) {
customers.push(new Robber(random(-100, -50), random(height)));
}
for (let i = 0; i < numPoliceOfficers; i++) {
customers.push(new PoliceOfficer(random(-100, -50), random(height)));
}
for (let i = 0; i < numBoatPeople; i++) {
customers.push(new BoatPerson(random(-100, -50), random(height)));
}
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-screen canvas and sets up text alignment and drawing modes for the entire sketch
let colorInput = prompt("Choose your character's color...");
Asks the player for their character's color, size, and emoji icon before spawning them into the world
for (let i = 0; i < numInitialAICustomers; i++) { customers.push(new Customer(...)); }
Creates the initial population of customers, robbers, police, and boat people off-screen to enter the restaurant
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire window, making the sketch responsive to screen size
textFont('Arial');- Sets the font to Arial, which works well with emoji characters and text rendering
textAlign(CENTER, CENTER);- Aligns all text to be centered horizontally and vertically, so coordinates represent the text's center point
rectMode(CENTER);- Makes all rectangles drawn with rect() use their first two arguments as the center point instead of the top-left corner
houseArea.height = windowHeight;- Sets the house area's height to match the full window height, making the house extend the full vertical span
let colorInput = prompt("Choose your character's color...");- Opens a browser dialog asking the player to enter a color name, hex code, or RGB values for their character
if (colorInput) playerCustomColor = colorInput;- If the player enters a color (doesn't cancel), updates the global playerCustomColor variable
player = new PlayerControlledCustomer(houseEntrance.x, houseEntrance.y, playerCustomColor, playerCustomSize, playerCustomIcon);- Creates the player's character at the house entrance using their customization choices, then stores a reference in the global player variable
customers.push(player);- Adds the player to the customers array so they are updated and displayed each frame along with all AI characters
for (let i = 0; i < numInitialAICustomers; i++) { customers.push(new Customer(...)); }- Loops 19 times, creating 19 AI customer objects and adding them to the customers array, all starting off-screen on the left