class PlayerBehavior extends Sup.Behavior {
  speed = 0.25;
  jumpSpeed = 0.55;
  paused = false;
  pausedOnce = false;
  keys : Sup.Actor
  keyPressed = false;
  hasGreen = false
  public colors
  audio : Sup.Audio.SoundPlayer
  
  awake() {
    this.keys = Sup.getActor("Keys");
    this.audio = Sup.Audio.playSound("Part 1/Music", 1.0, { loop: true });
  }

  onDestroy() {
    this.audio.stop();
  }

  update() {
    let guy = Sup.getActor("Guy");
    if (guy.getPosition().x - 5 < this.actor.getPosition().x && !this.pausedOnce) {
      this.paused = true;
      this.pausedOnce = true;
    }
    
    if (this.hasGreen) {
      this.colors = [90/360, 265/360];
    }
    else {
      this.colors = [200/360, 265/360];
    }
    
    if (this.keyPressed && !this.keys.isDestroyed()) {
      Sup.setTimeout(1000, () => this.keys.destroy());
    }
    
    if (!this.paused) {
      Sup.ArcadePhysics2D.setGravity(0, -0.03);
      Sup.ArcadePhysics2D.collides(this.actor.arcadeBody2D, Sup.ArcadePhysics2D.getAllBodies());

      // As explained above, we get the current velocity
      let velocity = this.actor.arcadeBody2D.getVelocity();

      // We override the `.x` component based on the player's input
      if (Sup.Input.isKeyDown("A") || Sup.Input.isKeyDown("LEFT")) {
        this.keyPressed = true;
        velocity.x = -this.speed;
        // When going left, we flip the sprite
        this.actor.spriteRenderer.setHorizontalFlip(true);
      } else if (Sup.Input.isKeyDown("D") || Sup.Input.isKeyDown("RIGHT")) {
        this.keyPressed = true;
        velocity.x = this.speed;
        // When going right, we clear the flip
        this.actor.spriteRenderer.setHorizontalFlip(false);
      } else velocity.x = 0;

      // If the player is on the ground and wants to jump,
      // we update the `.y` component accordingly
      let touchBottom = this.actor.arcadeBody2D.getTouches().bottom;
      if (touchBottom) {
        if (Sup.Input.wasKeyJustPressed("W") || Sup.Input.wasKeyJustPressed("SPACE") || Sup.Input.wasKeyJustPressed("UP")) {
          this.keyPressed = true;
          velocity.y = this.jumpSpeed;
          //this.actor.spriteRenderer.setAnimation("Jump");
        } else {
          // Here, we should play either "Idle" or "Run" depending on the horizontal speed
          if (velocity.x === 0) this.actor.spriteRenderer.setAnimation("Idle");
          else this.actor.spriteRenderer.setAnimation("Walk");
        }
      } else {
        // Here, we should play either "Jump" or "Fall" depending on the vertical speed
        //if (velocity.y >= 0) this.actor.spriteRenderer.setAnimation("Jump");
        //else this.actor.spriteRenderer.setAnimation("Fall");
      }

      // Finally, we apply the velocity back to the ArcadePhysics body
      this.actor.arcadeBody2D.setVelocity(velocity);
    }
    else {
      // guy will give stained glass to girl, girl will wait
      this.actor.arcadeBody2D.setVelocity(0, 0);
      Sup.ArcadePhysics2D.setGravity(0, 0);
      this.actor.spriteRenderer.setAnimation("Idle");
      guy.spriteRenderer.setAnimation("Give", false);
      Sup.setTimeout(2000, () => {
        this.paused = false;
        Sup.getActor("GREEN").setVisible(true);
        Sup.getActor("Shift").setVisible(true);
      });
    }
  }
}
Sup.registerBehavior(PlayerBehavior);
