setup()
setup() runs once at the start. In a game, use it to initialize canvas, set player and enemy positions, and enable input methods like pointer lock.
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
frameRate(60);
px = cellSize * 1.5; pz = cellSize * 1.5;
sx = cellSize * 8.5; sz = cellSize * 7.5;
canvas.onclick = () => { if(!document.pointerLockElement) requestPointerLock(); };
}
Line-by-line explanation (5 lines)
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a full-window 3D canvas using WEBGL mode, which enables 3D rendering and lighting
frameRate(60);- Locks the animation to 60 frames per second for smooth gameplay
px = cellSize * 1.5; pz = cellSize * 1.5;- Initializes the player's starting position at (600, 600) inside the maze—roughly cell (1,1)
sx = cellSize * 8.5; sz = cellSize * 7.5;- Initializes the stalker's starting position at (3400, 3000)—far away in the maze, in the upper right
canvas.onclick = () => { if(!document.pointerLockElement) requestPointerLock(); };- When the canvas is clicked, requests pointer lock so mouse movements control camera rotation instead of moving the cursor