setup()
setup() runs once when the sketch starts. It's the perfect place to initialize all objects, create the canvas, and set starting values. Notice how it calculates positions responsively based on windowWidth and windowHeight, so the layout adapts if the browser is resized.
function setup() {
createCanvas(windowWidth, windowHeight);
userStartAudio(); // Important for audio on mobile devices, even if no audio is used yet
// Define menu area
menuArea = {
x: windowWidth * 0.8,
y: 10,
w: windowWidth * 0.18,
h: windowHeight - 20
};
// Calculate restaurant size and position based on window size
let restaurantW = windowWidth * 0.4;
let restaurantH = windowHeight * 0.6;
let restaurantX = windowWidth * 0.3;
let restaurantY = windowHeight * 0.2;
restaurant = new Restaurant(restaurantX, restaurantY, restaurantW, restaurantH);
// Create bouncer instance first (its size is set in the constructor)
// Then calculate and set its initial position
bouncer = new Bouncer(0, 0); // Temporary position
bouncer.x = restaurant.orderingZone.x + restaurant.orderingZone.w + bouncer.size / 2;
bouncer.y = restaurant.orderingZone.y + restaurant.orderingZone.h / 2;
bouncer.targetX = bouncer.x; // Set target to initial position as well
bouncer.targetY = bouncer.y;
// Calculate table size and position
let tableW = restaurantW * 0.2;
let tableH = restaurantH * 0.2;
let paddingX = restaurantW * 0.05;
let paddingY = restaurantH * 0.05;
// Create 3 tables inside the restaurant
tables.push(new Table(restaurant.x + paddingX, restaurant.y + paddingY, tableW, tableH));
tables.push(new Table(restaurant.x + restaurantW - tableW - paddingX, restaurant.y + paddingY, tableW, tableH));
tables.push(new Table(restaurant.x + paddingX, restaurant.y + restaurantH - tableH - paddingY, tableW, tableH));
// Create some initial people outside the restaurant
// Capping at 200 people to maintain performance, as 100,000 would be too many for p5.js
for (let i = 0; i < 200; i++) {
// *** NEW CHANGE: 10% chance for a person to start with 0 money ***
let personStartingMoney = initialMoney;
if (random(1) < 0.1) { // 10% chance to be poor
personStartingMoney = 0;
}
people.push(new Person(random(width), random(height), personStartingMoney));
}
// Create the add food button
addFoodButton = createButton('Expand Menu');
addFoodButton.position(10, 10);
addFoodButton.size(150, 40);
addFoodButton.style('background-color', '#4CAF50'); // Green
addFoodButton.style('color', 'white');
addFoodButton.style('border', 'none');
addFoodButton.style('border-radius', '5px');
addFoodButton.style('font-size', '14px');
addFoodButton.mousePressed(addNewFoodToMenu); // Attach event handler
// *** Call addNewFoodToMenu automatically in setup() to pre-fill menu ***
addNewFoodToMenu();
}
Line-by-line explanation (13 lines)
🔧 Subcomponents:
restaurant = new Restaurant(restaurantX, restaurantY, restaurantW, restaurantH);
Instantiates the Restaurant class with calculated dimensions and positions, creating the main game environment
bouncer.x = restaurant.orderingZone.x + restaurant.orderingZone.w + bouncer.size / 2;
Places the bouncer next to the ordering zone so they can oversee customers placing orders
tables.push(new Table(restaurant.x + paddingX, restaurant.y + paddingY, tableW, tableH));
Creates three Table objects at different positions inside the restaurant, giving customers places to sit and eat
for (let i = 0; i < 200; i++) {
Spawns 200 customers with random positions and money (90% wealthy, 10% broke) to populate the initial simulation
addFoodButton = createButton('Expand Menu');
Creates an interactive button that lets players add more food items to the restaurant's menu
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window; windowWidth and windowHeight are built-in p5.js variables that auto-update
userStartAudio();- Initializes audio context on mobile devices—required before any sound can play (not used here but good practice)
menuArea = { x: windowWidth * 0.8, y: 10, w: windowWidth * 0.18, h: windowHeight - 20 };- Defines a rectangular menu area in the top-right corner (80% to the right, covering 18% width) where food items will be listed
restaurant = new Restaurant(restaurantX, restaurantY, restaurantW, restaurantH);- Creates the Restaurant object (a 40% × 60% rectangle centered on the canvas) that contains ordering zones, paying zones, and tables
bouncer = new Bouncer(0, 0);- Creates a Bouncer object at a temporary position; its final position is calculated next using the restaurant's dimensions
bouncer.x = restaurant.orderingZone.x + restaurant.orderingZone.w + bouncer.size / 2;- Positions the bouncer just to the right of the ordering zone so they can oversee customer orders (currently unused in the 'armed' mechanic)
tables.push(new Table(restaurant.x + paddingX, restaurant.y + paddingY, tableW, tableH));- Adds three Table objects to the tables array at calculated positions inside the restaurant, each with 2-person capacity
for (let i = 0; i < 200; i++) {- Loop that runs 200 times, spawning 200 initial customers scattered randomly across the canvas
if (random(1) < 0.1) { personStartingMoney = 0; }- 10% random chance that a customer starts with $0 (broke)—these customers will become armed when they try to order the expensive burger
people.push(new Person(random(width), random(height), personStartingMoney));- Creates a new Person object with random x/y position and the calculated money amount, adds it to the people array
addFoodButton = createButton('Expand Menu');- Creates an HTML button that players can click to add more food items to the restaurant menu
addFoodButton.mousePressed(addNewFoodToMenu);- Connects the button's click event to the addNewFoodToMenu() function so clicking expands the menu
addNewFoodToMenu();- Automatically calls the menu expansion function once at startup, so the full menu (pizza, burger, ice cream, etc.) is ready immediately