setup()
setup() runs once when the sketch starts. It's where you initialize variables, create UI elements, and prepare data structures. Notice how this sketch creates buttons and divs but hides many of them—they'll be shown/hidden later based on game state.
🔬 These lines create the left control button at the bottom-left. What happens if you change the position to (10, 10) and the size to (100, 100)? Where will the button move, and how much bigger will it get?
let leftButton = createButton('←');
leftButton.position(10, height - 70);
leftButton.size(50, 50);
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
textFont(moneyFont); // Set the loaded font for text() calls in WEBGL
// Initialize currentModelIndex and currentModel here, where p5.js is ready
currentModelIndex = 0;
currentModel = buildableModels[currentModelIndex];
console.log("Setup: currentModelIndex =", currentModelIndex, ", currentModel =", currentModel.name); // Verify initialization
// Create a button to reset the build process
resetButton = createButton('Reset Build');
resetButton.position(10, 10);
resetButton.mousePressed(resetBuild);
// Add text instructions
instructionsDiv = createElement('div', 'Tap anywhere on the canvas to add the next Lego block.');
instructionsDiv.style('color', '#333');
instructionsDiv.style('font-family', 'sans-serif');
instructionsDiv.style('position', 'absolute');
instructionsDiv.style('top', '10px');
instructionsDiv.style('left', '120px');
instructionsDiv.style('font-size', '14px');
instructionsDiv.style('max-width', '250px');
// Create "Build Another" button but hide it initially
buildAnotherButton = createButton('Build Another Vehicle'); // Updated text
buildAnotherButton.position(10, 10);
buildAnotherButton.mousePressed(buildNextModel); // Correctly calls buildNextModel
buildAnotherButton.hide(); // Hidden until a sale is made
// Create selection div but hide it initially
selectionDiv = createElement('div').id('selection-div');
selectionDiv.style('position', 'absolute');
selectionDiv.style('top', '50%');
selectionDiv.style('left', '50%');
selectionDiv.style('transform', 'translate(-50%, -50%)');
selectionDiv.style('padding', '20px');
selectionDiv.style('background', 'rgba(255, 255, 255, 0.9)');
selectionDiv.style('border-radius', '10px');
selectionDiv.style('box-shadow', '0 4px 8px rgba(0, 0, 0, 0.2)');
selectionDiv.style('text-align', 'center');
selectionDiv.style('font-family', 'sans-serif');
selectionDiv.style('z-index', '200');
selectionDiv.hide();
// Create Obby Game Controls
let leftButton = createButton('←');
leftButton.position(10, height - 70);
leftButton.size(50, 50);
leftButton.mousePressed(() => obbyPlayer.vel.x = -PLAYER_SPEED);
leftButton.mouseReleased(() => obbyPlayer.vel.x = 0);
leftButton.touchStarted(() => obbyPlayer.vel.x = -PLAYER_SPEED);
leftButton.touchEnded(() => obbyPlayer.vel.x = 0);
leftButton.hide();
obbyControls.push(leftButton);
let rightButton = createButton('→');
rightButton.position(70, height - 70);
rightButton.size(50, 50);
rightButton.mousePressed(() => obbyPlayer.vel.x = PLAYER_SPEED);
rightButton.mouseReleased(() => obbyPlayer.vel.x = 0);
rightButton.touchStarted(() => obbyPlayer.vel.x = PLAYER_SPEED);
rightButton.touchEnded(() => obbyPlayer.vel.x = 0);
rightButton.hide();
obbyControls.push(rightButton);
let jumpButton = createButton('Jump');
jumpButton.position(width - 110, height - 70);
jumpButton.size(100, 50);
jumpButton.mousePressed(obbyJump);
jumpButton.touchStarted(obbyJump);
jumpButton.hide();
obbyControls.push(jumpButton);
backToFactoryButton = createButton('Back to Factory');
backToFactoryButton.position(width / 2 - 75, height - 70);
backToFactoryButton.size(150, 50);
backToFactoryButton.mousePressed(exitObbyGame);
backToFactoryButton.hide();
// Initialize Obby Game
initializeObbyGame();
// Initial call to resetBuild to set up the first model
resetBuild();
}
Line-by-line explanation (11 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight, WEBGL);
Creates a full-window WEBGL canvas for 3D rendering of vehicle models
resetButton = createButton('Reset Build');
Creates interactive HTML buttons for resetting builds and controlling mini-games
obbyControls.push(leftButton);
Stores left, right, and jump buttons in an array for easy show/hide management during obby game
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a canvas that fills the entire window using WEBGL for 3D graphics. WEBGL is p5.js's 3D rendering context.
currentModelIndex = 0;- Initializes the starting vehicle index to 0 (the first helicopter in the buildableModels array)
currentModel = buildableModels[currentModelIndex];- Sets the current vehicle to build based on the index—this is the object we'll render and pull block steps from
resetButton = createButton('Reset Build');- Creates a clickable HTML button that will reset the current build when pressed
resetButton.mousePressed(resetBuild);- Attaches a callback so clicking the button calls the resetBuild() function
instructionsDiv = createElement('div', 'Tap anywhere on the canvas to add the next Lego block.');- Creates a text div with instructions that appears on-screen to guide the player
buildAnotherButton.hide();- Hides the 'Build Another Vehicle' button initially—it only appears after a sale
leftButton.size(50, 50);- Sets the left control button to 50×50 pixels, making it a clickable square for obby game control
leftButton.touchStarted(() => obbyPlayer.vel.x = -PLAYER_SPEED);- When the button is touched/pressed, set the AI player's horizontal velocity to move left
initializeObbyGame();- Pre-loads the obby game data (platforms, player, lava) so it's ready to play when unlocked
resetBuild();- Calls resetBuild() to initialize the first model and set the game to BUILDING state