/**
This activity uses conditionals to creat movemtn code with the mouse. Timer and random chances are used to call functions that
 create obstacles. The player must avoid the obstacles for as long as possible
 */

 //********************************Scene Object - Initialize When Scene Starts Event**********************//
 if ($this.scene.state() == "PLAY") {
    // set up the score variable
    $this.score = 0;
    // clone background image
    var farBackgroundClone = farBackground.clone();
    // place the cloned background image
    farBackgroundClone.y(0);
    farBackgroundClone.x(1000);
    
    // clone the background image
    var closeBackgroundClone = closeBackground.clone();
    // place the cloned background image
    closeBackgroundClone.y(0);
    closeBackgroundClone.x(1000);
    
    // every 2 seconds, spawn a new obstacle
    createTimer(2000, function() {
        var chance = random(100);
        if (chance <= 25) {
            $this.generateBottomObstacle();
        } else if(chance <= 50) {
            $this.generateTopObstacle();
        } else {
            $this.generateBottomObstacle();
            $this.generateTopObstacle();
        }
        // increase score after every spawn
        $this.score += 10; 
        scoreLabel.text($this.score);
    }); 
}

$this.generateBottomObstacle = function() {
    // clone the mite object
    var miteClone = mite.clone();
    // position it with random y
    miteClone.x(850);
    miteClone.y(random(350, 500));
    // activate the clone for collision
    miteClone.active = true;
};

$this.generateTopObstacle = function() {
    // clone the mite object
    var miteClone = mite.clone();
    // rotate it to flip its direction
    miteClone.rotation(180);
    // position it with random y
    miteClone.x(850);
    miteClone.y(random(100, 300));
    // activate the clone for collision
    miteClone.active = true;
}

 //*************************farBackground Object - Update Every Frame Event*******************************//
 // always move the background
$this.moveX();
// move the image when it goes completely off the screen
if ($this.x() <= -1000) {
    $this.x(1000);
}

//****************************closeBackground Object - Update Every Frame Event*****************************//
// always move the background
$this.moveX(); // provide more immersive feeling with parallax motion
/** objects close by seem to move faster than objects far away
closeBackground has faster speed than farBackground so when they 
 both move, it will give a sense of speed 
*/
if ($this.x() <= -1000) {
    $this.x(1000);
}

//****************************mite Object - Update Every Frame Event**************************************//
// always move the mite object
$this.moveX();

// if mite goes off screen, remove it
if ($this.x() < -50) {
    $this.remove();
}

// if the mite object is active and it's touching hotSpot
if ($this.active && $this.isTouching(hotSpot)) {
    //deactivate this mite object
    $this.active = false;
    // subtract points and update scoreLabel
    $this.scene.score -= 5; 
    scoreLabel.text($this.scene.score);
}

//****************************ninja Group - Update Every Frame Event**************************************//
// calculate how far away the mouse is from the ninja
var distanceToMouse = getMouseY() - $this.y();
hotSpot.z(0); // hide hotSpot behind ninja sprite
// if the mouse is above the ninja
// distanceToMouse will be negative
if (distanceToMouse < -5) {
    // move ninja up
    $this.moveY(-75);
}

if (distanceToMouse > 5) {
    // move ninja down
    $this.moveY(75);
}

// adjust the rotation of the ninja to tilt up and down
$this.rotation(distanceToMouse * 0.1);
