Code viewer for World: New World (clone by Mark)
let player;
let orbs = [];
let chasers = [];
const NUM_ORBS = 10;
const NUM_CHASERS = 3;
const PLAYER_SIZE = 30;
const ORB_SIZE = 15;
const CHASER_SIZE = 25;

let oceanImg;  // For the ocean background

function preload() {
    oceanImg = loadImage('/uploads/starter/1000_F_430301909_5my973xumg509jNnMCNYydJuRxpsl3th.jpg');
}

function setup() {
    createCanvas(windowWidth, windowHeight);
    
    // Initialize player
    player = {
        x: width / 2,
        y: height / 2,
        size: PLAYER_SIZE,
        color: 'blue'
    };
    
    // Initialize orbs
    for(let i = 0; i < NUM_ORBS; i++) {
        orbs.push({
            x: Math.random() * width,
            y: Math.random() * height,
            size: ORB_SIZE,
            color: 'red',
            collected: false
        });
    }
    
    // Initialize chasers (AI orbs)
    for(let i = 0; i < NUM_CHASERS; i++) {
        chasers.push({
            x: Math.random() * width,
            y: Math.random() * height,
            size: CHASER_SIZE,
            color: 'green'
        });
    }
}

function draw() {
    // Draw the ocean background
    background(oceanImg);

    // Move player with arrow keys
    if (keyIsDown(LEFT_ARROW)) player.x -= 5;
    if (keyIsDown(RIGHT_ARROW)) player.x += 5;
    if (keyIsDown(UP_ARROW)) player.y -= 5;
    if (keyIsDown(DOWN_ARROW)) player.y += 5;

    // Wrap around logic
    player.x = (player.x + width) % width;
    player.y = (player.y + height) % height;

    // Draw the player
    fill(player.color);
    ellipse(player.x, player.y, player.size);

    // Draw orbs and check for collection
    for(let i = 0; i < orbs.length; i++) {
        let orb = orbs[i];
        if (!orb.collected) {
            fill(orb.color);
            ellipse(orb.x, orb.y, orb.size);
            let d = dist(player.x, player.y, orb.x, orb.y);
            if (d < (player.size / 2 + orb.size / 2)) {
                orbs[i].collected = true;
            }
        }
    }

    // Move and draw chasers
    for(let i = 0; i < chasers.length; i++) {
        let chaser = chasers[i];
        moveChaserTowardsPlayer(chaser);
        fill(chaser.color);
        ellipse(chaser.x, chaser.y, chaser.size);
        
        let d = dist(player.x, player.y, chaser.x, chaser.y);
        if (d < (player.size / 2 + chaser.size / 2)) {
            endGame("You were caught by a chaser! Press 'R' to restart or 'ESC' to exit.");
        }
    }

    // Check if all orbs are collected
    let allOrbsCollected = orbs.every(orb => orb.collected);
    if(allOrbsCollected) {
        endGame("Congratulations! You collected all orbs! Press 'R' to restart or 'ESC' to exit.");
    }
}

function moveChaserTowardsPlayer(chaser) {
    if (chaser.x < player.x) {
        chaser.x += 2;  // Chaser speed
    } else {
        chaser.x -= 2;
    }
    
    if (chaser.y < player.y) {
        chaser.y += 2;
    } else {
        chaser.y -= 2;
    }
}

function endGame(message) {
    noLoop();  // Stop the game loop
    fill(50);
    textSize(32);
    textAlign(CENTER, CENTER);
    text(message, width / 2, height / 2);
}

function keyPressed() {
    // Check if 'ESC' key is pressed to exit
    if (keyCode === ESCAPE) {
        window.close(); // Close the current window (might not work on all browsers due to security restrictions)
    }
    
    // Check if 'R' key is pressed to restart game
    if (key === 'R' || key === 'r') {
        location.reload(); // Reloads the page, thereby restarting the game
    }
}

function windowResized() {
    resizeCanvas(windowWidth, windowHeight);
}