Load and use a single asset

This commit is contained in:
Bill Rossi 2025-06-03 21:33:59 -04:00
parent df931ca5e7
commit 6b1b31af29
5 changed files with 33 additions and 3 deletions

BIN
assets/RPGpack_sheet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

22
src/assets.js Normal file
View File

@ -0,0 +1,22 @@
export default class Assets {
constructor() {
this.assetMap = {}
}
get(assetName) {
return this.assetMap[assetName]
}
async load() {
const tilesheet = new Image()
this.assetMap.tilesheet = tilesheet
tilesheet.src = "./assets/RPGpack_sheet.png"
return new Promise((resolve, reject) => {
tilesheet.addEventListener("load", e => {
console.log(e)
resolve()
})
})
}
}

View File

@ -31,8 +31,7 @@ export default class Game {
draw() {
const { canvas, ctx } = this
ctx.fillStyle = "#222"
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(this.assets.get("tilesheet"), 0, 0)
this.actors.forEach(actor => actor.draw(ctx))
}
}

View File

@ -1,8 +1,12 @@
import Game from "./game.js"
import Assets from "./assets.js"
document.addEventListener("DOMContentLoaded", e => {
document.addEventListener("DOMContentLoaded", async e => {
const canvas = document.getElementById("game-canvas")
const game = new Game(canvas)
game.assets = new Assets()
await game.assets.load()
game.start()
})

5
src/room.js Normal file
View File

@ -0,0 +1,5 @@
import Actor from "./actor.js"
export default class Room extends Actor {
}