/**
 Decompose Robot Builder
* Make robot parts draggable
* Robot parts snap in correct spots
* Robot parts bounce back when incorrect
* findDistance() function will require teaching
* Review of .x() and .y() and how they work
*/

//All parts require $this.draggable(true); at Initialize When Scene Starts

//*************body Object - Mouse Button Up Event********************//
//find distance between body part and bodySpace
//store it in variable called distance
var distance = $this.findDistance(bodySpace);
if (distance < 20) { //if it's dragged close enough to bodySpace
    //snap it to x and y of bodySpace
    $this.x(bodySpace.x());
    $this.y(bodySpace.y());
    //make it no longer draggable
    $this.draggable(false); //Ninja Supplies
} else { //was dragged to the wrong spot
    //snap it back to where it started
    //Hungry Ninja code
    $this.x(616);
    $this.y(42);
}

//***********arm1 Object - Mouse Button Up Event**********************//
//find distance between arm1 part and arm1Space
//store it in variable called distance
var distance = $this.findDistance(arm1Space);
if (distance < 20) { //if it's dragged close enough to arm1Space
    //snap it to x and y of arm1Space
    $this.x(arm1Space.x());
    $this.y(arm1Space.y());
    //make it no longer draggable
    $this.draggable(false); //Ninja Supplies
} else { //was dragged to the wrong spot
    //snap it back to where it started
    //Hungry Ninja code
    $this.x(634);
    $this.y(244);
}

//*************arm2 Object - Mouse Button Up Event********************//
//find distance between arm2 part and arm2Space
//store it in variable called distance
var distance = $this.findDistance(arm2Space);
if (distance < 20) { //if it's dragged close enough to arm2Space
    //snap it to x and y of arm2Space
    $this.x(arm2Space.x());
    $this.y(arm2Space.y());
    //make it no longer draggable
    $this.draggable(false); //Ninja Supplies
} else { //was dragged to the wrong spot
    //snap it back to where it started
    //Hungry Ninja code
    $this.x(44);
    $this.y(242);
}

//*************head Object - Mouse Button Up Event********************//
//find distance between head part and headSpace
//store it in variable called distance
var distance = $this.findDistance(headSpace);
if (distance < 20) { //if it's dragged close enough to headSpace
    //snap it to x and y of headSpace
    $this.x(headSpace.x());
    $this.y(headSpace.y());
    //make it no longer draggable
    $this.draggable(false); //Ninja Supplies
} else { //was dragged to the wrong spot
    //snap it back to where it started
    //Hungry Ninja code
    $this.x(512);
    $this.y(408);
}

//*************leg1 Object - Mouse Button Up Event**********************//
//find distance between leg1 part and leg1Space
//store it in variable called distance
var distance = $this.findDistance(leg1Space);
if (distance < 20) { //if it's dragged close enough to leg1Space
    //snap it to x and y of leg1Space
    $this.x(leg1Space.x());
    $this.y(leg1Space.y());
    //make it no longer draggable
    $this.draggable(false); //Ninja Supplies
} else { //was dragged to the wrong spot
    //snap it back to where it started
    //Hungry Ninja code
    $this.x(58);
    $this.y(66);
}

//****************leg2 Object - Mouse Button Up Event*******************//
//find distance between leg2 part and leg2Space
//store it in variable called distance
var distance = $this.findDistance(leg2Space);
if (distance < 20) { //if it's dragged close enough to leg2Space
    //snap it to x and y of leg2Space
    $this.x(leg2Space.x());
    $this.y(leg2Space.y());
    //make it no longer draggable
    $this.draggable(false); //Ninja Supplies
} else { //was dragged to the wrong spot
    //snap it back to where it started
    //Hungry Ninja code
    $this.x(150);
    $this.y(476);
}

//************Scene Object - Initialize When Scene Starts***************//
//Make a variable to keep track of how many parts were snapped in correctly
//Also Add to Scene a label called message to display text
$this.correct = 0;
message.text("Welcome to Robot Building v.1.0");

//***********Scene Object - Update Every Frame************************//
//We will check if correct = 6 every frame 
//When it's 6, display congratulatory message
if($this.correct == 6) {
    message.text("Nice work, you finished!")
}