/* Base program for PCS 5th grade winter scene - practicing functions and 2D space  - Z. Wood */

/* animation variables */
boolean light = true;
boolean animSmoke = true;
boolean animDeer = true;

boolean grid = false;

int deerTx;
int smokeTy;

void drawGrid() {
  //the horizontal lines
  stroke(0, 0, 255);
  for (int i=0; i < height; i += 50) {
    line(0, i, width, i);
  }
  //the vertical lines
  stroke(255, 0, 255);
  for (int i=0; i < width; i += 50) {
    line(i, 0, i, height);
  }
}

/* a procedure to draw a tree at the specified location of a given size */
void drawTree(int treeX, int treeY, float treeSize) {
  noStroke();
  pushMatrix();
  translate(treeX, treeY);
  scale(treeSize);
  fill(31, 72, 29);
  triangle(-20, -10, 0, -40, 20, -10);
  fill(57, 106, 51);
  triangle(-15, -25, 0, -45, 15, -25);
  fill(64, 45, 9);
  rect(-5, -10, 10, 10);
  if (light) {
    fill(252, 240, 97, random(120, 210));
    ellipse(0, -45, random(8, 10), random(8, 10));
    fill(245, 250, 5, random(210, 250));
    ellipse(0, -45, random(4, 5), random(4, 5));
  }
  popMatrix();
}


int smallRandom[] = {
  -2, 4, 1, 3, 4, -1, 2
};

/* a procedure to draw a deer at a given location and of a given size */
void drawDeer(int DeerX, int DeerY, float deerSize) {
  fill(137, 80, 33);
  pushMatrix();
  translate(DeerX-deerTx, DeerY+8*sin(deerTx/2));
  scale(deerSize);
  translate(-76, -307);
  beginShape(); 
  vertex(25, 39);
  vertex(25, 44);
  vertex(0, 45);
  vertex(-25, 70);
  vertex(-25, 90);
  vertex(-50, 110);
  vertex(-50, 125);
  vertex(-25, 125);
  vertex(40, 100);
  vertex(80, 144);
  vertex(98, 198);
  vertex(76, 307);
  vertex(96, 307);
  vertex(132, 199);
  vertex(223, 178);
  vertex(276, 307);
  vertex(291, 307);
  vertex(290, 273);
  vertex(270, 215);
  vertex(291, 184);
  vertex(293, 116);
  vertex(308, 100);
  vertex(258, 91);        
  vertex(130, 91);
  vertex(42, 56);
  vertex(41, 36);
  endShape(CLOSE);

  //the eye
  fill(245);
  ellipse(10, 75, 20, 20);
  fill(0);
  ellipse(8, 77, 10, 14);

  stroke(0);
  strokeWeight(6);
  line(8, 50, -14, 18);
  line(8, 50, 23, 19);

  strokeWeight(1);
  //some spots
  for (int i=0; i < 3; i++) {
    for (int j=0; j < 2; j++) {
      fill(216, 143, 83);
      ellipse(245+i*13+smallRandom[i], 122+j*13+smallRandom[i+j], 10, 11);
    }
  }
  popMatrix();

  if (animDeer) {
    deerTx +=1;
    if (deerTx == width)
      deerTx = 0;
  }
}

/* a procedure to draw a present at a given location and size and color */
void drawPresent(int presentX, int presentY, float presentSize, color boxColor) {

  pushMatrix();
  translate(presentX, presentY);
  scale(presentSize);
  fill(boxColor);
  rect(0, -20, 30, 20);
  fill(boxColor+25);
  rect(12, -20, 4, 20);
  rect(0, -12, 30, 4);
  popMatrix();
}

/* a procedure to draw a house of at a given location and size */
void drawHouse(int houseX, int houseY, float houseSize) {
  noStroke();
  stroke(255);
  pushMatrix();
  translate(houseX, houseY);
  scale(houseSize);
  translate(0, -250);
  //house
  fill(0, 102, 204);
  rect(0, 0, 200, 250);

  //smoke
  fill(225);
  ellipse(54, -80+smokeTy, 19, 19);
  fill(250);
  ellipse(50, -85+smokeTy, 19, 19);

  fill(128);
  rect(45, -90, 20, 60);
  //roof
  fill(0, 0, 0);
  triangle(0, 0, 100, -100, 200, 0);

  stroke(0);
  strokeWeight(3);
  fill(252, 240, 97);
  rect(50, 50, 120, 60);
  line(50, 80, 170, 80);
  line(110, 50, 110, 110);

  strokeWeight(1);
  stroke(255);
  //door
  fill(153, 76, 0);
  rect(100, 150, 50, 100);
  fill(128);
  ellipse( 120, 195, 10, 10);

  popMatrix();

  if (animSmoke) {
    smokeTy-=5;
    if (smokeTy == -100)
      smokeTy = 0;
  }
}


void setup() {
  size(600, 400);
  frameRate(15);
  deerTx = 0;
  smokeTy = 0;
}

void draw() {
  background(12, 34, 56);
  fill(234);
  rect(0, .75*height, width, height);

  //TODO for students - add a call to draw any winter elements you'd like 
  //Note that you can draw more then one
  //For example, to draw a tree, you can call the drawTree(X, Y, SIZE);
  //where X and Y are the location to put the tree and the SIZE is how big you'd like it to be
  //for example try drawTree( 100, 330, 4);

  //For example, to draw a present, you can call the drawPresent(X, Y, SIZE, COLOR);
  //where X and Y are the location to put the present and the SIZE is how big you'd like it to be
  //and the COLOR is whatever color you want the present to be
  //for example try drawPresent(130, 350, 2, color(255, 0, 255));

  //For example, to draw a house, you can call the drawHouse(X, Y, SIZE);
  //where X and Y are the location to put the house and the SIZE is how big you'd like it to be
  //for example try drawHouse(435, 300, 0.75);

  //For example, to draw a deer, you can call the drawDeer(X, Y, SIZE);
  //where X and Y are the location to put the deer and the SIZE is how big you'd like it to be
  //for example trydrawDeer(400, 370, 0.4);

  fill(255);
  textSize(20);
  
  //TODO - you can also change the message to be whatever you'd like it to be
  text("Happy Winter!", 20, 20);


  if (grid) {
    drawGrid();
  }
}