

class Granjero {
  
    float x;
    int xdir;
    int speed;
    boolean moveLeft;
    boolean moveRight;
    
    Granjero (float x_) {
        this.x += x_;
        this.xdir = 0;
        this.speed = 5;
        this.moveLeft = false;
        this.moveRight = false;
    }
    
    void init() {
      
        String[] granjeroFrontSeq = {"img/GranjeroFront1.png", "img/GranjeroFront2.png", "img/GranjeroFront3.png"};
        String[] granjeroWalkSeq = {"img/GranjeroWalk1.png", "img/GranjeroWalk2.png", "img/GranjeroWalk3.png"};
        String[] granjeroWalkShootSeq = {"img/GranjeroWalkShoot1.png", "img/GranjeroWalkShoot2.png", "img/GranjeroWalkShoot3.png"};
        String[] granjeroStandShootSeq = {"img/GranjeroStandShoot1.png", "img/GranjeroStandShoot2.png", "img/GranjeroStandShoot3.png"};

        PImage[] granjeroFrontImgs = new PImage[3];
        PImage[] granjeroWalkImgs = new PImage[3];
        PImage[] granjeroWalkShootImgs = new PImage[3];
        PImage[] granjeroStandShootImgs = new PImage[3];
        
        loadAnimation(granjeroFrontSeq, granjeroFrontImgs);
        loadAnimation(granjeroWalkSeq, granjeroWalkImgs);
        loadAnimation(granjeroWalkShootSeq, granjeroWalkShootImgs);
        loadAnimation(granjeroStandShootSeq, granjeroStandShootImgs);
      
        granjeroFront = new AnimatedSprite(granjeroFrontImgs, 8, this.x, height-61, 115, 115 );  /////// + width/4 Center of screen
        granjeroFront.setFPS(4);
        granjeroFront.loop();
        
        granjeroWalk = new AnimatedSprite(granjeroWalkImgs, 8, this.x, height-61, 115, 115 );  /////// + width/4 Center of screen
        granjeroWalk.setFPS(12);
        granjeroWalk.loop();
        
        granjeroWalkShoot = new AnimatedSprite(granjeroWalkShootImgs, 8, this.x, height-61, 115, 115 );  /////// + width/4 Center of screen
        granjeroWalkShoot.setFPS(12);
        granjeroWalkShoot.loop();
        
        granjeroStandShoot = new AnimatedSprite(granjeroStandShootImgs, 8, this.x, height-61, 115, 115 );  /////// + width/4 Center of screen
        granjeroStandShoot.setFPS(12);
        granjeroStandShoot.loop();
        
        granjeroSprite = granjeroFront;
        granjeroPosX = granjeroSprite.x;

        //granjeroSprite = createSprite(width/2-16, height-61);  /////// + width/4 Center of screen
        //granjeroSprite.scale = 0.75;
        //granjeroSprite.addAnimation('stand', granjeroFrontSeq);
        //granjeroSprite.addAnimation('walk', granjeroWalkSeq);
        //granjeroSprite.addAnimation('walkShoot', granjeroWalkShootSeq);
        //granjeroSprite.addAnimation('standShoot', granjeroStandShootSeq);
    }

    //void reset() {
    //    granjeroSprite.x = width/2-16;
    //    granjeroSprite.y = height-61;
    //}

    void update() {
        if(this.moveLeft) {
            this.move(-1);
            granjeroSprite.mirrorX ( -1 );
            dogSprite.mirrorX ( 1 );
            chocloCorrec = -24;
        } else if(this.moveRight) {
            this.move(1);
            granjeroSprite.mirrorX ( 1 );
            dogSprite.mirrorX ( -1 );
            chocloCorrec = 24;
        }
        if(granjeroSprite.x < 20) {
            granjeroSprite.x = 20;
        }
        if(granjeroSprite.x > width-20) {
            granjeroSprite.x = width-20;
        }
        granjeroSprite.x = granjeroPosX;
        granjeroSprite.update(); 
        granjeroSprite.draw();
    }

    boolean hits(AnimatedSprite enemy) {

        float d = dist(granjeroSprite.x, granjeroSprite.y, enemy.x, enemy.y);
        
        if (d < (enemy.getWidth()/1.5)) {
            return true;
        } else {
            return false;
        }
    }

    void move(float speedMult) {
        granjeroPosX += (this.speed*speedMult);
        
        stand = false;
    }

    void shoot() {
        //shootSound.setVolume(0.1);
        //shootSound.play();
        choclos.add(new Choclo(granjeroSprite.x + chocloCorrec, height-60));
    }
}

float granjeroPosX = (width/2)-16;
