setup()
setup() runs once when the sketch loads. It configures the canvas, camera, and all initial game state. The asynchronous font loading allows the sketch to start immediately even if the internet is slow.
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
textSize(24);
textAlign(CENTER, CENTER);
perspective(Math.PI / 3, width / height, 1, 10000);
setAttributes({ depth: true });
// Load font ASYNC — sketch starts immediately, text appears once font loads
loadFont(
'https://cdn.jsdelivr.net/fontsource/fonts/vt323@latest/latin-400-normal.woff',
function(f) { gameFont = f; textFont(f); },
function(err) { console.warn('Font failed to load, using fallback'); }
);
generateBuildings();
resetGame();
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight, WEBGL);
Creates a WEBGL canvas that fills the entire window, enabling 3D rendering
perspective(Math.PI / 3, width / height, 1, 10000);
Sets up 3D perspective with 60-degree field of view and depth range from 1 to 10000 units
loadFont('https://cdn.jsdelivr.net/fontsource/fonts/vt323@latest/latin-400-normal.woff', function(f) { gameFont = f; textFont(f); }, function(err) { console.warn('Font failed to load, using fallback'); });
Loads a custom font from a CDN without blocking sketch execution; gracefully falls back if font fails
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a WEBGL canvas (3D mode) that stretches to fill the entire browser window
textSize(24);- Sets the default text size to 24 pixels for any text drawn on screen
textAlign(CENTER, CENTER);- Centers all text both horizontally and vertically around its coordinate
perspective(Math.PI / 3, width / height, 1, 10000);- Configures the 3D camera's perspective with a 60-degree field of view; only objects between depth 1 and 10000 are visible
setAttributes({ depth: true });- Enables depth testing so closer 3D objects appear in front of farther ones
loadFont('https://cdn.jsdelivr.net/fontsource/fonts/vt323@latest/latin-400-normal.woff', function(f) { gameFont = f; textFont(f); }, function(err) { console.warn('Font failed to load, using fallback'); });- Asynchronously loads a custom retro font from the internet; if it loads, use it; if it fails, the sketch keeps running without the custom font
generateBuildings();- Calls the helper function to create random city buildings
resetGame();- Initializes both characters' positions, health, and velocities to starting values