class Key {
  boolean isDown = false;
  boolean pstate = false;
  boolean justPressed=false;
  boolean justReleased=false;
  void reset() {
    justPressed=false;
    justReleased=false;
  }
  void end() {
    pstate=isDown;
  }
}

class MouseButton {
  boolean isDown = false;
  boolean pState = false;
  boolean justPressed = false;
}

class Input {
  Key A, B, Q, start, esc;
  Key left, right, up, down;
  MouseButton mouseLeft; //fazer algum dia com Key
  
  Input() {
    A= new Key();
    B= new Key();
    Q= new Key();
    left= new Key();
    right= new Key();
    up= new Key();
    down= new Key();
    start= new Key();
    esc= new Key();
    mouseLeft = new MouseButton();
  }
  
  void check(){
    if (mousePressed) {
      mouseLeft.isDown = true;
      if (!mouseLeft.pState) {
        mouseLeft.justPressed = true;
      }
      else {
        mouseLeft.justPressed = false;
      }
    }
    else {
      mouseLeft.isDown = false;
    }
    mouseLeft.pState = mouseLeft.isDown;
  }
  
  void end() {
    A.reset();
    B.reset();
    Q.reset();
    start.reset();
    up.reset();
    down.reset();
    left.reset();
    right.reset();
    esc.reset();
  }
}

void keyPressed() { //interceptar o ESC
  if (key == ENTER || key == ' ') {
    if (input.start.isDown==false) input.start.justPressed=true;
    input.start.isDown=true;
  }
  if (key == 'z' || key == 'Z') {
    if (input.A.isDown==false) input.A.justPressed=true;
    input.A.isDown=true;
  }
  if (key == 'q' || key == 'Q') {
    if (input.Q.isDown==false) input.Q.justPressed=true;
    input.Q.isDown=true;
  }
  
  if (key == 'w' || key == 'W') {
    if (input.up.isDown==false) input.up.justPressed=true;
    input.up.isDown=true;
  }
  if (key == 'a' || key == 'A') {
    if (input.left.isDown==false) input.left.justPressed=true;
    input.left.isDown=true;
  }
  if (key == 's' || key == 'S') {
    if (input.down.isDown==false) input.down.justPressed=true;
    input.down.isDown=true;
  }
  if (key == 'd' || key == 'D') {
    if (input.right.isDown==false) input.right.justPressed=true;
    input.right.isDown=true;
  }
  
  if (key == 'm') {
    //println("hero position: "+hero.x+", "+hero.y);
  }
  if (key==27) {
    key=0;
    input.esc.justPressed=true;
  }
}

void keyReleased() {
  if (key == ENTER || key == ' ') {
    input.start.justReleased=true;
    input.start.isDown=false;
  }
  if (key == 'z' || key == 'Z') {
    input.A.justReleased=true;
    input.A.isDown=false;
  }
  if (key == 'q' || key == 'Q') {
    input.Q.justReleased=true;
    input.Q.isDown=false;
  }
  
  if (key == 'w' || key == 'W') {
    input.up.justReleased=true;
    input.up.isDown=false;
  }
  if (key == 'a' || key == 'A') {
    input.left.justReleased=true;
    input.left.isDown=false;
  }
  if (key == 's' || key == 'S') {
    input.down.justReleased=true;
    input.down.isDown=false;
  }
  if (key == 'd' || key == 'D') {
    input.right.justReleased=true;
    input.right.isDown=false;
  }
}