/**

Decomposition: 
* Ninja jumps in y direction when space is pressed
* Ninja is animated every frame

* rock moves from right to left
* lose a point if the rock touches the ninja

* shuriken moves from right to left 
* shuriken y position is randomized
* lose a point if shuriken touches ninja

* Scene has ninja jump code
* Scene has rock spawn code
* Scene has shuriken spawn code
* Updates the score

 */

//****************Scene Object - Initialize When Scene Starts Event*****************//
if($this.scene.state() == "PLAY") {
    // set up score variables
    $this.score = 0;
    // timer for 2 seconds, spawn a new obstacle
    createTimer(2000, function() {
        var chance = random(100);
        if (chance <= 25) {
            $this.generateStone()
        } else if(chance <= 75) {
            $this.generateShuriken();
        } else {
            $this.generateStone();
            $this.generateShuriken();
        }
        // increase score after every spawn
        $this.score += 10;
        scoreLabel.text($this.score);
    });
}

$this.generateStone = function() {
    // clone the stone object
    var stoneClone = stone.clone();
    // position it off the screen
    stoneClone.x(850);
    // activate the clone for collision
    stoneClone.active = true; 
};

$this.generateShuriken = function() {
    //clone the shuriken object
    var shurikenClone = shuriken.clone();
    // position it with a random y
    shurikenClone.x(850);
    shurikenClone.y(random(150,400));
    // randomize the speedX
    shurikenClone.speedX(random(-200,-100));
    // activate the clone for collision
    shurikenClone.active = true; 
}; 

//*********************Stone Object - Update Every Frame Event********************//
// always be moving
$this.moveX();

// if object goes off the screen, remove it
if ($this.x() < -150) {
    $this.remove();
}

// if the object is active and it touches ninja
if ($this.active && $this.isTouching(ninja)) {
    //deactivate object
    $this.active = false;
    //subtract points and update the UI
    $this.scene.score -= 5;
    scoreLabel.text($this.scene.score);
}

//*********************Shuriken Object - Update Every Frame***********************//
// always be moving
$this.moveX();

// if the object goes off screen, remove it
if ($this.x() < 150) {
    $this.remove();
}

// if the object is active and touchs the ninja
if ($this.active && $this.isTouching(ninja)) {
    // deactivate this object
    $this.active = false;
    // subtract points and update the UI
    $this.scene -= 5;
    scoreLabel.text($this.scene.score);
}

//***********************Scene - Update Every Frame*********************************//
//check to see if spacebar is pressed
var spaceIsPressed = isKeyPressed(Keys.space);

// if the spacebar is pressed
// and ninja is not already jumping
if (spaceIsPressed && !ninja.isJumping) {
    ninja.velocityY = -450;
    ninja.isJumping = true; 
}

//*****************ninja Object - Initialize When Scene Starts Event*****************//

$this.velocityY = 0;
$this.isJumping = false;

//********************ninja Object - Update Every Frame Event***********************//
$this.incrementAnimation();

// if the ninja is jumping
if (this.isJumping) {
    // move the ninja based on its velocity
    $this.moveY($this.velocityY);

    //change the y velocity due to gravity
    $this.velocityY += 10;
}

// if the ninja touches the ground
if ($this.y() > 500) {
    $this.isJumping = false;
}

