//Avoid asteroid game - by Katie Davis and Zoe Wood May 2015 for PCS 5th grade
//Simple game with approximate collisions with falling asteroids - use the mouse
//to move the main character and avoid the asteroids
//hit the 'r' key to reset the game - when the creature is hit, the asteroids stop and
//the creature changes color
//TODO - students replace 'bee' with their own creature - note offSet collision values may
//need to be adjusted depending on the size of their creature
//
//Stars picture from http://khongthe.com/wallpapers/space/bright-cold-stars-of-space-244676.jpg

int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
int speed1, speed2, speed3, speed4, speed5;
final int picWidth = 432, picHeight = 768;
boolean collide;

// TODO: Fill in drawCreature to draw your own creature!
// Can you figure out how to change the color of your create when you 
// collide with the balls? 
// Hint: look for the line "if (collide)" as an example
void drawCreature(int tx, int ty)
{
  strokeWeight(1);
  pushMatrix();
  translate(15+tx, 15+ty);
  scale(2);
  rotate(180 * PI/180);
  translate(-15, -15);
  if (collide) {
    fill(204, 0, 0);
  }
  else {
    fill(245, 144, 12);
  }
  beginShape();
  vertex(11, 23);
  vertex(15, 23);
  vertex(17, 21);
  vertex(17, 19);
  vertex(16, 18);
  vertex(21, 18);
  vertex(23, 17);
  vertex(24, 15);
  vertex(23, 13);
  vertex(21, 13);
  vertex(19, 14);
  vertex(17, 15);
  vertex(16, 13);
  vertex(17, 11);
  vertex(17, 8);
  vertex(15, 6);

  vertex(14, 5);
  vertex(13, 4);
  vertex(12, 5);
  vertex(10, 6);
  vertex(9, 8);
  vertex(9, 11);
  vertex(10, 13);
  vertex(9, 15);
  vertex(7, 14);
  vertex(5, 13);
  vertex(3, 13);
  vertex(2, 15);
  vertex(3, 17);
  vertex(5, 18);
  vertex(10, 18);
  vertex(9, 19);

  vertex(9, 21);
  vertex(11, 23);
  endShape(CLOSE);

  stroke(0);
  line(10, 6, 15, 6);
  line(9, 8, 16, 8);
  line(9, 10, 17, 10);

  line(10, 12, 16, 12);
  line(9, 17, 3, 15);
  line(6, 16, 5, 13);
  line(6, 16, 4, 17);
  line(17, 17, 23, 15);
  line(19, 16, 21, 13);
  line(19, 16, 21, 18);

  fill(19, 100, 14);
  ellipse(10, 22, 3.25, 2.5);
  ellipse(16, 22, 3.25, 2.5);
  popMatrix();
}

void setup() {
  size(picWidth, picHeight);
  PImage starsPic = loadImage("stars.jpg");
  background(starsPic);

  y1 = y2 = y3 = y4 = y5 = 0;
  x1 = int(random(picWidth));
  x2 = int(random(picWidth));
  x3 = int(random(picWidth));
  x4 = int(random(picWidth));
  x5 = int(random(picWidth));
  speed1 = int(random(1, 8));
  speed2 = int(random(1, 8));
  speed3 = int(random(1, 8));
  speed4 = int(random(1, 8));
  speed5 = int(random(1, 8));

  mouseX = mouseY = 200;
  collide = false;
}

void updatePoints() {
  y1 += speed1; 
  y2 += speed2; 
  y3 += speed3; 
  y4 += speed4; 
  y5 += speed5; 

  if (y1 > picHeight) {
    y1 = 0;
    x1 = int(random(picWidth));
    speed1 = int(random(1, 8));
  }
  if (y2 > picHeight) {
    y2 = 0;
    x2 = int(random(picWidth));
    speed2 = int(random(1, 8));
  }
  if (y3 > picHeight) {
    y3 = 0;
    x3 = int(random(picWidth));
    speed3 = int(random(1, 8));
  }
  if (y4 > picHeight) {
    y4 = 0;
    x4 = int(random(picWidth));
    speed4 = int(random(1, 8));
  }
  if (y5 > picHeight) {
    y5 = 0;
    x5 = int(random(picWidth));
    speed5 = int(random(1, 8));
  }
}

boolean checkCollisions() {

  int offSetY, offSetX;
  offSetX = 65;
  offSetY = 70;
  if (x1 < mouseX + offSetX && x1 > mouseX && y1 < mouseY + offSetY && y1 > mouseY) {
    collide = true;
    return true;
  }
  if (x2 < mouseX + offSetX && x2 > mouseX && y2 < mouseY + offSetY && y2 > mouseY) {
    collide = true;
    return true;
  }
  if (x3 < mouseX + offSetX && x3 > mouseX && y3 < mouseY + offSetY && y3 > mouseY) {
    collide = true;
    return true;
  }
  if (x4 < mouseX + offSetX && x4 > mouseX && y4 < mouseY + offSetY && y4 > mouseY) {
    collide = true;
    return true;
  }
  if (x5 < mouseX + offSetX && x5 > mouseX && y5 < mouseY + offSetY && y5 > mouseY) {
    collide = true;
    return true;
  }
  return false;
}

void draw() {

  //draw a background image
  PImage starsPic = loadImage("stars.jpg");
  background(starsPic);

  //draw the asteroids
  updatePoints();
  fill(128, 128, 128);
  ellipse(x1, y1, 40, 40);
  ellipse(x2, y2, 40, 40);
  ellipse(x3, y3, 40, 40);
  ellipse(x4, y4, 40, 40);
  ellipse(x5, y5, 40, 40);

  if (checkCollisions()) {
    speed1 = speed2 = speed3 = speed4 = speed5 = 0;
  }

  drawCreature(mouseX, mouseY);
}

void keyPressed() {
  //when you press the 'a' key the witch will move to the left
  if (key == 'r') {
    ResetGame();
  }
}

void ResetGame() {
  y1 = y2 = y3 = y4 = y5 = 0;
  collide = false;
  x1 = int(random(picWidth));
  x2 = int(random(picWidth));
  x3 = int(random(picWidth));
  x4 = int(random(picWidth));
  x5 = int(random(picWidth));
  speed1 = int(random(1, 8));
  speed2 = int(random(1, 8));
  speed3 = int(random(1, 8));
  speed4 = int(random(1, 8));
  speed5 = int(random(1, 8));
}