/**
 * Game Objects required: add Text object named "message"
 */
//*****************Scene Object - Initialize When Scene Starts Event***************//
// world set up and randomization
if ($this.scene.state() == "PLAY") {
    // iterate 8 columns
    for (var x = 1; x < 8; x++) {
        // iterates 6 rows
        for (var y = 1; y < 6; y++) {
            // place wall at random locations
            if(random(100) < 25) {
                var clonedWall = wall.clone();
                // use wall width and height to place properly
                clonedWall.x(100 * x);
                clonedWall.y(100 * y)
            } else if (random(100) < 15) {
                var clonedCoin = coin.clone();
                clonedCoin.x(100 * x);
                clonedCoin.y(100 * y);
            }
            
        }
    }
}

//********************wall Object - Update Every Frame Event***********************//
// remove wall if touching stairs
if ($this.isTouching(stairs)) {
    $this.remove();
}

// if the wall is touching the ninja
if ($this.isTouching(ninja)) {
    // check the direction of movement
    switch (ninja.moveDirection) {
        case ("up"):
            ninja.y(ninja.y() + 5);
            break;
        case("down"):
            ninja.y(ninja.y() - 5 );
            break;
        case("left"):
            ninja.x(ninja.x() + 5);
            break;
        case("right"):
            ninja.x(ninja.x() - 5);
            break;
    }
}

//*********************coin Object - Update Every Frame Event**********************//
// remove coin if touching stairs
if($this.isTouching(stairs)) {
    $this.remove();
}

// animate the coin
$this.incrementAnimation();

// if the ninja collects a coin
// increment the ninja's coin total
// and remove the coin
if ($this.isTouching(ninja)) {
    ninja.coins += 1;
    $this.remove();
}

//*******************ninja Object - Update Every Frame Event***********************//
// new method of moving sprite introduced
// check to see what arrow keys are pressed
var upPressed = isKeyPressed(Keys.upArrow) || isKeyPressed(Keys.w);
var downPressed = isKeyPressed(Keys.downArrow) || isKeyPressed(Keys.s);
var leftPressed = isKeyPressed(Keys.leftArrow) || isKeyPressed(Keys.a);
var rightPressed = isKeyPressed(Keys.rightArrow) || isKeyPressed(Keys.d);

// store the ninja's direction as a string
$this.moveDirection = "";

// determine which key is pressed
// and update the moveDirection variable
if (upPressed && $this.y() > 0) {
    $this.moveDirection = "up";
}

if (downPressed && $this.y() < 520) {
    $this.moveDirection = "down";
}

if (leftPressed && $this.x() >0) {
    $this.moveDirection = "left";
}

if (rightPressed && $this.x() < 720) {
    $this.moveDirection = "right";
}

// switch on moveDirection variable
switch ($this.moveDirection) {
    // move the ninja down
    case "down":
        $this.moveY(100);
        break;
    // move the ninja up
    case "up":
        $this.moveY(-100);
        break;
    // move the ninja left
    case "left":
        $this.moveX(-100);
        break;
    // move the ninja right
    case "right":
        $this.moveX(100);
        break;
}

//******************ninja Object - Initialize When Scene Starts Event************//
// set up variable to track coins
$this.coins = 0;

//*******************stairs Object - Update Every Frame Event********************//
// if the ninja reaches the stairs 
if ($this.isTouching(ninja)) {
    message.text("You reached the exit with " + ninja.coins + " coins.");
    message.z(10);
    ninja.remove();
}
