////  Monster Mash ////

String title="M O N S T E R   M A S H";
String author="Joe Bloggs";

Hero me= new Hero();
Vampire dracula = new Vampire();
Monster frank  = new Monster();
int n=0;


int score;
float horizon, bottom;

float moonX,moonY;
float weedX=10, weedY=10;

float witchX=200, witchY=75, witchDX= -2;
float witchEyeX, witchEyeY;
float helpX, helpY, helpDY=0;


void setup() {
  size(700,500);
  horizon=  height * 1/4;
  bottom=  height;
  score=0;
  begin();
  
}
void begin() {
  ////  Move creatures back to starting positions.
  //
  moonX=  50 + random( width-100 );
  moonY=  witchY + random( 50 ); 
  helpX=width*2/3;
  helpY=height/3;
}

void draw() {
  //// Draw the frame.
  n++;
  scene();
  //  
  me.move();
  me.draw();
  
  //// Monsters
  text( dracula.name+":  "+dracula.x+","+dracula.y, 100,100 );
  text( frank.name+":  "+frank.x+","+frank.y, 200,200 );
  dracula.move();
  frank.move();
  //
  dracula.draw();
  frank.draw();
 
  witch();

  //// Write messages last (so they always show).
  messages();
}
void messages() {
  //// Messages on screen.
    //// Text on screen
    fill(0);
    text( title, 200, 20 );
    text( author, 20, height-20);
    text( witchX, 10,10 );
    // Score
    fill(255);
    if (score<0) fill(255,0,0);
    if (score != 0) text( "SCORE:  "+score, width*3/4, horizon-50 );
    if (key == '?') help();
    fill(255);
}  

void scene() {
  //// Sky & grass;
    background( 100,100,255 );
    fill( 200,100,100 );
    noStroke();
    ellipse( moonX, moonY, 70, 70 );    // moon
  //// field
  fill( 100,200,100 );
  noStroke();
  rectMode(CORNER);
  rect(0, horizon, width, height-horizon );
  seaweed( bottom );
}
void seaweed( float y ) {
  //// Make seaweed across the width of the screen.
  if (frameCount %10 == 0) {
    weedX=  10 - random(0,20);
    weedY=  20 - random(0,4);
  }
  for (float x=0; x<width+30; x+=30 )
  {
    strokeWeight(2);
    stroke( 50,100,0 );          // Dark-green seaweed.
    line(x,y, x+weedX, y-weedY );
    noStroke();
    weedX = weedX -1 + random(2);
  }
}


////// CREATURES //////
void witch() {
  //// Witch move & draw
  witchX += witchDX;
  if (witchX < 50) { 
    witchX=width;
    witchY=  random(50,100);
  moonX=  random(width);
  moonY=  random(horizon);

    score -= 25;
    return;
  }
  if (abs(witchX-moonX) < 15 ) {
    //// Witch crossing the moon.
    background(100,0,0);    // Flash (dark red)
    fill(255,255,0);
    text( "HEH \n     HEH      \n       HEH", witchX+50, witchY-50 );
  }
  drawWitch( witchX, witchY );
}
void drawWitch( float witchX, float witchY ) {
  //// Draw witch
  fill(100,50,50);
  rectMode(CENTER);
  rect( witchX, witchY, 40, 60 );    // Brown rectangle.
  fill(255,0,0);                      // Red face
  ellipse( witchX, witchY-40, 20, 20);
  // Yellow eye
  fill(255,255,0);
  ellipse( witchX-5, witchY-43, 5,5 );
  // Black hat.
  fill(0);
  triangle( witchX-10,witchY-50, witchX+10,witchY-50, witchX,witchY-70 );
  strokeWeight(3);
  stroke(0);
  line( witchX-20,witchY-50, witchX+20,witchY-50 );  // Hat brim.
  drawBroom( witchX-60, witchX+60, witchY+30 );
}
void drawBroom( float x1, float x2, float y ) {
  //// Broom from x1 to x2, at y
  strokeWeight(6);
  stroke(200,150,100);
  line( x1, y, x2, y );
  //// Bristles at x2.
  strokeWeight(1);
  float yy=  y-10;
  for (int j=0; j<8; j++ )
  {
    line( x2,yy, x2+40,yy );
    yy += 3;
  }
  noStroke();        // Reset
}



//// EVENT HANDLERS ////

void mousePressed() {
  //// Check for clicking on:  bird, witch, fish, octopus, etc.
      ////// Witch's eye adds 50 to score (and ends the night).
      if ( dist( mouseX,mouseY, witchX-20, witchY-50 )  <  15 ) { 
        background(0);
        score += 50;           // Witch's eye
        witchDX +=  1;
      }
}


    

void keyPressed() {
  //// Keys.

  if (key == 'q') exit();
}
void help() {
  //// Display help msgs
  helpDY=0;
  fill(0);
  text("H E L P", helpX, helpY+12*helpDY++ );
  text("H E L P", helpX, helpY+12*helpDY++ );
  text("H E L P", helpX, helpY+12*helpDY++ );
}


class Vampire {
  float x=10,y=75, dx=5, dy=0;
  String name="Vlad";
  
  void move() {
    x += dx + random(3);
    y += dy -10 +random(20);
    if (x>width) x= 10;        // wrap
    if (x<0) x= 20;
  }
  void draw() {
    fill(0);
    rect(x,y, 40,20);
  fill(255,0,0);
    text(name,x-10,y);
  }
}

class Monster {
  String name="Frank";
  float x=400, y=300,  dx=-12, dy=15;
  
  void move() {
    if (n % 10 == 0) {
      x += dx + random(2);
      y += dy - dy * random(2);

      if (x<20 || x>width-20) {    // bounce
        dx= -dx;
        x += dx*10;
      }
    }
  }
  void draw() {
    fill(0,150,100);
    rect(x,y, 50,90);
    rect(x,y-75, 40,40);
    rect(x,y-55, 20,20);
	fill(255,0,0);
    text(name,x-20,y);
  }

}



class Hero {
  String name="Lorcan";
  float x=100, y=200,  dx=-12, dy=15;
  
  void move() {
    if (mouseX>pmouseX) x += random(5);
    if (mouseX<pmouseX) x -= random(5);
    if (mouseY>pmouseY) y += random(3);
    if (mouseY<pmouseY) y -= random(3);
  }
  void draw() {
    fill(0,200,255);
	rectMode(CENTER);
    rect(x,y, 40,60);
    fill(255,0,0);
    text(name,x-10,y-10);
	fill(255,200,200);
	ellipse(x,y-50,40,40);
	fill(0,0,200);
	ellipse(x-10,y-50,10,10);
	ellipse(x+10,y-50,10,10);
  }

}

