setup()
setup() runs once when the sketch starts. Here it wires up audio objects early (in a muted state) so that later calls to .amp() and .freq() don't fail, and it restores saved progress from localStorage before the game begins.
function setup() {
createCanvas(windowWidth, windowHeight);
pointerX = width / 2;
pointerY = height / 2;
// Load High Score
if (typeof Storage !== "undefined") {
let saved = localStorage.getItem("abyssalHighScore");
if (saved) highScore = int(saved);
}
// Initialize Audio Objects (suspended state)
setupAudio();
resetGame();
textFont("Courier New");
textAlign(CENTER, CENTER);
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
if (typeof Storage !== "undefined") {
Checks that the browser supports localStorage before trying to read a previously saved high score
createCanvas(windowWidth, windowHeight);- Makes the canvas fill the entire browser window, so the game works full-screen on desktop and mobile.
pointerX = width / 2;- Starts the pointer (where you aim the tether) at the horizontal center of the screen.
let saved = localStorage.getItem("abyssalHighScore");- Reads the previous high score string from the browser's persistent storage, if one exists.
if (saved) highScore = int(saved);- Converts the saved text back into a number using p5's int() and stores it in highScore.
setupAudio();- Creates all five oscillator/noise objects up front, in a silent state, so they're ready the instant the player interacts.
resetGame();- Initializes the tether, score, health, stamina, and bubbles for a fresh game.
textFont("Courier New");- Sets the monospace retro font used everywhere text is drawn.