import Player from "./player.js"
import Input from "./input.js"

export default class Game {
  constructor(canvas) {
    this.canvas = canvas
    this.ctx = canvas.getContext("2d")
    this.timestamp = 0
    this.player = new Player(this, 200, 200)
    this.actors = [this.player]

    this.input = new Input().initialize()

    this.currentRoom = null
  }

  start() {
    this.currentRoom = this.assets.get("sampleRoom")
    requestAnimationFrame(this.loop.bind(this))
  }

  loop(timestamp) {
    const dt = timestamp - this.timestamp
    this.timestamp = timestamp
    this.tick(dt)
    this.draw()

    requestAnimationFrame(this.loop.bind(this))
  }

  tick(dt) {
    this.actors.forEach(actor => actor.tick(dt))
    this.currentRoom.tick(this, dt)
  }

  draw() {
    const { canvas, ctx } = this
    this.currentRoom.draw(ctx)
    this.actors.forEach(actor => actor.draw(ctx))
  }
}