class Room {
  Door entranceDoor;
  Door exitDoor;
  
  Room(){
    
  }
  void render(){
    image(bgImg, 0, 0);
    renderDoors();
  }
  void renderDoors(){
    entranceDoor.render();
    exitDoor.render();
  }
  void update(){
    entranceDoor.update();
    exitDoor.update();
    if (!entranceDoor.locked){
      entranceDoor.lock();
    }
  }
}

class TutorialRoom extends Room {
  TutorialRoom(){
    setBGM("mus_gameplay");
    entranceDoor = new Door (false,false,false);
    exitDoor = new Door (true,false,true);
  }
  void render(){
    image(tutorialImg, 0, 0);
    renderDoors();
  }
  void update(){
    super.update();
    exitDoor.unlock();
  }
}

class BattleRoom extends Room {
  BattleRoom(){
    setBGM("mus_gameplay");
    entranceDoor = new Door (false,false,true);
    exitDoor = new Door (true,true,true);
  }
  void update(){
    super.update();
    if (enemies.size() == 0 && exitDoor.locked == true) {
      exitDoor.unlock();
      SE.trigger(SE.gateOpen);
    }
  }
}

class MonologueRoom extends Room {
  String monologue;
  int currentCharacter;
  MonologueRoom(String m){
    setBGM("mus_muted");
    entranceDoor = new Door (false,false,true);
    exitDoor = new Door (true,false,true);
    monologue = m;
    currentCharacter = 0;
  }
  void update(){
    super.update();
    if (currentCharacter < monologue.length()) {
      if (animation % 3 == 0) currentCharacter++;
    }
    //else if (exitDoor.locked) {
      
    //}
  }
  void render(){
    super.render();
    fill(255);
    textLeading(28);
    text(monologue.substring(0, currentCharacter), 200, 200, width - 2 * 200, 100);
  }
}

class FinalRoom extends Room {
  FinalRoom(){
    //setBGM("mus_gameplay");
    BGMPlayer.shiftVolume(BGMPlayer.getVolume(), 0, 1000);
    entranceDoor = new Door (false,false,true);
    exitDoor = new Door (false,false,false);
  }
  void render(){
    image(lastRoomImg, 0, 0);
    renderDoors();
  }
  void update(){
    if (player.y < 400 && !fade.fading){
      SE.trigger(SE.menuConfirm);
      fade.start(120, 255, "endGame");
    }
  }
}




class Door extends Object{
  boolean isExit, locked, active;
  Door(boolean isExit, boolean locked, boolean active){
    this.isExit = isExit;
    this.locked = locked;
    this.active = active;
    x = width/2;
    if (isExit) {
      y = level.topBorder;
      hitBox = new HitBox(-20, -10, 40, 10);
    }
    else {
      y = level.bottomBorder;
      hitBox = new HitBox(-20, 0, 40, 10);
    }
    spriteOffsetX = -doorLocked.width/2; 
  }
  
  void render(){
    if (active){
      pushMatrix();
      translate(x, y);
      if (!isExit) scale (1, -1);
      translate(0,-56);
      image(locked ? doorLocked : doorUnlocked, spriteOffsetX, 0);
      popMatrix();
    }
    //hitBox.render();
  }
  void update(){
    hitBox.update(x, y);
    if (isExit && !locked && player.hitBox.overlap(hitBox)) {
      fade.start(30, 0, "nextRoom");
    }
  }
  void lock(){
    SE.trigger(SE.door);
    locked = true;
  }
  void unlock(){
    SE.trigger(SE.door);
    locked = false;
  }
}