setup()
setup() runs once when the sketch starts. It initializes three major systems: three.js graphics, cannon.js physics, and tone.js audio. It also connects all event listeners so clicks, touches, and keyboard presses trigger the right handlers. The keys object is the heart of continuous input—by storing keydown/keyup state, we can check any key's status during draw() without waiting for events.
function setup() {
p5Canvas = createCanvas(windowWidth, windowHeight);
p5Canvas.style('display', 'none'); // hide p5 canvas
voxelCountSpan = select('#voxel-count');
survivorStatusSpan = select('#survivor-status');
if (survivorStatusSpan) survivorStatusSpan.html('Alive');
weaponSelector = select('#weapon-selector');
weaponSelector.changed(onWeaponChange);
// Custom keyboard listeners
window.addEventListener('keydown', (e) => {
keys[e.code] = true;
// prevent page scroll on space / arrows / WASD
if (['Space', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight',
'KeyW', 'KeyA', 'KeyS', 'KeyD'].includes(e.code)) {
e.preventDefault();
}
});
window.addEventListener('keyup', (e) => {
keys[e.code] = false;
});
initializeAudio();
initializeThreeJS();
initializeCannonJS();
generateVoxelWorld();
createSurvivorPlayer();
// Mouse / touch events on the three.js canvas
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 (8 lines)
🔧 Subcomponents:
window.addEventListener('keydown', (e) => { keys[e.code] = true; ... });
Records which keys are currently pressed so applyPlayerInput() can check them every frame
renderer.domElement.addEventListener('mousedown', onMouseDown, false);
Connects click/tap events to weapon firing and interaction handlers
p5Canvas = createCanvas(windowWidth, windowHeight);- Creates the p5.js canvas at full window size, even though we hide it—p5 still runs in the background
p5Canvas.style('display', 'none'); // hide p5 canvas- Hides the p5 canvas because three.js renderer draws on top of it via renderer.domElement
voxelCountSpan = select('#voxel-count');- Grabs the HTML element that displays how many voxels remain in the game
weaponSelector.changed(onWeaponChange);- Tells the select dropdown to call onWeaponChange() whenever the player switches weapons
keys[e.code] = true;- When a key is pressed, stores its code (like 'KeyW') in the keys object to track active keys
e.preventDefault();- Stops the browser from scrolling when you press Space or arrow keys, so you can use them for movement
initializeAudio(); initializeThreeJS(); initializeCannonJS(); generateVoxelWorld(); createSurvivorPlayer();- Calls helper functions that set up the audio system, 3D scene, physics engine, voxel grid, and survivor character in sequence
renderer.domElement.addEventListener('mousedown', onMouseDown, false);- Binds the three.js canvas to listen for mouse and touch events so clicks trigger weapon firing