class PlayerBehavior4 extends Sup.Behavior {
  speed = 0.25;
  jumpSpeed = 0.55;
  paused = false;
  public colors
  audio : Sup.Audio.SoundPlayer
  
  awake() {
    this.audio = Sup.Audio.playSound("Part 4/Music", .8, { loop: true });
  }

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

  update() {
    let coffin = Sup.getActor("Coffin");
    if (Sup.ArcadePhysics2D.collides(this.actor.arcadeBody2D, coffin.arcadeBody2D)) {
      this.paused = true;
    }

    if (!this.paused) {
      Sup.ArcadePhysics2D.setGravity(0, -0.03);
      Sup.ArcadePhysics2D.collides(this.actor.arcadeBody2D, Sup.ArcadePhysics2D.getAllBodies());

      this.colors = [0, 360/360];

      // 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")) {
        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")) {
        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")) {
          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 {
      this.actor.arcadeBody2D.setVelocity(0, 0);
      Sup.ArcadePhysics2D.setGravity(0, 0);
      this.actor.spriteRenderer.setAnimation("Idle");
      Sup.setTimeout(2000, () => {
        coffin.setVisible(false);
        Sup.getActor("Coffin2").setVisible(true);
        Sup.getActor("Light").setVisible(false);
        this.actor.setVisible(false);
        Sup.getActor("BigLight").setVisible(true);
      });
    }
  }
}
Sup.registerBehavior(PlayerBehavior4);
