// alert("hello world");
const tileSize = 16;

// Sprites
let baba;
let tile, wall, rock, water, lava, flag;
let win, you, move, stop;

// CaracterMove
let babaX = 0;
let babaY = 0;

// Sounds
let bgm, winSound;
let isPlaying = false;

function preload(){
    bgm = loadSound("/assets/Baba Is You OST - Baba Is You Theme.mp3");
    baba = loadImage("/assets/Baba.webp");
    tile = loadImage("/assets/Tile.webp");
    wall = loadImage("/assets/Wall.webp");
    rock = loadImage("/assets/Rock.webp");
    water = loadImage("/assets/Water.webp");
    lava = loadImage("/assets/Lava.webp");
    flag = loadImage("/assets/Flag.webp");
}

function setup(){
    createCanvas(800, 400);
    background(51);
}

function draw(){
    // backgroundMusic();
    background(51);
    fill(255, 204, 0);
    image(baba, babaX, babaY, tileSize, tileSize);
    // squasre(babaX, babaY, tileSize);
}


// Background Music & Soundeffects
function backgroundMusic(){
    
}

function mousePressed(){
    if (isPlaying) {
        bgm.pause();
        isPlaying = false;
    } else {
        bgm.play();
        isPlaying = true;
    }
}

// Move ASDW
function keyPressed(){
    console.log(`key ${key} is pressed.`)
    // if(keyCode == 32){
    //     if(isPlaying){
    //         isPlaying = false;
    //     }else{
    //         isPlaying = true;
    //     }
    //     console.log(isPlaying);
    // }

    const maxX = width - tileSize;
    const maxY = height - tileSize;

    if(key=='a'||key=='A') {
        if (babaX - tileSize >= 0) {
            babaX -= tileSize;
            console.log('Left');
        }
    }
    if(key=='s'||key=='S') {
        if (babaY + tileSize <= maxY) {
            babaY += tileSize;
            console.log('Down');
        }
    }
    if(key=='d'||key=='D') {
        if (babaX + tileSize <= maxX) {
            babaX += tileSize;
            console.log('Right');
        }
    }
    if(key=='w'||key=='W') {
        if(babaY - tileSize >= 0){
            babaY -= tileSize;
            console.log('Up');
        }
    }
}