Load and use a single asset
This commit is contained in:
parent
df931ca5e7
commit
6b1b31af29
BIN
assets/RPGpack_sheet.png
Normal file
BIN
assets/RPGpack_sheet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 65 KiB |
22
src/assets.js
Normal file
22
src/assets.js
Normal 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()
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -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
5
src/room.js
Normal file
@ -0,0 +1,5 @@
|
||||
import Actor from "./actor.js"
|
||||
|
||||
export default class Room extends Actor {
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user