//Lab exercise for Peabody 5th grade - ball velocity
//Click to change direction of arrow and press 'enter' to see basketball move
//Students must fill in the velocity for directions SW, S, SE, and E and write
//the case for (North-west (NW)

final int N = 0, NE = 1, E = 2, SE = 3, S = 4, SW = 5, W = 6, NW = 7;
int xPos, yPos;
int xVel, yVel;
float tan;
boolean animate;
int dir;

void setVelocity() {
  if (dir == N) {
    xVel = 0;
    yVel = -1;
  }
  else if (dir == NE) {
    xVel = 1;
    yVel = -1;
  }
  else if (dir == E) {
    xVel = 1;
    yVel = 0;
  }    //TODO: FIX in velocities for these directions
  else if (dir == SE) {
    xVel = 0;
    yVel = 0;
  }
  else if (dir == S) {
    xVel = 0;
    yVel = 0;
  }
    else if (dir == SW) {
    xVel = 0;
    yVel = 0;
  }
  else if (dir == W) {
    xVel = 0;
    yVel = 0;
  }
  //TODO: Fill in all the code to set velocities for the final direction, NW
}

void setup() {
  background(255, 255, 255);
  size(400, 400);
  xPos = 200;
  yPos = 200;
  animate = false;
  dir = 0;
}

void drawArrow() {
  int degree = dir * 45;
  
  strokeWeight(6);
  stroke(32, 32, 32);
  fill(32, 32, 32);
  pushMatrix();
    translate(xPos, yPos);
    rotate(degree * PI / 180);
    translate(-xPos, -yPos);
    line(xPos, yPos, xPos, yPos - 50);
    triangle(xPos, yPos - 50, xPos - 10, yPos - 30, xPos + 10, yPos - 30);
  popMatrix();
}

void drawBasketball() {
  strokeWeight(1);
  stroke(0, 0, 0);
  fill(255, 128, 0);
  //TODO - if you finish everything else, you can change what the program draws, as long
  //as you use xPos and yPos to control where the object draws!!!
  //this draws a basketball
  ellipse(xPos, yPos, 80, 80);
  bezier(xPos - 27, yPos - 27, xPos - 10, yPos, xPos - 10, yPos, xPos - 27, yPos + 27);
  bezier(xPos + 27, yPos - 27, xPos + 10, yPos, xPos + 10, yPos, xPos + 27, yPos + 27);
  line(xPos - 40, yPos, xPos + 40, yPos);
  line(xPos, yPos - 40, xPos, yPos + 40);
 
}

void drawDirs() {
  int off = 30;
  int off2 = 20;
  textSize(24);
  text("NW", 0, off );
  text("N", width/2-off2, off);
  text("NE", width-off, off);
  text("E", width-off2, height/2);
  text("SE", width-off, height-off2);
  text("S", width/2-off2, height-off2);
  text("SW", 0, height-off2);
  text("W", 0, height/2);
}

void draw() {
  clear();
  background(255, 255, 255);
  setVelocity();
  if (animate == true) {
    xPos += xVel;
    yPos += yVel;
  }
  
  //reset position if ball reaches edge of screen
  if (xPos == 0 || yPos == 0 || xPos == 400 || yPos == 400) {
    animate = false;
    xPos = 200;
    yPos = 200;
  }
  
  drawBasketball();
  drawArrow();
  drawDirs();
}

void keyPressed() {
  if (key == ENTER) {
    animate = true;
    println("enter pressed");
  }
}

void mouseClicked() {
  dir = (dir + 1) % 8;
}