setup()
setup() is called once when the sketch starts. It initializes all the major systems (audio, 3D rendering, physics, voxel grid, and event listeners) so the game is ready to run. Notice how it delegates most work to helper functions—this keeps the code organized.
function setup() {
p5Canvas = createCanvas(windowWidth, windowHeight);
p5Canvas.style('display', 'none');
voxelCountSpan = select('#voxel-count');
voxelCountSpan.html(totalVoxels);
initializeAudio();
initializeThreeJS();
initializeCannonJS();
generateVoxelWorld();
renderer.domElement.addEventListener('mousedown', onMouseDown, false);
renderer.domElement.addEventListener('mouseup', onMouseUp, false);
renderer.domElement.addEventListener('mousemove', onMouseMove, false);
renderer.domElement.addEventListener('touchstart', onTouchStart, false);
renderer.domElement.addEventListener('touchend', onTouchEnd, false);
renderer.domElement.addEventListener('touchmove', onTouchMove, false);
}
Line-by-line explanation (14 lines)
🔧 Subcomponents:
p5Canvas = createCanvas(windowWidth, windowHeight);
p5Canvas.style('display', 'none');
Creates a minimal p5.js canvas but hides it since three.js handles all rendering
voxelCountSpan = select('#voxel-count');
voxelCountSpan.html(totalVoxels);
Grabs the HTML element that displays remaining voxel count and initializes it to the total
initializeAudio();
initializeThreeJS();
initializeCannonJS();
generateVoxelWorld();
Calls the four major initialization functions that set up audio, rendering, physics, and the voxel grid
renderer.domElement.addEventListener('mousedown', onMouseDown, false);
renderer.domElement.addEventListener('mouseup', onMouseUp, false);
renderer.domElement.addEventListener('mousemove', onMouseMove, false);
renderer.domElement.addEventListener('touchstart', onTouchStart, false);
renderer.domElement.addEventListener('touchend', onTouchEnd, false);
renderer.domElement.addEventListener('touchmove', onTouchMove, false);
Registers all mouse and touch event handlers so clicks/taps detect voxel destruction
p5Canvas = createCanvas(windowWidth, windowHeight);- Creates a minimal p5.js canvas that fills the window. We store a reference in p5Canvas but won't draw on it.
p5Canvas.style('display', 'none');- Hides the p5.js canvas completely so only the three.js canvas (which three.js appends to the body) is visible.
voxelCountSpan = select('#voxel-count');- Uses p5.js's select() function to grab the HTML element with id 'voxel-count' so we can update the display later.
voxelCountSpan.html(totalVoxels);- Sets the initial text content of the counter to show all 1000 voxels at the start.
initializeAudio();- Sets up tone.js and waits for the first user interaction to start the audio context (required by browsers).
initializeThreeJS();- Creates the three.js scene, camera, renderer, lights, and OrbitControls for 3D viewing and interaction.
initializeCannonJS();- Sets up the cannon.js physics world with gravity and collision detection optimization.
generateVoxelWorld();- Loops through all 1000 grid positions and creates a colored mesh and physics body for each voxel.
renderer.domElement.addEventListener('mousedown', onMouseDown, false);- Tells the browser to call onMouseDown() when the user presses the mouse button, to detect the start of a click or drag.
renderer.domElement.addEventListener('mouseup', onMouseUp, false);- Tells the browser to call onMouseUp() when the user releases the mouse button, to detect the end of interaction and perform destruction.
renderer.domElement.addEventListener('mousemove', onMouseMove, false);- Tells the browser to call onMouseMove() when the user moves the mouse while a button is held, to distinguish drags from clicks.
renderer.domElement.addEventListener('touchstart', onTouchStart, false);- Registers the mobile touch start event, marking the beginning of a potential tap or drag.
renderer.domElement.addEventListener('touchend', onTouchEnd, false);- Registers the mobile touch end event, performing destruction if it was a tap and not a drag.
renderer.domElement.addEventListener('touchmove', onTouchMove, false);- Registers the mobile touch move event to detect if the user is dragging the camera rather than tapping.