// Scene


class Scene {
  BackGround bg;
  String screenTextOverlay;
  int id = -1; // meant to be overridden
  
  Scene() {
    // Destroy everything first
    destroyAllObjects();
    
    // MAKE things!
    camera = new Camera();
    bg = new BackGround(0);
    player = new Player();
    beep = new Beep();
    boop = new Boop();
    boundaries = new ArrayList();
    doors = new ArrayList();
    particles = new ArrayList();
    
    // RESET values!
    isPaused = false;
    keyDown_UP = keyDown_DOWN = keyDown_LEFT = keyDown_RIGHT = false;
    
    // Default ground!
    groundY = height * 0.8;
    float groundHeight = height-groundY;
    boundaries.add(new Boundary(width*0.5,groundY+groundHeight*0.5, 1024,groundHeight, 0.0));
    boundaries.add(new Boundary(-10,height*0.5, 5,height, 0));
    boundaries.add(new Boundary(width+10,height*0.5, 5,height, 0));
    // DEFAULT properties!
    player.goTo(width * 0.5, groundY);
    beep.goTo(width * 0.4, groundY);
    boop.goTo(width * 0.6, groundY);
    
    player.y = groundY;
    beep.y = groundY;
    boop.y = groundY;
    
    // Reset pmillis so we start waiting for things AFTER done loading everything.
    pmillis = millis();
  }
  
  void draw() {
    if (isPaused) return;
    
    // We must always step through time!
    box2d.step();
    
    // Background it.
    background(255);
    bg.draw();
    
    // Boundaries!
    for (int i=0; i<boundaries.size(); i++) {
      Boundary tempBoundary = (Boundary) boundaries.get(i);
      tempBoundary.draw();
    }
    // Particles!
    for (int i=0; i<particles.size(); i++) {
      Particle tempParticle = (Particle) particles.get(i);
      tempParticle.draw();
    }
    // Doors!
    for (int i=0; i<doors.size(); i++) {
      Door tempDoor = (Door) doors.get(i);
      tempDoor.draw();
    }
    
    // Updates!
    player.update();
    beep.update();
    boop.update();
    
    // Draws!
    image(grassGround_img, 0,groundY-70, width,200);
    player.draw();
    beep.draw();
    boop.draw();
    if (poll != null) poll.draw();
    if (sequenceController.eventQueue.length>0 && sequenceController.eventQueue[0].alertName=="mousePressed") {
      fill(0, 200);
      textAlign(CENTER, TOP);
      textSize(18);
      text("click to continue", camera.x,camera.y-height*0.5+(1-camera.zoom)*height*0.5);
    }
    if (screenTextOverlay != null) {
      fill(0, 200);
      textAlign(CENTER);
      textSize(24);
      textLeading(32);
      text(screenTextOverlay, camera.x,camera.y+height*0.25, 500,600);
    }
    tint(0, 64);
    image(vignette_img, camera.x-width*0.5,camera.y-height*0.5, width,height);
    noTint();
  }
  
  void executeEventByName(String name, String howLastEventEnded) {
    hideAllSpeechBubbles();
    screenTextOverlay = null;
    poll = null; // nullify the poll by default
  }
  
  void hideAllSpeechBubbles() {
    beep.speechBubble.disappear();
    boop.speechBubble.disappear();
    player.speechBubble.disappear();
  }
  void makeBeepAndBoopExcited() {
    beep.addVelocity(-5,10);
    boop.addVelocity(5,10);
    beep.setState(beep.STATE_EXCITED);
    boop.setState(beep.STATE_EXCITED);
  }
  
}


void destroyAllObjects() {
  // Nullify easy things
  poll = null;
  doors = null;
  
  //Destroy all Box2D objects
  if (beep != null) beep.killBody();
  if (boop != null) boop.killBody();
  if (player != null) player.killBody();
  
  if (boundaries != null) {
    for (int i=0; i<boundaries.size(); i++) {
      PhysicsObject tempPO = (PhysicsObject) boundaries.get(i);
      tempPO.killBody();
    }
  }
  if (particles != null) {
    for (int i=0; i<particles.size(); i++) {
      Particle tempParticle = (Particle) particles.get(i);
      if (tempParticle.body!=null) tempParticle.killBody();
    }
  }
  
  
  
  
}
