/**
Ninjas are presented with a completely blank scene and will have to build an entire 
activity from the ground up using everything that they’ve learned in Yellow Belt.
*/

//*********************Scene Object - Initialize When Scene Starts Event***********//
if ($this.scene.state() == "PLAY") {
    var nrock = rocks.clone();
    nrock.x(random(800));
    nrock.y(random(600));
}

//**************rock Object - Initialize When Scene Starts Event******************//
if ($this.scene.state() == "PLAY") {
    $this.speedX(random(50,-50));
    $this.speedY(random(50,-50));
    $this.rot = random(30,-30);
}

//******************rock Object - Update Every Frame Event*************************//
moveX($this);
moveY($this);
$this.spin($this.rot);
// keep rocks in the scene
if($this.y() > 600) {
    $this.y(0);
}
if($this.y() < 0) {
    $this.y(600);
}
if($this.x() > 800) {
    $this.x(0);
}
if($this.x() < 0) {
    $this.x(800);
}

// collision check 
//find the bullet on the scene
var bullet = $this.scene.projectile;

if(bullet && $this.isTouching(bullet)) {
    //instead of making the variable, we add to the score directly
    lblScore.text(parseInt(lblScore.text() + 10));

    $this.scaleX($this.scaleX()/2); //start to reduce the rock by 1/2
    $this.scaleY($this.scaleY()/2);
    $this.offsetX($this.offsetX()/2);
    $this.offsetY($this.offsetY()/2);

    if($this.scaleX() >= 0.125) {
        var nrock = $this.clone(); //split the rock as long as as it's bigger than 0.125
    } else {
        $this.remove();
    }

    $this.scene.projectile = null; //remove bullet and reseet value of scene projectile
    bullet.remove();
}

//*******************rocket Group - Update Every Frame Event***********************//
var leftPressed = isKeyPressed(Keys.leftArrow);
var rightPressed = isKeyPressed(Keys.rightArrow);
var upPressed = isKeyPressed(Keys.upArrow);

if(upPressed) { // if pressed, move rocket forward
    $this.moveForwardByRotation();
}

if(leftPressed) { // if pressed, rotate to the left
    $this.spin(-40);
}

if(rightPressed) {
    $this.spin(40);
}

// keep rocket in the scene
if ($this.y() > 600) {
    $this.y(0);
}
if ($this.y() < 0) {
    $this.y(600);
}
if ($this.x()>800) {
    $this.x(0);
}
if( $this.x()<0) {
    $this.x(800);
}
// projectile functionality - rocket group talks to bullet, creates a 'moving' boolean
//bullet is listening for moving to be true
var spacePresssed = isKeyPressed(Keys.space)
var bullet = $this.findName("bullet"); //find the active bullet on the scene

if(spacePressed) {
    if(!$this.scene.projectile) {
        var b = bullet.clone(true, true, $this.scene); //cloning the bullet from bullet

        b.x(bullet.getStagePos().x); //getting the x,y and rotation in the scene
        b.y(bullet.getStagePos().y);
        b.rotation($this.rotation());
        b.moving = true; //set boolean to true so bullet will excecute the code when moving is true
        b.z(1000); //ensure it's always on top of scene

        $this.scene.projectile = b; 
    }
}

//*****************spaceShip Object - Update Every Frame Event********************//
// animation code
var upPressed = isKeyPressed(Keys.upArrow);
if (upPressed) { 
    $this.animation("thrust"); 
    $this.incrementAnimation(); 
    
} else { 
    $this.animation("default"); 
    
}

//******************bullet Object - Update Every Frame Event**********************//
//the moving boolean created from rocket group and so it knows to shoot when the 
//space bar is pressed
if ($this.moving) { 
    $this.moveForwardByRotation(); 
    // condition here will prevent overloading the screen with bullets,
    // it checks for current bullets on the screen
    if ($this.y() > 600 || $this.y() < 0 || $this.x() < 0 || $this.x() > 800) { 
        $this.scene.projectile = null; 
        $this.remove(); // rock is removed when collision occurs
    } 
}

