import processing.core.PImage;
import processing.core.PGraphics;

public class Player extends Sprite {

  PImage bodyImg, armImg;
  
  public Player(Game g) {
    x = 100;
    y = 100;
    
    bodyImg = g.loadImage("playerbody.gif");
    armImg = g.loadImage("playerarm.gif");
  }
  
  float angle = 0;
  boolean flip = false;
  
  public void update(Game g) {
    angle = (float)Math.atan2(g.a.mouseY-y+14,flip?x-g.a.mouseX:g.a.mouseX-x);
    if (Math.abs(angle)>g.a.radians(100))
      flip = !flip;
  }
  
  public void draw(PGraphics g) {
    g.push();
      g.translate(x,y);
      g.noStroke();
      float cos,sin,x,y;
      
      if (flip)
        g.scale(-1,1);
        
      g.beginShape(PGraphics.QUAD_STRIP); 
        g.texture(bodyImg);
        
        g.vertex(-17, 0, 0,  48);
        g.vertex( 31, 0, 48, 48);
        
//        g.rotate(angle*0.2f);
//        g.vertex( 31, -10, 48, 38);
//        g.vertex(-17, -10, 0,  38);
        cos=(float)Math.cos(angle*0.2f);
        sin=(float)Math.sin(angle*0.2f);
        x = 31; 
        y = -10;
        g.vertex(x*cos-y*sin, x*sin+y*cos, 48, 38);
        x = -17; 
        y = -10;
        g.vertex(x*cos-y*sin, x*sin+y*cos, 0, 38);
      
//        g.rotate(angle*0.2f);
//        g.vertex(-17, -20, 0,  28);
//        g.vertex( 31, -20, 48, 28);

        cos=(float)Math.cos(angle*0.4f);
        sin=(float)Math.sin(angle*0.4f);
        x = -17;
        y = -20;
        g.vertex(x*cos-y*sin, x*sin+y*cos, 0, 28);
        x = 31;
        y = -20;
        g.vertex(x*cos-y*sin, x*sin+y*cos, 48, 28);
        
//        g.rotate(angle*0.2f);
//        g.vertex( 31, -48, 48, 0);
//        g.vertex(-17, -48, 0,  0);

        cos=(float)Math.cos(angle*0.6f);
        sin=(float)Math.sin(angle*0.6f);
        x = 31;
        y = -48;
        g.vertex(x*cos-y*sin, x*sin+y*cos, 48, 0);
        x = -17;
        y = -48;
        g.vertex(x*cos-y*sin, x*sin+y*cos, 0, 0);
      g.endShape();
      
//      g.image(bodyImg, -17, -48);
      cos=(float)Math.cos(angle*0.4f);
      sin=(float)Math.sin(angle*0.4f);
      x = 0;
      y = -14;
      g.translate( x*cos-y*sin, x*sin+y*cos);
      g.rotate(angle);
      g.image(armImg, -14, -36);
    g.pop();
  }
}
