setup()
setup() runs once and prepares every piece of mutable state - the sun, the shop data, the event timers, and button positions - before draw() starts running 60 times per second.
function setup() {
createCanvas(windowWidth, windowHeight);
sun = new Sun();
initWorkers();
scheduleNextEvent();
positionButtons();
positionShopButton();
lastPassiveUpdate = millis();
eventClock = millis();
}
Line-by-line explanation (8 lines)
createCanvas(windowWidth, windowHeight);- Makes the canvas fill the entire browser window.
sun = new Sun();- Creates the clickable Sun object, defaulting to its constructor's position.
initWorkers();- Fills the global workers array with 20 hireable character objects.
scheduleNextEvent();- Resets event state and starts the 5-minute countdown to the first random event.
positionButtons();- Places the coin and worker upgrade buttons in the bottom corners based on current window size.
positionShopButton();- Vertically centers the SHOP toggle button on the left edge.
lastPassiveUpdate = millis();- Records the current time so updatePassiveIncome() can measure elapsed time correctly from frame one.
eventClock = millis();- Records the starting timestamp used to compute delta time (dt) for the event system in draw().