/**
Ninjas are presented with a completely blank scene and will have to build an entire 
activity from the ground up using everything that they’ve learned in Yellow Belt.
*/

//*********************Scene Object - Initialize When Scene Starts Event***********//
if ($this.scene.state() == "PLAY") {
    var nrock = rocks.clone();
    nrock.x(random(800));
    nrock.y(random(600));
}

//**************rock Object - Initialize When Scene Starts Event******************//
if ($this.scene.state() == "PLAY") {
    $this.speedX(random(50,-50));
    $this.speedY(random(50,-50));
    $this.rot = random(30,-30);
}

//******************rock Object - Update Every Frame Event*************************//
moveX($this);
moveY($this);
$this.spin($this.rot);
// keep rocks in the scene
if($this.y() > 600) {
    $this.y(0);
}
if($this.y() < 0) {
    $this.y(600);
}
if($this.x() > 800) {
    $this.x(0);
}
if($this.x() < 0) {
    $this.x(800);
}

// collision check 
//find the bullet on the scene
var bullet = $this.scene.projectile;

if(bullet && $this.isTouching(bullet)) {
    //instead of making the variable, we add to the score directly
    lblScore.text(parseInt(lblScore.text() + 10));

    $this.scaleX($this.scaleX()/2); //start to reduce the rock by 1/2
    $this.scaleY($this.scaleY()/2);
    $this.offsetX($this.offsetX()/2);
    $this.offsetY($this.offsetY()/2);

    if($this.scaleX() >= 0.125) {
        var nrock = $this.clone(); //split the rock as long as as it's bigger than 0.125
    } else {
        $this.remove();
    }

    $this.scene.projectile = null; //remove bullet and reseet value of scene projectile
    bullet.remove();
}

//*******************rocket Group - Update Every Frame Event***********************//
var leftPressed = isKeyPressed(Keys.leftArrow);
var rightPressed = isKeyPressed(Keys.rightArrow);
var upPressed = isKeyPressed(Keys.upArrow);

if(upPressed) { // if pressed, move rocket forward
    $this.moveForwardByRotation();
}

if(leftPressed) { // if pressed, rotate to the left
    $this.spin(-40);
}

if(rightPressed) {
    $this.spin(40);
}

// keep rocket in the scene
if ($this.y() > 600) {
    $this.y(0);
}
if ($this.y() < 0) {
    $this.y(600);
}
if ($this.x()>800) {
    $this.x(0);
}
if( $this.x()<0) {
    $this.x(800);
}
// projectile functionality - rocket group talks to bullet, creates a 'moving' boolean
//bullet is listening for moving to be true
var spacePresssed = isKeyPressed(Keys.space)
var bullet = $this.findName("bullet"); //find the active bullet on the scene

if(spacePressed) {
    if(!$this.scene.projectile) {
        var b = bullet.clone(true, true, $this.scene); //cloning the bullet from bullet

        b.x(bullet.getStagePos().x); //getting the x,y and rotation in the scene
        b.y(bullet.getStagePos().y);
        b.rotation($this.rotation());
        b.moving = true; //set boolean to true so bullet will excecute the code when moving is true
        b.z(1000); //ensure it's always on top of scene

        $this.scene.projectile = b; 
    }
}

//*****************spaceShip Object - Update Every Frame Event********************//
// animation code
var upPressed = isKeyPressed(Keys.upArrow);
if (upPressed) { 
    $this.animation("thrust"); 
    $this.incrementAnimation(); 
    
} else { 
    $this.animation("default"); 
    
}

//******************bullet Object - Update Every Frame Event**********************//
//the moving boolean created from rocket group and so it knows to shoot when the 
//space bar is pressed
if ($this.moving) { 
    $this.moveForwardByRotation(); 
    // condition here will prevent overloading the screen with bullets,
    // it checks for current bullets on the screen
    if ($this.y() > 600 || $this.y() < 0 || $this.x() < 0 || $this.x() > 800) { 
        $this.scene.projectile = null; 
        $this.remove(); // rock is removed when collision occurs
    } 
}

