top-down-action-adventure/src/game.js

42 lines
891 B
JavaScript
Raw Normal View History

import Player from "./player.js"
2025-06-02 18:54:00 -04:00
import Input from "./input.js"
2025-06-01 15:54:55 -04:00
2025-06-01 15:30:12 -04:00
export default class Game {
constructor(canvas) {
this.canvas = canvas
this.ctx = canvas.getContext("2d")
2025-06-01 15:37:12 -04:00
this.timestamp = 0
2025-06-04 21:29:23 -04:00
this.player = new Player(this, 200, 200)
this.actors = [this.player]
2025-06-02 18:54:00 -04:00
this.input = new Input().initialize()
2025-06-04 01:16:48 -04:00
this.currentRoom = null
2025-06-01 15:37:12 -04:00
}
start() {
2025-06-04 01:16:48 -04:00
this.currentRoom = this.assets.get("sampleRoom")
2025-06-01 15:37:12 -04:00
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) {
2025-06-01 15:54:55 -04:00
this.actors.forEach(actor => actor.tick(dt))
2025-06-04 21:29:23 -04:00
this.currentRoom.tick(this, dt)
2025-06-01 15:30:12 -04:00
}
draw() {
const { canvas, ctx } = this
2025-06-04 01:16:48 -04:00
this.currentRoom.draw(ctx)
2025-06-01 15:54:55 -04:00
this.actors.forEach(actor => actor.draw(ctx))
2025-06-01 15:30:12 -04:00
}
}