Skip to content

Instantly share code, notes, and snippets.

@cie
Last active February 14, 2025 09:59
Show Gist options
  • Save cie/50051d74d5e61162f988fe6b4cc0ae79 to your computer and use it in GitHub Desktop.
Save cie/50051d74d5e61162f988fe6b4cc0ae79 to your computer and use it in GitHub Desktop.
p5play scene manager
export function setup(level) {
let text = new Sprite();
text.color = "transparent";
text.stroke = "transparent";
text.text = `Level ${level}`;
text.textSize = 50;
text.textColor = "white";
text.life = 60;
}
export function update() {
if (mouse.presses()) {
loadScene("welcome");
}
}
export function drawFrame() {
background("black");
}
function setup() {
// global setup - add your global setup here
new Canvas(800, 600);
displayMode("maxed", "pixelated");
loadScene("welcome");
}
let currentScene;
async function loadScene(name, ...setupArgs) {
currentScene = await import(`./${name}.js`);
// clear everything - add your cleanup here
allSprites.remove();
world.gravity = new Vector(0, 0);
await currentScene.setup?.(...setupArgs);
}
function update() {
currentScene?.update?.();
}
function drawFrame() {
currentScene?.drawFrame?.();
}
export function setup() {
let text = new Sprite();
text.color = "transparent";
text.stroke = "transparent";
text.text = "Click to start";
text.textSize = 50;
text.textColor = "white";
}
export function update() {
if (mouse.presses()) {
loadScene("level", 1);
}
}
export function drawFrame() {
background("purple");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment